|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
| 3 | +import pickle |
| 4 | +from pySDC.helpers.stats_helper import get_sorted |
| 5 | +from pySDC.projects.GPU.configs.base_config import get_config |
| 6 | +from pySDC.projects.GPU.etc.generate_jobscript import write_jobscript, PROJECT_PATH |
| 7 | + |
| 8 | + |
| 9 | +class ScalingConfig(object): |
| 10 | + cluster = None |
| 11 | + config = '' |
| 12 | + base_resolution = -1 |
| 13 | + base_resolution_weak = -1 |
| 14 | + useGPU = False |
| 15 | + partition = None |
| 16 | + tasks_per_node = None |
| 17 | + ndim = 2 |
| 18 | + tasks_time = 1 |
| 19 | + max_steps_space = None |
| 20 | + max_steps_space_weak = None |
| 21 | + |
| 22 | + def __init__(self, space_time_parallel): |
| 23 | + if space_time_parallel in ['False', False]: |
| 24 | + self._tasks_time = 1 |
| 25 | + else: |
| 26 | + self._tasks_time = self.tasks_time |
| 27 | + |
| 28 | + def get_resolution_and_tasks(self, strong, i): |
| 29 | + if strong: |
| 30 | + return self.base_resolution, [1, self._tasks_time, 2**i] |
| 31 | + else: |
| 32 | + return self.base_resolution_weak * (2**i), [1, self._tasks_time, (2 * self.ndim) ** i] |
| 33 | + |
| 34 | + def run_scaling_test(self, strong=True): |
| 35 | + max_steps = self.max_steps_space if strong else self.max_steps_space_weak |
| 36 | + for i in range(max_steps): |
| 37 | + res, procs = self.get_resolution_and_tasks(strong, i) |
| 38 | + |
| 39 | + sbatch_options = [f'-n {np.prod(procs)}', f'-p {self.partition}'] |
| 40 | + if self.useGPU: |
| 41 | + srun_options = ['--cpus-per-task=4', '--gpus-per-task=1'] |
| 42 | + sbatch_options += ['--cpus-per-task=4', '--gpus-per-task=1'] |
| 43 | + else: |
| 44 | + srun_options = [] |
| 45 | + |
| 46 | + procs = (''.join(f'{me}/' for me in procs))[:-1] |
| 47 | + command = f'run_experiment.py --mode=run --res={res} --config={self.config} --procs={procs}' |
| 48 | + |
| 49 | + if self.useGPU: |
| 50 | + command += ' --useGPU=True' |
| 51 | + |
| 52 | + write_jobscript(sbatch_options, srun_options, command, self.cluster) |
| 53 | + |
| 54 | + def plot_scaling_test(self, strong, ax, plot_ideal=False, **plotting_params): |
| 55 | + timings = {} |
| 56 | + |
| 57 | + max_steps = self.max_steps_space if strong else self.max_steps_space_weak |
| 58 | + for i in range(max_steps): |
| 59 | + res, procs = self.get_resolution_and_tasks(strong, i) |
| 60 | + |
| 61 | + args = {'useGPU': self.useGPU, 'config': self.config, 'res': res, 'procs': procs, 'mode': None} |
| 62 | + |
| 63 | + config = get_config(args) |
| 64 | + |
| 65 | + path = f'data/{config.get_path(ranks=[me -1 for me in procs])}-stats-whole-run.pickle' |
| 66 | + with open(path, 'rb') as file: |
| 67 | + stats = pickle.load(file) |
| 68 | + |
| 69 | + timing_step = get_sorted(stats, type='timing_step') |
| 70 | + |
| 71 | + timings[np.prod(procs) / self.tasks_per_node] = np.mean([me[1] for me in timing_step]) |
| 72 | + |
| 73 | + ax.loglog(timings.keys(), timings.values(), **plotting_params) |
| 74 | + if plot_ideal: |
| 75 | + ax.loglog( |
| 76 | + timings.keys(), |
| 77 | + list(timings.values())[0] * list(timings.keys())[0] / np.array(list(timings.keys())), |
| 78 | + ls='--', |
| 79 | + color='grey', |
| 80 | + label='ideal', |
| 81 | + ) |
| 82 | + ax.set_xlabel(r'$N_\mathrm{nodes}$') |
| 83 | + ax.set_ylabel(r'$t_\mathrm{step}$') |
| 84 | + |
| 85 | + |
| 86 | +class CPUConfig(ScalingConfig): |
| 87 | + cluster = 'jusuf' |
| 88 | + partition = 'batch' |
| 89 | + tasks_per_node = 128 |
| 90 | + |
| 91 | + |
| 92 | +class GPUConfig(ScalingConfig): |
| 93 | + cluster = 'booster' |
| 94 | + partition = 'booster' |
| 95 | + tasks_per_node = 4 |
| 96 | + useGPU = True |
| 97 | + |
| 98 | + |
| 99 | +class GrayScottSpaceScalingCPU(CPUConfig, ScalingConfig): |
| 100 | + base_resolution = 2048 |
| 101 | + base_resolution_weak = 256 |
| 102 | + config = 'GS_scaling' |
| 103 | + max_steps_space = 10 |
| 104 | + max_steps_space_weak = 6 |
| 105 | + tasks_time = 3 |
| 106 | + |
| 107 | + |
| 108 | +class GrayScottSpaceScalingGPU(GPUConfig, ScalingConfig): |
| 109 | + base_resolution_weak = 256 * 32 |
| 110 | + base_resolution = 2048 |
| 111 | + config = 'GS_scaling' |
| 112 | + max_steps_space = 4 |
| 113 | + max_steps_space_weak = 4 |
| 114 | + tasks_time = 3 |
| 115 | + |
| 116 | + |
| 117 | +def plot_scalings(strong, problem, kwargs): |
| 118 | + if problem == 'GS': |
| 119 | + fig, ax = plt.subplots() |
| 120 | + |
| 121 | + plottings_params = [ |
| 122 | + {'plot_ideal': strong, 'marker': 'x', 'label': 'CPU'}, |
| 123 | + {'marker': '>', 'label': 'CPU space time parallel'}, |
| 124 | + {'marker': '^', 'label': 'GPU'}, |
| 125 | + ] |
| 126 | + configs = [ |
| 127 | + GrayScottSpaceScalingCPU(space_time_parallel=False), |
| 128 | + GrayScottSpaceScalingCPU(space_time_parallel=True), |
| 129 | + GrayScottSpaceScalingGPU(space_time_parallel=False), |
| 130 | + ] |
| 131 | + |
| 132 | + for config, params in zip(configs, plottings_params): |
| 133 | + config.plot_scaling_test(strong=strong, ax=ax, **params) |
| 134 | + ax.legend(frameon=False) |
| 135 | + fig.savefig(f'{PROJECT_PATH}/plots/{"strong" if strong else "weak"}_scaling_{problem}.pdf', bbox_inches='tight') |
| 136 | + else: |
| 137 | + raise NotImplementedError |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == '__main__': |
| 141 | + import argparse |
| 142 | + |
| 143 | + parser = argparse.ArgumentParser() |
| 144 | + parser.add_argument('--scaling', type=str, choices=['strong', 'weak'], default='strong') |
| 145 | + parser.add_argument('--mode', type=str, choices=['run', 'plot'], default='run') |
| 146 | + parser.add_argument('--problem', type=str, default='GS') |
| 147 | + parser.add_argument('--XPU', type=str, choices=['CPU', 'GPU'], default='CPU') |
| 148 | + parser.add_argument('--space_time', type=str, choices=['True', 'False'], default='False') |
| 149 | + |
| 150 | + args = parser.parse_args() |
| 151 | + |
| 152 | + strong = args.scaling == 'strong' |
| 153 | + |
| 154 | + if args.problem == 'GS': |
| 155 | + if args.XPU == 'CPU': |
| 156 | + configClass = GrayScottSpaceScalingCPU |
| 157 | + else: |
| 158 | + configClass = GrayScottSpaceScalingGPU |
| 159 | + else: |
| 160 | + raise NotImplementedError(f'Don\'t know problem {args.problem!r}') |
| 161 | + |
| 162 | + kwargs = {'space_time_parallel': args.space_time} |
| 163 | + config = configClass(**kwargs) |
| 164 | + |
| 165 | + if args.mode == 'run': |
| 166 | + config.run_scaling_test(strong=strong) |
| 167 | + elif args.mode == 'plot': |
| 168 | + plot_scalings(strong=strong, problem=args.problem, kwargs=kwargs) |
| 169 | + else: |
| 170 | + raise NotImplementedError(f'Don\'t know mode {args.mode!r}') |
0 commit comments