-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpiEx3b.c
35 lines (29 loc) · 989 Bytes
/
mpiEx3b.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include "mpi.h"
int main(int argc,char *argv[]){
int size, rank, dest, source, count, tag=1;
int inmsg, outmsg;
MPI_Status Stat;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
dest = 1;
source = size - 1;
outmsg = rank;
MPI_Send(&outmsg, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
MPI_Recv(&inmsg, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat);
printf("I am %d and I have received %d\n", rank, inmsg);
}else{
dest = (rank == size - 1) ? 0 : rank + 1;
source = rank - 1;
MPI_Recv(&inmsg, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat);
printf("I am %d and I have received %d\n", rank, inmsg);
outmsg = rank;
MPI_Send(&outmsg, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
}
MPI_Get_count(&Stat, MPI_CHAR, &count);
printf("Task %d: Received %d char(s) from task %d with tag %d \n",
rank, count, Stat.MPI_SOURCE, Stat.MPI_TAG);
MPI_Finalize();
}