GrPPI  0.2
Generic and Reusable Parallel Pattern Interface
omp/reduce.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_OMP_REDUCE_H
22 #define GRPPI_OMP_REDUCE_H
23 
24 #ifdef GRPPI_OMP
25 
26 #include "parallel_execution_omp.h"
27 #include "reduce.h"
28 
29 namespace grppi {
30 
51 template < typename InputIt, typename Identity, typename Combiner>
53  InputIt first, InputIt last,
54  Identity identity,
55  Combiner && combine_op)
56 {
57  int numElements = last - first;
58  int elemperthr = numElements/ex.concurrency_degree();
59  auto identityVal = identity;
60  //local output
61  std::vector<typename std::iterator_traits<InputIt>::value_type> out(ex.concurrency_degree());
62  //Create threads
63  #pragma omp parallel
64  {
65  #pragma omp single nowait
66  {
67  for(int i=1;i<ex.concurrency_degree();i++){
68 
69  auto begin = first + (elemperthr * i);
70  auto end = first + (elemperthr * (i+1));
71  if(i == ex.concurrency_degree() -1) end = last;
72 
73  #pragma omp task firstprivate (begin, end,i)
74  {
75  out[i] = identityVal;
76  while( begin != end ) {
77  out[i] = combine_op(*begin, out[i] );
78  begin++;
79  }
80  }
81 
82  }
83 
84  //Main thread
85  auto end = first + elemperthr;
86  out[0] = identityVal;
87  while(first!=end){
88  out[0] = combine_op(*first , out[0]);
89  first++;
90  }
91 
92  #pragma omp taskwait
93  }
94  }
95 
96  auto outVal = out[0];
97  for(unsigned int i = 1; i < out.size(); i++){
98  outVal = combine_op(outVal, out[i]);
99  }
100  return outVal;
101 }
102 
108 }
109 
110 
111 
112 #endif
113 
114 #endif
Definition: callable_traits.h:24
OpenMP parallel execution policy.
Definition: parallel_execution_omp.h:40
int concurrency_degree() const noexcept
Get number of grppi trheads.
Definition: parallel_execution_omp.h:85
auto reduce(parallel_execution_native &ex, InputIt first, InputIt last, Identity identity, Combiner &&combine_op)
Invoke Reduce pattern with identity value on a data sequence with parallel native execution...
Definition: native/reduce.h:51