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 に答える