Python:如何结合mpi4py和多线程处理

2024-06-18 11:06:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下两部分代码,第一部分是使用多处理的代码,第二部分是使用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-one

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

Tags: the代码infordatasizewithnp