-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc_pi_omp.c
55 lines (37 loc) · 1.27 KB
/
mc_pi_omp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// *******************************************************************
#pragma GCC optimize("O3","unroll-loops","omit-frame-pointer","inline", "unsafe-math-optimizations")
#pragma GCC option("arch=native","tune=native","no-zero-upper")
//************************************************************
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <omp.h>
#define SEED 1053608
#define N 5000000000000
unsigned int seed = 676767676 ;
//Random number generator with linear congruential generator
double randUint( long i ){
seed = seed * 1103515245 + 123456;
return seed / (double)UINT_MAX ;
}
int main()
{
long count=0;
double pi;
//Init Parallelazation with reduction techinue
#pragma omp parallel for reduction(+: count)
for (long i=0; i<N; i++) {
//Getting the coordinates y,x ε [0,1]
double x,y;
x = randUint(i);
y = randUint(i);
//Checking if in unit circle
if (x*x+y*y <= 1)
count = count + 1;
}
//Calcuting the ratio and as a result the pi
pi=((double)count/(double)N) * 4.0;
printf("OpenMP : # of trials = %14ld , estimate of pi is %1.16f AND an absolute error of %g\n",N,pi,fabs(pi - M_PI));
return 0;
}