close
Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": /usr/lib/python2.7/dist-packages/libsvn/_core.so: failed to map segment from shared object: Cannot allocate memory). Look in the Trac log for more information.
- Timestamp:
-
Jul 24, 2008, 12:06:13 PM (17 years ago)
- Author:
-
wade
- Comment:
-
--
Legend:
- Unmodified
- Added
- Removed
- Modified
-
|
v2
|
v3
|
|
| 2 | 2 | {{{ |
| 3 | 3 | /* Program: |
| 4 | | * mpich_example 內建範例,計算 pi 。 |
| | 4 | * 由使用者輸入一整數,再由 root node 將些整數發送給底下 nodes, |
| | 5 | * 每個 nodes 將收到的整數印出。 |
| 5 | 6 | * History: |
| 6 | | * 2008-04-11 BETA |
| 7 | | * 2008-06-19 增加可重複輸入欲計算之精準度 |
| 8 | | * 2008-06-23 加入 MPI_Barrier 以確保每個 node 在接受 n 後才執行 |
| 9 | | * / |
| | 7 | * 2008-07-24 BETA |
| | 8 | */ |
| 10 | 9 | |
| 11 | | #include "mpi.h" |
| | 10 | |
| 12 | 11 | #include <stdio.h> |
| 13 | | #include <math.h> |
| 14 | | #include <time.h> |
| | 12 | #include <mpi.h> |
| | 13 | main (int argc, char **argv) |
| | 14 | { |
| | 15 | int i = 0; |
| | 16 | int myid, numprocs; |
| | 17 | MPI_Init(&argc, &argv); |
| | 18 | MPI_Comm_size(MPI_COMM_WORLD, &numprocs); |
| | 19 | MPI_Comm_rank(MPI_COMM_WORLD, &myid); |
| 15 | 20 | |
| 16 | | double f( double ); |
| 17 | | double f( double a ) |
| 18 | | { |
| 19 | | return (4.0 / (1.0 + a*a)); |
| 20 | | } |
| | 21 | /* 這邊先印出所有 nodes 的整數 i 初始狀態 */ |
| | 22 | printf("[Node %d] i = %d \n", myid, i); |
| 21 | 23 | |
| 22 | | int main( int argc, char *argv[]) |
| 23 | | { |
| 24 | | int done = 0, n, myid, numprocs, i=0; |
| 25 | | double PI25DT = 3.141592653589793238462643; |
| 26 | | double mypi, pi, h, sum, x; |
| 27 | | double startwtime = 0.0, endwtime; |
| 28 | | int namelen; |
| 29 | | char processor_name[MPI_MAX_PROCESSOR_NAME]; |
| 30 | | MPI_Init(&argc,&argv); |
| 31 | | MPI_Comm_size(MPI_COMM_WORLD,&numprocs); |
| 32 | | MPI_Comm_rank(MPI_COMM_WORLD,&myid); |
| 33 | | MPI_Get_processor_name(processor_name,&namelen); |
| 34 | | fprintf(stderr,"Process %d on %s\n", |
| 35 | | myid, processor_name); |
| 36 | | n = 0; |
| 37 | | while (!done) |
| 38 | | { |
| 39 | | /* 由 node 0 將使用者輸入的值送給其它的 node */ |
| 40 | | if (myid == 0) |
| 41 | | { |
| 42 | | printf("Enter the number of intervals: (0 quits) "); |
| 43 | | scanf("%d", &n); |
| 44 | | startwtime = MPI_Wtime(); |
| 45 | | } |
| | 24 | /* node 0 (root) 會要求使用者輸入一個 INT 的整數 */ |
| | 25 | if (myid == 0) |
| | 26 | { |
| | 27 | printf("Please input a INT number >>"); |
| | 28 | scanf("%d", &i); |
| | 29 | } |
| 46 | 30 | |
| 47 | | /* 這非常重要,所有的 node 必需在此同步,才可以收到使用者輸入的 n */ |
| 48 | | MPI_Barrier(MPI_COMM_WORLD); |
| | 31 | /* node 0 (root) 會將所有 nodes 的 i 的值設為使用者所輸入的數 */ |
| | 32 | MPI_Bcast(&i, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 49 | 33 | |
| 50 | | /* 將 n 送給其它的 node */ |
| 51 | | MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 52 | | if (n == 0) |
| 53 | | done = 1; |
| 54 | | else |
| 55 | | { |
| | 34 | /* 所有的 nodes 在此同步,以確認 node 0 (root) 已將 i 送給所有 nodes */ |
| | 35 | MPI_Barrier(MPI_COMM_WORLD); |
| 56 | 36 | |
| 57 | | /* 此為計算 pi 的演算法 */ |
| 58 | | h = 1.0 / (double) n; |
| 59 | | sum = 0.0; |
| 60 | | for (i = myid + 1; i <= n; i += numprocs) |
| 61 | | { |
| 62 | | x = h * ((double)i - 0.5); |
| 63 | | sum += f(x); |
| 64 | | } |
| 65 | | mypi = h * sum; |
| | 37 | printf("[Node %d] i = %d \n", myid, i); |
| 66 | 38 | |
| 67 | | /* 將算完的結果傳給 node 0 加總 */ |
| 68 | | MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); |
| 69 | | if (myid == 0) |
| 70 | | { |
| 71 | | printf("pi is approximately %.16f, Error is %.16f\n", |
| 72 | | pi, fabs(pi - PI25DT)); |
| 73 | | endwtime = MPI_Wtime(); |
| 74 | | printf("wall clock time = %f\n", |
| 75 | | endwtime-startwtime); |
| 76 | | } |
| 77 | | } |
| 78 | | } |
| 79 | | MPI_Finalize(); |
| 80 | | return 0; |
| | 39 | MPI_Finalize(); |
| 81 | 40 | } |
| 82 | 41 | }}} |