Skip to content

Instantly share code, notes, and snippets.

@MelulekiDube
Created May 11, 2019 21:48
Show Gist options
  • Save MelulekiDube/05aedbc96d105921588a327c8018c4ea to your computer and use it in GitHub Desktop.
Save MelulekiDube/05aedbc96d105921588a327c8018c4ea to your computer and use it in GitHub Desktop.
//code adapted from http://mpitutorial.com/tutorials/mpi-send-and-receive/
#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
#define PING_PONG_LIMIT 8
int main(int argc, char *argv[]){
MPI_Init (&argc, &argv);//initilization
//find out the rank and size;
int world_rank, world_size;
//GET THE RANK OF THE PROCESS
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
//GET THE SIZE OF THE WORLD
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
double t1, t2;
int ping_pong_count = 0;
while(ping_pong_count < PING_PONG_LIMIT){
if(world_rank == ping_pong_count%world_size){
// Increment the ping pong count before you send it
int next_partner_rank = (world_rank+1)%world_size;
int a = PING_PONG_LIMIT-ping_pong_count;
ping_pong_count++;
if(world_rank == 0)
t1 = MPI_Wtime();//start counter
MPI_Send(&ping_pong_count,
1,
MPI_INT, next_partner_rank,
0,
MPI_COMM_WORLD);
printf("%d sent and incremented ping_pong_count "
"%d to %d\n", world_rank, ping_pong_count,
next_partner_rank);
if(a < world_size){
break;
}
}else{
MPI_Status status;
MPI_Recv(&ping_pong_count, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG,
MPI_COMM_WORLD, &status);
//take the timer it just received the message
if(world_rank == 0){
t2 = MPI_Wtime();
printf("Time taken before receiving the ping pong is %f\n", (t2-t1));
}
printf("%d received ping_pong_count %d from %d\n",
world_rank, ping_pong_count, status.MPI_SOURCE);
}
}
MPI_Finalize(); //clean up
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment