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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.