I am using openmp to improve a monte carlo approach to find PI. What I did was to add the pragma clause to the sequential code. The code is as follows.
float host_monte_carlo_parallel(long trials, int noOfThreads) {
float x, y;
long points_in_circle;
long i;
#pragma omp parallel for num_threads(noOfThreads) private(i, x, y) reduction(+:points_in_circle)
for (i = 0; i < trials; i++) {
x = rand() / (float) RAND_MAX;
y = rand() / (float) RAND_MAX;
//printf("%ld\n", i);
points_in_circle += (x * x + y * y <= 1.0f);
}
return 4.0f * points_in_circle / trials;
}
The problem is that the sequential code runs far earlier than the parallel one. Am I using the pragma correct? The running times are approximately like this.
CPU pi calculated in 6.413644 s.
CPU parallel pi calculated in 203.746460 s.