#include #include // Ping Pong program int main(int argc, char** argv) { MPI_Init(NULL, NULL); int nb_mpi_processes; MPI_Comm_size(MPI_COMM_WORLD, &nb_mpi_processes); int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); if(nb_mpi_processes != 2) { printf("This program is design to be run with 2 processes only"); return 0;} int tag = 7777; int ball = 0; int n; int niter = 6; for (n=1;n<=niter;n++) { if(rank==0) { MPI_Send ( &ball , 1 , MPI_INTEGER , 1 , tag , MPI_COMM_WORLD ); // 0 send ball to 1, and wait for transfer to be finished MPI_Recv ( &ball , 1 , MPI_INTEGER , 1 , tag , MPI_COMM_WORLD, MPI_STATUS_IGNORE); // 0 receive ball from 1, and wait for transfer to be finished ball = ball + 2; } if(rank==1) { MPI_Recv ( &ball , 1 , MPI_INTEGER , 0 , tag , MPI_COMM_WORLD, MPI_STATUS_IGNORE ); ball = ball + 1; MPI_Send ( &ball , 1 , MPI_INTEGER , 0 , tag , MPI_COMM_WORLD ); } printf("Process %d iter %d ball value is : %d\n",rank,n,ball); MPI_Barrier(MPI_COMM_WORLD); // A barrier. processes stop here, and can pass it only if ALL processes are here. Useful for debug, can impact performances } MPI_Finalize(); // Close MPI return 0; }