#include #include #include #ifdef _OPENMP #include #endif int main( int argc, char *argv[]) { int done = 0, n, i; double PI25DT = 3.141592653589793238462643; double pi, h; double x, y, x1, x2, y1, y2; int SqPoints, CiPoints; double startwtime = 0.0, endwtime; n = 0; while (!done) { printf("Enter the number of points: (0 quits) \n"); scanf("%d",&n); #ifdef _OPENMP startwtime = omp_get_wtime(); #endif if (n <= 0) done = 1; else { x1 = -1.0; x2 = 1.0; y1 = -1.0; y2 = 1.0; SqPoints = 0; CiPoints = 0; #pragma omp parallel for \ default(none) private(i,x,y) shared(n,x1,x2,y1,y2) \ reduction(+:SqPoints,CiPoints) for (i = 1; i <= n; i++) { x = rand(); x = x / RAND_MAX; x = x1 + x * (x2 - x1); y = rand(); y = y / RAND_MAX; y = y1 + y * (y2 - y1); SqPoints++; if ( sqrt( x*x + y*y ) <= 1.0 ) CiPoints++; } pi = 4.0 * (double)CiPoints / (double)SqPoints; printf("pi is approximately %.16f, Error is %.16f\n", pi, fabs(pi - PI25DT)); #ifdef _OPENMP endwtime = omp_get_wtime(); #endif printf("wall clock time = %f\n", endwtime-startwtime); } } return 0; }