Search code examples
c++mpiwaitblockingnonblocking

Is there any way to write non blocking MPI code without using MPI_Wait?


I'm having this code which works fine. My question is that is it possible to have a non-blocking code without MPI_Wait? I am thinking that whether by this way, my code behaves as a blocking mode inadvertently. Also, to confirm that this is a non-blocking code, shall I measure execution time? and if it is faster, I can conclude that this is non-blocking. However, here, since no work is involved between MPI_Wait and calculating of data; I think this does not work. So, How can I make sure that this behaves as a non-blocking mode?

#include <iostream>
#include <mpi.h>
using namespace std;

int main() {
    MPI_Init(NULL, NULL);
    MPI_Request request;
    MPI_Status status;
    int rank, size, data;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);



    if (rank > 0) {

        MPI_Irecv(&data, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD, &request);
        MPI_Wait(&request, &status);
        cout << "Rank " << rank << " has received message with data " << data << " from rank " << rank - 1 << endl;

    }
    cout << "Hello from rank " << rank << " out of " << size << "   "  << endl;
  

    data = rank;

    MPI_Isend(&data, 1, MPI_INT, (rank + 1) % size, 0, MPI_COMM_WORLD, &request);


    if (rank == 0) {
        MPI_Irecv(&data, 1, MPI_INT, size - 1, 0, MPI_COMM_WORLD, &request);
        MPI_Wait(&request, &status);
        cout << "Rank " << rank << " has received message with data " << data << " from rank " << size - 1 << endl;
    }


    MPI_Finalize();
    return 0;
}

Solution

  • My question is that is it possible to have a non-blocking code without MPI_Wait

    Yes, one can have a non-blocking code without the MPI_Wait; the problem is, however, that the data used on the non-blocking communication routines can get into inconsistent states. In other words, one cannot be sure if that data can be safely used (or not).

    In the current context, non-blocking simply means that one does not block waiting for the data to be copied to/from the buffer and received/send. After having called the non-blocking routine, one can immediately resume one's computation.

    I am thinking that whether by this way, my code behaves as a blocking mode inadvertently. Also, to confirm that this is a non-blocking code, shall I measure execution time? and if it is faster, I can conclude that this is non-blcoking.

    One can always test, but one can also trust the specification (and/or the implementation) whether a given routine is non-blocking.

    However, here, since no work is involved between MPI_Wait and calculating of data; I think this does not work.

    Indeed, one of the points of using such routines is to overlap computation with communication as long as that computation does not work on the data used on the communication.