pi3par.c 943 B
#include <omp.h>
#include <math.h>
#include <stdio.h>
int main(argc,argv)
int argc;
char *argv[];
{
int done = 0, n, tid, numprocs, i, len;
double PI25DT = 3.141592653589793238462643;
double pi, h, sum, x;
while (!done)
{
printf("Enter the number of intervals: (0 quits) ");
scanf("%d",&n);
if (n == 0) break;
h = 1.0 / (double) n;
sum = 0.0;
#pragma omp parallel private(i,x,tid,numprocs) default(shared) reduction(+:sum)
{
tid = omp_get_thread_num();
numprocs= omp_get_num_threads();
for (i = 1 + tid ; i <= n; i+= numprocs) {
printf("%d: %d\n",tid,i);
x = h * ((double)i - 0.5);
sum += 4.0 / (1.0 + x*x);
}
}
pi = h * sum;
printf("pi is approximately %.16f, Error is %.16f\n",
pi, fabs(pi - PI25DT));
}
return 0;
}