TypeError: recv() got an unexpected keyword argument 'dest'

1.1k Views Asked by At

I am trying to use MPI in python to do some parallel computing for midpoint integration. I am not really familiar with MPI and I have looked around at some examples to produce what I have thus far. I am having trouble with a couple of errors where MPI.COMM does not recognize a couple of input arguments. Again I am not that familiar with MPI.

Below I have attached my code:

from mpi4py import MPI
import numpy as np
from numpy import *
import time

#initialize variables
n = 10e5 #number of increments within each process
a = 0.0; #lower bound
b = 5.0; #upper bound
dest = 0; #define the process that computes the final result

#Functions
def integral(my_a, num, h):
    s = 0;
    h2 = h/2;
    for i in range(0,num):
        temp = my_a + i*h;
        s = s + fct(temp+h2)*h;
    return (s)

def fct(x):
    return x**2;

#Start the MPI process
comm = MPI.COMM_WORLD
p = comm.Get_size(); #gather number of processes
print "Number of processes (p): ", p;
myid = comm.Get_rank() #gather rank of the comm (number of cores)
print "Rank of (p): ", myid;

h = (b-a)/n; #length of increment
num = int(n/p); #number of intervals calculated by each process
print "Number of intervals calculated by a process: ", num;
my_range = (b-a)/p; #range per process
my_a = a + myid*my_range; #next lower limit
ti = time.clock();
my_result = integral(my_a,num,h) #get the result

print "Process " + str(myid) + " has the partial result of " + str(my_result) + ".";

if myid == 0:
    result = my_result;
    for i in range(1,p):
        source = 1;
        comm.recv(my_result, dest=1, tag=123);
        result = result + my_result;
    print "The result = " + str(result) + ".";

else :
    comm.send(my_result, source=0, tag=123);
    MPI_Finalize();


tf = time.clock();
print "Time(s): ", tf-ti;

Here is the error that I get when I try to run this code:

--------------------------------------------------------------------------
*******************************-VirtualBox ~/Documents/ME701/HW/HW5 $ mpirun -np 2 python HW5_prb3.py
Number of processes (p):  2
Rank of (p):  1
Number of intervals calculated by a process:  500000
Number of processes (p):  2
Rank of (p):  0
Number of intervals calculated by a process:  500000
Process 1 has the partial result of 36.4583333333.
Traceback (most recent call last):
  File "HW5_prb3.py", line 50, in <module>
    comm.send(my_result, source=0, tag=123);
  File "Comm.pyx", line 1127, in mpi4py.MPI.Comm.send (src/mpi4py.MPI.c:90067)
**TypeError: send() got an unexpected keyword argument 'source'**
Process 0 has the partial result of 5.20833333333.
Traceback (most recent call last):
  File "HW5_prb3.py", line 45, in <module>
    comm.recv(my_result, dest=1, tag=123);
  File "Comm.pyx", line 1142, in mpi4py.MPI.Comm.recv (src/mpi4py.MPI.c:90513)
**TypeError: recv() got an unexpected keyword argument 'dest'**
-------------------------------------------------------
Primary job  terminated normally, but 1 process returned
a non-zero exit code.. Per user-direction, the job has been aborted.
-------------------------------------------------------
--------------------------------------------------------------------------
mpirun detected that one or more processes exited with non-zero status, thus causing
the job to be terminated. The first process to do so was:

  Process name: [[45152,1],1]
  Exit code:    1
--------------------------------------------------------------------------
*******************************-VirtualBox ~/Documents/ME701/HW/HW5 $ 

The answer to the midpoint integration should be 41.66667. My teacher just wants us to perform a simple time study on parallel computing so we can see the power of it.

Thank you for your time.

1

There are 1 best solutions below

2
On

I guess you just mixed up send and recv arguments - source is the process rank you receive data from, and dest (short for destination) is the rank of the process you send data to (you can see the docs, if you want).

So, just interchanging source and dest keywords in send and receive should be fine.