我在使用mpi4py时遇到问题,我正在尝试进行斐波纳契数列的计算,计算Pi的数字并查找范围内的素数;主要问题是我很难找到一种方法来使用MPI排名以分担计算负荷。
一些帮助将不胜感激!
Fibbonachi代码:
from mpi4py import MPI #Import the Python MPI module
import itertools as it #Import the intger module
import time #Import the process to measure how long the process takes
start_time = time.time() #Start the timer
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
number_of_fib = int(10000) #Number of sequences.
def fibonacci():
x,y = 0,1
while True:
yield x
y = x+y
yield y
x = x+y
for x in it.islice(fibonacci(), number_of_fib):
print x, (rank) #Print the result and which Pi calculated the result.
print "This process took", time.time() - start_time, "to finish" #Print how long the process took.