I wrote this simple code to evaluate the π using the Monte Carlo method. This is the serial version:
long double compute_pi_serial (long interval const) {
srand (time (NULL));
double x, y;
int i, circle = 0;
for (i = 0; i <interval; i ++) {
x = (double) (rand () / (double) RAND_MAX);
y = (double) (rand () / (double) RAND_MAX);
if (pow (x, 2) + pow (y, 2) <= 1.0) circle ++;
}
return (double long) circle / interval * 4.0;
}
Afterwards, I wrote a parallel version using OpenMP
And this is the result:
long double compute_pi_omp (const long interval, const int threads) {
double x, y;
int i, circle = 0;
#pragma omp parallel private (x, y) num_threads (threads)
{
srand (SEED);
#pragma omp for reduction (+: circle)
for (i = 0; i <interval; i ++) {
x = (double) (rand () / (double) RAND_MAX);
y = (double) (rand () / (double) RAND_MAX);
if (pow (x, 2) + pow (y, 2) <= 1) circle ++;
}
}
return (double long) circle / interval * 4.0;
}
This is an efficient method or there is a more efficient version using always OpenMP
?