Parallelizing Sequential Code

Recall the emphasis we made on list-at-a-time operations and in particular the function lists:map/2? map/2 is defined like this:

 
map(_, []) -> [];
 
map(F, [H|T]) -> [F(H)|map(F, T)].

A simple strategy for speeding up our sequential programs would replace all calls to map with a new version of map that I’ll call pmap, which evaluates all its arguments in parallel.

lib_misc.erl
 
pmap(F, L) ->
 
S = self(),
 
%% make_ref() returns a unique reference
 
%% we'll match on this later
 
Ref = erlang:make_ref(),
 
Pids = map(​fun​(I) ->
 
spawn​(​fun​() -> do_f(S, Ref, F, I) ​end​)
 
end​, L),
 
%% gather the results
 
gather(Pids, Ref).
 
 
do_f(Parent, Ref, F, I) ->
 
Parent ! {self(), Ref, (​catch​ F(I))}.

Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.