#include <stdio.h>
#include “mpi.h”
int main ( int argc, char **argv )
{
int numtasks, rank, next, prev, buf [2], tag1 = 1, tag2 = 2;
MPI_Request reqs [4];
MPI_Status stats [2];
MPI_Init ( &argc, &argv );
MPI_Comm_size (MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
prev = rank -1;
next = rank + 1;
if ( rank == 0 ) prev = numtasks -1;
if ( rank == ( numtasks -1 ) ) next = 0;
MPI_Irecv ( &buf [0], 1, MPI_INT, prev, tag1, MPI_COMM_WORLD, &reqs [0]
);
MPI_Irecv ( &buf [1], 1, MPI_INT, next, tag2, MPI_COMM_WORLD, &reqs [1]
);
MPI_Isend ( &rank, 1, MPI_INT, prev, tag2, MPI_COMM_WORLD, &reqs [2] );
MPI_Isend ( &rank, 1, MPI_INT, next, tag1, MPI_COMM_WORLD, &reqs [3] );
MPI_Waitall (4, reqs, stats );
MPI_Finalize();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIOKAnG1waS5o4oCdCmludCBtYWluICggaW50IGFyZ2MsIGNoYXIgKiphcmd2ICkKewogaW50IG51bXRhc2tzLCByYW5rLCBuZXh0LCBwcmV2LCBidWYgWzJdLCB0YWcxID0gMSwgdGFnMiA9IDI7CiBNUElfUmVxdWVzdCByZXFzIFs0XTsKIE1QSV9TdGF0dXMgc3RhdHMgWzJdOwogTVBJX0luaXQgKCAmYXJnYywgJmFyZ3YgKTsKIE1QSV9Db21tX3NpemUgKE1QSV9DT01NX1dPUkxELCAmbnVtdGFza3MpOwogTVBJX0NvbW1fcmFuayAoTVBJX0NPTU1fV09STEQsICZyYW5rKTsKIHByZXYgPSByYW5rIC0xOwogbmV4dCA9IHJhbmsgKyAxOwogaWYgKCByYW5rID09IDAgKSBwcmV2ID0gbnVtdGFza3MgLTE7CiBpZiAoIHJhbmsgPT0gKCBudW10YXNrcyAtMSApICkgbmV4dCA9IDA7CiBNUElfSXJlY3YgKCAmYnVmIFswX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