MPI - Asynchronous Broadcast/Gather

2019-04-28 23:33发布

问题:

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?

回答1:

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;


标签: c++ process mpi