reduce_wrapper.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 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  * reduce_wrapper.h
22  * Oct 7, 2013
23  *
24  */
25 
26 #ifndef TILEDARRAY_TILE_OP_REDUCE_INTERFACE_H__INCLUDED
27 #define TILEDARRAY_TILE_OP_REDUCE_INTERFACE_H__INCLUDED
28 
30 #include <TiledArray/type_traits.h>
31 
32 namespace TiledArray {
33 namespace math {
34 
36 
40 template <typename Tile, typename Op>
41 class UnaryReduceWrapper : public Op {
42  public:
43  // typedefs
46  typedef typename Op::result_type result_type;
47  typedef Tile argument_type;
48 
49  // Constructors
51  UnaryReduceWrapper(const Op& op) : Op(op) {}
52  UnaryReduceWrapper(const UnaryReduceWrapper_& other) : Op(other) {}
53 
55  Op::operator=(other);
56  return *this;
57  }
58 
59  // Import base class functionality
60  using Op::operator();
61 
62  private:
63  template <typename T>
64  typename std::enable_if<is_lazy_tile<T>::value>::type reduce(
65  result_type& result, const T& arg) const {
66  typename eval_trait<T>::type eval_arg(arg);
67  Op::operator()(result, eval_arg);
68  }
69 
70  template <typename T>
71  typename std::enable_if<!is_lazy_tile<T>::value>::type reduce(
72  result_type& result, const T& arg) const {
73  Op::operator()(result, arg);
74  }
75 
76  public:
77  // Reduce an argument
78  void operator()(result_type& result, const argument_type& arg) const {
79  reduce(result, arg);
80  }
81 
82 }; // struct UnaryReduceWrapper
83 
85 
89 template <typename Op>
90 class UnaryReduceWrapper<typename Op::argument_type, Op> : public Op {
91  public:
92  // typedefs
95  typedef typename Op::result_type result_type;
96  typedef typename Op::argument_type
98 
99  // Constructors
101  UnaryReduceWrapper(const Op& op) : Op(op) {}
102  UnaryReduceWrapper(const UnaryReduceWrapper_& other) : Op(other) {}
103 
105  Op::operator=(other);
106  return *this;
107  }
108 
109  // Import base class functionality
110  using Op::operator();
111 
112 }; // struct UnaryReduceWrapper
113 
115 
120 template <typename Left, typename Right, typename Op>
121 struct BinaryReduceWrapper : public Op {
122  public:
123  // typedefs
126  typedef typename Op::result_type result_type;
127  typedef Left first_argument_type;
128  typedef Right
130 
131  // Constructors
133  BinaryReduceWrapper(const Op& op) : Op(op) {}
134  BinaryReduceWrapper(const BinaryReduceWrapper_& other) : Op(other) {}
135 
137  Op::operator=(other);
138  return *this;
139  }
140 
141  private:
142  template <typename L, typename R>
143  typename std::enable_if<is_lazy_tile<L>::value &&
145  reduce(result_type& result, const L& left, const R& right) const {
146  typename eval_trait<L>::type eval_left(left);
147  typename eval_trait<R>::type eval_right(right);
148  Op::operator()(result, eval_left, eval_right);
149  }
150 
151  template <typename L, typename R>
152  typename std::enable_if<(!is_lazy_tile<L>::value) &&
154  reduce(result_type& result, const L& left, const R& right) const {
155  typename eval_trait<R>::type eval_right(right);
156  Op::operator()(result, left, eval_right);
157  }
158 
159  template <typename L, typename R>
160  typename std::enable_if<is_lazy_tile<L>::value &&
161  (!is_lazy_tile<R>::value)>::type
162  reduce(result_type& result, const L& left, const R& right) const {
163  typename eval_trait<L>::type eval_left(left);
164  Op::operator()(result, eval_left, right);
165  }
166 
167  template <typename L, typename R>
168  typename std::enable_if<!(is_lazy_tile<L>::value ||
169  is_lazy_tile<R>::value)>::type
170  reduce(result_type& result, const L& left, const R& right) const {
171  Op::operator()(result, left, right);
172  }
173 
174  public:
175  // Import base class functionality
176  using Op::operator();
177 
178  // Reduce an argument pair
179  void operator()(result_type& result, const first_argument_type& left,
180  const second_argument_type& right) const {
181  reduce(result, left, right);
182  }
183 
184 }; // struct BinaryReduceWrapper
185 
187 template <typename Op>
189  typename Op::second_argument_type, Op> : public Op {
190  // typedefs
191  typedef BinaryReduceWrapper<typename Op::first_argument_type,
192  typename Op::second_argument_type, Op>
194  typedef typename Op::result_type result_type;
195  typedef typename Op::first_argument_type
197  typedef typename Op::second_argument_type
199 
200  // Constructors
202  BinaryReduceWrapper(const Op& op) : Op(op) {}
203  BinaryReduceWrapper(const BinaryReduceWrapper_& other) : Op(other) {}
204 
206  Op::operator=(other);
207  return *this;
208  }
209 
210  // Import base class functionality
211  using Op::operator();
212 
213 }; // struct BinaryReduceWrapper
214 
215 } // namespace math
216 } // namespace TiledArray
217 
218 #endif // TILEDARRAY_TILE_OP_REDUCE_INTERFACE_H__INCLUDED
UnaryReduceWrapper< Tile, Op > UnaryReduceWrapper_
This class type.
::blas::Op Op
Definition: blas.h:46
Unary reduction wrapper class that handles lazy tile evaluation.
Right second_argument_type
The reduction right-hand argument type.
void operator()(result_type &result, const argument_type &arg) const
BinaryReduceWrapper< typename Op::first_argument_type, typename Op::second_argument_type, Op > BinaryReduceWrapper_
This class type.
Unary reduction wrapper class that handles lazy tile evaluation.
UnaryReduceWrapper_ & operator=(const UnaryReduceWrapper_ &other)
void operator()(result_type &result, const first_argument_type &left, const second_argument_type &right) const
UnaryReduceWrapper(const UnaryReduceWrapper_ &other)
Left first_argument_type
The reduction left-hand argument type.
BinaryReduceWrapper< Left, Right, Op > BinaryReduceWrapper_
This class type.
BinaryReduceWrapper_ & operator=(const BinaryReduceWrapper_ &other)
Op::result_type result_type
The reduction result type.
Op::argument_type argument_type
The reduction argument type.
BinaryReduceWrapper(const BinaryReduceWrapper_ &other)
UnaryReduceWrapper_ & operator=(const UnaryReduceWrapper_ &other)
Binary reduction wrapper class that handles lazy tile evaluation.
UnaryReduceWrapper< typename Op::argument_type, Op > UnaryReduceWrapper_
This class type.
Determine the object type used in the evaluation of tensor expressions.
Definition: type_traits.h:580
Tile argument_type
The reduction argument type.
Detect lazy evaluation tiles.
Definition: type_traits.h:591
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:82
Op::result_type result_type
The reduction result type.