如何使用C语言在MPI中将通信器A中的一个进程的消息广播到通信器B的所有进程?

How to broadcast message from one process in a communicator A to all processes of a communicator B in MPI using C language?

我是并行处理的初学者,我想将一个属于通信器 A 的进程的一个值发送到通信器 B 中的所有进程,我尝试使用 MPI_Bcast(), process_sender 应该属于 Communicator B 但它不属于

MPI_Bcast(&value ,value_size ,MPI_INT ,process_sender_rank, CommunicatorB);

两个分离的通讯器之间如何通讯,请帮帮我

在 MPI 中,进程只能在其通信器内的进程之间进行通信。来自 source:

Internally, MPI has to keep up with (among other things) two major parts of a communicator, the context (or ID) that differentiates one communicator from another and the group of processes contained by the communicator. The context is what prevents an operation on one communicator from matching with a similar operation on another communicator. MPI keeps an ID for each communicator internally to prevent the mixups.

在您的情况下,您可以形成一个由进程 A 和属于通信器 B 的进程组成的新通信器。我们将其称为 CommunicatorC,然后您可以再次调用例程,但这次使用新的通信器:

MPI_Bcast(&value ,value_size ,MPI_INT ,process_sender_rank, CommunicatorC);

我想出了一个解决方案,它给出了预期的结果,但我认为它不是最好的, 首先,我将数据从 process_A(属于 communicator_A)发送到 process_B(属于communicator_B)使用点对点通信(MPI_Send()MPI_Recv()) 因为他们都属于 MPI_COMM_WORLD 通讯器 :

/*to get rank of my process in MPI_COMM_WORLD*/
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);


/*point to point communication*/
if (world_rank == process_A_rank_in_comm_world_communicator)
  MPI_Send( &data ,size_of_data, MPI_INT, process_B_rank, tag ,MPI_COMM_WORLD);

if (world_rank == process_B_rank_in_comm_world_communicator)
  MPI_Recv( &data ,size_of_data, MPI_INT, process_A_rank, tag ,MPI_COMM_WORLD, &lStatus);

然后我让 process_B 将接收到的数据广播到其通信器中的所有进程,即 communicator_B.


/*broadCasting*/
MPI_Bcast(&data, size_of_data, MPI_INT, process_B_rank_in_communicator_B, communicator_B) ;