How to set the number of processes in mpi4py

How to set a fixed number of processes in mpi4py? In mpi implementations of other languages, it is passed to init (args) as an argument. The documentation does not seem to mention this. Does anyone know how to do this? The program will run on a regular dual-core laptop and 24 nodes (96 cores), and I would like to emulate a cluster on a laptop.

PS. Sorry if it really is in the documentation - this is rather cryptic for someone new to mpi.

+4
source share
2 answers

The easiest way is to simply use mpiexec (or mpirun) to run the program, specifying the number of MPI tasks required:

$ cat foo.py from mpi4py import MPI comm = MPI.COMM_WORLD nprocs = comm.Get_size() rank = comm.Get_rank() if rank == 0: data = 'Hello!' comm.send(data, dest=nprocs-1, tag=1) elif rank == nprocs-1: data = comm.recv(source=0, tag=1) print 'Rank ', rank, ' received ', data $ mpiexec -np 4 python foo.py Rank 3 received Hello! 

Note, however, that working with 96 tasks on your laptop will probably not be particularly useful.

+10
source

The mpi4py tutorial does this as follows:

 from mpi4py import MPI import sys client_script = 'my_client.py' comm = MPI.COMM_SELF.Spawn(sys.executable, args=[client_script], maxprocs=5) 
+2
source

Source: https://habr.com/ru/post/1391683/


All Articles