- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <mpi.h>
- int main(int argc, char **argv)
- {
- int myrank;
- int comm_size;
- int message;
- int number;
- MPI_Status status;
- MPI_Init(&argc, &argv);
- sleep(5);
- MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
- MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
- int cs = comm_size-1;
- if(myrank==0)
- {
- number = 27;
- printf("Process %d: Sending number %d to process %d.\n", myrank, number, (myrank+1)%comm_size);
- MPI_Send(&number, 1, MPI_INT, (myrank+1)%comm_size, 0, MPI_COMM_WORLD);
- printf("Process %d: Number %d sent to process %d. Awaiting number from process %d.\n", myrank, number, (myrank+1)%comm_size, cs);
- MPI_Recv(&number, 1, MPI_INT, cs, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
- printf("Process %d: Number %d received from process %d. Finalizing...\n", myrank, number, cs);
- }
- else
- {
- int next = (myrank+1)%comm_size;
- printf("Process %d: Awaiting a number from process %d.\n", myrank, myrank-1);
- MPI_Recv(&number, 1, MPI_INT, myrank-1, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
- printf("Process %d: Number %d received from process %d. Adding %d to it...\n", myrank, number, myrank-1, 10-myrank);
- number = number + (10-myrank);
- printf("Process %d: Resulting number: %d. Sending to process %d.\n", myrank, number, next);
- MPI_Send(&number, 1, MPI_INT, next, 0, MPI_COMM_WORLD);
- printf("Process %d: Number sent to process %d.\n", myrank, next);
- }
- MPI_Finalize();
- return 0;
- }