0

Python で MPI を使用して、ミッドポイント統合のための並列計算を実行しようとしています。私は MPI にあまり詳しくありませんが、これまでに作成したものを作成するためにいくつかの例を見てきました。MPI.COMM がいくつかの入力引数を認識しないといういくつかのエラーで問題が発生しています。繰り返しますが、私は MPI にあまり詳しくありません。

以下に私のコードを添付しました:

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;

このコードを実行しようとすると、次のエラーが表示されます。

--------------------------------------------------------------------------
*******************************-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 $ 

中点積分の答えは 41.66667 です。私の先生は、私たちが並列計算の威力を理解できるように、並列計算について簡単な時間研究を行うようにと言っています。

お時間をいただきありがとうございます。

4

1 に答える 1

1

send引数を混同しただけだと思いますrecv-sourceはデータを受け取るプロセスのランクであり、 (宛先destの略)はデータを送信するプロセスのランクです(必要に応じてドキュメントを参照できます)。

したがって、送信と受信のキーワードを交換するだけで問題ありません sourcedest

于 2014-11-29T09:17:29.723 に答える