微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python:如何结合 mpi4py 和多处理

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。