Changes between Version 6 and Version 7 of mpich/test/cpi


Ignore:
Timestamp:
Apr 14, 2008, 11:36:33 AM (16 years ago)
Author:
wade
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • mpich/test/cpi

    v6 v7  
    11= CPI 的 loop 只會跑一次 =
     2{{{
     3#include "mpi.h"
     4#include <stdio.h>
     5#include <math.h>
    26
     7double f( double );
     8double f( double a )
     9{
     10    return (4.0 / (1.0 + a*a));
     11}
    312
     13int main( int argc, char *argv[])
     14{
     15    int done = 0, n, myid, numprocs, i, count=0;
     16    double PI25DT = 3.141592653589793238462643;
     17    double mypi, pi, h, sum, x;
     18    double startwtime = 0.0, endwtime;
     19    int  namelen;
     20    char processor_name[MPI_MAX_PROCESSOR_NAME];
     21
     22    MPI_Init(&argc,&argv);
     23    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
     24    MPI_Comm_rank(MPI_COMM_WORLD,&myid);
     25    MPI_Get_processor_name(processor_name,&namelen);
     26
     27    fprintf(stderr,"Process %d on %s\n",
     28            myid, processor_name);
     29
     30    n = 0;
     31    while (!done)
     32    {
     33
     34/* 註 1: 我在這邊開始計算 loop 執行的次數 */
     35    count++;
     36        if (myid == 0)
     37        {
     38/*
     39            printf("Enter the number of intervals: (0 quits) ");
     40            scanf("%d",&n);
     41*/
     42            if (n==0) n=100; else n=0;
     43
     44            startwtime = MPI_Wtime();
     45        }
     46        MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
     47        if (n == 0)
     48            done = 1;
     49        else
     50        {
     51            h   = 1.0 / (double) n;
     52            sum = 0.0;
     53            for (i = myid + 1; i <= n; i += numprocs)
     54            {
     55                x = h * ((double)i - 0.5);
     56                sum += f(x);
     57            }
     58            mypi = h * sum;
     59
     60            MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
     61
     62/* 註 2: 在這邊我把每個 node 所執行的 loop 次數印出 */
     63            if (myid == 0)
     64            {
     65                printf("pi is approximately %.16f, Error is %.16f\n",
     66                       pi, fabs(pi - PI25DT));
     67                endwtime = MPI_Wtime();
     68                printf("wall clock time = %f\n",
     69                       endwtime-startwtime);           
     70                printf("Node 0 loop runs  %d  times\n", count);
     71            } else
     72            {
     73                printf("Node not 0 loop runs  %d  times\n", count);
     74            }
     75
     76        }
     77    }
     78    MPI_Finalize();
     79    return 0;
     80}
     81}}}
     82結果
    483
    584