Created
April 22, 2014 17:25
-
-
Save StoneCypher/11187594 to your computer and use it in GitHub Desktop.
found this cleaning up editor windows, huhu
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
chunk(List) -> | |
part(List, []). | |
chunk([], Acc) -> | |
lists:reverse(Acc); | |
chunk([H], Acc) -> | |
lists:reverse([[H]|Acc]); | |
chunk([H1,H2|T], Acc) -> | |
part(T, [[H1,H2]|Acc]). | |
map_reduce(Functor, Workload) -> | |
Targets = nodes(), | |
NodeCount = length(Targets), | |
WorkLength = length(Workload), | |
WorkSeq = lists:seq(1, WorkLength), | |
AnnotedWork = lists:zip(WorkSeq, Workload), | |
ChunkSize = WorkLength div NodeCount, | |
AnnotedWorkChunks = chunk(AnnotedWork), | |
[ spawn(Tgt, fun() -> Functor(AWC) end) || {Tgt,AWC} = lists:zip(Targets, AnnotedWorkChunks) ], | |
receive_work(WorkLength, []). | |
receive_work(Length, Work) when length(Work) < Length -> | |
receive | |
{ workitem, Id, Item } -> | |
receive_work(Length, [{Id, Item}] ++ Work) | |
end; | |
receive_work(Length, Work) -> | |
[ WorkItem || {_Id, WorkItem} <- lists:keysort(1, Work) ]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment