I am trying to write a MPI program on Python to calculate the average of the neighbors value.
My algorithm is as follows
Initialize value with the neighbors you have. Then Compute the average of your neighbors value and subtract from new value.
I have written the following program
from mpi4py import MPI
import sys
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
myvalue=2
sum=0
neighb=1
nvalue=0
if rank == 0 or rank == size-1:
myvalue=1
neighb=myvalue
if rank < size-1:
msg = myvalue
comm.send(msg, dest=rank+1)
sys.stdout.write("rank send %d: %s" % (rank, myvalue))
comm.recv(nvalue,source=rank+1)
sum+=nvalue
sys.stdout.write("rank RECV %d: %s" % (rank, nvalue))
if rank > 0:
comm.recv(nvalue,rank-1)
sum+=nvalue
sys.stdout.write("rank RECV %d: %s" % (rank, nvalue))
comm.send(myvalue,rank-1)
sys.stdout.write("rank send %d: %s" % (rank, myvalue))
avg =sum/neighb
myvalue-=avg
But the program is not working it gets stuck, the following window appears when i try to run it on console using below command,
mpiexec -n 4 python p2p_linnear.py -m mpi4py
insert some print statements whether MPI has been correctly initialized. e.g. size and rank
did you install MPI from microsoft?
mpi4py requires that Microsoft MPI 6 be installed on the host system. That is a systemwide installation that is currently not available through conda. In order to successfully use mpi4py you must install Microsoft MPI and then append the bin directory of the MPI installation to your PATH environment variable. To install Microsoft MPI see https://www.microsoft.com/en-us/download/details.aspx?id=47259
and you are using blocking communication (send and recv) but what you want is sending the data to all other ranks and therefore you need non-blocking communication (isend, irecv).
Also: the recv method does not take the variable to be received as argument:
nvalue=comm.recv(source=rank+1)