GrPPI  0.2
Generic and Reusable Parallel Pattern Interface
seq/divideconquer.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_SEQ_DIVIDECONQUER_H
22 #define GRPPI_SEQ_DIVIDECONQUER_H
23 
24 #include "sequential_execution.h"
25 
26 namespace grppi {
27 
49 template <typename Input, typename Divider, typename Solver, typename Combiner>
50 typename std::result_of<Solver(Input)>::type
52  Input & input,
53  Divider && divider_op, Solver && solver_op,
54  Combiner && combiner_op)
55 {
56  auto subproblems = divider_op(input);
57 
58  if (subproblems.size()<=1) return solver_op(input);
59 
60  using Output = typename std::result_of<Solver(Input)>::type;
61  std::vector<Output> partials;
62  // FORK
63  for (auto && item : subproblems) {
64  //THREAD
65  partials.push_back(divide_conquer(ex, item,
66  std::forward<Divider>(divider_op), std::forward<Solver>(solver_op),
67  std::forward<Combiner>(combiner_op)));
68  //END THREAD
69  }
70  Output out = partials[0] ;
71  //JOIN
72  for(int i = 1; i<partials.size();i++){
73  out = combiner_op(out,partials[i]);
74  }
75  return out;
76 }
77 
83 }
84 #endif
Definition: callable_traits.h:24
std::result_of< Solver(Input)>::type divide_conquer(parallel_execution_native &ex, Input &problem, Divider &&divide_op, Solver &&solve_op, Combiner &&combine_op)
Invoke Divide/conquer pattern with native parallel execution.
Definition: native/divideconquer.h:124
Sequential execution policy.
Definition: sequential_execution.h:31