type_traits.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2015 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  * type_traits.h
22  * May 31, 2015
23  *
24  */
25 
26 #ifndef TILEDARRAY_TENSOR_TYPE_TRAITS_H__INCLUDED
27 #define TILEDARRAY_TENSOR_TYPE_TRAITS_H__INCLUDED
28 
29 #include <TiledArray/config.h>
30 
31 #include <TiledArray/type_traits.h>
32 #include <type_traits>
33 
34 namespace Eigen {
35 
36 // Forward declarations
37 template <typename>
38 class aligned_allocator;
39 
40 } // namespace Eigen
41 
42 namespace TiledArray {
43 
44 // Forward declarations
45 class Range;
46 class BlockRange;
47 template <typename T, typename A = Eigen::aligned_allocator<T>>
48 class Tensor;
49 template <typename>
50 class Tile;
51 
52 class Permutation;
53 class BipartitePermutation;
54 
55 namespace symmetry {
56 class Permutation;
57 }
58 
59 namespace detail {
60 
61 // Forward declarations
62 template <typename T, typename R, typename = Tensor<std::remove_const_t<T>>>
63 class TensorInterface;
64 template <typename>
65 class ShiftWrapper;
66 
67 // Type traits for detecting tensors and tensors of tensors.
68 // is_tensor_helper tests if individual types are tensors, while is_tensor
69 // tests a pack of types. Similarly is_tensor_of_tensor tests if
70 // one or more types are tensors of tensors.
71 // To extend the definition of tensors and tensors of tensor, add additional
72 // is_tensor_helper and is_tensor_of_tensor_helper (partial) specializations.
73 // Note: These type traits help differentiate different implementation
74 // functions for tensors, so a tensor of tensors is not considered a tensor.
75 
76 template <typename... Ts>
77 struct is_tensor;
78 template <typename... Ts>
80 
81 template <typename>
82 struct is_tensor_helper : public std::false_type {};
83 
84 template <typename T, typename A>
85 struct is_tensor_helper<Tensor<T, A>> : public std::true_type {};
86 
87 template <typename... Args>
88 struct is_tensor_helper<TensorInterface<Args...>> : public std::true_type {};
89 
90 template <typename T>
92 
93 template <typename T>
94 struct is_tensor_helper<ShiftWrapper<const T>> : public is_tensor_helper<T> {};
95 
96 template <typename T>
97 struct is_tensor_helper<Tile<T>> : public is_tensor_helper<T> {};
98 
99 template <typename T>
100 struct is_tensor_of_tensor_helper : public std::false_type {};
101 
102 template <typename T, typename A>
104 
105 template <typename T, typename... Args>
107  : public is_tensor_helper<T> {};
108 
109 template <typename T>
111  : public is_tensor_of_tensor_helper<T> {};
112 
113 template <typename T>
115  : public is_tensor_of_tensor_helper<T> {};
116 
117 template <>
118 struct is_tensor<> : public std::false_type {};
119 
120 template <typename T>
121 struct is_tensor<T> {
122  static constexpr bool value =
124 };
125 
126 template <typename T1, typename T2, typename... Ts>
127 struct is_tensor<T1, T2, Ts...> {
128  static constexpr bool value =
129  is_tensor<T1>::value && is_tensor<T2, Ts...>::value;
130 };
131 
134 template <typename... Ts>
135 constexpr const bool is_tensor_v = is_tensor<Ts...>::value;
136 
137 template <>
138 struct is_tensor_of_tensor<> : public std::false_type {};
139 
140 template <typename T>
142  static constexpr bool value = is_tensor_of_tensor_helper<T>::value;
143 };
144 
145 template <typename T1, typename T2, typename... Ts>
146 struct is_tensor_of_tensor<T1, T2, Ts...> {
147  static constexpr bool value =
149 };
150 
154 template <typename... Ts>
155 constexpr const bool is_tensor_of_tensor_v = is_tensor_of_tensor<Ts...>::value;
156 
157 template <typename T, typename Enabler = void>
158 struct is_ta_tensor : public std::false_type {};
159 
160 template <typename T, typename A>
161 struct is_ta_tensor<Tensor<T, A>> : public std::true_type {};
162 
163 template <typename T>
164 constexpr const bool is_ta_tensor_v = is_ta_tensor<T>::value;
165 
166 // Test if the tensor is contiguous
167 
168 template <typename T>
169 struct is_contiguous_range_helper : public std::false_type {};
170 
171 template <>
172 struct is_contiguous_range_helper<Range> : public std::true_type {};
173 
174 template <typename T>
175 struct is_contiguous_tensor_helper : public std::false_type {};
176 
177 template <typename T, typename A>
178 struct is_contiguous_tensor_helper<Tensor<T, A>> : public std::true_type {};
179 
180 template <typename T, typename R, typename OpResult>
182  : public is_contiguous_range_helper<R> {};
183 
184 template <typename T>
186  : public is_contiguous_tensor_helper<T> {};
187 
188 template <typename T>
190  : public is_contiguous_tensor_helper<T> {};
191 
192 template <typename... Ts>
194 
195 template <>
196 struct is_contiguous_tensor<> : public std::false_type {};
197 
198 template <typename T>
200 
201 template <typename T1, typename T2, typename... Ts>
202 struct is_contiguous_tensor<T1, T2, Ts...> {
203  static constexpr bool value = is_contiguous_tensor_helper<T1>::value &&
204  is_contiguous_tensor<T2, Ts...>::value;
205 };
206 
210 template <typename... Ts>
211 constexpr const bool is_contiguous_tensor_v =
212  is_contiguous_tensor<Ts...>::value;
213 
214 // Test if the tensor is shifted
215 
216 template <typename T>
217 struct is_shifted_helper : public std::false_type {};
218 
219 template <typename T>
220 struct is_shifted_helper<ShiftWrapper<T>> : public std::true_type {};
221 
222 template <typename... Ts>
223 struct is_shifted;
224 
225 template <>
226 struct is_shifted<> : public std::false_type {};
227 
228 template <typename T>
229 struct is_shifted<T> : public is_shifted_helper<T> {};
230 
231 template <typename T1, typename T2, typename... Ts>
232 struct is_shifted<T1, T2, Ts...> {
233  static constexpr bool value =
234  is_shifted_helper<T1>::value && is_shifted<T2, Ts...>::value;
235 };
236 
239 template <typename... Ts>
240 constexpr const bool is_shifted_v = is_shifted<Ts...>::value;
241 
242 // check if reduce_op can reduce set of types
243 template <typename Enabler, typename ReduceOp, typename Result,
244  typename... Args>
245 struct is_reduce_op_ : public std::false_type {};
246 
247 template <typename ReduceOp, typename Result, typename... Args>
249  std::void_t<decltype(std::declval<ReduceOp&>()(
250  std::declval<Result&>(), std::declval<const Args*>()...))>,
251  ReduceOp, Result, Args...> : public std::true_type {};
252 
253 template <typename ReduceOp, typename Result, typename... Args>
254 constexpr const bool is_reduce_op_v =
255  is_reduce_op_<void, ReduceOp, Result, Args...>::value;
256 
258 #ifdef TILEDARRAY_HAS_CUDA
259 template <typename T>
260 struct is_cuda_tile : public std::false_type {};
261 
262 template <typename T>
263 struct is_cuda_tile<Tile<T>> : public is_cuda_tile<T> {};
264 
265 template <typename T, typename Op>
266 struct is_cuda_tile<LazyArrayTile<T, Op>>
267  : public is_cuda_tile<typename LazyArrayTile<T, Op>::eval_type> {};
268 
269 template <typename T>
270 static constexpr const auto is_cuda_tile_v = is_cuda_tile<T>::value;
271 
272 #endif
273 
274 template <typename Tensor, typename Enabler = void>
276 
277 template <typename Tensor>
279  std::enable_if_t<!is_tensor_of_tensor_v<Tensor>>> {
281 };
282 
283 template <typename Tensor>
285  std::enable_if_t<is_tensor_of_tensor_v<Tensor>>> {
287 };
288 
289 template <typename Tensor>
291 
292 template <typename T, typename Enabler = void>
293 struct is_permutation : public std::false_type {};
294 
295 template <>
296 struct is_permutation<TiledArray::Permutation> : public std::true_type {};
297 
298 template <>
300  : public std::true_type {};
301 
302 template <>
304  : public std::true_type {};
305 
306 template <typename T>
307 static constexpr const auto is_permutation_v = is_permutation<T>::value;
308 
309 template <typename T>
310 static constexpr const auto is_bipartite_permutation_v =
311  std::is_same_v<T, TiledArray::BipartitePermutation>;
312 
313 template <typename T>
314 static constexpr const auto is_bipartite_permutable_v =
315  is_free_function_permute_anyreturn_v<
316  const T&, const TiledArray::BipartitePermutation&>;
317 
318 } // namespace detail
319 
324 enum class OrdinalType { RowMajor = -1, ColMajor = 1, Other = 0, Invalid };
325 
326 namespace detail {
327 
329 template <typename Ordinal, typename Enabler = void>
331 
333 template <>
335  static constexpr const auto type = OrdinalType::RowMajor;
336 };
337 
339 template <typename T>
340 struct ordinal_traits<T, std::enable_if_t<is_contiguous_tensor_v<T>>> {
341  static constexpr const auto type = ordinal_traits<
342  std::decay_t<decltype(std::declval<const T&>().range())>>::type;
343 };
344 
345 } // namespace detail
346 
347 } // namespace TiledArray
348 
349 #endif // TILEDARRAY_TENSOR_TYPE_TRAITS_H__INCLUDED
::blas::Op Op
Definition: blas.h:46
Forward declarations.
Definition: foreach.h:34
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:130
constexpr const bool is_tensor_of_tensor_v
Definition: type_traits.h:155
typename default_permutation< Tensor >::type default_permutation_t
Definition: type_traits.h:290
constexpr const bool is_tensor_v
Definition: type_traits.h:135
ordinal trait specifies properties of the ordinal
Definition: type_traits.h:330
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:117
constexpr const bool is_reduce_op_v
Definition: type_traits.h:254
constexpr const bool is_shifted_v
Definition: type_traits.h:240
Tensor interface for external data.
constexpr const bool is_ta_tensor_v
Definition: type_traits.h:164
constexpr const bool is_contiguous_tensor_v
Definition: type_traits.h:211
An N-dimensional tensor object.
Definition: tensor.h:50
Permutation of a bipartite set.
Definition: permutation.h:610
An N-dimensional shallow copy wrapper for tile objects.
Definition: tile.h:82
A (hyperrectangular) interval on , space of integer -indices.
Definition: range.h:46