Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set number of processes in mpi4py

Tags:

python

mpi

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

PS. Sorry if it actually is in the documentation - it is quite cryptic to someone new to mpi.

like image 476
kyooryu Avatar asked Dec 13 '22 06:12

kyooryu


1 Answers

Easiest is just to use mpiexec (or mpirun) to launch the program, specifying the number of MPI tasks you want:

$ 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, though, that running with 96 tasks on your laptop is probably not going to be especially useful.

like image 186
Jonathan Dursi Avatar answered Jan 01 '23 17:01

Jonathan Dursi