6

I have a project which requires 'n' number of processes to work until the problem is solved. Each slave process executes the same code. When a certain condition arises, the process needs to notify all of the other processes in a non-blocking way. The other processes also need to receive this message in a non-blocking way.

Is there a way to do with without threading a separate loop?

tshepang
  • 12,111
  • 21
  • 91
  • 136
xxf8xx
  • 163
  • 2
  • 9

1 Answers1

4

It's been a while since I've used MPI. But the I functions are non-blocking. Maybe something like this:

int comm_size = comm.Get_size();
int comm_rank = comm.Get_rank();

int* data = new int[comm_size];

while (some_condition)
{
    //During each iteration, check for messages from other nodes
    for (int node = 0; node < comm_size; node++)
    {
        if (node != comm_rank)
        {
            if (comm.Iprobe(node, TAG_NUM))
            {
                comm.Irecv(data[node], 1, MPI_INT, node, TAG_NUM);
            }
        }
    }

    if (some_other_condition)
    {
        //Send the message to every node
        for (int node = 0; node < comm_size; node++)
        {
            if (node != comm_rank)
            {
                comm.Isend(data[node], 1, MPI_INT, node, TAG_NUM);
            }
        }
    }

    //do normal work here.
}

delete [] data;
Geoff Montee
  • 2,587
  • 13
  • 14
  • 1
    Yep that's exactly what I ended up doing. I don't consider this TRULY asynchronous since it still has to loop through and send all the messages, but it's good enough for what I am doing. Thanks. – xxf8xx Oct 10 '12 at 17:07
  • I'm having a similar problem: broadcasting data in an asynchronous way (It's not that important that the data at all cores is up to date). A problem I have with the proposed implementation is that it uses a lot of bandwidth: each message is sent p-1 times. Is their no way (with bit masks or something), to send a message as cheap as possible over the network (for instance if we one is using a switch, the switch can do the duplication work). I'm using MPJ Express. – Willem Van Onsem Mar 09 '13 at 22:04