#include <stdio.h>
#include <time.h>
#include <omp.h>

int num_steps = 10000000;
double step;

int main(int argc, char* argv[])
{
        clock_t start, stop;
        double x, pi, sum=0.0;
        int i;
        step = 1./(double)num_steps;
        start = clock();

        #pragma omp parallel for private(x) reduction(+:sum)
//      #pragma omp parallel for
        for (i=0; i<num_steps; i++)
        {
//              #pragma omp critical
                {
                x = (i + .5)*step;
                sum = sum + 4.0/(1.+ x*x);
                }
        }

        pi = sum*step;
        stop = clock();

        printf("The value of PI is %10.7f\n",pi);
        printf("The time to calculate PI was %lf milliseconds\n",((double)(stop - start)));
        return 0;
}

-- MattWalsh - 30 Oct 2002

Topic revision: r1 - 30 Oct 2002 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback