Changes between Version 9 and Version 10 of mpich/test/cpi


Ignore:
Timestamp:
Apr 15, 2008, 5:13:32 PM (16 years ago)
Author:
wade
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • mpich/test/cpi

    v9 v10  
    44#include <stdio.h>
    55#include <math.h>
     6#include <stdlib.h>
    67
    78double f( double );
     
    1314int main( int argc, char *argv[])
    1415{
    15     int done = 0, n, myid, numprocs, i, count=0;
     16    int done = 0, n, myid, numprocs, i=0, count=0;
    1617    double PI25DT = 3.141592653589793238462643;
    1718    double mypi, pi, h, sum, x;
     
    2829            myid, processor_name);
    2930
    30     n = 0;
     31    n = 0; 
    3132    while (!done)
    3233    {
    33 
    34 /* 註 1: 我在這邊開始計算 loop 執行的次數 */
    3534        count++;
    36 
     35        printf("Node %d : loop runs  %d  times\n", myid, count);
    3736        if (myid == 0)
    3837        {
     
    4241*/
    4342            if (n==0) n=100; else n=0;
    44 
    4543            startwtime = MPI_Wtime();
    4644        }
     45        printf("Node %d :  n = %d before MPI_Bcast\n", myid, n);
    4746        MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
     47        printf("Node %d :  n = %d before MPI_Bcast\n", myid, n);
    4848        if (n == 0)
    4949            done = 1;
     
    6161            MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
    6262
    63 /* 註 2: 在這邊我把每個 node 所執行的 loop 次數印出 */
    6463            if (myid == 0)
    6564            {
     
    6968                printf("wall clock time = %f\n",
    7069                       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);
    7570            }
    76 
    7771        }
    7872    }
    7973    MPI_Finalize();
     74
    8075    return 0;
    8176}
    8277}}}
    8378== 結果 ==
    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)]]
    8681
    8782