11 | | main(int argc, char **argv) |
12 | | { |
13 | | int myrank, i, numprocs; |
14 | | char message[20]; |
15 | | MPI_Status status; |
16 | | MPI_Init(&argc, &argv); |
17 | | MPI_Comm_size(MPI_COMM_WORLD, &numprocs); |
18 | | MPI_Comm_rank(MPI_COMM_WORLD, &myrank); |
19 | | if(myrank == 0) |
20 | | { |
21 | | printf("I am %d \n", myrank); |
22 | | for(i = 1; i < (numprocs); i++) |
23 | | { |
24 | | MPI_Recv(message, 20, MPI_CHAR, i, 99, MPI_COMM_WORLD, &status); |
25 | | printf(" I GOT %s \n", message); |
26 | | } |
27 | | printf("\n"); |
28 | | } |
29 | | if(myrank != 0) |
30 | | { |
31 | | sprintf(message, "i am %d", myrank); |
32 | | MPI_Send(message, 20, MPI_CHAR, 0, 99, MPI_COMM_WORLD); |
33 | | printf("I am %d \n", myrank); |
34 | | printf("I send '%s\n\n ", message); |
35 | | } |
| 20 | /* 取得 node 總數 */ |
| 21 | MPI_Comm_size(MPI_COMM_WORLD,&numprocs); |
| 22 | |
| 23 | /* 取得本身 node id / rank */ |
| 24 | MPI_Comm_rank(MPI_COMM_WORLD,&myid); |
| 25 | |
| 26 | /* 取得本身 host name */ |
| 27 | MPI_Get_processor_name(name, &len); |
| 28 | printf("This is machine %d of %d name = %s\n", myid, numprocs, name); |