Last active
December 24, 2015 03:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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