Coverage for pySDC/tutorial/step_6/C_MPI_parallelization.py: 97%

30 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 14:51 +0000

1import os 

2import subprocess 

3 

4 

5def main(cwd): 

6 """ 

7 A simple test program to test MPI-parallel PFASST controllers 

8 Args: 

9 cwd: current working directory 

10 """ 

11 

12 # try to import MPI here, will fail if things go wrong (and not in the subprocess part) 

13 try: 

14 import mpi4py 

15 

16 del mpi4py 

17 except ImportError as e: 

18 raise ImportError('petsc tests need mpi4py') from e 

19 

20 # Set python path once 

21 my_env = os.environ.copy() 

22 my_env['PYTHONPATH'] = '../../..:.' 

23 my_env['COVERAGE_PROCESS_START'] = 'pyproject.toml' 

24 

25 # set list of number of parallel steps (even) 

26 num_procs_list = [1, 2, 4, 8] 

27 

28 # set up new/empty file for output 

29 fname = 'step_6_C1_out.txt' 

30 f = open(cwd + '/../../../data/' + fname, 'w') 

31 f.close() 

32 

33 # run code with different number of MPI processes 

34 for num_procs in num_procs_list: 

35 print('Running code with %2i processes...' % num_procs) 

36 cmd = ( 

37 'mpirun -np ' + str(num_procs) + ' python playground_parallelization.py ../../../../data/' + fname 

38 ).split() 

39 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=my_env, cwd=cwd) 

40 # while True: 

41 # line = p.stdout.readline() 

42 # print(line) 

43 # if not line: break 

44 p.wait() 

45 assert p.returncode == 0, 'ERROR: did not get return code 0, got %s with %2i processes' % ( 

46 p.returncode, 

47 num_procs, 

48 ) 

49 

50 # set list of number of parallel steps (odd) 

51 num_procs_list = [3, 5, 7, 9] 

52 

53 # set up new/empty file for output 

54 fname = 'step_6_C2_out.txt' 

55 f = open(cwd + '/../../../data/' + fname, 'w') 

56 f.close() 

57 

58 # run code with different number of MPI processes 

59 for num_procs in num_procs_list: 

60 print('Running code with %2i processes...' % num_procs) 

61 cmd = ( 

62 'mpirun -np ' + str(num_procs) + ' python playground_parallelization.py ../../../../data/' + fname 

63 ).split() 

64 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=my_env, cwd=cwd) 

65 # while True: 

66 # line = p.stdout.readline() 

67 # print(line) 

68 # if not line: break 

69 p.wait() 

70 assert p.returncode == 0, 'ERROR: did not get return code 0, got %s with %2i processes' % ( 

71 p.returncode, 

72 num_procs, 

73 ) 

74 

75 

76if __name__ == "__main__": 

77 main('.')