| 1 | = Source code = |
| 2 | {{{ |
| 3 | /* Program: |
| 4 | * 每個 node 將自己運算後的結果傳回給 root node,而 |
| 5 | * root node 將結果印出 |
| 6 | * History: |
| 7 | * 2008-06-10 BETA |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <mpi.h> |
| 12 | main (int argc, char **argv) |
| 13 | { |
| 14 | int rank, size, i; |
| 15 | int myid, numprocs; |
| 16 | int myTotal = 0; |
| 17 | int Total = 0; |
| 18 | MPI_Init(&argc, &argv); |
| 19 | MPI_Comm_size(MPI_COMM_WORLD, &numprocs); |
| 20 | MPI_Comm_rank(MPI_COMM_WORLD, &myid); |
| 21 | |
| 22 | for(i = 1; i <= 100; i++) |
| 23 | { |
| 24 | if((i % numprocs) == myid) |
| 25 | { |
| 26 | myTotal += i; |
| 27 | } |
| 28 | MPI_Reduce(&myTotal, &Total, 1, MPI_INT, MPI_SUM, 0,MPI_COMM_WORLD); |
| 29 | } |
| 30 | if(myid==0) |
| 31 | { |
| 32 | printf("Total = %d\n", Total); |
| 33 | } |
| 34 | MPI_Finalize(); |
| 35 | } |
| 36 | |
| 37 | }}} |
| 38 | = Result = |