gpt4 book ai didi

c++ - MPI_Irecv 没有收到所有发送?

转载 作者:太空狗 更新时间:2023-10-29 21:47:21 24 4
gpt4 key购买 nike

我试图在这个简化的代码中实现的是:

  • 2 种类型的进程(根进程和子进程,ids/rank 分别为 10 和 0-9)
  • 初始化:
    • root 会听 child “完成”
    • children 将在所有完成后收听 root 通知
  • 虽然没有获胜者(尚未全部完成):
    • children 将有 20% 的机会完成(并通知 root 他们完成了)
    • root 将检查是否所有都已完成
      • 如果全部完成:向“获胜者”的 child 发送通知

我有这样的代码:

int numprocs, id, arr[10], winner = -1;
bool stop = false;
MPI_Request reqs[10], winnerNotification;

MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &id);

for (int half = 0; half < 1; half++) {
for (int round = 0; round < 1; round++) {
if (id == 10) { // root
// keeps track of who has "completed"
fill_n(arr, 10, -1);
for (int i = 0; i < 10; i++) {
MPI_Irecv(&arr[i], 1, MPI_INT, i, 0, MPI_COMM_WORLD, &reqs[i]);
}
} else if (id < 10) { // children
// listen to root of winner notification/indication to stop
MPI_Irecv(&winner, 1, MPI_INT, 10, 1, MPI_COMM_WORLD, &winnerNotification);
}

while (winner == -1) {
//cout << id << " is in loop" << endl;

if (id < 10 && !stop && ((rand() % 10) + 1) < 3) {
// children has 20% chance to stop (finish work)
MPI_Send(&id, 1, MPI_INT, 10, 0, MPI_COMM_WORLD);
cout << id << " sending to root" << endl;
stop = true;
} else if (id == 10) {
// root checks number of children completed
int numDone = 0;
for (int i = 0; i < 10; i++) {
if (arr[i] >= 0) {
//cout << "root knows that " << i << " has completed" << endl;
numDone++;
}
}
cout << "numDone = " << numDone << endl;

// if all done, send notification to players to stop
if (numDone == 10) {
winner = 1;
for (int i = 0; i < 10; i++) {
MPI_Send(&winner, 1, MPI_INT, i, 1, MPI_COMM_WORLD);
}
cout << "root sent notification of winner" << endl;
}
}
}
}
}

MPI_Finalize();

调试 cout 的输出看起来像:问题似乎是 root 没有收到所有 child 的完成通知?

2 sending to root
3 sending to root
0 sending to root
4 sending to root
1 sending to root
8 sending to root
9 sending to root
numDone = 1
numDone = 1
... // many numDone = 1, but why 1 only?
7 sending to root
...

我想也许我不能接收到一个数组:但我试过了

if (id == 1) {
int x = 60;
MPI_Send(&x, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
} else if (id == 0) {
MPI_Recv(&arr[1], 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
cout << id << " recieved " << arr[1] << endl;
}

哪个有效。

更新

如果我在 while 循环结束前添加一个 MPI_Barrier(MPI_COMM_WORLD) 似乎可以解决这个问题,但为什么呢?即使进程不同步,最终,子进程也会将他们已完成的消息发送给 root,而 root 应该“监听”并进行相应处理吗?似乎正在发生的事情是 root 一直在运行,占用所有资源供 children 执行?或者这里发生了什么?

更新 2:一些 child 没有收到来自 root 的通知

好的,现在问题是 root 没有收到 child 的通知,他们已经完成了 @MichaelSh 的回答,我关注的是 child 没有收到 parent 的通知。这是重现该问题的代码:

int numprocs, id, arr[10], winner = -1;
bool stop = false;
MPI_Request reqs[10], winnerNotification;

MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &id);

srand(time(NULL) + id);

if (id < 10) {
MPI_Irecv(&winner, 1, MPI_INT, 10, 0, MPI_COMM_WORLD, &winnerNotification);
}
MPI_Barrier(MPI_COMM_WORLD);

while (winner == -1) {
cout << id << " is in loop ..." << endl;
if (id == 10) {
if (((rand() % 10) + 1) < 2) {
winner = 2;
for (int i = 0; i < 10; i++) {
MPI_Send(&winner, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
cout << "winner notifications sent" << endl;
}
}
}

cout << id << " b4 MPI_Finalize. winner is " << winner << endl;

MPI_Finalize();

输出看起来像:

# 1 run
winner notifications sent
10 b4 MPI_Finalize. winner is 2
9 b4 MPI_Finalize. winner is 2
0 b4 MPI_Finalize. winner is 2

# another run
winner notifications sent
10 b4 MPI_Finalize. winner is 2
8 b4 MPI_Finalize. winner is 2

注意到一些进程似乎没有从父进程那里得到通知?为什么,子进程的 MPI_Wait 只会挂起它们?那么我该如何解决呢?

还有

All MPI_Barrier does in your case -- it waits for child responses to complete. Please check my answer for a better solution

如果我不这样做,我想每个 child 的 react 只需要几毫秒?所以即使我不等待/障碍,我希望接收仍然会在发送后不久发生吗?除非进程最终占用资源并且其他进程不运行?

最佳答案

请尝试此代码块(为简单起见省略了错误检查):

...
// root checks number of children completed
int numDone = 0;
MPI_Status statuses[10];
MPI_Waitall(10, reqs, statuses);
for (int i = 0; i < 10; i++) {
...

编辑 更好的解决方案:
每个子节点发起root winner notification receipt并将其通知发送给root。
Root 向数组发起获胜者通知接收并进入等待接收所有通知,然后将获胜者的 id 发送给 child 。在 for (int round = 0; round < 1; round++) 之后插入此代码

            if (id == 10) 
{ // root
// keeps track of who has "completed"
memset(arr, -1, sizeof(arr));
for (int i = 0; i < 10; i++)
{
MPI_Irecv(&arr[i], 1, MPI_INT, i, 0, MPI_COMM_WORLD, &reqs[i]);
}
}
else if (id < 10)
{ // children
// listen to root of winner notification/indication to stop
MPI_Irecv(&winner, 1, MPI_INT, 10, 1, MPI_COMM_WORLD, &winnerNotification);
}

if (id < 10)
{
while(((rand() % 10) + 1) < 3) ;

// children has 20% chance to stop (finish work)
MPI_Send(&id, 1, MPI_INT, 10, 0, MPI_COMM_WORLD);
std::cout << id << " sending to root" << std::endl;
// receive winner notification
MPI_Status status;
MPI_Wait(&winnerNotification, &status);
// Process winner notification
}
else if (id == 10)
{
MPI_Status statuses[10];
MPI_Waitall(10, reqs, statuses);

// if all done, send notification to players to stop
{
winner = 1;
for (int i = 0; i < 10; i++)
{
MPI_Send(&winner, 1, MPI_INT, i, 1, MPI_COMM_WORLD);
}
std::cout << "root sent notification of winner" << std::endl;
}
}

关于c++ - MPI_Irecv 没有收到所有发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com