1

我有类似的东西:

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) {
    int completed = 0;
    int* completedIndexes;
    for (int i = 0; i < 10; i++) {
        MPI_Irecv(&arr[i], 1, MPI_INT, i, 0, MPI_COMM_WORLD, &reqs[i]);
    }
    MPI_Waitsome(10, reqs, &completed, completedIndexes, MPI_STATUSES_IGNORE);
    cout << completed << " seems to have completed" << endl;
    for (int i = 0; i < completed; i++) {
        cout << completedIndexes[i] << " have completed!";
    }
}

while (id < 10 && winner == -1) {
    if (((rand() % 100) + 1) < 5) { // players have 5% chance of completing
        MPI_Send(&id, 1, MPI_INT, 10, 0, MPI_COMM_WORLD);
        cout << id << " completed" << endl;
        MPI_Wait(&winnerNotification, MPI_STATUS_IGNORE);
    }
}

MPI_Finalize();

哪个导致分段错误,我认为它与 Waitsome 有关?但是什么?错误看起来像:

1 completed
[JM:01317] *** Process received signal ***
[JM:01317] Signal: Segmentation fault (11)
[JM:01317] Signal code: Address not mapped (1)
[JM:01317] Failing at address: 0x646574
4 completed
5 completed
[JM:01320] *** Process received signal ***
[JM:01320] Signal: Segmentation fault (11)
[JM:01320] Signal code: Address not mapped (1)
[JM:01320] Failing at address: 0x646574
3 completed
[JM:01319] *** Process received signal ***
[JM:01319] Signal: Segmentation fault (11)
[JM:01319] Signal code: Address not mapped (1)
[JM:01319] Failing at address: 0x646574
...
4

1 回答 1

2

MPI_Waitsome需要一个整数数组,其中存储已完成请求的索引,但你给它的是一个未初始化的int指针。鉴于您已硬编码10为请求数,以下代码应该可以工作:

if (id == 10) {
    int completed = 0;
    int completedIndexes[10];
    for (int i = 0; i < 10; i++) {
        MPI_Irecv(&arr[i], 1, MPI_INT, i, 0, MPI_COMM_WORLD, &reqs[i]);
    }
    MPI_Waitsome(10, reqs, &completed, completedIndexes, MPI_STATUSES_IGNORE);
    cout << completed << " seems to have completed" << endl;
    for (int i = 0; i < completed; i++) {
        cout << completedIndexes[i] << " have completed!";
    }
}
于 2012-11-17T15:09:06.693 回答