TiledArray  0.7.0
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 
29 #include <TiledArray/type_traits.h>
30 #include <TiledArray/madness.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
45  typedef typename Op::result_type result_type;
46  typedef Tile argument_type;
47 
48  // Constructors
49  UnaryReduceWrapper() : Op() { }
50  UnaryReduceWrapper(const Op& op) : Op(op) { }
51  UnaryReduceWrapper(const UnaryReduceWrapper_& other) : Op(other) { }
52 
54  Op::operator=(other);
55  return *this;
56  }
57 
58  // Import base class functionality
59  using Op::operator();
60 
61  private:
62 
63  template <typename T>
64  typename std::enable_if<is_lazy_tile<T>::value>::type
65  reduce(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
72  reduce(result_type& result, const T& arg) const {
73  Op::operator()(result, arg);
74  }
75 
76  public:
77 
78  // Reduce an argument
79  void operator()(result_type& result, const argument_type& arg) const {
80  reduce(result, arg);
81  }
82 
83  }; // struct UnaryReduceWrapper
84 
86 
90  template <typename Op>
91  class UnaryReduceWrapper<typename Op::argument_type, Op> : public Op {
92  public:
93  // typedefs
96  typedef typename Op::result_type result_type;
97  typedef typename Op::argument_type argument_type;
98 
99  // Constructors
100  UnaryReduceWrapper() : Op() { }
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
125  typedef typename Op::result_type result_type;
126  typedef Left first_argument_type;
127  typedef Right second_argument_type;
128 
129  // Constructors
130  BinaryReduceWrapper() : Op() { }
131  BinaryReduceWrapper(const Op& op) : Op(op) { }
132  BinaryReduceWrapper(const BinaryReduceWrapper_& other) : Op(other) { }
133 
135  Op::operator=(other);
136  return *this;
137  }
138 
139  private:
140 
141  template <typename L, typename R>
142  typename std::enable_if<is_lazy_tile<L>::value && is_lazy_tile<R>::value>::type
143  reduce(result_type& result, const L& left, const R& right) const {
144  typename eval_trait<L>::type eval_left(left);
145  typename eval_trait<R>::type eval_right(right);
146  Op::operator()(result, eval_left, eval_right);
147  }
148 
149  template <typename L, typename R>
150  typename std::enable_if<(!is_lazy_tile<L>::value) && is_lazy_tile<R>::value>::type
151  reduce(result_type& result, const L& left, const R& right) const {
152  typename eval_trait<R>::type eval_right(right);
153  Op::operator()(result, left, eval_right);
154  }
155 
156  template <typename L, typename R>
157  typename std::enable_if<is_lazy_tile<L>::value && (!is_lazy_tile<R>::value)>::type
158  reduce(result_type& result, const L& left, const R& right) const {
159  typename eval_trait<L>::type eval_left(left);
160  Op::operator()(result, eval_left, right);
161  }
162 
163  template <typename L, typename R>
164  typename std::enable_if<!(is_lazy_tile<L>::value || is_lazy_tile<R>::value)>::type
165  reduce(result_type& result, const L& left, const R& right) const {
166  Op::operator()(result, left, right);
167  }
168 
169  public:
170 
171  // Import base class functionality
172  using Op::operator();
173 
174  // Reduce an argument pair
175  void operator()(result_type& result, const first_argument_type& left,
176  const second_argument_type& right) const {
177  reduce(result, left, right);
178  }
179 
180  }; // struct BinaryReduceWrapper
181 
183  template <typename Op>
185  typename Op::second_argument_type, Op> : public Op
186  {
187  // typedefs
188  typedef BinaryReduceWrapper<typename Op::first_argument_type,
189  typename Op::second_argument_type, Op> BinaryReduceWrapper_;
190  typedef typename Op::result_type result_type;
191  typedef typename Op::first_argument_type first_argument_type;
192  typedef typename Op::second_argument_type second_argument_type;
193 
194  // Constructors
195  BinaryReduceWrapper() : Op() { }
196  BinaryReduceWrapper(const Op& op) : Op(op) { }
197  BinaryReduceWrapper(const BinaryReduceWrapper_& other) : Op(other) { }
198 
200  Op::operator=(other);
201  return *this;
202  }
203 
204  // Import base class functionality
205  using Op::operator();
206 
207  }; // struct BinaryReduceWrapper
208 
209  } // namespace math
210 } // namespace TiledArray
211 
212 #endif // TILEDARRAY_TILE_OP_REDUCE_INTERFACE_H__INCLUDED
Tile argument_type
The reduction argument type.
Right second_argument_type
The reduction right-hand argument type.
UnaryReduceWrapper(const UnaryReduceWrapper_ &other)
Op::argument_type argument_type
The reduction argument type.
UnaryReduceWrapper_ & operator=(const UnaryReduceWrapper_ &other)
UnaryReduceWrapper< typename Op::argument_type, Op > UnaryReduceWrapper_
This class type.
void operator()(result_type &result, const argument_type &arg) const
BinaryReduceWrapper< Left, Right, Op > BinaryReduceWrapper_
This class type.
Op::result_type result_type
The reduction result type.
Left first_argument_type
The reduction left-hand argument type.
Op::result_type result_type
The reduction result type.
BinaryReduceWrapper_ & operator=(const BinaryReduceWrapper_ &other)
void operator()(result_type &result, const first_argument_type &left, const second_argument_type &right) const
Binary reduction wrapper class that handles lazy tile evaluation.
BinaryReduceWrapper< typename Op::first_argument_type, typename Op::second_argument_type, Op > BinaryReduceWrapper_
This class type.
UnaryReduceWrapper< Tile, Op > UnaryReduceWrapper_
This class type.
UnaryReduceWrapper_ & operator=(const UnaryReduceWrapper_ &other)
Unary reduction wrapper class that handles lazy tile evaluation.
Unary reduction wrapper class that handles lazy tile evaluation.
BinaryReduceWrapper(const BinaryReduceWrapper_ &other)
Detect lazy evaluation tiles.
Definition: type_traits.h:388
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:80