|
| 1 | +from argparse import ArgumentParser |
| 2 | +import numpy as np |
| 3 | +from mpi4py import MPI |
| 4 | + |
| 5 | +from pySDC.helpers.stats_helper import filter_stats, sort_stats |
| 6 | +from pySDC.implementations.collocation_classes.gauss_radau_right import CollGaussRadau_Right |
| 7 | +from pySDC.implementations.controller_classes.controller_MPI import controller_MPI |
| 8 | +from pySDC.implementations.sweeper_classes.imex_1st_order import imex_1st_order |
| 9 | +from pySDC.implementations.problem_classes.AllenCahn_MPIFFT import allencahn_imex, allencahn_imex_timeforcing |
| 10 | +from pySDC.implementations.transfer_classes.TransferMesh_MPIFFT import fft_to_fft |
| 11 | +from pySDC.projects.AllenCahn_Bayreuth.AllenCahn_dump import dump |
| 12 | + |
| 13 | + |
| 14 | +def run_simulation(name=None, nprocs_space=None): |
| 15 | + """ |
| 16 | + A simple test program to do PFASST runs for the AC equation |
| 17 | + """ |
| 18 | + |
| 19 | + # set MPI communicator |
| 20 | + comm = MPI.COMM_WORLD |
| 21 | + |
| 22 | + world_rank = comm.Get_rank() |
| 23 | + world_size = comm.Get_size() |
| 24 | + |
| 25 | + # split world communicator to create space-communicators |
| 26 | + if nprocs_space is not None: |
| 27 | + color = int(world_rank / nprocs_space) |
| 28 | + else: |
| 29 | + color = int(world_rank / 1) |
| 30 | + space_comm = comm.Split(color=color) |
| 31 | + space_comm.Set_name('Space-Comm') |
| 32 | + space_size = space_comm.Get_size() |
| 33 | + space_rank = space_comm.Get_rank() |
| 34 | + |
| 35 | + # split world communicator to create time-communicators |
| 36 | + if nprocs_space is not None: |
| 37 | + color = int(world_rank % nprocs_space) |
| 38 | + else: |
| 39 | + color = int(world_rank / world_size) |
| 40 | + time_comm = comm.Split(color=color) |
| 41 | + time_comm.Set_name('Time-Comm') |
| 42 | + time_size = time_comm.Get_size() |
| 43 | + time_rank = time_comm.Get_rank() |
| 44 | + |
| 45 | + # print(time_size, space_size, world_size) |
| 46 | + |
| 47 | + # initialize level parameters |
| 48 | + level_params = dict() |
| 49 | + level_params['restol'] = 1E-08 |
| 50 | + level_params['dt'] = 1E-03 |
| 51 | + level_params['nsweeps'] = [3, 1] |
| 52 | + |
| 53 | + # initialize sweeper parameters |
| 54 | + sweeper_params = dict() |
| 55 | + sweeper_params['collocation_class'] = CollGaussRadau_Right |
| 56 | + sweeper_params['num_nodes'] = [3] |
| 57 | + sweeper_params['QI'] = ['LU'] # For the IMEX sweeper, the LU-trick can be activated for the implicit part |
| 58 | + sweeper_params['initial_guess'] = 'zero' |
| 59 | + |
| 60 | + # initialize problem parameters |
| 61 | + problem_params = dict() |
| 62 | + # This defines the number of 'patches' for the simulation per dimension in 2D. L=4 means: 4x4 patches |
| 63 | + problem_params['L'] = 4.0 |
| 64 | + # problem_params['L'] = 16.0 |
| 65 | + # This defines the number of nodes in space, ideally with about 144 nodes per patch (48 * 12 / 4) |
| 66 | + problem_params['nvars'] = [(48 * 12, 48 * 12), (8 * 12, 8 * 12)] |
| 67 | + # problem_params['nvars'] = [(48 * 48, 48 * 48), (8 * 48, 8 * 48)] |
| 68 | + problem_params['eps'] = [0.04] |
| 69 | + problem_params['radius'] = 0.25 |
| 70 | + problem_params['comm'] = space_comm |
| 71 | + problem_params['name'] = name |
| 72 | + problem_params['init_type'] = 'circle_rand' |
| 73 | + problem_params['spectral'] = False |
| 74 | + |
| 75 | + if name == 'AC-bench-constforce': |
| 76 | + problem_params['dw'] = [-23.59] |
| 77 | + |
| 78 | + # initialize step parameters |
| 79 | + step_params = dict() |
| 80 | + step_params['maxiter'] = 50 |
| 81 | + |
| 82 | + # initialize controller parameters |
| 83 | + controller_params = dict() |
| 84 | + controller_params['logger_level'] = 20 if space_rank == 0 else 99 # set level depending on rank |
| 85 | + controller_params['predict_type'] = 'fine_only' |
| 86 | + # controller_params['hook_class'] = dump # activate to get data output at each step |
| 87 | + |
| 88 | + # fill description dictionary for easy step instantiation |
| 89 | + description = dict() |
| 90 | + description['problem_params'] = problem_params # pass problem parameters |
| 91 | + description['sweeper_class'] = imex_1st_order |
| 92 | + description['sweeper_params'] = sweeper_params # pass sweeper parameters |
| 93 | + description['level_params'] = level_params # pass level parameters |
| 94 | + description['step_params'] = step_params # pass step parameters |
| 95 | + description['space_transfer_class'] = fft_to_fft |
| 96 | + |
| 97 | + if name == 'AC-bench-noforce' or name == 'AC-bench-constforce': |
| 98 | + description['problem_class'] = allencahn_imex |
| 99 | + elif name == 'AC-bench-timeforce': |
| 100 | + description['problem_class'] = allencahn_imex_timeforcing |
| 101 | + else: |
| 102 | + raise NotImplementedError(f'{name} is not implemented') |
| 103 | + |
| 104 | + # set time parameters |
| 105 | + t0 = 0.0 |
| 106 | + Tend = 4 * 0.001 |
| 107 | + |
| 108 | + if space_rank == 0 and time_rank == 0: |
| 109 | + out = f'---------> Running {name} with {time_size} process(es) in time and {space_size} process(es) in space...' |
| 110 | + print(out) |
| 111 | + |
| 112 | + # instantiate controller |
| 113 | + controller = controller_MPI(controller_params=controller_params, description=description, comm=time_comm) |
| 114 | + |
| 115 | + # get initial values on finest level |
| 116 | + P = controller.S.levels[0].prob |
| 117 | + uinit = P.u_exact(t0) |
| 118 | + |
| 119 | + # call main function to get things done... |
| 120 | + uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend) |
| 121 | + |
| 122 | + timing = sort_stats(filter_stats(stats, type='timing_setup'), sortby='time') |
| 123 | + max_timing_setup = time_comm.allreduce(timing[0][1], MPI.MAX) |
| 124 | + timing = sort_stats(filter_stats(stats, type='timing_run'), sortby='time') |
| 125 | + max_timing = time_comm.allreduce(timing[0][1], MPI.MAX) |
| 126 | + |
| 127 | + if space_rank == 0 and time_rank == time_size - 1: |
| 128 | + print() |
| 129 | + |
| 130 | + out = f'Setup time: {max_timing_setup:.4f} sec.' |
| 131 | + print(out) |
| 132 | + |
| 133 | + out = f'Time to solution: {max_timing:.4f} sec.' |
| 134 | + print(out) |
| 135 | + |
| 136 | + iter_counts = sort_stats(filter_stats(stats, type='niter'), sortby='time') |
| 137 | + niters = np.array([item[1] for item in iter_counts]) |
| 138 | + out = f'Mean number of iterations: {np.mean(niters):.4f}' |
| 139 | + print(out) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + # Add parser to get number of processors in space and setup (have to do this here to enable automatic testing) |
| 144 | + # Run this file via `mpirun -np N python run_parallel_AC_MPIFFT.py -n P`, |
| 145 | + # where N is the overall number of processors and P is the number of processors used for spatial parallelization. |
| 146 | + parser = ArgumentParser() |
| 147 | + parser.add_argument("-s", "--setup", help='Specifies the setup', type=str, default='AC-bench-noforce', |
| 148 | + choices=['AC-bench-noforce', 'AC-bench-constforce', 'AC-bench-timeforce']) |
| 149 | + parser.add_argument("-n", "--nprocs_space", help='Specifies the number of processors in space', type=int) |
| 150 | + args = parser.parse_args() |
| 151 | + |
| 152 | + run_simulation(name=args.setup, nprocs_space=args.nprocs_space) |
| 153 | + |
0 commit comments