|
| 1 | +# %% |
| 2 | +import os |
| 3 | + |
| 4 | +import torch |
| 5 | +from args import parse_args |
| 6 | + |
| 7 | +args = parse_args() |
| 8 | +# %% |
| 9 | +root_path = args.root_path |
| 10 | +region = args.region |
| 11 | + |
| 12 | +data_path = f"{region}/cctorch" |
| 13 | +result_path = f"{region}/cctorch/ccpairs" |
| 14 | + |
| 15 | +# data_path = f"{region}/cctorch_ca" |
| 16 | +# result_path = f"{region}/cctorch_ca/ccpairs" |
| 17 | + |
| 18 | +# data_path = f"{region}/cctorch_gamma" |
| 19 | +# result_path = f"{region}/cctorch_gamma/ccpairs" |
| 20 | + |
| 21 | +if not os.path.exists(f"{root_path}/{result_path}"): |
| 22 | + os.makedirs(f"{root_path}/{result_path}") |
| 23 | + |
| 24 | + |
| 25 | +## based on GPU memory |
| 26 | + |
| 27 | +batch = 1_024 |
| 28 | +block_size1 = 1000_000 |
| 29 | +block_size2 = 1000_000 |
| 30 | + |
| 31 | +if args.dtct_pair: |
| 32 | + dt_ct = f"{root_path}/{region}/hypodd/dt.ct" |
| 33 | + pair_list = f"{root_path}/{region}/hypodd/pairs.txt" |
| 34 | + lines = [] |
| 35 | + with open(dt_ct, "r") as fp: |
| 36 | + for line in fp: |
| 37 | + if line.startswith("#"): |
| 38 | + ev1, ev2 = line.split()[1:3] |
| 39 | + if ev1 > ev2: |
| 40 | + ev1, ev2 = ev2, ev1 |
| 41 | + lines.append(f"{ev1},{ev2}\n") |
| 42 | + |
| 43 | + print(f"Number of pairs from hypodd dt.ct: {len(lines)}") |
| 44 | + with open(f"{root_path}/{region}/hypodd/pairs.txt", "w") as fp: |
| 45 | + fp.writelines(lines) |
| 46 | + base_cmd = f"../CCTorch/run.py --pair_list={root_path}/{region}/hypodd/pairs.txt --data_path1={root_path}/{region}/cctorch/template.dat --data_format1=memmap --config={root_path}/{region}/cctorch/config.json --batch_size={batch} --block_size1={block_size1} --block_size2={block_size2} --result_path={root_path}/{result_path}" |
| 47 | + |
| 48 | +else: |
| 49 | + base_cmd = ( |
| 50 | + f"../CCTorch/run.py --pair_list={root_path}/{data_path}/pairs.txt --data_path1={root_path}/{data_path}/template.dat --data_format1=memmap " |
| 51 | + f"--data_list1={root_path}/{data_path}/cctorch_picks.csv " |
| 52 | + f"--events_csv={root_path}/{data_path}/cctorch_events.csv --picks_csv={root_path}/{data_path}/cctorch_picks.csv --stations_csv={root_path}/{data_path}/cctorch_stations.csv " |
| 53 | + f"--config={root_path}/{data_path}/config.json --batch_size={batch} --block_size1={block_size1} --block_size2={block_size2} " |
| 54 | + f"--result_path={root_path}/{result_path}" |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +if torch.cuda.is_available(): |
| 59 | + device = "cuda" |
| 60 | + num_gpu = torch.cuda.device_count() |
| 61 | +elif torch.backends.mps.is_available(): |
| 62 | + device = "mps" |
| 63 | + num_gpu = 0 |
| 64 | +else: |
| 65 | + device = "cpu" |
| 66 | + num_gpu = 0 |
| 67 | + |
| 68 | +if num_gpu > 0: |
| 69 | + cmd = f"torchrun --standalone --nproc_per_node {num_gpu} {base_cmd} --device={device}" |
| 70 | +else: |
| 71 | + cmd = f"python {base_cmd} --device={device}" |
| 72 | +print(cmd) |
| 73 | +os.system(cmd) |
| 74 | + |
| 75 | +# %% |
| 76 | +for rank in range(num_gpu): |
| 77 | + if not os.path.exists(f"{root_path}/{result_path}/CC_{rank:03d}_{num_gpu:03d}.csv"): |
| 78 | + continue |
| 79 | + if rank == 0: |
| 80 | + cmd = f"cat {root_path}/{result_path}/CC_{rank:03d}_{num_gpu:03d}.csv > {root_path}/{data_path}/dtcc.csv" |
| 81 | + else: |
| 82 | + cmd = ( |
| 83 | + f"tail -n +2 {root_path}/{result_path}/CC_{rank:03d}_{num_gpu:03d}.csv >> {root_path}/{data_path}/dtcc.csv" |
| 84 | + ) |
| 85 | + print(cmd) |
| 86 | + os.system(cmd) |
| 87 | + |
| 88 | + |
| 89 | +cmd = f"cat {root_path}/{result_path}/CC_*_{num_gpu:03d}_dt.cc > {root_path}/{data_path}/dt.cc" |
| 90 | +print(cmd) |
| 91 | +os.system(cmd) |
| 92 | + |
| 93 | +# # %% |
| 94 | +# os.chdir(f"{root_path}/{region}/cctorch") |
| 95 | +# source_file = f"ccpairs/CC_{num_gpu:03d}_dt.cc" |
| 96 | +# target_file = f"dt.cc" |
| 97 | +# print(f"{source_file} -> {target_file}") |
| 98 | +# if os.path.lexists(target_file): |
| 99 | +# os.remove(target_file) |
| 100 | +# os.symlink(source_file, target_file) |
| 101 | + |
| 102 | +# source_file = f"ccpairs/CC_{num_gpu:03d}.csv" |
| 103 | +# target_file = f"dtcc.csv" |
| 104 | +# print(f"{source_file} -> {target_file}") |
| 105 | +# if os.path.lexists(target_file): |
| 106 | +# os.remove(target_file) |
| 107 | +# os.symlink(source_file, target_file) |
0 commit comments