GrPPI  0.2
Generic and Reusable Parallel Pattern Interface
tbb/mapreduce.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_TBB_MAPREDUCE_H
22 #define GRPPI_TBB_MAPREDUCE_H
23 
24 #ifdef GRPPI_TBB
25 
26 #include "parallel_execution_tbb.h"
27 
28 #include <tbb/tbb.h>
29 
30 namespace grppi {
31 
55 template <typename InputIt, typename Transformer, typename Identity, typename Combiner>
56 Identity map_reduce ( parallel_execution_tbb& p, InputIt first, InputIt last, Identity identity, Transformer && transform_op, Combiner && combine_op){
57 
58  using namespace std;
59  tbb::task_group g;
60 
61  Identity out = identity;
62  std::vector<Identity> partialOuts(p.concurrency_degree());
63  int numElements = last - first;
64  int elemperthr = numElements/p.concurrency_degree();
66  for(int i=1;i<p.concurrency_degree();i++){
67  auto begin = first + (elemperthr * i);
68  auto end = first + (elemperthr * (i+1));
69  if(i == p.concurrency_degree() -1 ) end= last;
70  g.run(
71  [&, begin, end, i](){
72  partialOuts[i] = map_reduce(s, begin, end, partialOuts[i], std::forward<Transformer>(transform_op), std::forward<Combiner>(combine_op));
73  }
74 
75  );
76  }
77 
78  partialOuts[0] = map_reduce(s, first, (first+elemperthr), partialOuts[0], std::forward<Transformer>(transform_op), std::forward<Combiner>(combine_op));
79  g.wait();
80 
81  for( auto & res : partialOuts){
82  out = combine_op(out, res);
83  }
84  return out;
85 }
86 
87 }
88 #endif
89 
90 #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
TBB parallel execution policy.
Definition: parallel_execution_tbb.h:37
int concurrency_degree() const noexcept
Get number of grppi trheads.
Definition: parallel_execution_tbb.h:73
Sequential execution policy.
Definition: sequential_execution.h:31