You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
806 B
C
31 lines
806 B
C
9 months ago
|
#include <mpi.h>
|
||
|
#include <stdio.h>
|
||
|
int main(int argc, char *argv[]){
|
||
|
int commsize, my_rank;
|
||
|
int msg = 0;
|
||
|
|
||
|
MPI_Init(&argc,&argv);
|
||
|
MPI_Comm_size(MPI_COMM_WORLD, &commsize);
|
||
|
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
|
||
|
|
||
|
if (my_rank == 0) {
|
||
|
printf("[%d] msg = %d\n", my_rank, msg);
|
||
|
if (commsize == 1)
|
||
|
goto finalize;
|
||
|
MPI_Send(&msg, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
|
||
|
MPI_Recv(&msg, 1, MPI_INT, commsize - 1, 0, MPI_COMM_WORLD, NULL);
|
||
|
printf("[%d] msg = %d\n", my_rank, ++msg);
|
||
|
}
|
||
|
else {
|
||
|
MPI_Recv(&msg, 1, MPI_INT, my_rank - 1, 0, MPI_COMM_WORLD, NULL);
|
||
|
printf("[%d] msg = %d\n", my_rank, ++msg);
|
||
|
if (my_rank + 1 < commsize)
|
||
|
MPI_Send(&msg, 1, MPI_INT, my_rank + 1, 0, MPI_COMM_WORLD);
|
||
|
else
|
||
|
MPI_Send(&msg, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
|
||
|
}
|
||
|
|
||
|
finalize:
|
||
|
MPI_Finalize();
|
||
|
}
|