GrPPI  0.2
Generic and Reusable Parallel Pattern Interface
omp/stream_reduce.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_OMP_STREAM_REDUCE_H
22 #define GRPPI_OMP_STREAM_REDUCE_H
23 
24 #ifdef GRPPI_OMP
25 
26 #include "parallel_execution_omp.h"
27 #include "reduce.h"
28 
29 namespace grppi {
30 
55 template <typename Identity, typename Combiner, typename Consumer,
56  typename Generator>
58  int window_size, int offset, Identity identity,
59  Generator && generate_op, Combiner && combine_op,
60  Consumer && consume_op)
61 {
62  using namespace std;
63  using generated_type = typename result_of<Generator()>::type;
64  using generated_value_type = typename generated_type::value_type;
65 
66  // TODO: Evaluate better structure than vector
67  vector<generated_value_type> values;
68  values.reserve(window_size);
69 
70  // TODO: Set generator and consumer in separate threads
71  auto item = generate_op();
72  for (;;) {
73  while (item && values.size()!=window_size) {
74  values.push_back(*item);
75  item = generate_op();
76  }
77  if (values.size()>0) {
78  auto reduced_value = reduce(ex, values.begin(), values.end(), identity,
79  std::forward<Combiner>(combine_op));
80  consume_op(reduced_value);
81  if (item) {
82  if (offset <= window_size) {
83  values.erase(values.begin(), values.begin() + offset);
84  }
85  else {
86  values.erase(values.begin(), values.end());
87  auto diff = offset - window_size;
88  while (diff > 0 && item) {
89  item = generate_op();
90  diff--;
91  }
92  }
93  }
94  }
95  if (!item) break;
96  }
97 }
98 
104 }
105 
106 #endif
107 
108 #endif
Definition: callable_traits.h:24
STL namespace.
OpenMP parallel execution policy.
Definition: parallel_execution_omp.h:40
void stream_reduce(parallel_execution_native &ex, int window_size, int offset, Identity identity, Generator &&generate_op, Combiner &&combine_op, Consumer &&consume_op)
Invoke Stream reduction pattern on a stream with native parallel execution.
Definition: native/stream_reduce.h:56
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