GrPPI  0.3.1
Generic and Reusable Parallel Pattern Interface
reduce_pattern.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_COMMON_REDUCE_PATTERN_H
22 #define GRPPI_COMMON_REDUCE_PATTERN_H
23 
24 
25 namespace grppi{
26 
33 template <typename Combiner, typename Identity>
34 class reduce_t {
35 public:
36 
44  reduce_t(int wsize, int offset, Identity id, Combiner && combine_op) :
45  window_size_{wsize}, offset_{offset},
46  identity_{id}, combiner_{combine_op}
47  {}
48 
55  void add_item(Identity && item) {
56  if (remaining>0) {
57  remaining--;
58  }
59  else {
60  items.push_back(std::forward<Identity>(item));
61  }
62  }
63 
67  bool reduction_needed() const {
68  return !items.empty() && (items.size() >= window_size_);
69  }
70 
75  Combiner combiner() const { return combiner_; }
76 
81  int window_size() const { return window_size_; }
82 
87  int offset() const { return offset_; }
88 
93  template <typename E>
94  auto reduce_window(const E & e) {
95  auto red = e.reduce(items.begin(), items.size(), identity_, combiner_);
96  if (offset_ > window_size_) {
97  remaining = offset_ - window_size_;
98  items.clear();
99  }
100  else {
101  items.erase(items.begin(), std::next(items.begin(), offset_));
102  }
103  return red;
104  }
105 
106  template<typename T>
107  auto operator()(T &&item){
108  return Identity{};
109  }
110 
111 private:
112  int window_size_;
113  int offset_;
114  Identity identity_;
115  Combiner combiner_;
116 
117  std::vector<Identity> items{};
118  int remaining = 0;
119 };
120 
121 namespace internal {
122 
123 template<typename T>
124 struct is_reduce : std::false_type {};
125 
126 template <typename C, typename I>
127 struct is_reduce<reduce_t<C,I>> :std::true_type {};
128 
129 }
130 
131 template <typename T>
133 
134 template <typename T>
135 using requires_reduce = std::enable_if_t<is_reduce<T>,int>;
136 
137 } // end namespace grppi
138 
139 #endif
Definition: callable_traits.h:26
reduce_t(int wsize, int offset, Identity id, Combiner &&combine_op)
Construct a reduction pattern object.
Definition: reduce_pattern.h:44
std::enable_if_t< is_reduce< T >, int > requires_reduce
Definition: reduce_pattern.h:135
constexpr bool is_reduce
Definition: reduce_pattern.h:132
Combiner combiner() const
Get the combiner.
Definition: reduce_pattern.h:75
Definition: reduce_pattern.h:124
auto reduce_window(const E &e)
Reduce values from a window.
Definition: reduce_pattern.h:94
int window_size() const
Get the window size.
Definition: reduce_pattern.h:81
Representation of reduce pattern. Represents a reduction that can be used as a stage on a pipeline...
Definition: reduce_pattern.h:34
int offset() const
Get the offset.
Definition: reduce_pattern.h:87
bool reduction_needed() const
Check if a reduction can be performed.
Definition: reduce_pattern.h:67
void add_item(Identity &&item)
Add an item to the reduction buffer. If there are remaining items before reaching the next window sta...
Definition: reduce_pattern.h:55
auto operator()(T &&item)
Definition: reduce_pattern.h:107