Last active
April 14, 2017 11:41
-
-
Save feyyazesat/c4a172c848933d17271351d901f2e29b to your computer and use it in GitHub Desktop.
parallel programming : pi calculation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <omp.h> | |
double piCalculation() { | |
static int num_total_steps = 100000; | |
int num_steps = (num_total_steps / omp_get_num_threads()); | |
double step; | |
double sum = 0.0; | |
double execution_time = omp_get_wtime(); | |
step = 1.0 / (double) num_total_steps; | |
#pragma omp parallel num_threads(1) | |
{ | |
int i; | |
double x; | |
double localSum = 0.0; | |
for (i = omp_get_thread_num() * num_steps; i < num_steps; i++) { | |
x = (i + 0.5) * step; | |
localSum += 4.0 / (1.0 + x * x); | |
} | |
#pragma omp critical | |
sum += localSum; | |
}; | |
printf("Execution time : %f\n", omp_get_wtime() - execution_time); | |
return step * sum; | |
} | |
int main() { | |
omp_set_num_threads(4); | |
printf("Result : %f\n", piCalculation()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used the mutex to sync
sum_execution_time
andsum
shared variables with using#pragma omp critical
directive. Could you check this again @muatik