The Computer Language
Benchmarks Game

thread-ring Erlang HiPE #3 program

source code

% The Computer Language Benchmarks Game
% http://benchmarksgame.alioth.debian.org/
% Contributed by John Shahbazian

-module(threadring).
-export([main/1,process_spawner/2,ring_node/1]).

-define(TOTAL_PROCESSES, 503).

main([Arg]) ->
  N = list_to_integer(Arg),
  Pid = process_spawner(?TOTAL_PROCESSES,[]),
  Pid ! N.

process_spawner(Num_Processes,[]) ->
  Pid = spawn(threadring,ring_node,[self()]),
  register(list_to_atom("number" ++ integer_to_list(1)),Pid),
  Next_Pid = spawn(threadring,ring_node,[process_spawner(Num_Processes-2,Pid)]),
  register(list_to_atom("number" ++ integer_to_list(2)),Next_Pid),
  Pid ! {update,Next_Pid},
  Pid;
process_spawner(1,Starter_Pid) ->
  Pid = spawn(threadring,ring_node,[Starter_Pid]),
  register(list_to_atom("number" ++ integer_to_list(?TOTAL_PROCESSES)),Pid),
  Pid;
process_spawner(Num_Processes,Starter_Pid) ->
  Pid = spawn(threadring,ring_node,[process_spawner(Num_Processes-1,Starter_Pid)]),
  register(list_to_atom("number" ++ integer_to_list(?TOTAL_PROCESSES - (Num_Processes-1) )),Pid),
  Pid.

ring_node(Send_To_Pid) ->
  receive
    {update, New_Pid} ->
      ring_node(New_Pid);
    0 ->
      {_,Process_Name} = process_info(self(),registered_name),
      Process_Name2 = string:sub_string(atom_to_list(Process_Name),7),
      io:fwrite("~s~n",[Process_Name2]),
      erlang:halt();     
    N ->
      Send_To_Pid ! (N-1),
      ring_node(Send_To_Pid)        
  end.
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
Erlang/OTP 19 [erts-8.0] [source] [async-threads:10] [hipe] [kernel-poll:false]



Fri, 24 Jun 2016 02:33:58 GMT

MAKE:
mv threadring.hipe-3.hipe threadring.erl
/usr/local/src/otp_src_19.0_nosmp/bin/erlc +native +"{hipe, [o3]}"  threadring.erl
0.50s to complete and log all make actions

COMMAND LINE:
/usr/local/src/otp_src_19.0_nosmp/bin/erl -smp disable -noshell -run  threadring main 50000000

PROGRAM OUTPUT:
292