-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmonte_carlo.c
More file actions
93 lines (54 loc) · 1.68 KB
/
monte_carlo.c
File metadata and controls
93 lines (54 loc) · 1.68 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<stdio.h>
#include <stdlib.h>
#include<time.h>
#include<math.h>
#include"omp.h"
int random_gg() {
static int c = 0;
int a[] = {5,4,2,8,9,0,1,3,7,6};
return a[(c++)%10];
}
int main(){
static long interval = 1000000000;
double pi=0.0;
double c_point=0.0;
double s_point=0.0;
double s_time=omp_get_wtime();
srand(time(NULL));
printf("Serial code Start:%lf",s_time) ;
unsigned int see = 1;
for(long int i=0; i<interval;i++){
double r_x=(double)(rand_r(&see) % (interval + 1)) / interval;
double r_y=(double)(rand_r(&see) % (interval + 1)) / interval;
double tot= (r_x*r_x)+(r_y*r_y);
if(tot<=1.0)
c_point++;
s_point++;
}
double e_time=omp_get_wtime();
printf("\nSerial code End:%lf",e_time) ;
printf("\nExecution Time:%lf",e_time-s_time) ;
pi= (double)c_point/s_point * 4;
printf("\nCalculated pi value Serial:%lf\n",pi);
/* pi=0.0;
c_point=0.0;
s_point=0.0;
s_time=omp_get_wtime();
srand(time(NULL));
printf("Parallel code Start:%lf",s_time) ;
#pragma omp parallel for reduction(+:c_point,s_point)
for(long int i=0; i<interval*interval;i++){
double r_x=(double)(random() % (interval + 1)) / interval;
double r_y=(double)(random() % (interval + 1)) / interval;
double tot= r_x*r_x+r_y*r_y;
if(tot<=1.0)
c_point++;
s_point++;
}
e_time=omp_get_wtime();
printf("\nParallel code End:%lf",e_time) ;
printf("\nExecution Time:%lf",e_time-s_time) ;
pi= 4*c_point/s_point;
printf("\nCalculated pi value:%lf\n",pi);*/
return 0;
}