#include #include #include int main (int argc, char *argv[]) { double h; double estimate; double sum2; double wtime; double x; int i, n; int nthreads, tid; int procs; n = atoi(argv[1]); h = 1.0 / ( double ) ( 2 * n ); printf("h = %lf n = %d\n",h,n); sum2 = 0.0; /* Fork a team of threads giving them their own copies of variables */ procs = omp_get_num_procs(); omp_set_num_threads(procs); wtime = omp_get_wtime ( ); #pragma omp parallel private( i, x ) shared( h, n ) { #pragma omp for for ( i = 1; i <= n; i++ ) { x = h * ( double ) ( 2 * i - 1 ); #pragma omp critical sum2 = sum2 + 1.0 / ( 1.0 + x * x ); } } wtime = omp_get_wtime ( ) - wtime; estimate = 4.0 * sum2 / ( double ) ( n ); printf("The estimate of pi is %15.10f\n",estimate); printf ( " The time is %15.10f\n", wtime ); return (0); }