我有类似的东西:
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
...
答案 0 :(得分: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!";
}
}