Browse Source

Improved printing; Erlang rough equivalent

main
Tony Garnock-Jones 11 months ago
parent
commit
26e29ac1ce
  1. 7
      src/test/java/org/syndicate_lang/actors/example/example2/Main.java
  2. 56
      src/test/java/org/syndicate_lang/actors/example/example2/ring.erl

7
src/test/java/org/syndicate_lang/actors/example/example2/Main.java

@ -45,12 +45,13 @@ public class Main implements IForwarder {
public void handleMessage(int hopCount) {
this._remainingToReceive--;
if (this._remainingToReceive == 0) {
long delta = System.currentTimeMillis() - this._startTime;
double delta = (System.currentTimeMillis() - this._startTime) / 1000.0;
long nMessages = _nActors * _nRounds;
double hz = nMessages / (delta / 1000.0);
double hz = nMessages / delta;
Actor.current().stop();
Actor.log().info(String.format("Stop after %d messages; %.1f Hz",
Actor.log().info(String.format("Stop after %d messages; %.1f seconds, %.1f Hz",
nMessages,
delta,
hz));
}
}

56
src/test/java/org/syndicate_lang/actors/example/example2/ring.erl

@ -0,0 +1,56 @@
-module(ring).
%% erlc ring.erl; erl +P 10000000 -eval 'io:format("~p~n", [ring:start(1000000, 1000)]), init:stop()'
-export([start/2]).
start(NActors, NRounds) ->
io:format("Starting threads...~n"),
First = spawn1(NRounds),
Peers = [Last | _] = spawnN(NActors - 1, [First], NRounds),
First ! {peer, Last},
io:format("Starting measurement (~p processes)...~n", [length(Peers)]),
{Microseconds, ok} =
timer:tc(fun () ->
[P ! {hop, 0} || P <- Peers],
wait_for_all(NActors)
end),
Seconds = Microseconds / 1000000.0,
{ok, {{seconds, Seconds}, {hz, NActors * NRounds / Seconds}}}.
wait_for_all(0) ->
ok;
wait_for_all(Remaining) ->
receive
one_actor_done ->
wait_for_all(Remaining - 1)
end.
spawn1(NRounds) ->
Main = self(),
spawn(fun () -> forwarder_main(Main, NRounds) end).
spawnN(0, Peers, _NRounds) ->
Peers;
spawnN(Remaining, Peers = [Peer | _], NRounds) ->
A = spawn1(NRounds),
A ! {peer, Peer},
spawnN(Remaining - 1, [A | Peers], NRounds).
forwarder_main(MainActor, NRounds) ->
receive
{peer, Peer} ->
forwarder_main_loop(MainActor, Peer, NRounds)
end.
forwarder_main_loop(MainActor, Peer, NRounds) ->
receive
{hop, HopCount} ->
if
HopCount < NRounds - 1 ->
Peer ! {hop, HopCount + 1};
true ->
MainActor ! one_actor_done
end,
forwarder_main_loop(MainActor, Peer, NRounds)
end.
Loading…
Cancel
Save