-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwebLoad.erl
More file actions
104 lines (79 loc) · 2.26 KB
/
webLoad.erl
File metadata and controls
104 lines (79 loc) · 2.26 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
94
95
96
97
98
99
100
101
102
103
104
%% The Computer Language Benchmarks Game
%% http://shootout.alioth.debian.org/
%% Contributed by Fredrik Svahn based on Per Gustafsson's mandelbrot program
-module(webLoad).
-export([start/0, mandel_rpc/0]).
-define(LIM_SQR, 4.0).
-define(ITER, 50).
-define(SR, 1.5).
-define(SI, 1.0).
start() ->
Mandel = spawn(webLoad, mandel_rpc, []),
Mandel ! { mandel, 2},
Mandel ! { mandel, 4},
Mandel ! { mandel, 8},
Mandel ! { mandel, 16},
Mandel ! { mandel, 32},
Mandel ! { mandel, 64},
Mandel ! { cr },
Mandel ! { mandel, 2},
Mandel ! { mandel, 4},
Mandel ! { mandel, 8},
Mandel ! { mandel, 16},
Mandel ! { mandel, 32},
Mandel ! { mandel, 64},
Mandel ! { cr },
Mandel ! { mandel, 2},
Mandel ! { mandel, 4},
Mandel ! { mandel, 8},
Mandel ! { mandel, 16},
Mandel ! { mandel, 32},
Mandel ! { mandel, 64},
Mandel ! { cr },
Mandel ! { mandel, 2},
Mandel ! { mandel, 4},
Mandel ! { mandel, 8},
Mandel ! { mandel, 16},
Mandel ! { mandel, 32},
Mandel ! { mandel, 64},
Mandel ! { cr },
Mandel ! stop,
0.
mandel_rpc() ->
receive
{ mandel, N } ->
Start = now(),
Excess = mandel(N),
Stop = now(),
RunTime = timer:now_diff(Stop, Start),
%%io:format("Mandel(~4.B) -> ~8.B. App time (us) ~8.B. ~n", [N, Excess, RunTime]),
mandel_rpc();
{ cr } ->
io:format("~n"),
mandel_rpc();
stop ->
void
end.
mandel(N) ->
pixel(N, 0, 0, 0).
pixel(N, _X, Y, Excess) when Y =:= N ->
Excess;
pixel(N, X, Y, Excess) when X =:= N ->
pixel(N, 0, Y + 1, Excess);
pixel(N, X, Y, Excess) ->
Bit = bit(X, Y, N),
case Bit of
0 -> pixel(N, X + 1, Y, Excess + 1);
1 -> pixel(N, X + 1, Y, Excess - 1)
end.
% Compute the magnitude at (X, Y)
bit(X, Y, N) ->
mloop(?ITER, (2.0 * X) / N - 1.5, (2.0 * Y) / N - 1.0).
mloop(Iter, CR, CI) ->
mloop(Iter, CR, CI, 0.0, 0.0).
mloop(Iter, CR, CI, ZR, ZI) ->
case ZR * ZR + ZI * ZI > ?LIM_SQR of
false when Iter > 0 -> mloop(Iter - 1, CR, CI, ZR * ZR - ZI * ZI + CR, 2 * ZR * ZI + CI);
false -> 1;
true -> 0
end.