Coverage for pySDC/projects/Performance/run_simple_forcing_benchmark.py: 0%

83 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-29 09:02 +0000

1from argparse import ArgumentParser 

2import numpy as np 

3from mpi4py import MPI 

4 

5from pySDC.helpers.stats_helper import get_sorted 

6 

7from pySDC.implementations.controller_classes.controller_MPI import controller_MPI 

8from pySDC.implementations.sweeper_classes.imex_1st_order import imex_1st_order 

9from pySDC.implementations.problem_classes.AllenCahn_MPIFFT import allencahn_imex, allencahn_imex_timeforcing 

10from pySDC.implementations.transfer_classes.TransferMesh_MPIFFT import fft_to_fft 

11 

12# from pySDC.projects.AllenCahn_Bayreuth.AllenCahn_dump import dump 

13 

14# from pySDC.projects.Performance.controller_MPI_scorep import controller_MPI 

15 

16 

17def run_simulation(name=None, nprocs_space=None): 

18 """ 

19 A simple test program to do PFASST runs for the AC equation 

20 """ 

21 

22 # set MPI communicator 

23 comm = MPI.COMM_WORLD 

24 

25 world_rank = comm.Get_rank() 

26 world_size = comm.Get_size() 

27 

28 # split world communicator to create space-communicators 

29 if nprocs_space is not None: 

30 color = int(world_rank / nprocs_space) 

31 else: 

32 color = int(world_rank / 1) 

33 space_comm = comm.Split(color=color) 

34 space_comm.Set_name('Space-Comm') 

35 space_size = space_comm.Get_size() 

36 space_rank = space_comm.Get_rank() 

37 

38 # split world communicator to create time-communicators 

39 if nprocs_space is not None: 

40 color = int(world_rank % nprocs_space) 

41 else: 

42 color = int(world_rank / world_size) 

43 time_comm = comm.Split(color=color) 

44 time_comm.Set_name('Time-Comm') 

45 time_size = time_comm.Get_size() 

46 time_rank = time_comm.Get_rank() 

47 

48 # print(time_size, space_size, world_size) 

49 

50 # initialize level parameters 

51 level_params = dict() 

52 level_params['restol'] = 1e-08 

53 level_params['dt'] = 1e-03 

54 level_params['nsweeps'] = [3, 1] 

55 

56 # initialize sweeper parameters 

57 sweeper_params = dict() 

58 sweeper_params['quad_type'] = 'RADAU-RIGHT' 

59 sweeper_params['num_nodes'] = [3] 

60 sweeper_params['QI'] = ['LU'] # For the IMEX sweeper, the LU-trick can be activated for the implicit part 

61 sweeper_params['initial_guess'] = 'zero' 

62 

63 # initialize problem parameters 

64 problem_params = dict() 

65 problem_params['L'] = 4.0 

66 # problem_params['L'] = 16.0 

67 problem_params['nvars'] = [(48 * 12, 48 * 12), (8 * 12, 8 * 12)] 

68 # problem_params['nvars'] = [(48 * 48, 48 * 48), (8 * 48, 8 * 48)] 

69 problem_params['eps'] = [0.04] 

70 problem_params['radius'] = 0.25 

71 problem_params['comm'] = space_comm 

72 problem_params['name'] = name 

73 problem_params['init_type'] = 'circle_rand' 

74 problem_params['spectral'] = False 

75 

76 if name == 'AC-bench-constforce': 

77 problem_params['dw'] = [-23.59] 

78 

79 # initialize step parameters 

80 step_params = dict() 

81 step_params['maxiter'] = 50 

82 

83 # initialize controller parameters 

84 controller_params = dict() 

85 controller_params['logger_level'] = 30 if space_rank == 0 else 99 # set level depending on rank 

86 controller_params['predict_type'] = 'fine_only' 

87 # controller_params['hook_class'] = dump # activate to get data output at each step 

88 

89 # fill description dictionary for easy step instantiation 

90 description = dict() 

91 description['problem_params'] = problem_params # pass problem parameters 

92 description['sweeper_class'] = imex_1st_order 

93 description['sweeper_params'] = sweeper_params # pass sweeper parameters 

94 description['level_params'] = level_params # pass level parameters 

95 description['step_params'] = step_params # pass step parameters 

96 description['space_transfer_class'] = fft_to_fft 

97 

98 if name == 'AC-bench-noforce' or name == 'AC-bench-constforce': 

99 description['problem_class'] = allencahn_imex 

100 elif name == 'AC-bench-timeforce': 

101 description['problem_class'] = allencahn_imex_timeforcing 

102 else: 

103 raise NotImplementedError(f'{name} is not implemented') 

104 

105 # set time parameters 

106 t0 = 0.0 

107 Tend = 240 * 0.001 

108 

109 if space_rank == 0 and time_rank == 0: 

110 out = f'---------> Running {name} with {time_size} process(es) in time and {space_size} process(es) in space...' 

111 print(out) 

112 

113 # instantiate controller 

114 controller = controller_MPI(controller_params=controller_params, description=description, comm=time_comm) 

115 

116 # get initial values on finest level 

117 P = controller.S.levels[0].prob 

118 uinit = P.u_exact(t0) 

119 

120 # call main function to get things done... 

121 uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend) 

122 

123 timing = get_sorted(stats, type='timing_setup', sortby='time') 

124 max_timing_setup = time_comm.allreduce(timing[0][1], MPI.MAX) 

125 timing = get_sorted(stats, type='timing_run', sortby='time') 

126 max_timing = time_comm.allreduce(timing[0][1], MPI.MAX) 

127 

128 if space_rank == 0 and time_rank == time_size - 1: 

129 print() 

130 

131 out = f'Setup time: {max_timing_setup:.4f} sec.' 

132 print(out) 

133 

134 out = f'Time to solution: {max_timing:.4f} sec.' 

135 print(out) 

136 

137 iter_counts = get_sorted(stats, type='niter', sortby='time') 

138 niters = np.array([item[1] for item in iter_counts]) 

139 out = f'Mean number of iterations: {np.mean(niters):.4f}' 

140 print(out) 

141 

142 space_comm.Free() 

143 time_comm.Free() 

144 

145 

146if __name__ == "__main__": 

147 # Add parser to get number of processors in space and setup (have to do this here to enable automatic testing) 

148 parser = ArgumentParser() 

149 parser.add_argument( 

150 "-s", 

151 "--setup", 

152 help='Specifies the setup', 

153 type=str, 

154 default='AC-bench-noforce', 

155 choices=['AC-bench-noforce', 'AC-bench-constforce', 'AC-bench-timeforce'], 

156 ) 

157 parser.add_argument("-n", "--nprocs_space", help='Specifies the number of processors in space', type=int) 

158 args = parser.parse_args() 

159 

160 run_simulation(name=args.setup, nprocs_space=args.nprocs_space)