[erlang-questions] Parallel Shootout & a style question

Hynek Vychodil <>
Tue Sep 2 11:13:37 CEST 2008


I argue to check monitor reference in receive every time. It is not good
practice omitting it. You don't know which depthLoop's 'DOWN' message you
received. In some particular case it can doesn't matter (as in this concrete
do!) but do it is safer and good practice.

On Tue, Sep 2, 2008 at 9:42 AM, Mats Cronqvist <>wrote:

> Kevin Scaldeferri wrote:
> > First,  I don't think it's been mentioned here, but the language
> > benchmarks shootout finally got some multi-core hardware!
> >
> > http://shootout.alioth.debian.org/u64q/
> >
> > At the moment, though, there are almost no submissions of parallelized
> > code, so the results are about the same as the existing hardware.
> >
> > I figured (slightly spurred on by the Haskell community) that we
> > should try to submit some modified versions that actually use the
> > multiple cores.  So, for example, I made a slight change to the binary
> > trees code and got a nearly 2x speedup on my 2-core machine.  In doing
> > so, I did run into one of those little things that I've never really
> > known the preferred approach for.  My modified function looks like this:
> >
> > depthLoop(D,M) when D > M -> ok;
> > depthLoop(D,M) ->
> >      Self = self(),
> >      spawn(fun() ->
> >                    N = 1 bsl (M-D + ?Min),
> >                    io:fwrite("~w\t trees of depth ~w\t check: ~w~n",
> >                              [ 2*N, D, sumLoop(N,D,0) ]),
> >                    Self ! done
> >            end),
> >      depthLoop (D+2,M),
> >      receive done -> done end.
> >
> >
> >
>   i'm partial to the monitor-exit idiom. gets rid of the Self for one
> thing.
>
>  mats
>
>  depthLoop(D,M) when D > M -> ok;
>  depthLoop(D,M) ->
>    erlang:spawn_monitor(fun()-> slave(D, M) end),
>   depthLoop (D+2,M),
>   receive {'DOWN',_,_,_,done} -> done end.
>
>  slave(D, M) ->
>    N = 1 bsl (M-D + ?Min),
>   io:fwrite("~w\t trees of depth ~w\t check: ~w~n",
>         [ 2*N, D, sumLoop(N,D,0) ]),
>    exit(done).
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



-- 
--Hynek (Pichi) Vychodil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080902/4a96a2af/attachment.html>


More information about the erlang-questions mailing list