Coverage for pySDC/tutorial/step_9/E_paradiag_MPI.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-25 20:28 +0000

1""" 

2This script shows how to run ParaDiag with actual MPI parallelism across the time-steps. 

3 

4Parts A to D all used the "virtually parallel" controller, which holds every step in one process and 

5is what you want while developing. Here we use ``controller_ParaDiag_MPI`` instead: one time-step per 

6rank, with the communicator spanning the block that ParaDiag diagonalizes across. 

7 

8The point is that nothing about the *method* changes. The description, the controller parameters and 

9the alpha settings are the ones Part D already used -- ``run`` there takes a communicator, and passing 

10one is the entire difference. So you can develop a setup serially and then run it in parallel without 

11touching it. Run this the way you would run any MPI program:: 

12 

13 mpirun -np 4 python E_paradiag_MPI.py 

14 

15We always integrate the same total number of time-steps and only vary how many of them run in 

16parallel, so the number of ranks is the block size: four steps in total with a block size of one, two 

17or four means windowing through four, two or one block respectively. The README discusses what that 

18does and does not leave unchanged; the short version is that at a given block size this has to agree 

19exactly with Part D, and across block sizes it solves the same problem either way. 

20 

21Two properties of ParaDiag are worth keeping in mind when going parallel, because they differ from 

22PFASST: 

23 

24- All steps of a block have to iterate together. In PFASST an early step can converge and drop out of 

25 the iteration, which is what makes it pipelined. ParaDiag cannot do that: the transform in time 

26 needs every step, so a step that stopped early would leave the others waiting forever. 

27- The block is therefore always full. If the end time does not divide into whole blocks, ParaDiag 

28 solves past it rather than truncating, and says so. 

29""" 

30 

31import sys 

32from pathlib import Path 

33 

34from mpi4py import MPI 

35 

36from pySDC.tutorial.step_9.D_adaptive_alpha import alpha_settings, format_result, run 

37 

38 

39def main(fname='step_9_E_out.txt'): 

40 """ 

41 Run every alpha setting on this communicator, one time-step per rank. 

42 

43 The block size is simply ``MPI.COMM_WORLD.size``, so there is nothing to configure: run it on one 

44 rank and ParaDiag windows through four blocks of one step, run it on four and it does one block 

45 of four. 

46 

47 Args: 

48 fname (str): file under ``data/`` to append the results to 

49 """ 

50 

51 comm = MPI.COMM_WORLD 

52 

53 lines = [] 

54 for alpha in alpha_settings: 

55 uend, niter, error, final_alpha = run(alpha, comm.size, comm=comm) 

56 # the block size goes in the label: the tutorial's output file holds every block size 

57 lines.append(format_result(f'MPI on {comm.size}', alpha, niter, error, final_alpha)) 

58 

59 # only the last rank holds the end point of the block, so only it writes the output 

60 if comm.rank == comm.size - 1: 

61 Path("data").mkdir(parents=True, exist_ok=True) 

62 with open('data/' + fname, 'a') as f: 

63 for line in lines: 

64 f.write(line + '\n') 

65 print(line) 

66 

67 

68if __name__ == "__main__": 

69 # Run it as you would any MPI program, one rank per time-step: 

70 # mpirun -np 4 python E_paradiag_MPI.py 

71 main(sys.argv[1] if len(sys.argv) == 2 else 'step_9_E_out.txt')