shift.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  * shift.h
22  * June 7, 2015
23  *
24  */
25 
26 #ifndef TILEDARRAY_TILE_OP_SHIFT_H__INCLUDED
27 #define TILEDARRAY_TILE_OP_SHIFT_H__INCLUDED
28 
29 #include "../tile_interface/permute.h"
30 #include "../tile_interface/shift.h"
31 
32 namespace TiledArray {
33 namespace detail {
34 
36 
45 template <typename Result, typename Arg, bool Consumable>
46 class Shift {
47  public:
49  typedef Arg argument_type;
50  typedef Result result_type;
51 
53  static constexpr bool is_consumable =
54  Consumable && std::is_same<result_type, argument_type>::value;
55 
56  private:
57  std::vector<long> range_shift_;
58 
59  // Permuting tile evaluation function
60  // These operations cannot consume the argument tile since this operation
61  // requires temporary storage space.
62  template <typename Perm, typename = std::enable_if_t<
63  TiledArray::detail::is_permutation_v<Perm>>>
64  result_type eval(const argument_type& arg, const Perm& perm) const {
67  result_type result = permute(arg, perm);
68  shift_to(result, range_shift_);
69  return result;
70  }
71 
72  // Non-permuting tile evaluation functions
73  // The compiler will select the correct functions based on the
74  // consumability of the arguments.
75 
76  template <bool C, typename = void>
77  auto eval(const argument_type& arg) const {
79  return shift(arg, range_shift_);
80  }
81 
82  template <bool C, typename = typename std::enable_if<C>::type>
83  auto eval(argument_type& arg) const {
85  shift_to(arg, range_shift_);
86  return arg;
87  }
88 
89  template <bool C, typename = typename std::enable_if<C>::type>
90  auto eval(argument_type&& arg) const {
92  shift_to(arg, range_shift_);
93  return arg;
94  }
95 
96  public:
97  // Compiler generated functions
98  Shift() = delete;
99  Shift(const Shift_&) = default;
100  Shift(Shift_&&) = default;
101  ~Shift() = default;
102  Shift& operator=(const Shift_&) = default;
103  Shift& operator=(Shift_&&) = default;
104 
106 
108  Shift(const std::vector<long>& range_shift) : range_shift_(range_shift) {}
109 
111 
115  template <typename Perm, typename = std::enable_if_t<
116  TiledArray::detail::is_permutation_v<Perm>>>
117  result_type operator()(const argument_type& arg, const Perm& perm) const {
118  return eval(arg, perm);
119  }
120 
122 
126  template <typename A>
127  result_type operator()(A&& arg) const {
128  return Shift_::template eval<is_consumable>(std::forward<A>(arg));
129  }
130 
132 
136  template <typename A>
137  result_type consume(A& arg) const {
138  constexpr bool can_consume =
140  std::is_same<result_type, argument_type>::value;
141  return Shift_::template eval<can_consume>(arg);
142  }
143 
144 }; // class Shift
145 
147 
154 template <typename Result, typename Arg, typename Scalar, bool Consumable>
155 class ScalShift {
156  public:
159  typedef Arg argument_type;
160  typedef Scalar scalar_type;
161  typedef Result result_type;
162 
163  static constexpr bool is_consumable =
164  Consumable && std::is_same<result_type, argument_type>::value;
165 
166  private:
167  std::vector<long> range_shift_;
168  scalar_type factor_;
169 
170  public:
171  // Permuting tile evaluation function
172  // These operations cannot consume the argument tile since this operation
173  // requires temporary storage space.
174  template <typename Perm, typename = std::enable_if_t<
175  TiledArray::detail::is_permutation_v<Perm>>>
176  result_type eval(const argument_type& arg, const Perm& perm) const {
177  using TiledArray::scale;
178  using TiledArray::shift_to;
179  result_type result = scale(arg, factor_, perm);
180  return shift_to(result, range_shift_);
181  }
182 
183  // Non-permuting tile evaluation functions
184  // The compiler will select the correct functions based on the
185  // consumability of the arguments.
186 
187  template <bool C>
188  typename std::enable_if<!C, result_type>::type eval(
189  const argument_type& arg) const {
190  using TiledArray::scale;
191  using TiledArray::shift_to;
192  result_type result = scale(arg, factor_);
193  return shift_to(result, range_shift_);
194  }
195 
196  template <bool C>
197  typename std::enable_if<C, result_type>::type eval(argument_type& arg) const {
198  using TiledArray::scale_to;
199  using TiledArray::shift_to;
200  scale_to(arg, factor_);
201  shift_to(arg, range_shift_);
202  return arg;
203  }
204 
205  public:
206  // Compiler generated functions
207  ScalShift() = delete;
208  ScalShift(const ScalShift_&) = default;
209  ScalShift(ScalShift_&&) = default;
210  ~ScalShift() = default;
211  ScalShift_& operator=(const ScalShift_&) = default;
213 
215 
217  ScalShift(const std::vector<long>& range_shift, const scalar_type factor)
218  : range_shift_(range_shift), factor_(factor) {}
219 
221 
225  template <typename Perm, typename = std::enable_if_t<
226  TiledArray::detail::is_permutation_v<Perm>>>
227  result_type operator()(const argument_type& arg, const Perm& perm) const {
228  return eval(arg, perm);
229  }
230 
232 
236  template <typename A>
237  result_type operator()(A&& arg) const {
238  return ScalShift_::template eval<is_consumable>(std::forward<A>(arg));
239  }
240 
242 
246  constexpr bool can_consume =
248  std::is_same<result_type, argument_type>::value;
249  return ScalShift_::template eval<can_consume>(arg);
250  }
251 
252 }; // class ScalShift
253 
254 } // namespace detail
255 } // namespace TiledArray
256 
257 #endif // TILEDARRAY_TILE_OP_SHIFT_H__INCLUDED
Arg argument_type
The argument type.
Definition: shift.h:159
Tile< Arg > & shift_to(Tile< Arg > &arg, const Index &range_shift)
Shift the range of arg in place.
Definition: tile.h:704
ScalShift< Result, Arg, Scalar, Consumable > ScalShift_
This object type.
Definition: shift.h:158
Shift the range of tile.
Definition: shift.h:183
Tile< Result > & scale_to(Tile< Result > &result, const Scalar factor)
Scale to the result tile.
Definition: tile.h:1204
Consumable tile type trait.
Definition: type_traits.h:611
std::enable_if< C, result_type >::type eval(argument_type &arg) const
Definition: shift.h:197
std::enable_if<!C, result_type >::type eval(const argument_type &arg) const
Definition: shift.h:188
void permute(InputOp &&input_op, OutputOp &&output_op, Result &result, const Perm &perm, const Arg0 &arg0, const Args &... args)
Construct a permuted tensor copy.
Definition: permute.h:117
detail::ShiftWrapper< T > shift(T &tensor)
Shift a tensor from one range to another.
Scalar scalar_type
The scaling factor type.
Definition: shift.h:160
Shift & operator=(const Shift_ &)=default
result_type operator()(A &&arg) const
Consuming shift operation.
Definition: shift.h:237
Result result_type
The result tile type.
Definition: shift.h:161
Shift(const std::vector< long > &range_shift)
Default constructor.
Definition: shift.h:108
Shift(Shift_ &&)=default
ScalShift(ScalShift_ &&)=default
ScalShift(const ScalShift_ &)=default
result_type operator()(const argument_type &arg, const Perm &perm) const
Shift and permute operator.
Definition: shift.h:117
ScalShift_ & operator=(ScalShift_ &&)=default
Shift(const Shift_ &)=default
Shift the range of tile in place.
Definition: shift.h:192
result_type operator()(A &&arg) const
Consuming shift operation.
Definition: shift.h:127
static constexpr bool is_consumable
Indicates whether it is possible to consume the left tile.
Definition: shift.h:53
decltype(auto) scale(const Tile< Arg > &arg, const Scalar factor)
Scalar the tile argument.
Definition: tile.h:1174
Permute a tile.
Definition: permute.h:134
result_type consume(A &arg) const
Explicit consuming shift operation.
Definition: shift.h:137
result_type eval(const argument_type &arg, const Perm &perm) const
Definition: shift.h:176
Shift< Result, Arg, Consumable > Shift_
This object type.
Definition: shift.h:48
ScalShift(const std::vector< long > &range_shift, const scalar_type factor)
Default constructor.
Definition: shift.h:217
Arg argument_type
The argument type.
Definition: shift.h:49
result_type consume(argument_type &arg) const
Explicit consuming shift operation.
Definition: shift.h:245
static constexpr bool is_consumable
Definition: shift.h:163
result_type operator()(const argument_type &arg, const Perm &perm) const
Shift and permute operator.
Definition: shift.h:227
Tile shift operation.
Definition: shift.h:155
Tile shift operation.
Definition: shift.h:46
Result result_type
The result tile type.
Definition: shift.h:50
ScalShift_ & operator=(const ScalShift_ &)=default
Shift & operator=(Shift_ &&)=default