|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import pandas |
| 4 | +import pickle |
| 5 | +import subprocess |
| 6 | +from multiprocessing import ( |
| 7 | + Pool, |
| 8 | + current_process, |
| 9 | +) |
| 10 | +from tqdm import tqdm |
| 11 | + |
| 12 | +root_path = "/data/TheStack_IR/OJ_Samples/Collated/C" |
| 13 | +out_path = "/data/TheStack_IR/OJ_Samples/Scripts/C" |
| 14 | + |
| 15 | + |
| 16 | +def compile_worker(dir_list_chunk): |
| 17 | + result = pandas.DataFrame(columns=["Source", "Perf_Compile_Status", "Size_Compile_Status"]) |
| 18 | + result = result.set_index("Source") |
| 19 | + with ( |
| 20 | + open( |
| 21 | + f"{out_path}/Logs/out_{current_process().pid}.txt", |
| 22 | + "a+" |
| 23 | + ) |
| 24 | + if os.path.exists(f"{out_path}/Logs/out_{current_process().pid}.txt") |
| 25 | + else open( |
| 26 | + f"{out_path}/Logs/out_{current_process().pid}.txt", |
| 27 | + "x+" |
| 28 | + ) as outfile, |
| 29 | + open( |
| 30 | + f"{out_path}/Logs/err_{current_process().pid}.txt", |
| 31 | + "a+" |
| 32 | + ) |
| 33 | + if os.path.exists(f"{out_path}/Logs/err_{current_process().pid}.txt") |
| 34 | + else open( |
| 35 | + f"{out_path}/Logs/err_{current_process().pid}.txt", |
| 36 | + "x+" |
| 37 | + ) as errfile |
| 38 | + ): |
| 39 | + for folder in tqdm( |
| 40 | + dir_list_chunk, |
| 41 | + desc=f"Worker - {current_process().pid}: " |
| 42 | + ): |
| 43 | + src_path = f"{root_path}/{folder}/source.c" |
| 44 | + perf_path = f"{root_path}/{folder}/llvm_O3.ll" |
| 45 | + size_path = f"{root_path}/{folder}/llvm_OZ.ll" |
| 46 | + |
| 47 | + perf_returncode = subprocess.call( |
| 48 | + [ |
| 49 | + "clang", "-S", |
| 50 | + "-O3", |
| 51 | + "-fno-discard-value-names", |
| 52 | + "-fstandalone-debug", |
| 53 | + "-emit-llvm", |
| 54 | + f"{src_path}", |
| 55 | + "-o", f"{perf_path}" |
| 56 | + ], |
| 57 | + shell=False, |
| 58 | + stdout=outfile, |
| 59 | + stderr=errfile |
| 60 | + ) |
| 61 | + |
| 62 | + size_returncode = subprocess.call( |
| 63 | + [ |
| 64 | + "clang", "-S", |
| 65 | + "-Oz", |
| 66 | + "-fno-discard-value-names", |
| 67 | + "-fstandalone-debug", |
| 68 | + "-emit-llvm", |
| 69 | + f"{src_path}", |
| 70 | + "-o", f"{size_path}" |
| 71 | + ], |
| 72 | + shell=False, |
| 73 | + stdout=outfile, |
| 74 | + stderr=errfile |
| 75 | + ) |
| 76 | + |
| 77 | + result.loc[folder] = [perf_returncode, size_returncode] |
| 78 | + |
| 79 | + return result |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +def main(args): |
| 84 | + dir_list = os.listdir(root_path) |
| 85 | + dir_list.sort() |
| 86 | + if args.subset: |
| 87 | + dir_list = dir_list[:args.subset] |
| 88 | + print(f"Obtained {len(dir_list)} source files for compilation") |
| 89 | + with ( |
| 90 | + open( |
| 91 | + f"{out_path}/dir_list.pickle", |
| 92 | + "wb" |
| 93 | + ) |
| 94 | + if os.path.exists(f"{out_path}/dir_list.pickle") |
| 95 | + else open( |
| 96 | + f"{out_path}/dir_list.pickle", |
| 97 | + "xb" |
| 98 | + ) |
| 99 | + ) as sp: |
| 100 | + pickle.dump(dir_list, sp) |
| 101 | + |
| 102 | + dir_list_chunked = [dir_list[i::args.num_workers] for i in range(args.num_workers)] |
| 103 | + with Pool(args.num_workers) as pool: |
| 104 | + results_list = pool.map(compile_worker, dir_list_chunked) |
| 105 | + results_df = pandas.concat(results_list, ignore_index=False) |
| 106 | + results_df.to_parquet( |
| 107 | + f"{out_path}/results.parquet" |
| 108 | + ) |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 112 | + parser.add_argument( |
| 113 | + "--num_workers", |
| 114 | + type=int, |
| 115 | + required=False, |
| 116 | + default=8, |
| 117 | + help="Number of workers to spawn for compilation" |
| 118 | + ) |
| 119 | + parser.add_argument( |
| 120 | + "--subset", |
| 121 | + type=int, |
| 122 | + required=False, |
| 123 | + default=None, |
| 124 | + help="Number of source files to compile" |
| 125 | + ) |
| 126 | + args = parser.parse_args() |
| 127 | + main(args) |
0 commit comments