Debian Codegraph @

docs/ipyparallel_intro.py

raw
1 # # Introduction to IPython parallel
2 # The following demos heavily rely on {py:mod}`IPython-parallel<ipyparallel>` to illustrate how
3 # checkpointing works when using multiple {py:mod}`MPI<mpi4py>` processes.
4 # We illustrate what happens in parallel by launching three MPI processes
5 # using IPython-parallel's MPI engine support.
6
7 import logging
8
9 import ipyparallel as ipp
10
11
12 def hello_mpi():
13 # We define all imports inside the function as they have to be launched on the remote engines
14 from mpi4py import MPI
15
16 print(f"Hello from rank {MPI.COMM_WORLD.rank}/{MPI.COMM_WORLD.size - 1}")
17
18
19 with ipp.Cluster(engines="mpi", n=3, log_level=logging.ERROR) as cluster:
20 # We send the query to run the function `hello_mpi` on all engines
21 query: ipp.AsyncResult = cluster[:].apply_async(hello_mpi)
22 # We wait for all engines to finish
23 query.wait()
24 # We check that all engines exited successfully
25 assert query.successful(), query.error
26 # We print the output from each engine
27 print("".join(query.stdout))