PySDM_examples.Bulenok_2023_MasterThesis.performance_comparison_Srivastava_Setup
1import subprocess 2import os 3 4from datetime import datetime 5import numba # pylint: disable=unused-import 6 7from PySDM_examples.Bulenok_2023_MasterThesis.utils import ( 8 go_benchmark, 9 process_results, 10 plot_processed_results, 11 write_to_file, 12) 13from PySDM_examples.Bulenok_2023_MasterThesis.setups import ( 14 setup_coalescence_only_sim, 15 setup_breakup_only_sim, 16 setup_coalescence_breakup_sim, 17) 18 19from PySDM.backends import GPU, CPU 20from PySDM.physics import si 21 22 23def main(plot: bool = True, save: str = None): 24 25 TIMESTAMP = str(datetime.now().strftime("%Y-%d-%m_%Hh-%Mm-%Ss")) 26 27 SIM_RUN_FNAME = "env_name_" + TIMESTAMP 28 29 assert not os.path.isfile(SIM_RUN_FNAME) 30 31 cmd = [ 32 "bash", 33 "-c", 34 # pylint: disable=no-member 35 f"echo NUMBA_DEFAULT_NUM_THREADS: {numba.config.NUMBA_DEFAULT_NUM_THREADS}" 36 + f">> {SIM_RUN_FNAME}", 37 ] 38 subprocess.run(cmd, check=False) 39 subprocess.run( 40 [ # pylint: disable=no-member 41 "bash", 42 "-c", 43 f"echo NUMBA_NUM_THREADS: {numba.config.NUMBA_NUM_THREADS} >> {SIM_RUN_FNAME}", 44 ], 45 check=False, 46 ) 47 subprocess.run(["bash", "-c", f"lscpu >> {SIM_RUN_FNAME}"], check=False) 48 subprocess.run(["bash", "-c", f"nvidia-smi >> {SIM_RUN_FNAME}"], check=False) 49 subprocess.run(["bash", "-c", f"nvidia-smi -L >> {SIM_RUN_FNAME}"], check=False) 50 subprocess.run(["bash", "-c", f"cat /proc/cpuinfo >> {SIM_RUN_FNAME}"], check=False) 51 52 CI = "CI" in os.environ 53 54 exponents = [3, 5, 8, 10, 12, 14, 16, 18, 20, 22, 24] if not CI else [3, 5] 55 n_sds = [2**i for i in exponents] 56 57 numba_n_threads = [1, 2, 4, 5, 6, 8, 10] if not CI else [1, 2] 58 59 n_realisations = 3 if not CI else 2 60 seeds = list(range(n_realisations)) 61 62 n_steps_short = 100 if not CI else 3 63 n_steps_full = 2048 if not CI else 3 64 65 # # Benchmark regular setup (without scaling) 66 67 # ### Coalescence-only 68 69 res_coalescence_only = go_benchmark( 70 setup_coalescence_only_sim, 71 n_sds, 72 n_steps_short, 73 seeds, 74 numba_n_threads=numba_n_threads, 75 double_precision=True, 76 sim_run_filename=SIM_RUN_FNAME + "-coalescence", 77 backends=[CPU, GPU], 78 ) 79 coalescence_only_processed = process_results(res_coalescence_only) 80 filename = f"{SIM_RUN_FNAME}-results-coalescence-double-n_steps{n_steps_short}" 81 if plot: 82 plot_processed_results( 83 coalescence_only_processed, 84 plot_title=f"coalescence-only (n_steps: {n_steps_short})", 85 plot_filename=filename + ".svg", 86 ) 87 if save is not None: 88 write_to_file(filename=filename + ".txt", d=coalescence_only_processed) 89 90 # ### Breakup-only 91 92 res_breakup_only = go_benchmark( 93 setup_breakup_only_sim, 94 n_sds, 95 n_steps_short, 96 seeds, 97 numba_n_threads=numba_n_threads, 98 double_precision=True, 99 sim_run_filename=SIM_RUN_FNAME + "-breakup", 100 backends=[CPU, GPU], 101 ) 102 breakup_only_processed = process_results(res_breakup_only) 103 filename = f"{SIM_RUN_FNAME}-results-breakup-double-n_steps{n_steps_short}" 104 if plot: 105 plot_processed_results( 106 breakup_only_processed, 107 plot_title=f"breakup-only (n_steps: {n_steps_short})", 108 plot_filename=filename + ".svg", 109 ) 110 if save is not None: 111 write_to_file(filename=filename + ".txt", d=breakup_only_processed) 112 113 # ### Coalescence and Breakup 114 115 res_coal_breakup = go_benchmark( 116 setup_coalescence_breakup_sim, 117 n_sds, 118 n_steps_full, 119 seeds, 120 numba_n_threads=numba_n_threads, 121 double_precision=True, 122 sim_run_filename=SIM_RUN_FNAME + "-coal-break", 123 backends=[CPU, GPU], 124 ) 125 coal_breakup_processed = process_results(res_coal_breakup) 126 filename = f"{SIM_RUN_FNAME}-results-coal_with_breakup-double-n_steps{n_steps_full}" 127 if plot: 128 plot_processed_results( 129 coal_breakup_processed, 130 plot_title=f"coalescence+breakup (n_steps: {n_steps_full})", 131 plot_filename=filename + ".svg", 132 ) 133 if save is not None: 134 write_to_file(filename=filename + ".txt", d=coal_breakup_processed) 135 136 # # Benchmark setup with scaling 137 138 def total_number_from_n_sd(n_sd): 139 return n_sd * 1e8 140 141 def dv_from_n_sd(n_sd): 142 return n_sd * (0.125 * si.m**3) 143 144 # ### Coalescence-only 145 146 res_coalescence_only_scaled = go_benchmark( 147 setup_coalescence_only_sim, 148 n_sds, 149 n_steps_short, 150 seeds, 151 numba_n_threads=numba_n_threads, 152 double_precision=True, 153 sim_run_filename=SIM_RUN_FNAME + "-coalescence-scaled", 154 total_number=total_number_from_n_sd, 155 dv=dv_from_n_sd, 156 backends=[CPU, GPU], 157 ) 158 coalescence_only_processed_scaled = process_results(res_coalescence_only_scaled) 159 filename = ( 160 f"{SIM_RUN_FNAME}-results-scaled-coalescence-double-n_steps{n_steps_short}" 161 ) 162 if plot: 163 plot_processed_results( 164 coalescence_only_processed_scaled, 165 plot_title=f"coalescence-only with scaling (n_steps: {n_steps_short})", 166 plot_filename=filename + ".svg", 167 ) 168 if save is not None: 169 write_to_file(filename=filename + ".txt", d=coalescence_only_processed_scaled) 170 171 # ### Breakup-only 172 173 res_breakup_only_scaled = go_benchmark( 174 setup_breakup_only_sim, 175 n_sds, 176 n_steps_short, 177 seeds, 178 numba_n_threads=numba_n_threads, 179 double_precision=True, 180 sim_run_filename=SIM_RUN_FNAME + "-breakup-scaled", 181 total_number=total_number_from_n_sd, 182 dv=dv_from_n_sd, 183 backends=[CPU, GPU], 184 ) 185 breakup_only_processed_scaled = process_results(res_breakup_only_scaled) 186 filename = f"{SIM_RUN_FNAME}-results-scaled-breakup-double-n_steps{n_steps_short}" 187 if plot: 188 plot_processed_results( 189 breakup_only_processed_scaled, 190 plot_title=f"breakup-only with scaling (n_steps: {n_steps_short})", 191 plot_filename=filename + ".svg", 192 ) 193 if save is not None: 194 write_to_file(filename=filename + ".txt", d=breakup_only_processed_scaled) 195 196 # ### Coalescence and Breakup 197 198 res_coal_breakup_scaled = go_benchmark( 199 setup_coalescence_breakup_sim, 200 n_sds, 201 n_steps_full, 202 seeds, 203 numba_n_threads=numba_n_threads, 204 double_precision=True, 205 sim_run_filename=SIM_RUN_FNAME + "-coal-break-scaled", 206 total_number=total_number_from_n_sd, 207 dv=dv_from_n_sd, 208 backends=[CPU, GPU], 209 ) 210 coal_breakup_processed_scaled = process_results(res_coal_breakup_scaled) 211 filename = ( 212 f"{SIM_RUN_FNAME}-results-scaled-coal_with_breakup-double-n_steps{n_steps_full}" 213 ) 214 if plot: 215 plot_processed_results( 216 coal_breakup_processed_scaled, 217 plot_title=f"coalescence+breakup with scaling (n_steps: {n_steps_full})", 218 plot_filename=filename + ".svg", 219 ) 220 if save is not None: 221 write_to_file(filename=filename + ".txt", d=coal_breakup_processed_scaled) 222 223 224if __name__ == "__main__": 225 main(plot="CI" not in os.environ, save=".")
def
main(plot: bool = True, save: str = None):
24def main(plot: bool = True, save: str = None): 25 26 TIMESTAMP = str(datetime.now().strftime("%Y-%d-%m_%Hh-%Mm-%Ss")) 27 28 SIM_RUN_FNAME = "env_name_" + TIMESTAMP 29 30 assert not os.path.isfile(SIM_RUN_FNAME) 31 32 cmd = [ 33 "bash", 34 "-c", 35 # pylint: disable=no-member 36 f"echo NUMBA_DEFAULT_NUM_THREADS: {numba.config.NUMBA_DEFAULT_NUM_THREADS}" 37 + f">> {SIM_RUN_FNAME}", 38 ] 39 subprocess.run(cmd, check=False) 40 subprocess.run( 41 [ # pylint: disable=no-member 42 "bash", 43 "-c", 44 f"echo NUMBA_NUM_THREADS: {numba.config.NUMBA_NUM_THREADS} >> {SIM_RUN_FNAME}", 45 ], 46 check=False, 47 ) 48 subprocess.run(["bash", "-c", f"lscpu >> {SIM_RUN_FNAME}"], check=False) 49 subprocess.run(["bash", "-c", f"nvidia-smi >> {SIM_RUN_FNAME}"], check=False) 50 subprocess.run(["bash", "-c", f"nvidia-smi -L >> {SIM_RUN_FNAME}"], check=False) 51 subprocess.run(["bash", "-c", f"cat /proc/cpuinfo >> {SIM_RUN_FNAME}"], check=False) 52 53 CI = "CI" in os.environ 54 55 exponents = [3, 5, 8, 10, 12, 14, 16, 18, 20, 22, 24] if not CI else [3, 5] 56 n_sds = [2**i for i in exponents] 57 58 numba_n_threads = [1, 2, 4, 5, 6, 8, 10] if not CI else [1, 2] 59 60 n_realisations = 3 if not CI else 2 61 seeds = list(range(n_realisations)) 62 63 n_steps_short = 100 if not CI else 3 64 n_steps_full = 2048 if not CI else 3 65 66 # # Benchmark regular setup (without scaling) 67 68 # ### Coalescence-only 69 70 res_coalescence_only = go_benchmark( 71 setup_coalescence_only_sim, 72 n_sds, 73 n_steps_short, 74 seeds, 75 numba_n_threads=numba_n_threads, 76 double_precision=True, 77 sim_run_filename=SIM_RUN_FNAME + "-coalescence", 78 backends=[CPU, GPU], 79 ) 80 coalescence_only_processed = process_results(res_coalescence_only) 81 filename = f"{SIM_RUN_FNAME}-results-coalescence-double-n_steps{n_steps_short}" 82 if plot: 83 plot_processed_results( 84 coalescence_only_processed, 85 plot_title=f"coalescence-only (n_steps: {n_steps_short})", 86 plot_filename=filename + ".svg", 87 ) 88 if save is not None: 89 write_to_file(filename=filename + ".txt", d=coalescence_only_processed) 90 91 # ### Breakup-only 92 93 res_breakup_only = go_benchmark( 94 setup_breakup_only_sim, 95 n_sds, 96 n_steps_short, 97 seeds, 98 numba_n_threads=numba_n_threads, 99 double_precision=True, 100 sim_run_filename=SIM_RUN_FNAME + "-breakup", 101 backends=[CPU, GPU], 102 ) 103 breakup_only_processed = process_results(res_breakup_only) 104 filename = f"{SIM_RUN_FNAME}-results-breakup-double-n_steps{n_steps_short}" 105 if plot: 106 plot_processed_results( 107 breakup_only_processed, 108 plot_title=f"breakup-only (n_steps: {n_steps_short})", 109 plot_filename=filename + ".svg", 110 ) 111 if save is not None: 112 write_to_file(filename=filename + ".txt", d=breakup_only_processed) 113 114 # ### Coalescence and Breakup 115 116 res_coal_breakup = go_benchmark( 117 setup_coalescence_breakup_sim, 118 n_sds, 119 n_steps_full, 120 seeds, 121 numba_n_threads=numba_n_threads, 122 double_precision=True, 123 sim_run_filename=SIM_RUN_FNAME + "-coal-break", 124 backends=[CPU, GPU], 125 ) 126 coal_breakup_processed = process_results(res_coal_breakup) 127 filename = f"{SIM_RUN_FNAME}-results-coal_with_breakup-double-n_steps{n_steps_full}" 128 if plot: 129 plot_processed_results( 130 coal_breakup_processed, 131 plot_title=f"coalescence+breakup (n_steps: {n_steps_full})", 132 plot_filename=filename + ".svg", 133 ) 134 if save is not None: 135 write_to_file(filename=filename + ".txt", d=coal_breakup_processed) 136 137 # # Benchmark setup with scaling 138 139 def total_number_from_n_sd(n_sd): 140 return n_sd * 1e8 141 142 def dv_from_n_sd(n_sd): 143 return n_sd * (0.125 * si.m**3) 144 145 # ### Coalescence-only 146 147 res_coalescence_only_scaled = go_benchmark( 148 setup_coalescence_only_sim, 149 n_sds, 150 n_steps_short, 151 seeds, 152 numba_n_threads=numba_n_threads, 153 double_precision=True, 154 sim_run_filename=SIM_RUN_FNAME + "-coalescence-scaled", 155 total_number=total_number_from_n_sd, 156 dv=dv_from_n_sd, 157 backends=[CPU, GPU], 158 ) 159 coalescence_only_processed_scaled = process_results(res_coalescence_only_scaled) 160 filename = ( 161 f"{SIM_RUN_FNAME}-results-scaled-coalescence-double-n_steps{n_steps_short}" 162 ) 163 if plot: 164 plot_processed_results( 165 coalescence_only_processed_scaled, 166 plot_title=f"coalescence-only with scaling (n_steps: {n_steps_short})", 167 plot_filename=filename + ".svg", 168 ) 169 if save is not None: 170 write_to_file(filename=filename + ".txt", d=coalescence_only_processed_scaled) 171 172 # ### Breakup-only 173 174 res_breakup_only_scaled = go_benchmark( 175 setup_breakup_only_sim, 176 n_sds, 177 n_steps_short, 178 seeds, 179 numba_n_threads=numba_n_threads, 180 double_precision=True, 181 sim_run_filename=SIM_RUN_FNAME + "-breakup-scaled", 182 total_number=total_number_from_n_sd, 183 dv=dv_from_n_sd, 184 backends=[CPU, GPU], 185 ) 186 breakup_only_processed_scaled = process_results(res_breakup_only_scaled) 187 filename = f"{SIM_RUN_FNAME}-results-scaled-breakup-double-n_steps{n_steps_short}" 188 if plot: 189 plot_processed_results( 190 breakup_only_processed_scaled, 191 plot_title=f"breakup-only with scaling (n_steps: {n_steps_short})", 192 plot_filename=filename + ".svg", 193 ) 194 if save is not None: 195 write_to_file(filename=filename + ".txt", d=breakup_only_processed_scaled) 196 197 # ### Coalescence and Breakup 198 199 res_coal_breakup_scaled = go_benchmark( 200 setup_coalescence_breakup_sim, 201 n_sds, 202 n_steps_full, 203 seeds, 204 numba_n_threads=numba_n_threads, 205 double_precision=True, 206 sim_run_filename=SIM_RUN_FNAME + "-coal-break-scaled", 207 total_number=total_number_from_n_sd, 208 dv=dv_from_n_sd, 209 backends=[CPU, GPU], 210 ) 211 coal_breakup_processed_scaled = process_results(res_coal_breakup_scaled) 212 filename = ( 213 f"{SIM_RUN_FNAME}-results-scaled-coal_with_breakup-double-n_steps{n_steps_full}" 214 ) 215 if plot: 216 plot_processed_results( 217 coal_breakup_processed_scaled, 218 plot_title=f"coalescence+breakup with scaling (n_steps: {n_steps_full})", 219 plot_filename=filename + ".svg", 220 ) 221 if save is not None: 222 write_to_file(filename=filename + ".txt", d=coal_breakup_processed_scaled)