#include <stdio.h> #include <stdlib.h> #include <tgmath.h> #include <time.h> #include <omp.h> #define NUM_STEPS (10 * 1000 * 1000) static double random_coord (void) { return 2 * ((double) rand () / RAND_MAX) - 1; } static double pi_func (size_t n) { size_t inside = 0; printf ("Started\n"); srand (time (NULL)); #pragma omp parallel for reduction(+:inside) for (size_t i = 0; i < n; i++) { double x = random_coord (); double y = random_coord (); double r2 = pow (x, 2) + pow (y, 2); if (r2 <= 1.0) inside++; } return (4 * ((double) inside / n)); } int main (void) { double start = omp_get_wtime (); double pi = pi_func (NUM_STEPS); double stop = omp_get_wtime (); printf ("pi = %lf, took %.3f seconds\n", pi, stop-start); return 0; }