I'm trying to parallelize with MPI a "search problem", where you iterate over several elements and check if they verify a certain property, if one does (several of them can verify the property, but you only need one) you return it and are done. When paralellizing the code, I split the elements between the processes, but, when one finds an element, I want the other processes to stop their search, so no time is wasted.
I've tried using MPI_ISend and MPI_IRecv so each process checks periodically while searching the elements if another process has sent a message telling that they have found one, and if it receives the message, it exits its loop.
int search(...) {
int meh,out = 0;
MPI_Request req;
MPI_Irecv(&meh, 1, MPI_INT, MPI_ANY_SOURCE,0, MPI_COMM_WORLD, &req);
for(int i = 0 ; i < max_tries && out == 0; i++){
test element
if (found) return found;
MPI_Test(&req,&fuera,MPI_IGNORE_STATUS);
}
return -1;
}
int main() {
MPI_Init(NULL, NULL);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int result = search(...)
if(result != -1) {
for (int i = 0; i < world_size; i++) {
MPI_Request req;
int ok = 51914
if (i != world_rank) {
MPI_Isend(&ok,1,MPI_INT,i,0,MPI_COMM_WORLD,&req);
MPI_Request_free(req);
}
}
cout << result << endl;
}
MPI_Finalize();
return 0;
}
Seems to work fine, but, in the (rare, but possible) case that two processes find an element "at the same time" both of them get to the message sending part, send the "Someone has found it!" messages twice, print their found elemend and, after all of that, not before, the program crashes with a MPI error saying that there was a segmentation fault. It works and I get the result, but it's ugly, and, to write ugly code for myself only and no to show it I would just have used MPI_Abort.
Thanks for your help.