简体   繁体   English

向MPI中的不同进程发送和接收

[英]Sending to and receiving from different processes in MPI

So I'm writing a program to bounce a "virtual ball" between processes. 因此,我正在编写一个程序,以在进程之间反弹“虚拟球”。 The root process ie the task with rank 0 initializes the game and then sends it to a random process which is determined by rand() % size (with the random number generated seeded by the initial rank). 根进程(即等级为0的任务)会初始化游戏,然后将其发送到由rand() % size确定的随机过程(生成的随机数由初始等级决定)。

I've tried doing: 我试着做:

int rnk= rand() % size; MPI_Send(&ball,1, MPI_INT, rnk, MPI_COMM_WORLD);

This sends the ball to the next random process but upon running this the code is held up by the blocking MPI_Send. 这会将球发送到下一个随机过程,但是在运行该过程时,代码将由阻塞的MPI_Send阻止。 I've just begun parallel programming so I don't have enough grasp of this. 我刚刚开始并行编程,所以我对此没有足够的了解。 How do I send to a random process and then they further send it to any random process? 如何发送到随机过程,然后他们进一步将其发送到任何随机过程?

Any pointers, tips, books and tutorials are welcome. 欢迎任何指针,技巧,书籍和教程。

There is an issue here if the root initially tries to send to itself (which can happen since rand()%size could be zero). 如果根最初尝试发送给自身,则会出现问题(因为rand()%size可能为零,所以可能会发生)。

  • if you post the receive first on the root then it will block as it will never get to the send call (as pointed out by @Gregor above); 如果您首先在根目录上发送接收,则它将阻塞,因为它将永远无法到达发送调用(如上面@Gregor所指出的);
  • however, if you post the send first on the root then there is no guarantee that it will ever progress to the receive call as MPI_Send() is not guaranteed to be asynchronous (ie it could be implemented as a synchronous send which will wait forever for a matching receive). 但是,如果您先将发送发送到根目录,则不能保证它将继续进行到接收调用,因为不能保证MPI_Send()是异步的(即,可以将其实现为同步发送,它将永远等待匹配的接收)。

You need to either ensure that the sender never sends to itself, or use non-blocking sends (or non-blocking receives) to avoid the potential deadlock. 您需要确保发件人从不向自己发送消息,或者使用非阻塞发送(或非阻塞接收)以避免潜在的死锁。

You can use MPI_Recv with MPI_ANY_SOURCE as source (see comment by francis). 您可以将MPI_RecvMPI_ANY_SOURCE用作源(请参阅francis的评论)。

Make sure that you first call MPI_Recv in all processes except for the root, which first needs to send. 确保首先在所有需要发送的根目录之外的所有进程中调用MPI_Recv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM