forked from tgmattso/OpenMP_intro_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi_mc.c
executable file
·123 lines (85 loc) · 3.45 KB
/
pi_mc.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
NAME:
Pi_mc: PI Monte Carlo
Purpose:
This program uses a Monte Carlo algorithm to compute PI as an
example of how random number generators are used to solve problems.
Note that if your goal is to find digits of pi, there are much
better algorithms you could use.
Usage:
To keep the program as simple as possible, you must edit the file
and change the value of num_trials to change the number of samples
used. Then compile and run the program.
Algorithm:
The basic idea behind the algorithm is easy to visualize. Draw a
square on a wall. Inside the square, draw a circle. Now randomly throw
darts at the wall. some darts will land inside the square. Of those,
some will fall inside the circle. The probability of landing inside
the circle or the square is proportional to their areas.
We can use a random number generator to "throw the darts" and count
how many "darts" fall inside the square and how many inside the
cicle. Dividing these two numbers gives us the ratio of their areas
and from that we can compute pi.
Algorithm details:
To turn this into code, I need a bit more detail. Assume the circle
is centered inside the square. the circle will have a radius of r and
each side of the square will be of area 2*r (i.e. the diameter of the
circle).
A(circle) = pi * r^2
A(square) = (2*r)*(2*r) = 4*r^2
ratio = A(circle)/A(square) = pi/4
Since the probability (P) of a dart falling inside a figure (i.e. the square
or the circle) is proportional to the area, we have
ratio = P(circle)/P(square) = pi/4
If I throw N darts as computed by random numbers evenly distributed
over the area of the square
P(sqaure) = N/N .... i.e. every dart lands in the square
P(circle) = N(circle)/N
ratio = (N(circle)/N)/(N/N) = N(circle)/N
Hence, to find the area, I compute N random "darts" and count how many fall
inside the circle. The equation for a circle is
x^2 + y^2 = r^2
So I randomly compute "x" and "y" evenly distributed from -r to r and
count the "dart" as falling inside the cicle if
x^2 + y^2 < or = r
Results:
Remember, our goal is to demonstrate a simple monte carlo algorithm,
not compute pi. But just for the record, here are some results (Intel compiler
version 10.0, Windows XP, core duo laptop)
100 3.160000
1000 3.148000
10000 3.154000
100000 3.139920
1000000 3.141456
10000000 3.141590
100000000 3.141581
As a point of reference, the first 7 digits of the true value of pi
is 3.141592
History:
Written by Tim Mattson, 9/2007.
*/
#include <stdio.h>
#include <omp.h>
#include "random.h"
//
// The monte carlo pi program
//
static long num_trials = 10000;
int main ()
{
long i; long Ncirc = 0;
double pi, x, y, test;
double r = 1.0; // radius of circle. Side of squrare is 2*r
seed(-r, r); // The circle and square are centered at the origin
#pragma omp parallel for private(x,y,test) reduction(+:Ncirc)
for(i=0;i<num_trials; i++)
{
x = drandom();
y = drandom();
test = x*x + y*y;
if (test <= r*r) Ncirc++;
}
pi = 4.0 * ((double)Ncirc/(double)num_trials);
printf("\n %ld trials, pi is %lf \n",num_trials, pi);
return 0;
}