Skip to content
Snippets Groups Projects
Commit f66ef448 authored by ef007323's avatar ef007323
Browse files

Add new file

parent 19052e41
Branches main
No related tags found
No related merge requests found
double calculate_in_circle_random_count(void) {
static const unsigned total = 1000000000;
unsigned in_circle = 0;
#pragma omp parallel reduction(+:in_circle)
{
// do per-thread initialization
rand_state seed = omp_srand();
int count = total / omp_get_num_threads();
int i;
// then do this thread's portion of the computation:
for (i = 0; i < count; ++i) {
double x = (double)omp_rand(&seed) / OMP_RAND_MAX;
double y = (double)omp_rand(&seed) / OMP_RAND_MAX;
double val = x * x + y * y;
in_circle += val < 1.0;
}
}
return 4.0 * in_circle / total;
}
int main(int argc, char const *argv[])
{
int num_threads = 1;
if (argc > 1)
num_threads = atoi(argv[1]);
omp_set_num_threads(num_threads);
float pi_approx = calculate_in_circle_random_count();
printf("Pi approximation: %.6f\n", pi_approx);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment