I am studying this tutorial about OpenMP and I came across this exercise, on page 19. It is a pi calculation algorithm which I have to parallelize:
static long num_steps = 100000; double step; void main () { int i; double x, pi double sum = 0.0; step = 1.0 / (double)num_steps; for(i = 0; i < num_steps; i++) { x = (I + 0.5) * step; sum = sum + 4.0 / (1.0 + x*x); } pi = step * sum; } I can not use, up to this point, #pragma parallel for. I can only use:
#pragma omp parallel {} omp_get_thread_num(); omp_set_num_threads(int); omp_get_num_threads(); My implementation looks like this :
#define NUM_STEPS 800 int main(int argc, char **argv) { int num_steps = NUM_STEPS; int i; double x; double pi; double step = 1.0 / (double)num_steps; double sum[num_steps]; for(i = 0; i < num_steps; i++) { sum[i] = 0; } omp_set_num_threads(num_steps); #pragma omp parallel { x = (omp_get_thread_num() + 0.5) * step; sum[omp_get_thread_num()] += 4.0 / (1.0 + x * x); } double totalSum = 0; for(i = 0; i < num_steps; i++) { totalSum += sum[i]; } pi = step * totalSum; printf("Pi: %.5f", pi); } Ignoring the problem by using an sum array (It explains later that it needs to define a critical section for the sum value with #pragma omp critical or #pragma omp atomic), the above impelentation only works for a limited number of threads (800 in my case), where the serial code uses 100000 steps. Is there a way to achieve this with only the aforementioned OpenMP commands, or am I obliged to use #pragma omp parallel for, which hasn't been mentioned yet in the tutorial?
Thanks a lot for your time, I am really trying to grasp the concept of parallelization in C using OpenMP.
#pragma omp atomic?