#include #include #include "mytime.h" // kernel double integrate(int n) { double h, x, sum; int i; sum = 0.0; h = 1.0 / (double) n; #pragma omp parallel for reduction(+:sum) private(i,x) for (i = 1; i <= n; i++) { x = h * ((double)i - 0.5); sum += 4.0 / (1.0 + x*x); } return sum * h; } int main(int argc, char *argv[]) { int n; double PI25DT = 3.141592653589793238462643; double pi; struct timeval startwtime, endwtime, diffwtime; printf("%d processors available\n", omp_get_num_procs()); omp_set_num_threads(omp_get_num_procs()); while (1) { printf("Enter the number of intervals: (0 quits) ");fflush(stdout); scanf("%d",&n); gettimeofday(&startwtime, NULL); if (n == 0) break; pi = integrate(n); gettimeofday(&endwtime, NULL); MINUS_UTIME(diffwtime, endwtime, startwtime); printf("pi is approximately %.16f, Error is %.16f\n", pi, fabs(pi - PI25DT)); printf("wall clock time = %d.%06d\n", diffwtime.tv_sec, diffwtime.tv_usec); } return 0; }