-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmontecarlo.adb
88 lines (79 loc) · 2.3 KB
/
montecarlo.adb
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
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;
with Ada.Real_Time;
use Ada.Real_Time;
with Ada.Numerics.Float_Random;
use Ada.Numerics.Float_Random;
procedure MonteCarloPi is
EXPERIMENTS : constant Integer := 100;
ITERATIONS : constant Integer := 2**24;
task type Master(workers : Integer) is
entry Result(pi : in Float);
end Master;
task body Master is
Start : Time;
Total : Time_Span;
Results : File_Type;
Total_Pi : Float;
begin
Create(File => Results, Mode => Out_File, Name => "results/mcp-ada-" & Integer'Image(workers) & ".csv");
Start := Clock;
for i in 1..EXPERIMENTS loop
Total_Pi := 0.0;
for w in 1..workers loop
accept Result(pi : in Float) do
Total_Pi := Total_Pi + pi;
end Result;
end loop;
Total_Pi := Total_Pi / Float(workers);
Total := (Clock - Start) * 1000000000;
Put_Line(Float'Image(Total_Pi) & " in " & Float'Image(Float(To_Duration(Total))) & "ns");
Put_Line(Results, Float'Image(Float(To_Duration(Total))));
Start := Clock;
end loop;
end Master;
Master_Task : access Master := null;
task type Worker(iters : Integer);
task body Worker is
gen : Generator;
in_circle : Integer := 0;
x : Float;
y : Float;
pi : Float;
begin
for i in 1..EXPERIMENTS loop
for j in 1..iters loop
x := Random(gen);
y := Random(gen);
if x**2 + y**2 <= 1.0 then
in_circle := in_circle + 1;
end if;
end loop;
pi := (4.0 * Float(in_circle)) / Float(iters);
Master_Task.Result(pi);
in_circle := 0;
end loop;
end Worker;
procedure Experiment(writers : Integer) is
workers : array(1..writers) of access Worker;
begin
Master_Task := new Master(writers);
for i in 1..writers loop
workers(i) := new Worker(ITERATIONS / writers);
end loop;
end Experiment;
begin
Put_Line("Monte Carlo Pi Benchmark");
Put_Line("1");
Experiment(1);
Put_Line("2");
Experiment(2);
Put_Line("4");
Experiment(4);
Put_Line("8");
Experiment(8);
Put_Line("16");
Experiment(16);
end MonteCarloPi;