MPI_Bcast 所有进程发送给所有其余进程

MPI_Bcast all processes sends to all the rest of process

我试图让每个进程都以这种方式向所有其他进程广播

#include "mpi.h"
#include <stdio.h>

int main(argc,argv)
int argc;
char **argv;
{

  int MyProc, size,tag=1;
  int msg_recpt;
  MPI_Status status;

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &MyProc);
  MPI_Comm_size(MPI_COMM_WORLD, &size);

  printf("Process # %d started \n", MyProc);
  MPI_Barrier(MPI_COMM_WORLD);


    MPI_Bcast( &MyProc, 1, MPI_INT, MyProc, MPI_COMM_WORLD);
    MPI_Recv(&msg_recpt, 1, MPI_CHAR, MyProc, tag, MPI_COMM_WORLD, &status);
    printf("Proc #%d recv'd message from Proc #%d \n", MyProc, msg_recpt) ;


  printf("Finishing proc %d\n", MyProc);

  MPI_Barrier(MPI_COMM_WORLD);
  MPI_Finalize();
}

我需要在 MPI_Recv 之后的消息上显示发件人进程,但我认为它已被阻止执行未到达 printf("Finishing proc %d\n", MyProc);

MPI_Bcast()是集体行动。通信器中的所有 MPI 任务都必须使用匹配的签名(例如计数和数据类型)调用它,并且它们都必须使用相同的 root 任务。

您的代码可能会在 MPI_Bcast() 中阻塞(由于消息非常小,这不太可能,但您不应认为这是理所当然的)。如果不匹配,它肯定会阻塞 MPI_Recv(),因为没有匹配的发送操作。

集体和点对点操作在 MPI 中是独立的。

你要么需要一个MPI_Bcast()的循环,要么你有足够的内存接收所有行列的消息,你可以使用一次MPI_Alltoallv()