Multicore

Aspects

Speeding up programs on a multicore CPU

General guidelines

Making code run in parallel

code

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

Parallel map

pmap(F, Xs) ->
   S = self(),
   Ref = make_ref(),
   Pids = map(fun(X) -> spawn( fun() -> 
                                  S ! {self(), Ref, F(X)} 
                               end )
              end, Xs),
   gather(Pids, Ref).

gather([Pid|T], Ref) -> 
   receive
       {Pid, Ref, Ret} -> [Ret|gather(T, Ref)]
   end ;
gather([], _) -> [].


Concurrent Programming 2016 - Chalmers University of Technology & Gothenburg University