0

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.

  • The way you've implemented it -- testing a nonblocking receive for a signal from the "winning" process withing the search loop -- may be ugly, but it's probably the best approach. – Trevor Keller Jun 10 '19 at 21:38
  • But there is no way to avoid the segmentation fault from ocurring? – Joserichi Jun 10 '19 at 22:03
  • 1
    I suspect the error is caused by `MPI_Request_free()` (and fwiw, I think it is incorrect to invoke it here). If you need help debugging the crash, you should either provide a stack trace or even better, a [MCVE]. – Gilles Gouaillardet Jun 11 '19 at 00:09
  • Possible duplicate of [MPI asynchronous broadcast from unknown source](https://stackoverflow.com/questions/43973504/mpi-asynchronous-broadcast-from-unknown-source) (please take a close look at all the answers) – Zulan Jun 11 '19 at 07:35

0 Answers0