Aspects
Applications to run faster if we have multicore CPUs
Is the code going to run faster just by having more cores?
No!
It might if it has lots of non-interferent processes and no sequential bottlenecks
Erlang uses all the cores on the SMP machine by default
Symmetric MultiProcessing
erl -smp +S N
The number N
indicates how many schedulers (govern
Erlang virtual machines)
Useful to test how the program works on different amount of cores
General guidelines
Use lots of processes
Avoid side effects
Avoid sequential bottlenecks
Write “small messages, big computations” code.
map
map(_, []) -> []; map(F, [H|T]) -> [F(H)|map(F, T)].
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([], _) -> [].
map(F,L) =:= pmap(F,L)
Order of elements?
Side-effects?
What if the list is very big and the computation F
very small?