Erlang dynamic variable names on each iteration -
program working now. simplest solution best. registered processes did not work. having trouble call await/2 base case of start/6. turned out using modified list t_t.
%variables t = 10000000, p = 4, tpart = 2500000, width = 1.0 / t, pi = pi(t, p, [], 1), calls pi/4 , gets list of pid start(t, p, tpart, width, pi, 1). %calls start pi variable should contain 4 pids % create list of p pids , return pi(_, p, list, count) when count > p -> list; pi(t, p, list, count) -> pid = spawn(pi, child, []), count2 = count + 1, pi(t,p, [pid|list], count2). %start iterates through list of pids , sends work order each process start(_, p, _, _, _, staticlist, count) when count > p -> await(staticlist, 0.0); start(t, p, tpart, width, [head|tail], staticlist, count) -> head ! {work, self(), p, count, tpart, width}, count2 = count + 1, start(t,p,tpart,width,tail, staticlist, count2). % collects partial sums child processes. print final output await([], final) -> io:format(" final sum: ~.8f \n", [(final * 4.0)]); await([pid | rest], final) -> receive {done, pid, sum} -> partial = final + sum, await(rest, partial) end.
you cannot name pids directly. can take process name, however, , register using register/2
. should careful, though, because registered process name needs atom , potentially fill atom table if aren't careful.
%call function pi = pi(t,p, [], 1, getname(1)), %list of pids % main function should return list of pid's different names. pi(t, p, list, count) when count >= p -> list; pi(t, p, list, count) -> pid = spawn(pi, child, []), % want have name be: pidx = ... count2 = count + 1, name = getname(count2), register(name,pid), pi(t,p, [name|list], count2). %helper method returns atom. want use variable name in main function. getname(count) -> x = "pid", y = integer_to_list(count), name = x ++ y, list_to_atom(name).
if need list of pids, why name them @ all? this:
%call function pi = pi(t,p, [], 1, getname(1)), %list of pids % main function should return list of pid's different names. pi(t, p, list, count) when count >= p -> list; pi(t, p, list, count) -> pid = spawn(pi, child, []), % want have name be: pidx = ... count2 = count + 1, pi(t,p, [pid|list], count2).
this return list of pids without need of other helper function.