Last active
November 14, 2017 10:40
-
-
Save nicklasos/c177478b972e74872b3b to your computer and use it in GitHub Desktop.
Erlang. Parallel map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(plists). | |
-export([pmap/2,pforeach/2,npforeach/2, parmap/2]). | |
%%% Map | |
pmap(F, L) -> | |
S = self(), | |
Pids = lists:map(fun(I) -> spawn(fun() -> pmap_f(S, F, I) end) end, L), | |
pmap_gather(Pids). | |
pmap_gather([H|T]) -> | |
receive | |
{H, Ret} -> [Ret|pmap_gather(T)] | |
end; | |
pmap_gather([]) -> | |
[]. | |
pmap_f(Parent, F, I) -> | |
Parent ! {self(), (catch F(I))}. | |
%%% Foreach | |
pforeach(F, L) -> | |
S = self(), | |
Pids = pmap(fun(I) -> spawn(fun() -> pforeach_f(S,F,I) end) end, L), | |
pforeach_wait(Pids). | |
pforeach_wait([H|T]) -> | |
receive | |
H -> pforeach_wait(T) | |
end; | |
pforeach_wait([]) -> ok. | |
pforeach_f(Parent, F, I) -> | |
_ = (catch F(I)), | |
Parent ! self(). | |
npforeach(F, L) -> | |
S = self(), | |
Pid = spawn(fun() -> npforeach_0(S,F,L) end), | |
receive Pid -> ok end. | |
npforeach_0(Parent,F,L) -> | |
S = self(), | |
Pids = pmap(fun(I) -> spawn(fun() -> npforeach_f(S,F,I) end) end, L), | |
npforeach_wait(S,length(Pids)), | |
Parent ! S. | |
npforeach_wait(_S,0) -> ok; | |
npforeach_wait(S,N) -> | |
receive | |
S -> npforeach_wait(S,N-1) | |
end. | |
npforeach_f(Parent, F, I) -> | |
_ = (catch F(I)), | |
Parent ! Parent. | |
%%% Map list | |
parmap(F, L) -> | |
Parent = self(), | |
[receive {Pid, Result} -> Result end || Pid <- [spawn(fun() -> Parent ! {self(), F(X)} end) || X <- L]]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Which is better to use, parmap or pmap?