| | 52 | int main(int argc,char *argv[]) |
| | 53 | { |
| | 54 | int n, myid, numprocs, i=0; |
| | 55 | MPI_Status status; |
| | 56 | MPI_Init(&argc,&argv); |
| | 57 | MPI_Comm_size(MPI_COMM_WORLD,&numprocs); |
| | 58 | MPI_Comm_rank(MPI_COMM_WORLD,&myid); |
| | 59 | |
| | 60 | /* node 0 will send the first message */ |
| | 61 | if(myid==0) |
| | 62 | { |
| | 63 | n = 0; |
| | 64 | /* |
| | 65 | MPI_Send(&n, 1, MPI_INT, 1, 99, MPI_COMM_WORLD); |
| | 66 | */ |
| | 67 | printf("[Ndde %d] send number %d to Node %d\n\n", myid, n, myid+1); |
| | 68 | } |
| | 69 | |
| | 70 | /* node 1 to node n-2 will send message to the next node */ |
| | 71 | if(myid<numprocs-1) |
| | 72 | { |
| | 73 | MPI_Recv(&n, 1, MPI_INT, myid-1, 99, MPI_COMM_WORLD, &status); |
| | 74 | printf("[Node %d] recive number %d\n", myid, n); |
| | 75 | n = myid; |
| | 76 | MPI_Send(&n, 1, MPI_INT, myid+1, 99, MPI_COMM_WORLD); |
| | 77 | printf("[Ndde %d] send number %d to Node %d\n\n", myid, n, myid+1); |
| | 78 | } |
| | 79 | |
| | 80 | /* the final node n-1 will will not send any message but print itself*/ |
| | 81 | if(myid==numprocs-1) |
| | 82 | { |
| | 83 | MPI_Recv(&n, 1, MPI_INT, myid-1, 99, MPI_COMM_WORLD, &status); |
| | 84 | printf("[Node %d] recive number %d\n", myid, n); |
| | 85 | } |
| | 86 | MPI_Finalize(); |
| | 87 | return 0; |
| | 88 | } |
| | 89 | |
| | 90 | {{{ |
| | 91 | |
| | 92 | }}} |