close
Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": /usr/lib/python2.7/dist-packages/libsvn/_repos.so: failed to map segment from shared object: Cannot allocate memory). Look in the Trac log for more information.
- Timestamp:
-
Jun 25, 2008, 5:15:48 PM (18 years ago)
- Author:
-
wade
- Comment:
-
--
Legend:
- Unmodified
- Added
- Removed
- Modified
-
|
v6
|
v7
|
|
| 7 | 7 | * 2008-06-06 改變 #30 printf 位置,以了解 MPI 函式執行順序關係 |
| 8 | 8 | * 2008-06-12 更改顯示方法。 |
| | 9 | * 2008-06-17 變更變數名 myid -> myrank,增加註解 |
| 9 | 10 | */ |
| 10 | 11 | |
| … |
… |
|
| 12 | 13 | #include <stdio.h> |
| 13 | 14 | |
| 14 | | int main(int argc,char *argv[]) |
| | 15 | main(int argc,char **argv) |
| 15 | 16 | { |
| 16 | | int n, myid, numprocs; |
| | 17 | int n, myrank, numprocs; |
| 17 | 18 | MPI_Status status; |
| 18 | 19 | MPI_Init(&argc,&argv); |
| 19 | 20 | MPI_Comm_size(MPI_COMM_WORLD,&numprocs); |
| 20 | | MPI_Comm_rank(MPI_COMM_WORLD,&myid); |
| | 21 | MPI_Comm_rank(MPI_COMM_WORLD,&myrank); |
| 21 | 22 | |
| 22 | 23 | /* node 0 will send the first message */ |
| 23 | | if(myid==0) |
| | 24 | if(myrank == 0) |
| 24 | 25 | { |
| 25 | | n = myid; |
| | 26 | n = myrank; |
| 26 | 27 | MPI_Send(&n, 1, MPI_INT, 1, 99, MPI_COMM_WORLD); |
| 27 | | printf("[Ndde %d]「%d」 >> [Node %d]\n\n", myid, n, myid+1); |
| | 28 | printf("[Ndde %d]「%d」 >> [Node %d]\n\n", myrank, n, myrank+1); |
| 28 | 29 | } |
| 29 | 30 | |
| 30 | 31 | /* node 1 to node n-2 will send message to the next node */ |
| 31 | | if(myid>0 && myid<numprocs-1) |
| | 32 | if(myrank>0 && myrank<numprocs-1) |
| 32 | 33 | { |
| 33 | | MPI_Recv(&n, 1, MPI_INT, myid-1, 99, MPI_COMM_WORLD, &status); |
| 34 | | printf("[Node %d] << 「%d」[Node %d]\n", myid, n, status.MPI_SOURCE); |
| 35 | | n = myid; |
| 36 | | MPI_Send(&n, 1, MPI_INT, myid+1, 99, MPI_COMM_WORLD); |
| 37 | | printf("[Ndde %d]「%d」 >> [Node %d]\n\n", myid, n, myid+1); |
| | 34 | MPI_Recv(&n, 1, MPI_INT, myrank-1, 99, MPI_COMM_WORLD, &status); |
| | 35 | printf("[Node %d] << 「%d」[Node %d]\n", myrank, n, status.MPI_SOURCE); |
| | 36 | n = myrank; |
| | 37 | MPI_Send(&n, 1, MPI_INT, myrank+1, 99, MPI_COMM_WORLD); |
| | 38 | printf("[Ndde %d]「%d」 >> [Node %d]\n\n", myrank, n, myrank+1); |
| 38 | 39 | } |
| 39 | 40 | |
| 40 | 41 | /* the final node n-1 will not send any message but receive*/ |
| 41 | | if(myid==numprocs-1) |
| | 42 | if(myrank==numprocs-1) |
| 42 | 43 | { |
| 43 | | MPI_Recv(&n, 1, MPI_INT, myid-1, 99, MPI_COMM_WORLD, &status); |
| 44 | | printf("[Node %d] << 「%d」[Node %d]\n", myid, n, status.MPI_SOURCE); |
| | 44 | MPI_Recv(&n, 1, MPI_INT, myrank-1, 99, MPI_COMM_WORLD, &status); |
| | 45 | printf("[Node %d] << 「%d」[Node %d]\n", myrank, n, status.MPI_SOURCE); |
| 45 | 46 | } |
| 46 | 47 | MPI_Finalize(); |
| 47 | | return 0; |
| | 48 | } |
| 48 | 49 | }}} |
| 49 | 50 | = Result = |