sem1 homework
This commit is contained in:
parent
d82d9189fb
commit
149ce4231c
3 changed files with 89 additions and 0 deletions
30
sem1/homework/task3/send.c
Normal file
30
sem1/homework/task3/send.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#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();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue