#include #include #include #include #include double f(double x) { double y; y = 4.0/(1.0+x*x); return(y); } int main() { double cplusplus_internal_pi; double partial_pi; double pi_difference; double openmp_pi; double width; double x; int nthreads; int i; int j; int k; int n; char buff[100]; time_t now = time (NULL); printf(" Program starts\n"); strftime (buff, 100, "%Y-%m-%d %H:%M:%S.000", localtime (&now)); printf (" %s\n", buff); time_t t1 = time( NULL ); time_t t2 = time( NULL ); nthreads = omp_get_max_threads(); cplusplus_internal_pi = 4.0*atan(1.0); printf(" Maximum number of threads = %3d\n",nthreads); k = 1; for(;;) { n = 100000; omp_set_num_threads(k); printf(" ************************\n"); printf(" Number of threads = %2d\n" , k ); printf(" ************************\n" ); for (j = 1;j<= 5;j++) { width = 1.0/n; partial_pi = 0.0; #pragma omp parallel for private(x) shared(width) reduction(+:partial_pi) for(i = 1;i<= n;i++) { x = width*((double)(i)-0.5); partial_pi = partial_pi + f(x); } openmp_pi = width*partial_pi; t2 = time(NULL); printf( " %4.0f seconds ", difftime( t2, t1 ) ); pi_difference = fabs(openmp_pi-cplusplus_internal_pi); printf(" %10d %20.16f %20.16f %20.16f \n",n,openmp_pi,cplusplus_internal_pi,pi_difference); t1=t2; n = n*10; } k = k+1; if (k>nthreads) goto finish; } finish:printf(" Program ends\n" ); now = time (NULL); strftime (buff, 100, "%Y-%m-%d %H:%M:%S.000", localtime (&now)); printf (" %s\n", buff); return(0); }