python - 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
解决方案
推荐阅读
- javascript - 通过javascript切换显示/隐藏具有相同css类的所有段落
- javascript - 是否有任何库可以让我查看 PDF 并添加自定义右键单击选项?
- javascript - 避免每次刷新页面时keycloak回调过程
- sql-server - Gitlab CI 无法连接到 ASP .Net Core APP 上的 Azure SQL Server
- android - 在 gms 视觉中加载 com.google.android.gms.dynamite.DynamiteModule$LoadingException 时出错
- javascript - Firebase如何链接使用phoneNumber创建的帐户
- python - Python 使用文件字节访问 zip 文件并遍历每个文件以将它们保存到数据帧中返回未找到文件的错误
- c++ - 带有引用的对象的 STL 容器
- git - 自动解决樱桃挑选冲突是否可能?
- microsoft-graph-api - MS Graph - SharePoint 错误:未添加成员