GrPPI  0.2
Generic and Reusable Parallel Pattern Interface
omp/mapreduce.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_OMP_MAPREDUCE_H
22 #define GRPPI_OMP_MAPREDUCE_H
23 
24 #ifdef GRPPI_OMP
25 
26 #include "parallel_execution_omp.h"
27 
28 namespace grppi {
29 
53 template <typename InputIt, typename Transformer, typename Result,
54  typename Combiner>
56  InputIt first, InputIt last, Result identity,
57  Transformer && transform_op,
58  Combiner && combine_op)
59 {
60  using namespace std;
61  Result result{identity};
62 
63  std::vector<Result> partial_results(ex.concurrency_degree());
64  #pragma omp parallel
65  {
66  #pragma omp single nowait
67  {
68  int num_elements = distance(first,last);
69  int elements_per_thread = num_elements/ex.concurrency_degree();
71 
72  for (int i=1;i<ex.concurrency_degree();i++) {
73  #pragma omp task firstprivate(i)
74  {
75  auto begin = next(first, elements_per_thread * i);
76  auto end = (i==ex.concurrency_degree()-1) ? last :
77  next(first, elements_per_thread * (i+1));
78  partial_results[i] = map_reduce(seq,
79  begin, end, partial_results[i],
80  std::forward<Transformer>(transform_op),
81  std::forward<Combiner>(combine_op));
82  }
83  }
84 
85  partial_results[0] = map_reduce(seq,
86  first, first+elements_per_thread, partial_results[0],
87  std::forward<Transformer>(transform_op),
88  std::forward<Combiner>(combine_op));
89  #pragma omp taskwait
90  }
91  }
92 
93  for (auto && p : partial_results){
94  result = combine_op(result, p);
95  }
96  return result;
97 }
98 
104 }
105 #endif
106 
107 #endif
Definition: callable_traits.h:24
STL namespace.
Result map_reduce(parallel_execution_native &ex, InputIt first, InputIt last, Result identity, Transformer &&transform_op, Combiner &&combine_op)
Invoke Map/reduce pattern on a data sequence with native parallel execution.
Definition: native/mapreduce.h:53
OpenMP parallel execution policy.
Definition: parallel_execution_omp.h:40
Sequential execution policy.
Definition: sequential_execution.h:31
int concurrency_degree() const noexcept
Get number of grppi trheads.
Definition: parallel_execution_omp.h:85