mpi4py 附加值 id 以列出每个通信器实例

mpi4py append value id to list for each communicator instance

如果我想列出每个通讯器的变量id,我该怎么做?下面是一个证明这个想法的尝试:

from mpi4py import MPI
comm = MPI.COMM_WORLD

obj = "I am an example. My ID is unique to each communicator."
mpi_id = 'rank %i has id %s'%(comm.rank, str(id(obj)))
comm.send(mpi_id, tag=11, dtest=comm.rank)

mpi_id_list = []
for i in range(comm.size):
    mpi_id_list.append( comm.recv(source=i, tag=11))

print mpi_id_list

在 MPI 中,每个 comm.send(...,dest=x) 应该匹配一个 comm.recv(...) 由排名 x 的进程执行。所有消息都可以发送给等级为0的进程,进程0必须接收所有这些消息。此操作是称为归约的集体操作。

输入mpirun -np 4 main.py

可以在4个进程上执行以下代码
from mpi4py import MPI
comm = MPI.COMM_WORLD

obj = "I am an example. My ID is unique to each communicator."
mpi_id = 'rank %i has id %s'%(comm.rank, str(id(obj)))
comm.send(mpi_id, tag=11, dest=0)

mpi_id_list = []
if comm.rank==0:
   mpi_id_list = []
   for i in range(comm.size):
      mpi_id_list.append( comm.recv(source=i, tag=11))

   print mpi_id_list

#broadcasting the list
mpi_id_list = comm.bcast(mpi_id_list, root=0)

#now, the list is the same on all processes.
print "rank "+str(comm.rank)+" has list "+str(mpi_id_list)

请注意,此示例使用集体操作 comm.bcast() 将结果列表广播到所有进程。有关不同集合操作的 mpi4py 示例,请参阅 https://mpi4py.scipy.org/docs/usrman/tutorial.html。例如,您被 comm.allreduce() 操作所诱惑:

list=comm.allreduce([mpi_id])
print list