shk*_*hkk 25 mpi distributed-programming
我在MPI中实现了一个程序,其中主进程(rank = 0)应该能够接收来自其他进程的请求,这些进程要求只知道root的变量值.如果我按等级0制作MPI_Recv(...),我必须指定向根发送请求的进程的等级,但我无法控制,因为进程不按1,2,3的顺序运行,....如何从任何级别接收请求并使用发送过程的编号向其发送必要的信息?
Kei*_*thB 52
这假设您正在使用C.在C++和Fortran中有类似的概念.你只需指定MPI_ANY_SOURCE作为源的源MPI_recv().status结构包含消息的实际来源.
int buf[32];
MPI_Status status;
// receive message from any source
MPI_recv(buf, 32, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
int replybuf[];
// send reply back to sender of the message received above
MPI_send(buf, 32, MPI_INT, status.MPI_SOURCE, tag, MPI_COMM_WORLD);
Run Code Online (Sandbox Code Playgroud)