[erlang-questions] question re parallel map code on pg 366 of "Programming Erlang"
Ulf Wiger
ulf@REDACTED
Fri Jan 23 13:41:52 CET 2009
I gave a tutorial at the DAMP workshop (in conjunction with POPL) this week,
and used pmap() as an example of how parallelizing sequential functions in
Erlang is not always as straightforward as one might think.
http://ulf.wiger.net/weblog/2009/01/23/erlang-programming-for-multicore/
BR,
Ulf W
2009/1/22 David Cabana <drcabana@REDACTED>:
> 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