GrPPI  1.0
Generic and Reusable Parallel Pattern Interface
reduce_pattern.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Universidad Carlos III de Madrid
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef GRPPI_COMMON_REDUCE_PATTERN_H
17 #define GRPPI_COMMON_REDUCE_PATTERN_H
18 
19 
20 namespace grppi{
21 
28 template <typename Combiner, typename Identity>
29 class reduce_t {
30 public:
31 
39  reduce_t(int wsize, int offset, Identity id, Combiner && combine_op) :
40  window_size_{wsize}, offset_{offset},
41  identity_{id}, combiner_{combine_op}
42  {}
43 
50  void add_item(Identity && item) {
51  if (remaining>0) {
52  remaining--;
53  }
54  else {
55  items.push_back(std::forward<Identity>(item));
56  }
57  }
58 
63  bool reduction_needed() const {
64  return !items.empty() && (static_cast<int>(items.size()) >= window_size_);
65  }
66 
71  Combiner combiner() const { return combiner_; }
72 
77  int window_size() const { return window_size_; }
78 
83  int offset() const { return offset_; }
84 
89  template <typename E>
90  auto reduce_window(const E & e) {
91  auto red = e.reduce(items.begin(), items.size(), identity_, combiner_);
92  if (offset_ > window_size_) {
93  remaining = offset_ - window_size_;
94  items.clear();
95  }
96  else {
97  items.erase(items.begin(), std::next(items.begin(), offset_));
98  }
99  return red;
100  }
101 
102  template<typename T>
103  auto operator()(T &&){
104  return Identity{};
105  }
106 
107 private:
108  int window_size_;
109  int offset_;
110  Identity identity_;
111  Combiner combiner_;
112 
113  std::vector<Identity> items{};
114  int remaining = 0;
115 };
116 
117 namespace internal {
118 
119 template<typename T>
120 struct is_reduce : std::false_type {};
121 
122 template <typename C, typename I>
123 struct is_reduce<reduce_t<C,I>> :std::true_type {};
124 
125 }
126 
127 template <typename T>
129 
130 template <typename T>
131 using requires_reduce = std::enable_if_t<is_reduce<T>,int>;
132 
133 } // end namespace grppi
134 
135 #endif
Representation of reduce pattern. Represents a reduction that can be used as a stage on a pipeline.
Definition: reduce_pattern.h:29
auto reduce_window(const E &e)
Reduce values from a window.
Definition: reduce_pattern.h:90
Combiner combiner() const
Get the combiner.
Definition: reduce_pattern.h:71
int window_size() const
Get the window size.
Definition: reduce_pattern.h:77
int offset() const
Get the offset.
Definition: reduce_pattern.h:83
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:50
bool reduction_needed() const
Check if a reduction can be performed.
Definition: reduce_pattern.h:63
reduce_t(int wsize, int offset, Identity id, Combiner &&combine_op)
Construct a reduction pattern object.
Definition: reduce_pattern.h:39
auto operator()(T &&)
Definition: reduce_pattern.h:103
Definition: callable_traits.h:21
std::enable_if_t< is_reduce< T >, int > requires_reduce
Definition: reduce_pattern.h:131
constexpr bool is_reduce
Definition: reduce_pattern.h:128
Definition: reduce_pattern.h:120