首页 > 解决方案 > Python:如何结合 mpi4py 和多处理

问题描述

我有以下两部分代码,第一部分是使用多处理的代码,第二部分是使用 mpi4py 的代码。

多孔处理一

def simple(data): #(np.arrray) # its a function from other module
result = do something with the data
return result #(np.arrray)

def lesssimple(data, num):
    num_cores = num
    inputs = tqdm(x)
    processed_list = Parallel(n_jobs=num_cores)(delayed(simple)(i, num) for i in inputs)
    return processed_list

Mpi4py 一

comm = MPI.COMM_WORLD
rank = comm.Get_rank() 
size = comm.Get_size()


if rank == 0: 
     data = np.array_split(dat, size) 
else:
     data = None


recvbuf = comm.scatter(data, root=0)

result = []
for item in recvbuf:
    result.append(somefunction(item))


newData = comm.gather(result,root=0)
if rank == 0:
      with open('result.data', 'wb') as filehandle:
      pickle.dump(newData, filehandle)

我的问题是,如果我想在 Mpi4py 指定的每个节点中进行多处理作业,可以像下面这样简单地组合它们吗?(对于列表中的每个数据,计算都是独立的,不需要通信)。

#all the others the same with mpi4py    

result = []
for item in recvbuf:
   result.append(lesssimple(data, num)) ### plugging the multiprocessing function into this part

#all the others the same with mpi4py

标签: pythonpython-3.xparallel-processingmultiprocessingmpi4py

解决方案


推荐阅读