ebb_flow.erl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. -module(ebb_flow).
  2. %%% Smart constructors
  3. -export([func/1, func/2,
  4. value/1, values/1,
  5. pipe/1, pipe/2,
  6. par/1, par/2,
  7. route/2,
  8. sync/1, sync/2,
  9. split/1, split/2,
  10. merge/1, merge/2,
  11. switch/1]).
  12. -export([to_op/1, par_pipes/1, pipe_pars/1]).
  13. %%% Convenience operations
  14. -export([id/0, id/1, nop/0, select/2]).
  15. %%% Parallelism
  16. -export([fanout/1, fanout/2,
  17. map/2, map_split/3,
  18. reduce/2, reduce/3, reduce_split/3,
  19. map_reduce/3, map_reduce/4, map_reduce_split/4]).
  20. %%% Sequencing
  21. -export([seq/1, seq/2, ignore/1]).
  22. %%% Choice
  23. -export([cases/1, case_of/2,
  24. iff/2, if_else/3,
  25. fanin/1, fanin/2]).
  26. %%%-----------------------------------------------------------------------------
  27. %%% Smart constructors
  28. %%%-----------------------------------------------------------------------------
  29. value(X) ->
  30. ebb_prim:value(X).
  31. values(Xs) ->
  32. ebb_prim:par([ ebb_prim:value(X) || X <- Xs ]).
  33. func(F) ->
  34. ebb_prim:func(F).
  35. func(F, N) ->
  36. ebb_prim:pipe([ebb_prim:func(F), ebb_prim:split(N)]).
  37. pipe(Xs) ->
  38. case {ebb_prim:is_operation(Xs), is_list(Xs), is_tuple(Xs)} of
  39. {true, _, _} -> Xs;
  40. {_, true, _} -> ebb_prim:pipe([ to_op(X) || X <- Xs ]);
  41. {_, _, true} -> ebb_prim:pipe([ to_op(X) || X <- tuple_to_list(Xs) ]);
  42. {_, _, _} -> to_op(Xs)
  43. end.
  44. pipe(X, Y) ->
  45. pipe([X, Y]).
  46. par(Xs) ->
  47. case {ebb_prim:is_operation(Xs), is_list(Xs), is_tuple(Xs)} of
  48. {true, _, _} -> Xs;
  49. {_, true, _} -> ebb_prim:par([ to_op(X) || X <- Xs ]);
  50. {_, _, true} -> ebb_prim:par([ to_op(X) || X <- tuple_to_list(Xs) ]);
  51. {_, _, _} -> to_op(Xs)
  52. end.
  53. par(X, Y) ->
  54. par([X, Y]).
  55. route(N, Map) ->
  56. ebb_prim:route(N, Map).
  57. sync(N) ->
  58. ebb_prim:sync(N).
  59. sync(X1, X2) ->
  60. Op1 = to_op(X1),
  61. Op2 = to_op(X2),
  62. ebb_prim:pipe([Op1, sync(ebb_prim:out_arity(Op1)), Op2]).
  63. split(N) ->
  64. ebb_prim:split(N).
  65. split(X1, X2) ->
  66. Op1 = to_op(X1),
  67. Op2 = to_op(X2),
  68. ebb_prim:pipe([Op1, split(ebb_prim:in_arity(Op2)), Op2]).
  69. merge(N) ->
  70. ebb_prim:merge(N).
  71. merge(X1, X2) ->
  72. Op1 = to_op(X1),
  73. Op2 = to_op(X2),
  74. ebb_prim:pipe([Op1, merge(ebb_prim:out_arity(Op1)), Op2]).
  75. switch(Branches) ->
  76. ebb_prim:switch([ {Tag, to_op(X)} || {Tag, X} <- Branches ]).
  77. to_op(X) ->
  78. case {ebb_prim:is_operation(X), is_function(X), is_list(X), is_tuple(X)} of
  79. {true, _, _, _} -> X;
  80. {_, true, _, _} -> ebb_prim:func(X);
  81. {_, _, true, _} -> ebb_prim:par([ to_op(Y) || Y <- X ]);
  82. {_, _, _, true} -> ebb_prim:par([ to_op(Y) || Y <- tuple_to_list(X) ]);
  83. {_, _, _, _} -> ebb_prim:value(X)
  84. end.
  85. par_pipes(Args) ->
  86. ebb_prim:par([ nested_operation(X, fun pipe_pars/1) || X <- Args ]).
  87. pipe_pars(Args) ->
  88. ebb_prim:pipe([ nested_operation(X, fun par_pipes/1) || X <- Args ]).
  89. nested_operation(X, Continue) ->
  90. case {ebb_prim:is_operation(X), is_function(X), is_list(X), is_tuple(X)} of
  91. {true, _, _, _} -> X;
  92. {_, true, _, _} -> ebb_prim:func(X);
  93. {_, _, true, _} -> Continue(X);
  94. {_, _, _, true} -> Continue(tuple_to_list(X));
  95. {_, _, _, _} -> ebb_prim:value(X)
  96. end.
  97. %%%-----------------------------------------------------------------------------
  98. %%% Convenience operations
  99. %%%-----------------------------------------------------------------------------
  100. id() ->
  101. route(1, [1]).
  102. id(N) ->
  103. route(N, lists:seq(1,N)).
  104. nop() ->
  105. value('nop').
  106. select(I, N) ->
  107. route(N, [I]).
  108. %%%-----------------------------------------------------------------------------
  109. %%% Parallelism
  110. %%%-----------------------------------------------------------------------------
  111. fanout(Copies) ->
  112. fanout(1, Copies).
  113. fanout(In, Copies) ->
  114. Out = Copies*In,
  115. route(In, [ (I rem In) + 1 || I <- lists:seq(0, Out-1) ]).
  116. map(Func, List) ->
  117. FOp = to_op(Func),
  118. LOp = to_op(List),
  119. FIn = ebb_prim:in_arity(FOp),
  120. LOut = ebb_prim:out_arity(LOp),
  121. case {LOut div FIn, LOut rem FIn} of
  122. {N, 0} -> pipe(LOp, par(lists:duplicate(N, FOp)));
  123. {_, _} -> erlang:error(badarg, [Func, List])
  124. end.
  125. map_split(N, Func, List) ->
  126. map(Func, pipe(to_op(List), split(N))).
  127. reduce(Func, List) ->
  128. try
  129. reduce_right(to_op(Func), to_op(List))
  130. catch
  131. throw:badarg -> erlang:error(badarg, [Func, List])
  132. end.
  133. reduce(Func, Z, List) ->
  134. try
  135. ZOp = to_op(Z),
  136. case ebb_prim:in_arity(ZOp) of
  137. 0 -> ok;
  138. _ -> throw(badarg)
  139. end,
  140. reduce_right(to_op(Func), ZOp, to_op(List))
  141. catch
  142. throw:badarg -> erlang:error(badarg, [Func, List])
  143. end.
  144. reduce_split(N, Func, List) ->
  145. reduce(Func, pipe(to_op(List), split(N))).
  146. reduce_right(FOp, LOp) ->
  147. FIn = ebb_prim:in_arity(FOp),
  148. LOut = ebb_prim:out_arity(LOp),
  149. case {LOut, LOut div FIn, LOut rem FIn} of
  150. {0, 0, _} -> LOp;
  151. {_, 0, _} -> throw(badarg);
  152. {_, 1, 0} -> pipe(LOp, FOp);
  153. {_, N, 0} -> reduce_right(
  154. FOp, pipe(LOp, par(lists:duplicate(N, FOp))));
  155. {_, N, M} -> reduce_right(
  156. FOp, pipe(LOp, par([id(M) | lists:duplicate(N, FOp)])))
  157. end.
  158. reduce_right(FOp, ZOp, LOp) ->
  159. FIn = ebb_prim:in_arity(FOp),
  160. LOut = ebb_prim:out_arity(LOp),
  161. ZOut = ebb_prim:out_arity(ZOp),
  162. N = LOut div FIn,
  163. Rem = LOut rem FIn,
  164. Need = FIn - Rem,
  165. case {LOut, N, Rem, Need div ZOut, Need rem ZOut} of
  166. {0, 0, _, _, _} -> LOp;
  167. {_, 0, _, Z, 0} -> pipe(par([LOp | lists:duplicate(Z, ZOp)]), FOp);
  168. {_, 1, 0, _, _} -> pipe(LOp, FOp);
  169. {_, N, 0, _, _} -> reduce_right(
  170. FOp, ZOp,
  171. pipe(LOp, par(lists:duplicate(N, FOp))));
  172. {_, N, _, Z, 0} -> reduce_right(
  173. FOp, ZOp,
  174. pipe(par([LOp | lists:duplicate(Z, ZOp)]),
  175. par(lists:duplicate(N+1, FOp))));
  176. {_, _, _, _, _} -> throw(badarg)
  177. end.
  178. map_reduce(Map, Red, List) ->
  179. reduce(Red, map(Map, List)).
  180. map_reduce(Map, Red, Z, List) ->
  181. reduce(Red, Z, map(Map, List)).
  182. map_reduce_split(N, Map, Red, List) ->
  183. reduce(Red, map_split(N, Map, List)).
  184. %%%-----------------------------------------------------------------------------
  185. %%% Sequencing
  186. %%%-----------------------------------------------------------------------------
  187. seq(Xs = [_|_]) ->
  188. Ops = [Op1|Rest] = [ to_op(X) || X <- Xs ],
  189. {In, _} = ebb_prim:flatten_arity(Ops),
  190. Produced1 = ebb_prim:out_arity(Op1),
  191. Remaining1 = In-ebb_prim:in_arity(Op1),
  192. {Seq, _, _} =
  193. lists:foldl(fun(Op, {Seq,Produced,Remaining}) ->
  194. Produced2 = Produced+ebb_prim:out_arity(Op),
  195. Remaining2 = Remaining-ebb_prim:in_arity(Op),
  196. {sync(Seq, par([id(Produced),
  197. Op,
  198. id(Remaining2)])),
  199. Produced2, Remaining2}
  200. end,
  201. {par(Op1, id(Remaining1)),
  202. Produced1, Remaining1},
  203. Rest),
  204. Seq;
  205. seq(X) ->
  206. to_op(X).
  207. seq(X, Y) ->
  208. seq([X, Y]).
  209. ignore(N) ->
  210. ebb_prim:route(N, []).
  211. %%%-----------------------------------------------------------------------------
  212. %%% Choice
  213. %%%-----------------------------------------------------------------------------
  214. cases(Branches) ->
  215. switch(Branches).
  216. case_of(Disc, Branches) ->
  217. Cases = cases(Branches),
  218. pipe_pars([[to_op(Disc), id(ebb_prim:in_arity(Cases)-1)],
  219. Cases]).
  220. iff(Then, Else) ->
  221. cases([{true, Then},
  222. {false, Else}]).
  223. if_else(Test, Then, Else) ->
  224. case_of(Test, [{true, Then},
  225. {false, Else}]).
  226. fanin(Copies) ->
  227. cases([ {I , select(I, Copies)} || I <- lists:seq(1, Copies) ]).
  228. fanin(Copies, Out) ->
  229. In = Copies*Out,
  230. cases([ {I, ebb_flow:route(In, [ (I-1)*Out + J || J <- lists:seq(1, Out)])}
  231. || I <- lists:seq(1, Copies) ]).