[erlang-questions] Parallel Shootout & a style question

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Tue Sep 2 09:22:11 CEST 2008


I guess there should be a utility library with
some different ways of parallelizing a computation...

Does the shootout maintain different source trees
for the single-core and multicore programs?

Otherwise, we'll incur a slight penalty on the
sequential benchmarks. If it's noticeable, we
can always split the sequential and parallel
parts using a cheap test:

case erlang:system_info(schedulers) of
     1 -> seq(...);
     _ -> par(...)
end.


I don't think rpc:pmap() does quite the right
thing. It tries to spread the computations
across erlang nodes. We want a much simpler pmap.


depthLoop(D, M) ->
    L = lists:seq(D,M,2),
    F = fun(D1) ->
           check(D1,M)
        end,
    Results = pmap(F, lists:seq(D,M,2)),
    lists:foreach(
       fun(Res) ->
          io:fwrite("~w\t trees of depth ~w\t check: ~w~n",
                    Res)
       end, Results).

check(D, M) ->
    N = 1 bsl (M-D + ?Min),
    [2*N, D, sumLoop(N,D,0)].

pmap(F, L) ->
    Me = self(),
    Pids = [spawn(fun() ->
               Me ! {self(),catch F(X)}
            end || X <- L],
    [receive {P,Res} -> Res end || P <- Pids].


(I haven't tried the above, or thought too much
about whether it's exactly the right solution.
See it as an implementation sketch.)


BR,
Ulf W


Kevin Scaldeferri skrev:
> 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.
> 
> 
> parallelizing would only require the addition of the spawn, except  
> that if I do that, the function finishes executing and the program  
> exits before most of the processes run at all.  So, I need to add the  
> call to self(), the send, and the receive in order to prevent  
> premature termination.  So, what I'm wondering is, is there a better  
> idiom for achieving this goal?
> 
> 
> Thanks,
> 
> -kevin
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list