/* * This is a simple program that shows the numerical integration example. * It computers pi by approximating the area under the curve: * f(x) = 4 / (1+x*x) between 0 and 1. * To do this intergration numerically, the interval from 0 to 1 is divided into * some number (num_sub_intervals) subintervals and added up the area of rectangles * The larger the value of the num_sub_interval the more accurate your result well * * The program first asks the user to input a value for subintervals, it computes * the approximation for pi, and then compares it to a more accurate aproximate * value of pi in math.h library. * * Hwo to Compile: * $ cc omp_pi_integration.c -o omp_pi_integration * * How to Setup Runtime Environment: * $ export OMP_NUM_THREADS=8 * * How to Run: * $ aprun -n 1 -d 8 ./omp_pi_integration * * Run the program multiple times. Each time Increase the number of threads. * Also when it ask you for the number of iterations, you can start with 1000 * and each time you run add a zero, and see what have changed. * */ #include // Is needed for printing the final results #include // Is needed for exiting early if an error occurs #include // Is needed for fabs()/absolute value of floating point numbers #include // Is needed or OpenMP related stuff #define PI_VALUE 3.14159265358979323846 // This value is copied from math.h int main(int argc, char *argv[]) { int num_sub_intervals = 0; double start_time, end_time, time_diff; double x, pi; double sum = 0.0; printf("Please enter the number of iterations used to compute pi:\n "); scanf("%d",&num_sub_intervals); double step = 1.0/(double) num_sub_intervals; // Record the start time from here on: start_time = omp_get_wtime(); int i; #pragma omp parallel if(num_sub_intervals > 100) reduction(+:sum) private(x) { #pragma omp for schedule(static) for(i=0; i < num_sub_intervals; i++){ x = (i+0.5)*step; sum += 4.0/(1.0+x*x); } } pi = step *sum; //End recording the time here. end_time = omp_get_wtime(); time_diff = end_time - start_time; // print the result here: printf("computed pi value is = %g (%17.15f)\n\n", pi,pi); printf("PI accurate value from math.h is: %17.15f \n\n", PI_VALUE); printf("difference between computerd pi and math.h PI_VALUE = %17.15f\n\n", fabs(pi - PI_VALUE)); printf("Time to computer = %g seconds\n\n", time_diff); return EXIT_SUCCESS; }