| 1 | = Source code = |
| 2 | {{{ |
| 3 | /* Program: |
| 4 | * 每個 node 將訊息傳送給 node 0,由,node 0 統一印出 |
| 5 | * History: |
| 6 | * 2008-06-12 BETA |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <mpi.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | main(int argc, char **argv) |
| 14 | { |
| 15 | int myrank, i, numprocs; |
| 16 | char message[20]; |
| 17 | MPI_Status status; |
| 18 | MPI_Init(&argc, &argv); |
| 19 | MPI_Comm_size(MPI_COMM_WORLD, &numprocs); |
| 20 | MPI_Comm_rank(MPI_COMM_WORLD, &myrank); |
| 21 | if(myrank == 0) |
| 22 | { |
| 23 | printf("I am %d \n", myrank); |
| 24 | for(i = 1; i < (numprocs); i++) |
| 25 | { |
| 26 | MPI_Recv(message, 20, MPI_CHAR, i, 99, MPI_COMM_WORLD, &status); |
| 27 | printf(" I GOT %s \n", message); |
| 28 | } |
| 29 | printf("\n"); |
| 30 | } |
| 31 | if(myrank != 0) |
| 32 | { |
| 33 | sprintf(message, "i am %d", myrank); |
| 34 | MPI_Send(message, 20, MPI_CHAR, 0, 99, MPI_COMM_WORLD); |
| 35 | printf("I am %d \n", myrank); |
| 36 | printf("I send '%s\n\n ", message); |
| 37 | } |
| 38 | MPI_Finalize(); |
| 39 | } |
| 40 | |
| 41 | }}} |
| 42 | = Result = |