unary_reduction.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2014 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Justus Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * unary_reduction.h
22  * Apr 2, 2014
23  *
24  */
25 
26 #ifndef TILEDARRAY_TILE_OP_UNARY_REDUCTION_H__INCLUDED
27 #define TILEDARRAY_TILE_OP_UNARY_REDUCTION_H__INCLUDED
28 
31 
32 namespace TiledArray {
33 
35 
38 template <typename Tile>
39 class SumReduction {
40  public:
41  // typedefs
42  using result_type = decltype(sum(std::declval<Tile>()));
44 
45  // Reduction functions
46 
47  // Make an empty result object
48  result_type operator()() const { return result_type(0); }
49 
50  // Post process the result
51  const result_type& operator()(const result_type& result) const {
52  return result;
53  }
54 
55  // Reduce two result objects
56  void operator()(result_type& result, const result_type& arg) const {
57  result += arg;
58  }
59 
60  // Reduce an argument
61  void operator()(result_type& result, const argument_type& arg) const {
62  using TiledArray::sum;
63  result += sum(arg);
64  }
65 }; // class SumReduction
66 
68 
71 template <typename Tile>
73  public:
74  // typedefs
75  using result_type = decltype(product(std::declval<Tile>()));
77 
78  // Reduction functions
79 
80  // Make an empty result object
81  result_type operator()() const { return result_type(1); }
82 
83  // Post process the result
84  const result_type& operator()(const result_type& result) const {
85  return result;
86  }
87 
88  // Reduce two result objects
89  void operator()(result_type& result, const result_type& arg) const {
90  result *= arg;
91  }
92 
93  // Reduce an argument
94  void operator()(result_type& result, const argument_type& arg) const {
95  using TiledArray::product;
96  result *= product(arg);
97  }
98 
99 }; // class ProductReduction
100 
102 
106 template <typename Tile>
108  public:
109  // typedefs
112 
113  // Reduction functions
114 
115  // Make an empty result object
116  result_type operator()() const { return result_type(0); }
117 
118  // Post process the result
119  const result_type& operator()(const result_type& result) const {
120  return result;
121  }
122 
123  // Reduce two result objects
124  void operator()(result_type& result, const result_type& arg) const {
125  result += arg;
126  }
127 
128  // Reduce an argument
129  void operator()(result_type& result, const argument_type& arg) const {
130  using TiledArray::trace;
131  result += trace(arg);
132  }
133 
134 }; // class TraceReduction
135 
137 
141 template <typename Tile>
143  public:
144  // typedefs
145  using result_type = decltype(squared_norm(std::declval<Tile>()));
147 
148  // Reduction functions
149 
150  // Make an empty result object
151  result_type operator()() const { return result_type(0); }
152 
153  // Post process the result
154  const result_type& operator()(const result_type& result) const {
155  return result;
156  }
157 
158  // Reduce two result objects
159  void operator()(result_type& result, const result_type& arg) const {
160  result += arg;
161  }
162 
163  // Reduce an argument
164  void operator()(result_type& result, const argument_type& arg) const {
166  result += squared_norm(arg);
167  }
168 
169 }; // class SquaredNormReduction
170 
172 
176 template <typename Tile, typename Enabler = void>
178 
179 template <typename Tile>
180 class MinReduction<Tile, typename std::enable_if<detail::is_strictly_ordered<
181  detail::numeric_t<Tile>>::value>::type> {
182  public:
183  // typedefs
184  using result_type = decltype(min(std::declval<Tile>()));
186 
187  // Reduction functions
188 
189  // Make an empty result object
192  }
193 
194  // Post process the result
195  const result_type& operator()(const result_type& result) const {
196  return result;
197  }
198 
199  // Reduce two result objects
200  void operator()(result_type& result, const result_type& arg) const {
201  result = std::min(result, arg);
202  }
203 
204  // Reduce an argument
205  void operator()(result_type& result, const argument_type& arg) const {
206  using TiledArray::min;
207  result = std::min(result, min(arg));
208  }
209 
210 }; // class MinReduction
211 
213 
217 template <typename Tile, typename Enabler = void>
219 
220 template <typename Tile>
221 class MaxReduction<Tile, typename std::enable_if<detail::is_strictly_ordered<
222  detail::numeric_t<Tile>>::value>::type> {
223  public:
224  // typedefs
225  using result_type = decltype(max(std::declval<Tile>()));
227 
228  // Reduction functions
229 
230  // Make an empty result object
233  }
234 
235  // Post process the result
236  const result_type& operator()(const result_type& result) const {
237  return result;
238  }
239 
240  // Reduce two result objects
241  void operator()(result_type& result, const result_type& arg) const {
242  result = std::max(result, arg);
243  }
244 
245  // Reduce an argument
246  void operator()(result_type& result, const argument_type& arg) const {
247  using TiledArray::max;
248  result = std::max(result, max(arg));
249  }
250 
251 }; // class MaxReduction
252 
254 
257 template <typename Tile>
259  public:
260  // typedefs
261  using result_type = decltype(abs_min(std::declval<Tile>()));
263 
264  // Reduction functions
265 
266  // Make an empty result object
269  }
270 
271  // Post process the result
272  const result_type& operator()(const result_type& result) const {
273  return result;
274  }
275 
276  // Reduce two result objects
277  void operator()(result_type& result, const result_type& arg) const {
278  result = std::min(result, std::abs(arg));
279  }
280 
281  // Reduce an argument
282  void operator()(result_type& result, const argument_type& arg) const {
283  using TiledArray::abs_min;
284  result = std::min(result, abs_min(arg));
285  }
286 
287 }; // class AbsMinReduction
288 
290 
293 template <typename Tile>
295  public:
296  // typedefs
297  using result_type = decltype(abs_max(std::declval<Tile>()));
299 
300  // Reduction functions
301 
302  // Make an empty result object
303  result_type operator()() const { return result_type(0); }
304 
305  // Post process the result
306  const result_type& operator()(const result_type& result) const {
307  return result;
308  }
309 
310  // Reduce two result objects
311  void operator()(result_type& result, const result_type& arg) const {
312  result = std::max(result, std::abs(arg));
313  }
314 
315  // Reduce an argument
316  void operator()(result_type& result, const argument_type& arg) const {
317  using TiledArray::abs_max;
318  result = std::max(result, abs_max(arg));
319  }
320 
321 }; // class AbsMaxReduction
322 
323 } // namespace TiledArray
324 
325 #endif // TILEDARRAY_TILE_OP_UNARY_REDUCTION_H__INCLUDED
void operator()(result_type &result, const result_type &arg) const
const result_type & operator()(const result_type &result) const
void operator()(result_type &result, const argument_type &arg) const
const result_type & operator()(const result_type &result) const
const result_type & operator()(const result_type &result) const
result_type operator()() const
const result_type & operator()(const result_type &result) const
result_type operator()() const
const result_type & operator()(const result_type &result) const
void operator()(result_type &result, const argument_type &arg) const
decltype(sum(std::declval< Tile >())) result_type
decltype(trace(std::declval< T >())) result_of_trace_t
Definition: trace.h:102
void operator()(result_type &result, const result_type &arg) const
KroneckerDeltaTile< _N >::numeric_type min(const KroneckerDeltaTile< _N > &arg)
decltype(abs_max(std::declval< Tile >())) result_type
decltype(auto) trace(const Tile< Arg > &arg)
Sum the hyper-diagonal elements a tile.
Definition: tile.h:1477
Maxabs tile reduction.
result_type operator()() const
decltype(abs_min(std::declval< Tile >())) result_type
Tile trace reduction.
Maximum tile reduction.
Minimum tile reduction.
decltype(auto) min(const Tile< Arg > &arg)
Minimum element of a tile.
Definition: tile.h:1559
Tile product reduction.
decltype(squared_norm(std::declval< Tile >())) result_type
auto abs_min(const DistArray< Tile, Policy > &a)
Definition: dist_array.h:1630
auto abs_max(const DistArray< Tile, Policy > &a)
Definition: dist_array.h:1635
decltype(product(std::declval< Tile >())) result_type
result_type operator()() const
KroneckerDeltaTile< _N >::numeric_type max(const KroneckerDeltaTile< _N > &arg)
auto squared_norm(const DistArray< Tile, Policy > &a)
Definition: dist_array.h:1655
void operator()(result_type &result, const argument_type &arg) const
void operator()(result_type &result, const argument_type &arg) const
void operator()(result_type &result, const result_type &arg) const
void operator()(result_type &result, const argument_type &arg) const
Squared norm tile reduction.
Minabs tile reduction.
auto abs(const ComplexConjugate< T > &a)
Definition: complex.h:270
decltype(auto) product(const Tile< Arg > &arg)
Multiply the elements of a tile.
Definition: tile.h:1506
Tile sum reduction.
decltype(auto) max(const Tile< Arg > &arg)
Maximum element of a tile.
Definition: tile.h:1549
decltype(auto) sum(const Tile< Arg > &arg)
Sum the elements of a tile.
Definition: tile.h:1496
void operator()(result_type &result, const result_type &arg) const
void operator()(result_type &result, const result_type &arg) const
result_of_trace_t< Tile > result_type
void operator()(result_type &result, const argument_type &arg) const
void operator()(result_type &result, const result_type &arg) const
result_type operator()() const
const result_type & operator()(const result_type &result) const
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:82