-
Notifications
You must be signed in to change notification settings - Fork 1
/
phw-monte-carlo.occ
74 lines (68 loc) · 2.06 KB
/
phw-monte-carlo.occ
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
#INCLUDE "course.module"
#INCLUDE "maths.module"
#INCLUDE "random.module"
VAL INT N.WARMUP.RESCHEDULES IS 1000000: -- trick for heuristics
VAL INT N.ITERATIONS IS 1<<24: -- was INT TRUNC POWER(2.0, 24.0), i.e. 1<<24
VAL INT N.REPEATS IS 100: -- was 100
VAL INT N.WORKERS IS 8: -- was 8 (set to 1 for serial time)
PROC reschedule (VAL INT n) -- trick for heuristics
SEQ i = 0 FOR n
RESCHEDULE ()
:
PROC monte.carlo.pi(VAL INT iterations, SHARED CHAN REAL64 to.master!)
INT in.circle:
RANDOM.STATE state:
REAL32 x, y:
REAL64 in.circle.real:
REAL64 iterations.real:
SEQ
reschedule (N.WARMUP.RESCHEDULES) -- trick for heuristics
in.circle := 0
random.init.from.time(state)
SEQ count = 0 FOR iterations
SEQ
x, state := random.real32(1.0, state)
y, state := random.real32(1.0, state)
IF
((x * x) + (y * y)) <= 1.0
in.circle := in.circle + 1
TRUE
SKIP
in.circle.real := REAL64 ROUND in.circle
iterations.real := REAL64 ROUND iterations
CLAIM to.master!
to.master ! (4.0 * in.circle.real) / iterations.real
:
PROC experiment(VAL INT iterations, workers, CHAN BYTE out!)
SHARED CHAN REAL64 c:
REAL64 pi, tmp:
TIMER tim:
INT t0, t1:
SEQ index = 0 FOR N.REPEATS
SEQ
tim ? t0
PAR
PAR i = 0 FOR workers
monte.carlo.pi((iterations/workers), c!)
SEQ
pi := 0.0
SEQ i = 0 FOR workers
SEQ
CLAIM c?
c ? tmp
pi := pi + tmp
pi := pi / (REAL64 ROUND workers)
tim ? t1
t1 := t1 - t0
out.int(t1, 0, out)
out.string("*n", 0, out)
tim ? t0
:
-- PROC main(CHAN BYTE kyb?, scr!, err!)
PROC main(CHAN BYTE scr!)
-- INT iterations:
SEQ
-- iterations := INT TRUNC POWER(2.0, 24.0)
-- experiment(iterations, 8, scr)
experiment(N.ITERATIONS, N.WORKERS, scr!)
: