[erlang-questions] question re parallel map code on pg 366 of "Programming Erlang"

Joe Armstrong erlang@REDACTED
Thu Jan 22 12:46:19 CET 2009


It's because I wanted pmap to always terminate - and the order of the
results to be the same as the order in a regular map.

The point of the example was to show how to parallelise a map - not
to take care of all possible error cases. I could have done this without
a catch but then pmap would sometimes fail.

One meta-principle of the book is "only confuse the reader with one
thing at a time - I broke the principle here - possibly an example
without the
catch might be better - but then it would fail sometimes and people would
wonder why.

pmap is actually quite useful - I've run it on a multi-core to parse a bundle of
files in parallel - works great

/Joe

On Thu, Jan 22, 2009 at 4:51 AM, David Cabana <drcabana@REDACTED> wrote:
> I have a question re Joe's parallel map code on page 366 of Programming Erlang.
> Here's the code, modulo a couple of variable name changes.
>
> pmap(F, Arglist) ->
>    S = self(),
>    TaskID = make_ref(),
>    Workers = lists:map( fun(X) ->
>                                 spawn( fun() -> do_F(S, TaskID, F, X )  end)
>                         end, Arglist),
>    gather(Workers, TaskID).
>
> do_F(Caller, TaskID, F, X) ->
>    Caller ! {self(), TaskID, catch(F(X))}.
>
> gather([W|R], TaskID) ->
>    receive
>        {W, TaskID, Val}->
>            [Val | gather(R, TaskID)]
>    end;
>
> gather([], _) ->
>    [].
>
> My question concerns the use of catch inside the do_F function. Here's what Joe
> says on page 367:
>
> "In pmap we use catch(F(H)) when we map the function over the list. In
> map we just
> use F(H). This is because we want to make sure pmap terminates
> correctly in the case
> where the computation of F(H) raises an exception."
>
> I don't get it.
>
> If the computation of F(H) raises an exception, presumably it would do
> so whether F was called by
> map or by pmap. Why catch the exception in the one case but not the other?
> Why is it important that pmap terminate normally, but not so that map
> terminate normally?
>
> Thanks,
> drc
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list