close
Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": /usr/lib/python2.7/dist-packages/libsvn/_fs.so: failed to map segment from shared object: Cannot allocate memory). Look in the Trac log for more information.
- Timestamp:
-
Apr 15, 2008, 5:13:32 PM (18 years ago)
- Author:
-
wade
- Comment:
-
--
Legend:
- Unmodified
- Added
- Removed
- Modified
-
|
v9
|
v10
|
|
| 4 | 4 | #include <stdio.h> |
| 5 | 5 | #include <math.h> |
| | 6 | #include <stdlib.h> |
| 6 | 7 | |
| 7 | 8 | double f( double ); |
| … |
… |
|
| 13 | 14 | int main( int argc, char *argv[]) |
| 14 | 15 | { |
| 15 | | int done = 0, n, myid, numprocs, i, count=0; |
| | 16 | int done = 0, n, myid, numprocs, i=0, count=0; |
| 16 | 17 | double PI25DT = 3.141592653589793238462643; |
| 17 | 18 | double mypi, pi, h, sum, x; |
| … |
… |
|
| 28 | 29 | myid, processor_name); |
| 29 | 30 | |
| 30 | | n = 0; |
| | 31 | n = 0; |
| 31 | 32 | while (!done) |
| 32 | 33 | { |
| 33 | | |
| 34 | | /* 註 1: 我在這邊開始計算 loop 執行的次數 */ |
| 35 | 34 | count++; |
| 36 | | |
| | 35 | printf("Node %d : loop runs %d times\n", myid, count); |
| 37 | 36 | if (myid == 0) |
| 38 | 37 | { |
| … |
… |
|
| 42 | 41 | */ |
| 43 | 42 | if (n==0) n=100; else n=0; |
| 44 | | |
| 45 | 43 | startwtime = MPI_Wtime(); |
| 46 | 44 | } |
| | 45 | printf("Node %d : n = %d before MPI_Bcast\n", myid, n); |
| 47 | 46 | MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| | 47 | printf("Node %d : n = %d before MPI_Bcast\n", myid, n); |
| 48 | 48 | if (n == 0) |
| 49 | 49 | done = 1; |
| … |
… |
|
| 61 | 61 | MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); |
| 62 | 62 | |
| 63 | | /* 註 2: 在這邊我把每個 node 所執行的 loop 次數印出 */ |
| 64 | 63 | if (myid == 0) |
| 65 | 64 | { |
| … |
… |
|
| 69 | 68 | printf("wall clock time = %f\n", |
| 70 | 69 | endwtime-startwtime); |
| 71 | | printf("Node 0 loop runs %d times\n", count); |
| 72 | | } else |
| 73 | | { |
| 74 | | printf("Node not 0 loop runs %d times\n", count); |
| 75 | 70 | } |
| 76 | | |
| 77 | 71 | } |
| 78 | 72 | } |
| 79 | 73 | MPI_Finalize(); |
| | 74 | |
| 80 | 75 | return 0; |
| 81 | 76 | } |
| 82 | 77 | }}} |
| 83 | 78 | == 結果 == |
| 84 | | 就算去掉 while loop 也能執行。 |
| 85 | | [[Image(cpi-02.png)]] |
| | 79 | 去掉 while loop 也能執行,是因為 while loop 的目的為展現出,每個 Node 在第二次進入 while loop 時,Node 0 會將 n = 0 藉由 MPI_Bcast 廣播出去。由下圖可發現,每個 Node 只會執行二次,因為在 MPI_Bcast 中,每個 Node 皆會被 lock 住,直到 Node 0 將 n = 0 送到其它的 Node ,每個 Node 才會接著往下做,不過 lock 的機制,似乎與「群」有關,有些 function 如 sprintf,本身就是一個群,所以可以看見前面所有的 Node 會全部將結果顯示在螢幕上才繼續做下一「群」的程式,而我們也可以發現,在下一「群」的程式中,也是將這「群」的程式執行完後才將結果顯示出。 |
| | 80 | [[Image(cpi_02.png)]] |
| 86 | 81 | |
| 87 | 82 | |