<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Exchange Server">
<!-- converted from rtf -->
<style><!-- .EmailQuote { margin-left: 1pt; padding-left: 4pt; border-left: #800000 2px solid; } --></style>
</head>
<body>
<font face="Arial" size="2"><span style="font-size:10pt;">
<div>Hi,</div>
<div> </div>
<div>I'm a newbie, playing with a toy programme to see how parallel processing works in Erlang. </div>
<div> </div>
<div>The goal is to calculate the value of pi using Leibnitz series. The function pi/2 takes as arguments the number of processes, Np, and the number of terms handled by each process, Nt.</div>
<div> </div>
<div>The machine is an HP workstation with Windows Vista Enterprise.</div>
<div> </div>
<div> </div>
<div>Here's the code: </div>
<div> </div>
<div style="padding-left:19pt;">-module(pp).</div>
<div style="padding-left:19pt;">-compile(export_all).</div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;">%% calculate PI using Np parallel processes, each calculating Nt terms of the </div>
<div style="padding-left:19pt;">%% Liebnitz series</div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;">pi(Np, Nt) -> </div>
<div style="padding-left:19pt;"> Collector = spawn(?MODULE, collect, [Np, self()]),</div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;"> [spawn_opt(</div>
<div style="padding-left:19pt;"> fun() -> Collector ! foldsum((N-1)*Nt, N*Nt -1) end %% if I use sum, instead of foldsum, everything is ok</div>
<div style="padding-left:19pt;"> ) || N <- lists:seq(1,Np)],</div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;"> receive</div>
<div style="padding-left:19pt;"> Result -></div>
<div style="padding-left:19pt;"> io:format("result is ~p~n", [4 * Result])</div>
<div style="padding-left:19pt;"> end.</div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;">sum(Kfrom, Kto) -></div>
<div style="padding-left:19pt;"> sum (Kfrom, Kto, 0).</div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;">sum(Kfrom, Kto, Sum) when Kfrom =:= Kto -></div>
<div style="padding-left:19pt;"> Sum;</div>
<div style="padding-left:19pt;">sum(Kfrom, Kto, Sum) -></div>
<div style="padding-left:19pt;"> sum(Kfrom+1, Kto, Sum + contribution(Kfrom)).</div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;">foldsum(Kfrom, Kto) -> </div>
<div style="padding-left:19pt;"> lists:foldl(</div>
<div style="padding-left:19pt;"> fun(K, Sum) -> Sum + contribution(K) end, </div>
<div style="padding-left:19pt;"> 0, </div>
<div style="padding-left:19pt;"> lists:seq(Kfrom,Kto)).</div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;">contribution(K) -></div>
<div style="padding-left:19pt;"> Value = 1 / (2 * K + 1),</div>
<div style="padding-left:19pt;"> case K rem 2 of</div>
<div style="padding-left:19pt;"> 0 -></div>
<div style="padding-left:19pt;"> Value;</div>
<div style="padding-left:19pt;"> 1 -></div>
<div style="padding-left:19pt;"> -Value</div>
<div style="padding-left:19pt;"> end.</div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;">collect (Np, For) -></div>
<div style="padding-left:19pt;"> collect(Np, For, 0, 0).</div>
<div style="padding-left:19pt;"> </div>
<div style="padding-left:19pt;">collect (Np, For, Ncollected, Sum) when Np =:= Ncollected -></div>
<div style="padding-left:19pt;"> For ! Sum;</div>
<div style="padding-left:19pt;">collect(Np, For, Ncollected, Sum) -></div>
<div style="padding-left:19pt;"> receive</div>
<div style="padding-left:19pt;"> R -></div>
<div style="padding-left:19pt;"> collect(Np, For, Ncollected + 1, Sum + R)</div>
<div style="padding-left:19pt;"> end.</div>
<div style="padding-left:19pt;"> </div>
<div> </div>
<div> </div>
<div>If I try to calculate pi using 16 processes, each processing 1 million terms, the result is ok. </div>
<div>If, on the other hand, I try with 10 million terms each, erlang crashes.</div>
<div>Even after closing the werl window, the process keeps running and has to be killed brutally.</div>
<div> </div>
<div> </div>
<div>Erlang R15B (erts-5.9) [smp:4:4] [async-threads:0]</div>
<div> </div>
<div>Eshell V5.9 (abort with ^G)</div>
<div>1> c(pp).</div>
<div>{ok,pp}</div>
<div>2> pp:pi(16,1000000).</div>
<div>result is 3.1415925910897737</div>
<div>ok</div>
<div>3> pp:pi(16,10000000).</div>
<div> </div>
<div>Crash dump was written to: erl_crash.dump</div>
<div>eheap_alloc: Cannot allocate 40121760 bytes of memory (of type "heap").</div>
<div> </div>
<div> </div>
<div>Abnormal termination</div>
<div> </div>
<div>If I try to read the crash dump with the crash dump viewer tool, I get an error saying the file is not an erlang crash dump:</div>
<div><font size="3"><span style="font-size:12pt;">erl_crash.dump is not an Erlang crash dump </span></font></div>
<div><font size="3"><span style="font-size:12pt;"> </span></font></div>
<div><font size="3"><span style="font-size:12pt;"> </span></font></div>
<div>Why is this happening? Shouldn't foldl be tail recursive? Is this a bug? Or am I doing something wrong?</div>
<div> </div>
<div> </div>
<div>Thanks and best regards</div>
<div>Arif Ishaq</div>
<div> </div>
<div> </div>
</span></font>
</body>
</html>