How to assign requests in nonblocking all to all mpi communication

  c++, mpi

Each process has its own buffer at the start of the program, we want to have all to all ring communication while each process should send its buffer to the next process and receive the buffer from the previous process.

I want to use Isend and Irecv, and each request should be unique. How can I specify MPI_Request in here? and how can I use MPI_Wait to be sure we are following the algorithm?

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

#define MAX_SIZE 2

int main(int argc, char *argv[])
  // MPI Start
  int pCurrentID, pRightID, pLeftID, tag = 1 , size, value;
  MPI_Comm_rank(MPI_COMM_WORLD, &pCurrentID);
  MPI_Comm_size(MPI_COMM_WORLD, &size);
  MPI_Request req;

  double* buf = new double[MAX_SIZE];
  for (int i = 0; i < MAX_SIZE; i++) {
    buf[i] = pCurrentID * 1.0;

  for (int i=0; i<(size-1); i++) {

    // Calculate the rank of the left and right processes in the ring
    pRightID = (pCurrentID + 1)%size;
    pLeftID = (0 == pCurrentID) ? (size - 1) : (pCurrentID - 1);

    MPI_Isend(buf, MAX_SIZE, MPI_DOUBLE, pRightID, pCurrentID, MPI_COMM_WORLD, &req);
    cout<<"p"<<pCurrentID<<" send "<<buf[0]<<" to p"<<pRightID<<endl;

    MPI_Irecv(buf, MAX_SIZE, MPI_DOUBLE, pLeftID, pLeftID , MPI_COMM_WORLD, &req);
    cout<<"p"<<pCurrentID<<" recieved "<<buf[0]<<" from p"<<pLeftID<<endl;
    MPI_Wait(&req, MPI_STATUS_IGNORE);
   // MPI_Barrier(MPI_COMM_WORLD);

  /* All done */
  return 0;


Source: Windows Questions C++