Erlang: pmap with max number of processes
up vote
1
down vote
favorite
I'm working through Joe Armstrong's Programming Erlang 2nd E. The book has exercises at the end of each chapter. Chapter 26, Question 5 is:
Write a function called
pmap(F, L, Max)
that computes the list[F(I) || I <- L]
in parallel but is subject to the restriction that no more thanMax
parallel processes run simultaneously.
My solution is:
-module(pmap_cap).
-export([pmap/3]).
pmap(F, L, Max) ->
S = self(),
Ref = make_ref(),
Pids = lists:map(fun(SubL) ->
spawn(fun() -> do_f(S, Ref, F, SubL) end)
end,
partition(L, min(length(L), Max))),
gather(Pids, Ref).
partition(L, N) ->
M = length(L),
if
M =< N -> lists:map(fun(X) -> [X] end, L);
true -> partition(L, M div N, M rem N)
end.
partition(, _Q, _R) -> ;
partition(L, Q, R) ->
Extra = if R > 0 -> 1; true -> 0 end,
[lists:sublist(L, Q + Extra)|
partition(lists:sublist(L, Q + Extra + 1, length(L)), Q, R-1)].
do_f(Parent, Ref, F, SubL) ->
Parent ! {self(), Ref, lists:map(fun(X) -> catch F(X) end, SubL)}.
gather([Pid|T], Ref) ->
receive
{Pid, Ref, Ret} ->
lists:append(Ret, gather(T, Ref))
end;
gather(, _) -> .
What could be improved? Are there performance issues? Can the code be written more idiomatically?
erlang actor
bumped to the homepage by Community♦ 38 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
1
down vote
favorite
I'm working through Joe Armstrong's Programming Erlang 2nd E. The book has exercises at the end of each chapter. Chapter 26, Question 5 is:
Write a function called
pmap(F, L, Max)
that computes the list[F(I) || I <- L]
in parallel but is subject to the restriction that no more thanMax
parallel processes run simultaneously.
My solution is:
-module(pmap_cap).
-export([pmap/3]).
pmap(F, L, Max) ->
S = self(),
Ref = make_ref(),
Pids = lists:map(fun(SubL) ->
spawn(fun() -> do_f(S, Ref, F, SubL) end)
end,
partition(L, min(length(L), Max))),
gather(Pids, Ref).
partition(L, N) ->
M = length(L),
if
M =< N -> lists:map(fun(X) -> [X] end, L);
true -> partition(L, M div N, M rem N)
end.
partition(, _Q, _R) -> ;
partition(L, Q, R) ->
Extra = if R > 0 -> 1; true -> 0 end,
[lists:sublist(L, Q + Extra)|
partition(lists:sublist(L, Q + Extra + 1, length(L)), Q, R-1)].
do_f(Parent, Ref, F, SubL) ->
Parent ! {self(), Ref, lists:map(fun(X) -> catch F(X) end, SubL)}.
gather([Pid|T], Ref) ->
receive
{Pid, Ref, Ret} ->
lists:append(Ret, gather(T, Ref))
end;
gather(, _) -> .
What could be improved? Are there performance issues? Can the code be written more idiomatically?
erlang actor
bumped to the homepage by Community♦ 38 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm working through Joe Armstrong's Programming Erlang 2nd E. The book has exercises at the end of each chapter. Chapter 26, Question 5 is:
Write a function called
pmap(F, L, Max)
that computes the list[F(I) || I <- L]
in parallel but is subject to the restriction that no more thanMax
parallel processes run simultaneously.
My solution is:
-module(pmap_cap).
-export([pmap/3]).
pmap(F, L, Max) ->
S = self(),
Ref = make_ref(),
Pids = lists:map(fun(SubL) ->
spawn(fun() -> do_f(S, Ref, F, SubL) end)
end,
partition(L, min(length(L), Max))),
gather(Pids, Ref).
partition(L, N) ->
M = length(L),
if
M =< N -> lists:map(fun(X) -> [X] end, L);
true -> partition(L, M div N, M rem N)
end.
partition(, _Q, _R) -> ;
partition(L, Q, R) ->
Extra = if R > 0 -> 1; true -> 0 end,
[lists:sublist(L, Q + Extra)|
partition(lists:sublist(L, Q + Extra + 1, length(L)), Q, R-1)].
do_f(Parent, Ref, F, SubL) ->
Parent ! {self(), Ref, lists:map(fun(X) -> catch F(X) end, SubL)}.
gather([Pid|T], Ref) ->
receive
{Pid, Ref, Ret} ->
lists:append(Ret, gather(T, Ref))
end;
gather(, _) -> .
What could be improved? Are there performance issues? Can the code be written more idiomatically?
erlang actor
I'm working through Joe Armstrong's Programming Erlang 2nd E. The book has exercises at the end of each chapter. Chapter 26, Question 5 is:
Write a function called
pmap(F, L, Max)
that computes the list[F(I) || I <- L]
in parallel but is subject to the restriction that no more thanMax
parallel processes run simultaneously.
My solution is:
-module(pmap_cap).
-export([pmap/3]).
pmap(F, L, Max) ->
S = self(),
Ref = make_ref(),
Pids = lists:map(fun(SubL) ->
spawn(fun() -> do_f(S, Ref, F, SubL) end)
end,
partition(L, min(length(L), Max))),
gather(Pids, Ref).
partition(L, N) ->
M = length(L),
if
M =< N -> lists:map(fun(X) -> [X] end, L);
true -> partition(L, M div N, M rem N)
end.
partition(, _Q, _R) -> ;
partition(L, Q, R) ->
Extra = if R > 0 -> 1; true -> 0 end,
[lists:sublist(L, Q + Extra)|
partition(lists:sublist(L, Q + Extra + 1, length(L)), Q, R-1)].
do_f(Parent, Ref, F, SubL) ->
Parent ! {self(), Ref, lists:map(fun(X) -> catch F(X) end, SubL)}.
gather([Pid|T], Ref) ->
receive
{Pid, Ref, Ret} ->
lists:append(Ret, gather(T, Ref))
end;
gather(, _) -> .
What could be improved? Are there performance issues? Can the code be written more idiomatically?
erlang actor
erlang actor
asked Feb 3 at 8:58
Tianxiang Xiong
1062
1062
bumped to the homepage by Community♦ 38 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 38 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
General
Use standard library as many as possible, usually it given better result therefore function partition can be easily rewritting with wrapper on lists:split/2 and it will be consider as perfomance improvement
split(List,Max)->
L = length(List),
split_by(List,L rem Max + L div Max,).
split_by(,_N,R)->lists:reverse(R);
split_by(List,N,R)->
{Part,NewList} = lists:split(N,List),
split_by(NewList,N,[Part|R]).
Idiomaticness
Add to head of list and reverse instead append. For explanation - SO question.
Tail recursion better for readability and may be faster, so function gather become:
gather([Pid|T], Ref,R) ->
receive
{Pid, Ref, Ret} -> gather(T, Ref,[Ret|R])
end;
gather(, _,R) -> lists:flatten(lists:reverse(R)).
List comprehensions instead lists:map/2:
Min = min(length(L), Max),
Pids = [spawn(
fun() -> do_f(S, Ref, F, SubL) end) || SubL <- split(L, Min)],
...
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
General
Use standard library as many as possible, usually it given better result therefore function partition can be easily rewritting with wrapper on lists:split/2 and it will be consider as perfomance improvement
split(List,Max)->
L = length(List),
split_by(List,L rem Max + L div Max,).
split_by(,_N,R)->lists:reverse(R);
split_by(List,N,R)->
{Part,NewList} = lists:split(N,List),
split_by(NewList,N,[Part|R]).
Idiomaticness
Add to head of list and reverse instead append. For explanation - SO question.
Tail recursion better for readability and may be faster, so function gather become:
gather([Pid|T], Ref,R) ->
receive
{Pid, Ref, Ret} -> gather(T, Ref,[Ret|R])
end;
gather(, _,R) -> lists:flatten(lists:reverse(R)).
List comprehensions instead lists:map/2:
Min = min(length(L), Max),
Pids = [spawn(
fun() -> do_f(S, Ref, F, SubL) end) || SubL <- split(L, Min)],
...
add a comment |
up vote
0
down vote
General
Use standard library as many as possible, usually it given better result therefore function partition can be easily rewritting with wrapper on lists:split/2 and it will be consider as perfomance improvement
split(List,Max)->
L = length(List),
split_by(List,L rem Max + L div Max,).
split_by(,_N,R)->lists:reverse(R);
split_by(List,N,R)->
{Part,NewList} = lists:split(N,List),
split_by(NewList,N,[Part|R]).
Idiomaticness
Add to head of list and reverse instead append. For explanation - SO question.
Tail recursion better for readability and may be faster, so function gather become:
gather([Pid|T], Ref,R) ->
receive
{Pid, Ref, Ret} -> gather(T, Ref,[Ret|R])
end;
gather(, _,R) -> lists:flatten(lists:reverse(R)).
List comprehensions instead lists:map/2:
Min = min(length(L), Max),
Pids = [spawn(
fun() -> do_f(S, Ref, F, SubL) end) || SubL <- split(L, Min)],
...
add a comment |
up vote
0
down vote
up vote
0
down vote
General
Use standard library as many as possible, usually it given better result therefore function partition can be easily rewritting with wrapper on lists:split/2 and it will be consider as perfomance improvement
split(List,Max)->
L = length(List),
split_by(List,L rem Max + L div Max,).
split_by(,_N,R)->lists:reverse(R);
split_by(List,N,R)->
{Part,NewList} = lists:split(N,List),
split_by(NewList,N,[Part|R]).
Idiomaticness
Add to head of list and reverse instead append. For explanation - SO question.
Tail recursion better for readability and may be faster, so function gather become:
gather([Pid|T], Ref,R) ->
receive
{Pid, Ref, Ret} -> gather(T, Ref,[Ret|R])
end;
gather(, _,R) -> lists:flatten(lists:reverse(R)).
List comprehensions instead lists:map/2:
Min = min(length(L), Max),
Pids = [spawn(
fun() -> do_f(S, Ref, F, SubL) end) || SubL <- split(L, Min)],
...
General
Use standard library as many as possible, usually it given better result therefore function partition can be easily rewritting with wrapper on lists:split/2 and it will be consider as perfomance improvement
split(List,Max)->
L = length(List),
split_by(List,L rem Max + L div Max,).
split_by(,_N,R)->lists:reverse(R);
split_by(List,N,R)->
{Part,NewList} = lists:split(N,List),
split_by(NewList,N,[Part|R]).
Idiomaticness
Add to head of list and reverse instead append. For explanation - SO question.
Tail recursion better for readability and may be faster, so function gather become:
gather([Pid|T], Ref,R) ->
receive
{Pid, Ref, Ret} -> gather(T, Ref,[Ret|R])
end;
gather(, _,R) -> lists:flatten(lists:reverse(R)).
List comprehensions instead lists:map/2:
Min = min(length(L), Max),
Pids = [spawn(
fun() -> do_f(S, Ref, F, SubL) end) || SubL <- split(L, Min)],
...
answered Feb 4 at 21:57
user110702
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186662%2ferlang-pmap-with-max-number-of-processes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown