Changes between Version 6 and Version 7 of mpich/point_to_point/demo2
- Timestamp:
- Jun 25, 2008, 5:15:48 PM (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
mpich/point_to_point/demo2
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, my id, 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,&my id);21 MPI_Comm_rank(MPI_COMM_WORLD,&myrank); 21 22 22 23 /* node 0 will send the first message */ 23 if(my id==0)24 if(myrank == 0) 24 25 { 25 n = my id;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", my id, 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(my id>0 && myid<numprocs-1)32 if(myrank>0 && myrank<numprocs-1) 32 33 { 33 MPI_Recv(&n, 1, MPI_INT, my id-1, 99, MPI_COMM_WORLD, &status);34 printf("[Node %d] << 「%d」[Node %d]\n", my id, n, status.MPI_SOURCE);35 n = my id;36 MPI_Send(&n, 1, MPI_INT, my id+1, 99, MPI_COMM_WORLD);37 printf("[Ndde %d]「%d」 >> [Node %d]\n\n", my id, 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(my id==numprocs-1)42 if(myrank==numprocs-1) 42 43 { 43 MPI_Recv(&n, 1, MPI_INT, my id-1, 99, MPI_COMM_WORLD, &status);44 printf("[Node %d] << 「%d」[Node %d]\n", my id, 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 =