= CPI 的 loop 只會跑一次 = {{{ #include "mpi.h" #include #include double f( double ); double f( double a ) { return (4.0 / (1.0 + a*a)); } int main( int argc, char *argv[]) { int done = 0, n, myid, numprocs, i, count=0; double PI25DT = 3.141592653589793238462643; double mypi, pi, h, sum, x; double startwtime = 0.0, endwtime; int namelen; char processor_name[MPI_MAX_PROCESSOR_NAME]; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); MPI_Get_processor_name(processor_name,&namelen); fprintf(stderr,"Process %d on %s\n", myid, processor_name); n = 0; while (!done) { /* 註 1: 我在這邊開始計算 loop 執行的次數 */ count++; if (myid == 0) { /* printf("Enter the number of intervals: (0 quits) "); scanf("%d",&n); */ if (n==0) n=100; else n=0; startwtime = MPI_Wtime(); } MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); if (n == 0) done = 1; else { h = 1.0 / (double) n; sum = 0.0; for (i = myid + 1; i <= n; i += numprocs) { x = h * ((double)i - 0.5); sum += f(x); } mypi = h * sum; MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); /* 註 2: 在這邊我把每個 node 所執行的 loop 次數印出 */ if (myid == 0) { printf("pi is approximately %.16f, Error is %.16f\n", pi, fabs(pi - PI25DT)); endwtime = MPI_Wtime(); printf("wall clock time = %f\n", endwtime-startwtime); printf("Node 0 loop runs %d times\n", count); } else { printf("Node not 0 loop runs %d times\n", count); } } } MPI_Finalize(); return 0; } }}} == 結果 == 就算去掉 while loop 也能執行。 [[Image(cpi-02.png)]] = MPICH Performance Report = == Machine Specification == || Node || 8 nodes (1 server , 7 client provide disks) || || CPU || Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz || || Memory || 2GB DDR2 667 (each node) || = CPI.c = == Description == 這是一個 mpich 內附用來算 pi 的程式。 = Performance = 以下為使用 1~10 顆的 CPU 測試 100 次的結果 || PI || 3.14159265358979323846 || || Our result || 3.14159265358979267191 || || error || 0.00000000000000056655 || {{{ cpus 平均數 1 0.3792575 2 0.1898436 3 0.1280467 4 0.1004294 5 0.0812890 6 0.0696091 7 0.0672654 8 0.0662498 9 0.0625782 10 0.0587502 }}} [[Image(cpi_01.png)]]