TiledArray  0.7.0
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/shift.h"
30 #include "../tile_interface/permute.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 
58  std::vector<long> range_shift_;
59 
60  // Permuting tile evaluation function
61  // These operations cannot consume the argument tile since this operation
62  // requires temporary storage space.
63 
65  eval(const argument_type& arg, const Permutation& perm) const {
68  result_type result = permute(arg, perm);
69  shift_to(result, range_shift_);
70  return result;
71  }
72 
73  // Non-permuting tile evaluation functions
74  // The compiler will select the correct functions based on the
75  // consumability of the arguments.
76 
77  template <bool C, typename = void>
78  auto eval(const argument_type& arg) const {
80  return shift(arg, range_shift_);
81  }
82 
83  template <bool C, typename = typename std::enable_if<C>::type>
84  auto eval(argument_type& arg) const {
86  shift_to(arg, range_shift_);
87  return arg;
88  }
89 
90  template <bool C, typename = typename std::enable_if<C>::type>
91  auto eval(argument_type&& arg) const {
93  shift_to(arg, range_shift_);
94  return arg;
95  }
96 
97  public:
98 
99  // Compiler generated functions
100  Shift() = delete;
101  Shift(const Shift_&) = default;
102  Shift(Shift_&&) = default;
103  ~Shift() = default;
104  Shift& operator=(const Shift_&) = default;
105  Shift& operator=(Shift_&&) = default;
106 
107 
109 
111  Shift(const std::vector<long>& range_shift) :
112  range_shift_(range_shift)
113  { }
114 
116 
121  operator()(const argument_type& arg, const Permutation& perm) const {
122  return eval(arg, perm);
123  }
124 
126 
130  template <typename A>
131  result_type operator()(A&& arg) const {
132  return Shift_::template eval<is_consumable>(std::forward<A>(arg));
133  }
134 
136 
140  template <typename A>
141  result_type consume(A& arg) const {
142  constexpr bool can_consume = is_consumable_tile<argument_type>::value &&
143  std::is_same<result_type, argument_type>::value;
144  return Shift_::template eval<can_consume>(arg);
145  }
146 
147  }; // class Shift
148 
149 
151 
158  template <typename Result, typename Arg, typename Scalar, bool Consumable>
159  class ScalShift {
160  public:
163  typedef Arg argument_type;
164  typedef Scalar scalar_type;
165  typedef Result result_type;
166 
167  static constexpr bool is_consumable =
168  Consumable && std::is_same<result_type, argument_type>::value;
169 
170  private:
171 
172  std::vector<long> range_shift_;
173  scalar_type factor_;
174 
175  public:
176 
177  // Permuting tile evaluation function
178  // These operations cannot consume the argument tile since this operation
179  // requires temporary storage space.
180 
182  eval(const argument_type& arg, const Permutation& perm) const {
183  using TiledArray::scale;
184  using TiledArray::shift_to;
185  result_type result = scale(arg, factor_, perm);
186  return shift_to(result, range_shift_);
187  }
188 
189  // Non-permuting tile evaluation functions
190  // The compiler will select the correct functions based on the
191  // consumability of the arguments.
192 
193  template <bool C>
194  typename std::enable_if<!C, result_type>::type
195  eval(const argument_type& arg) const {
196  using TiledArray::scale;
197  using TiledArray::shift_to;
198  result_type result = scale(arg, factor_);
199  return shift_to(result, range_shift_);
200  }
201 
202  template <bool C>
203  typename std::enable_if<C, result_type>::type
204  eval(argument_type& arg) const {
205  using TiledArray::scale_to;
206  using TiledArray::shift_to;
207  scale_to(arg, factor_);
208  shift_to(arg, range_shift_);
209  return arg;
210  }
211 
212  public:
213 
214  // Compiler generated functions
215  ScalShift() = delete;
216  ScalShift(const ScalShift_&) = default;
217  ScalShift(ScalShift_&&) = default;
218  ~ScalShift() = default;
219  ScalShift_& operator=(const ScalShift_&) = default;
220  ScalShift_& operator=(ScalShift_&&) = default;
221 
223 
225  ScalShift(const std::vector<long>& range_shift,
226  const scalar_type factor) :
227  range_shift_(range_shift), factor_(factor)
228  { }
229 
230 
232 
237  operator()(const argument_type& arg, const Permutation& perm) const {
238  return eval(arg, perm);
239  }
240 
242 
246  template <typename A>
247  result_type operator()(A&& arg) const {
248  return ScalShift_::template eval<is_consumable>(std::forward<A>(arg));
249  }
250 
252 
256  constexpr bool can_consume = is_consumable_tile<argument_type>::value &&
257  std::is_same<result_type, argument_type>::value;
258  return ScalShift_::template eval<can_consume>(arg);
259  }
260 
261  }; // class ScalShift
262 
263  } // namesapce detail
264 } // namespace TiledArray
265 
266 #endif // TILEDARRAY_TILE_OP_SHIFT_H__INCLUDED
Shift the range of tile in place.
Definition: shift.h:178
Arg argument_type
The argument type.
Definition: shift.h:163
Result result_type
The result tile type.
Definition: shift.h:165
detail::ShiftWrapper< T > shift(T &tensor)
Shift a tensor from one range to another.
result_type consume(A &arg) const
Explicit consuming shift operation.
Definition: shift.h:141
Shift the range of tile.
Definition: shift.h:168
void permute(InputOp &&input_op, OutputOp &&output_op, Result &result, const Permutation &perm, const Arg0 &arg0, const Args &... args)
Construct a permuted tensor copy.
Definition: permute.h:122
Tile< Result > & scale_to(Tile< Result > &result, const Scalar factor)
Scale to the result tile.
Definition: tile.h:725
void scale(DistArray< Tile, Policy > &a, typename DistArray< Tile, Policy >::element_type scaling_factor)
Definition: utils.h:108
result_type operator()(A &&arg) const
Consuming shift operation.
Definition: shift.h:131
ScalShift< Result, Arg, Scalar, Consumable > ScalShift_
This object type.
Definition: shift.h:162
Shift & operator=(const Shift_ &)=default
std::enable_if< C, result_type >::type eval(argument_type &arg) const
Definition: shift.h:204
result_type eval(const argument_type &arg, const Permutation &perm) const
Definition: shift.h:182
result_type operator()(A &&arg) const
Consuming shift operation.
Definition: shift.h:247
static constexpr bool is_consumable
Indicates whether it is possible to consume the left tile.
Definition: shift.h:53
Permute a tile.
Definition: permute.h:130
static constexpr bool is_consumable
Definition: shift.h:167
Tile shift operation.
Definition: shift.h:46
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:225
Result result_type
The result tile type.
Definition: shift.h:50
std::enable_if<!C, result_type >::type eval(const argument_type &arg) const
Definition: shift.h:195
Scalar scalar_type
The scaling factor type.
Definition: shift.h:164
Consumable tile type trait.
Definition: type_traits.h:406
Arg argument_type
The argument type.
Definition: shift.h:49
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:119
Tile< Arg > & shift_to(Tile< Arg > &arg, const Index &range_shift)
Shift the range of arg in place.
Definition: tile.h:346
Shift(const std::vector< long > &range_shift)
Default constructor.
Definition: shift.h:111
ScalShift_ & operator=(const ScalShift_ &)=default
Tile shift operation.
Definition: shift.h:159
result_type operator()(const argument_type &arg, const Permutation &perm) const
Shift and permute operator.
Definition: shift.h:121
result_type operator()(const argument_type &arg, const Permutation &perm) const
Shift and permute operator.
Definition: shift.h:237
result_type consume(argument_type &arg) const
Explicit consuming shift operation.
Definition: shift.h:255