C++ MPI致电僵局
当我尝试运行此代码消息“死锁:尝试在没有事先匹配的情况下将消息发送到本地过程”时,
#include "pch.h"
#include <iostream>
#include <mpi.h>
using namespace std;
void main(int argc, char* argv[])
{ int ierr, procid, numprocs;
ierr = MPI_Init(&argc, &argv);
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &procid);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
// All procids send the value - procid to procid 0
double val = -1.0 * procid;
MPI_Send(&val, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
cout << "ProciD " << procid << " send value " << val << " to procid 0.\n";
if (procid == 0)
{
// procid 0 must recieve numprocs values
int i; double val, sum = 0; MPI_Status status;
for (i = 0; i != numprocs; ++i)
{
ierr = MPI_Recv(&val, 1, MPI_DOUBLE, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
if (ierr == MPI_SUCCESS)
{
cout << "Procid " << procid << " recieve value " << val;
sum = sum + val;
}
else
MPI_Abort(MPI_COMM_WORLD, 1);
}
cout << " The Total is " << sum << "\n";
}
ierr = MPI_Finalize();
}
我不明白为什么此错误发生
When I trying to run this code Message "DEADLOCK: attempting to send a message to the local process without a prior matching receive"
#include "pch.h"
#include <iostream>
#include <mpi.h>
using namespace std;
void main(int argc, char* argv[])
{ int ierr, procid, numprocs;
ierr = MPI_Init(&argc, &argv);
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &procid);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
// All procids send the value - procid to procid 0
double val = -1.0 * procid;
MPI_Send(&val, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
cout << "ProciD " << procid << " send value " << val << " to procid 0.\n";
if (procid == 0)
{
// procid 0 must recieve numprocs values
int i; double val, sum = 0; MPI_Status status;
for (i = 0; i != numprocs; ++i)
{
ierr = MPI_Recv(&val, 1, MPI_DOUBLE, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
if (ierr == MPI_SUCCESS)
{
cout << "Procid " << procid << " recieve value " << val;
sum = sum + val;
}
else
MPI_Abort(MPI_COMM_WORLD, 1);
}
cout << " The Total is " << sum << "\n";
}
ierr = MPI_Finalize();
}
I don't understand why this error happend
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要有多少清晰的错误消息?它谈到了发送到本地流程的。因此,那就是第一次发送您的代码。这是处理零。因此,“本地过程”是指零。
显然,过程零是在不首先进行接收的情况下发送给自己的。再次,是的。问题在于,只有在有接收等待的情况下,发送才能成功。如果没有接收,就像这里一样,那么发送将坐下来等待。因此,您的代码僵局。
好的,这就是理论。实际上,MPI实施有时可以使您在僵局中进行发送,并且没有僵局。这就是所谓的“急切发送”。我得出的结论是,您的MPI不允许急切发送, @jjramsey的MPI确实可以发送,这就是为什么您的问题不完全可重复的原因。
用于解决方案。错误消息说您首先需要进行接收。但这也会僵硬。因此,您有两个选择:1。不要发送给自己,只副本。或2。在发送发送之前,请发布一堆
mpi_irecv
。或3。使用mpi_isend
发送。然后他们 can 首先。How much clearer do you want an error message to be? It talks about a send to the local process. So that would be that first send in your code. Which is to process zero. Therefore "local process" refers to zero.
And apparently process zero is doing a send to itself without first doing a receive. Again, true. The problem is that a send can only succeed if there a receive waiting for it. If there is no receive, like here, then the send will sit and wait forever. So your code deadlocks.
Ok, that's the theory. In practice, MPI implementation sometimes allow you to do a send in a deadlocking scenario, and get no deadlock. That's called an "eager send". I conclude that your MPI does not allow eager sends, and @jjramsey's MPI does, which is why your problem is not totally reproducible.
For the solution. The error message says that you first need to do a receive. But that would also give deadlock. So you have two options: 1. do not send to yourself, only copy. Or 2. post a bunch of
MPI_Irecv
before you do the sends. Or 3. useMPI_Isend
for the sends. Then they can come first.