Skip to content

Instantly share code, notes, and snippets.

@upman
Last active December 24, 2015 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save upman/6739485 to your computer and use it in GitHub Desktop.
Save upman/6739485 to your computer and use it in GitHub Desktop.
C program written using MPI which passes an integer through processes in a cyclic (ring like) pattern.
#include <stdio.h>
#include <mpi.h>
int main(int argc,char *argv[])
{
int myid, numprocs;
int tag,source,destination,count;
int buffer;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
count=1;
if(myid == 0){
printf("Enter an integer:\t");
scanf("%d",&buffer);
MPI_Send(&buffer,count,MPI_INT,myid+1,1,MPI_COMM_WORLD);
printf("process %d sent %d to %d\n",myid,buffer,myid+1);
MPI_Recv(&buffer,count,MPI_INT,numprocs-1,1,MPI_COMM_WORLD,&status);
printf("process %d received %d from %d \n",myid,buffer,numprocs-1);
}
else if(myid==numprocs-1){
MPI_Recv(&buffer,count,MPI_INT,myid-1,1,MPI_COMM_WORLD,&status);
printf("process %d received %d from %d \n",myid,buffer,myid-1);
MPI_Send(&buffer,count,MPI_INT,0,1,MPI_COMM_WORLD);
printf("process %d sent %d to %d\n",myid,buffer,0);
}
else {
MPI_Recv(&buffer,count,MPI_INT,myid-1,1,MPI_COMM_WORLD,&status);
printf("process %d received %d from %d \n",myid,buffer,myid-1);
MPI_Send(&buffer,count,MPI_INT,myid+1,1,MPI_COMM_WORLD);
printf("process %d sent %d to %d\n",myid,buffer,myid+1);
}
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment