MPI_Irecv没有收到所有发送?

时间:2012-11-17 01:33:01

标签: c++ openmpi

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

  • 2种类型的进程(root和children,ids / rank = 10和0-9)
  • INIT:
    • root会听孩子们“完成”
    • 孩子将在完成所有操作后收听根通知
  • 虽然没有赢家(还没有完成):
    • 孩子将有20%的机会完成(并通知他们已完成)
    • root将检查是否已完成所有操作
      • 如果全部完成:向“获胜者”的孩子发送通知

我的代码如下:

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没有收到所有孩子的通知他们已经完成了?

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持续运行,占用了所有资源供孩子们执行?或者这里发生了什么?

更新2:有些孩子没有收到root的通知

现在好了,根本没有收到儿童通知他们已经通过@MichaelSh的答案完成的问题,我专注于没有从父母那里收到的孩子。这是一个重现该问题的代码:

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子进程只会挂起它们?那么我该如何解决这个问题呢?

另外

  

所有MPI_Barrier都在您的情况下 - 它等待孩子的回复完成。请检查我的答案以获得更好的解决方案

如果我不这样做,我想每个孩子的反应只需要几毫秒?所以,即使我不等待/屏障,我也希望收到发送后很快就能收到?除非进程最终占用资源而其他进程不运行?

1 个答案:

答案 0 :(得分:2)

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

...
// 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向阵列发起获胜者通知收据并等待接收所有通知,然后将获胜者的ID发送给孩子。 在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;
                }
            }