TensorDimensions.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CXX11_TENSOR_TENSOR_DIMENSIONS_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_DIMENSIONS_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
34 // Boilerplate code
35 namespace internal {
36 
37 template <std::ptrdiff_t n, typename Dimension>
38 struct dget {
39  static const std::ptrdiff_t value = get<n, Dimension>::value;
40 };
41 
42 template <typename Index, std::ptrdiff_t NumIndices, std::ptrdiff_t n, bool RowMajor>
44  template <typename Dimensions>
46  const Dimensions& dimensions) {
47  return array_get < RowMajor ? n - 1
48  : (NumIndices - n) > (indices) + dget < RowMajor ? n - 1
49  : (NumIndices - n),
51  indices, dimensions);
52  }
53 };
54 
55 template <typename Index, std::ptrdiff_t NumIndices, bool RowMajor>
57  template <typename Dimensions>
59  return 0;
60  }
61 };
62 
63 template <typename Index, std::ptrdiff_t n>
65  template <typename Dimensions>
66  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Index run(const Index index, const Dimensions& dimensions) {
67  const Index mult = (index == n - 1) ? 1 : 0;
68  return array_get<n - 1>(dimensions) * mult +
70  }
71 };
72 
73 template <typename Index>
75  template <typename Dimensions>
76  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Index run(const Index, const Dimensions&) {
77  return 0;
78  }
79 };
80 
81 } // end namespace internal
82 
83 // Fixed size
84 template <typename std::ptrdiff_t... Indices>
85 struct Sizes {
86  typedef internal::numeric_list<std::ptrdiff_t, Indices...> Base;
87  const Base t = Base();
88  static const std::ptrdiff_t total_size = internal::arg_prod(Indices...);
89  static const ptrdiff_t count = Base::count;
90 
91  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t rank() const { return Base::count; }
92 
93  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t TotalSize() { return internal::arg_prod(Indices...); }
94 
96  template <typename DenseIndex>
98  // todo: add assertion
99  }
100  template <typename... DenseIndex>
102  explicit EIGEN_DEVICE_FUNC Sizes(std::initializer_list<std::ptrdiff_t> /*l*/) {
103  // todo: add assertion
104  }
105 
106  template <typename T>
107  Sizes& operator=(const T& /*other*/) {
108  // add assertion failure if the size of other is different
109  return *this;
110  }
111 
112  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t operator[](const std::ptrdiff_t index) const {
114  }
115 
116  template <typename DenseIndex>
119  indices, t);
120  }
121  template <typename DenseIndex>
124  indices, t);
125  }
126 };
127 
128 namespace internal {
129 template <typename std::ptrdiff_t... Indices>
131  return Sizes<Indices...>::total_size;
132 }
133 } // namespace internal
134 
135 // Boilerplate
136 namespace internal {
137 template <typename Index, std::ptrdiff_t NumIndices, std::ptrdiff_t n, bool RowMajor>
140  array<Index, NumIndices> const& dimensions) {
141  return array_get < RowMajor ? n
142  : (NumIndices - n - 1) > (indices) + array_get < RowMajor
143  ? n
144  : (NumIndices - n - 1) >
146  indices, dimensions);
147  }
148 };
149 
150 template <typename Index, std::ptrdiff_t NumIndices, bool RowMajor>
153  array<Index, NumIndices> const&) {
154  return array_get < RowMajor ? 0 : NumIndices - 1 > (indices);
155  }
156 };
157 } // end namespace internal
158 
159 // Dynamic size
160 template <typename DenseIndex, int NumDims>
161 struct DSizes : array<DenseIndex, NumDims> {
163  static const int count = NumDims;
164 
165  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const { return NumDims; }
166 
168  return (NumDims == 0) ? 1 : internal::array_prod(*static_cast<const Base*>(this));
169  }
170 
172  for (int i = 0; i < NumDims; ++i) {
173  (*this)[i] = 0;
174  }
175  }
177 
178  EIGEN_DEVICE_FUNC explicit DSizes(const DenseIndex i0) {
179  eigen_assert(NumDims == 1);
180  (*this)[0] = i0;
181  }
182 
184  for (int i = 0; i < NumDims; ++i) {
185  (*this)[i] = a[i];
186  }
187  }
188 
189  // Enable DSizes index type promotion only if we are promoting to the
190  // larger type, e.g. allow to promote dimensions of type int to long.
191  template <typename OtherIndex>
193  const array<OtherIndex, NumDims>& other,
194  // Default template parameters require c++11.
195  std::enable_if_t<
197  void*> = 0) {
198  for (int i = 0; i < NumDims; ++i) {
199  (*this)[i] = static_cast<DenseIndex>(other[i]);
200  }
201  }
202 
203  template <typename FirstType, typename... OtherTypes>
205  for (int i = 0; i < dimensions.count; ++i) {
206  (*this)[i] = dimensions[i];
207  }
208  }
209 
210  template <typename std::ptrdiff_t... Indices>
212  for (int i = 0; i < NumDims; ++i) {
213  (*this)[i] = a[i];
214  }
215  }
216 
217  template <typename... IndexTypes>
218  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit DSizes(DenseIndex firstDimension, DenseIndex secondDimension,
219  IndexTypes... otherDimensions)
220  : Base({{firstDimension, secondDimension, otherDimensions...}}) {
221  EIGEN_STATIC_ASSERT(sizeof...(otherDimensions) + 2 == NumDims, YOU_MADE_A_PROGRAMMING_MISTAKE)
222  }
223 
225  *static_cast<Base*>(this) = other;
226  return *this;
227  }
228 
229  // A constexpr would be so much better here
232  indices, *static_cast<const Base*>(this));
233  }
236  indices, *static_cast<const Base*>(this));
237  }
238 };
239 
240 template <typename IndexType, int NumDims>
241 std::ostream& operator<<(std::ostream& os, const DSizes<IndexType, NumDims>& dims) {
242  os << "[";
243  for (int i = 0; i < NumDims; ++i) {
244  if (i > 0) os << ", ";
245  os << dims[i];
246  }
247  os << "]";
248  return os;
249 }
250 
251 // Boilerplate
252 namespace internal {
253 template <typename Index, std::ptrdiff_t NumIndices, std::ptrdiff_t n, bool RowMajor>
256  std::vector<DenseIndex> const& dimensions) {
257  return array_get < RowMajor ? n
258  : (NumIndices - n - 1) > (indices) + array_get < RowMajor
259  ? n
260  : (NumIndices - n - 1) >
262  indices, dimensions);
263  }
264 };
265 
266 template <typename Index, std::ptrdiff_t NumIndices, bool RowMajor>
269  std::vector<DenseIndex> const&) {
270  return array_get < RowMajor ? 0 : NumIndices - 1 > (indices);
271  }
272 };
273 } // end namespace internal
274 
275 namespace internal {
276 
277 template <typename DenseIndex, int NumDims>
278 struct array_size<const DSizes<DenseIndex, NumDims> > {
279  static const ptrdiff_t value = NumDims;
280 };
281 template <typename DenseIndex, int NumDims>
282 struct array_size<DSizes<DenseIndex, NumDims> > {
283  static const ptrdiff_t value = NumDims;
284 };
285 template <typename std::ptrdiff_t... Indices>
286 struct array_size<const Sizes<Indices...> > {
287  static const std::ptrdiff_t value = Sizes<Indices...>::count;
288 };
289 template <typename std::ptrdiff_t... Indices>
290 struct array_size<Sizes<Indices...> > {
291  static const std::ptrdiff_t value = Sizes<Indices...>::count;
292 };
293 template <std::ptrdiff_t n, typename std::ptrdiff_t... Indices>
295  return get<n, internal::numeric_list<std::ptrdiff_t, Indices...> >::value;
296 }
297 template <std::ptrdiff_t n>
299  eigen_assert(false && "should never be called");
300  return -1;
301 }
302 
303 template <typename Dims1, typename Dims2, ptrdiff_t n, ptrdiff_t m>
305  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool run(Dims1&, Dims2&) { return false; }
306 };
307 template <typename Dims1, typename Dims2, ptrdiff_t n>
308 struct sizes_match_below_dim<Dims1, Dims2, n, n> {
309  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool run(Dims1& dims1, Dims2& dims2) {
310  return numext::equal_strict(array_get<n - 1>(dims1), array_get<n - 1>(dims2)) &&
312  }
313 };
314 template <typename Dims1, typename Dims2>
315 struct sizes_match_below_dim<Dims1, Dims2, 0, 0> {
316  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool run(Dims1&, Dims2&) { return true; }
317 };
318 
319 } // end namespace internal
320 
321 template <typename Dims1, typename Dims2>
325 }
326 
327 } // end namespace Eigen
328 
329 #endif // EIGEN_CXX11_TENSOR_TENSOR_DIMENSIONS_H
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
#define EIGEN_ALWAYS_INLINE
Definition: Macros.h:845
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
@ RowMajor
Definition: Constants.h:320
const Scalar * a
Definition: level2_cplx_impl.h:32
constexpr EIGEN_DEVICE_FUNCdecltype(reduce< product_op, Ts... >::run((*((Ts *) 0))...)) EIGEN_STRONG_INLIN arg_prod)(Ts... ts)
Definition: MoreMeta.h:435
constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto array_prod(const array< T, N > &arr) -> decltype(array_reduce< product_op, T, N >(arr, static_cast< T >(1)))
Definition: MoreMeta.h:497
constexpr T & array_get(std::array< T, N > &a)
Definition: EmulateArray.h:251
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool equal_strict(const X &x, const Y &y)
Definition: Meta.h:571
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool dimensions_match(Dims1 dims1, Dims2 dims2)
Definition: TensorDimensions.h:322
auto run(Kernel kernel, Args &&... args) -> decltype(kernel(args...))
Definition: gpu_test_helper.h:414
std::array< T, N > array
Definition: EmulateArray.h:231
squared absolute value
Definition: GlobalFunctions.h:87
std::ostream & operator<<(std::ostream &s, const DiagonalBase< Derived > &m)
Definition: IO.h:227
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: Meta.h:75
Definition: Eigen_Colamd.h:49
Definition: TensorDimensions.h:161
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DSizes()
Definition: TensorDimensions.h:171
EIGEN_DEVICE_FUNC DSizes(const array< DenseIndex, NumDims > &a)
Definition: TensorDimensions.h:176
EIGEN_DEVICE_FUNC DSizes(const DimensionList< DenseIndex, NumDims > &a)
Definition: TensorDimensions.h:183
EIGEN_DEVICE_FUNC DSizes(const DenseIndex i0)
Definition: TensorDimensions.h:178
EIGEN_DEVICE_FUNC DSizes(const array< OtherIndex, NumDims > &other, std::enable_if_t< internal::is_same< DenseIndex, typename internal::promote_index_type< DenseIndex, OtherIndex >::type >::value, void * >=0)
Definition: TensorDimensions.h:192
static const int count
Definition: TensorDimensions.h:163
EIGEN_DEVICE_FUNC DSizes(const Eigen::IndexList< FirstType, OtherTypes... > &dimensions)
Definition: TensorDimensions.h:204
EIGEN_DEVICE_FUNC DSizes & operator=(const array< DenseIndex, NumDims > &other)
Definition: TensorDimensions.h:224
EIGEN_DEVICE_FUNC DSizes(const Sizes< Indices... > &a)
Definition: TensorDimensions.h:211
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex TotalSize() const
Definition: TensorDimensions.h:167
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DSizes(DenseIndex firstDimension, DenseIndex secondDimension, IndexTypes... otherDimensions)
Definition: TensorDimensions.h:218
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const
Definition: TensorDimensions.h:165
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex IndexOfColMajor(const array< DenseIndex, NumDims > &indices) const
Definition: TensorDimensions.h:230
array< DenseIndex, NumDims > Base
Definition: TensorDimensions.h:162
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex IndexOfRowMajor(const array< DenseIndex, NumDims > &indices) const
Definition: TensorDimensions.h:234
Definition: TensorDimensionList.h:29
Definition: TensorIndexList.h:271
Definition: TensorDimensions.h:85
static const std::ptrdiff_t total_size
Definition: TensorDimensions.h:88
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t operator[](const std::ptrdiff_t index) const
Definition: TensorDimensions.h:112
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t TotalSize()
Definition: TensorDimensions.h:93
const Base t
Definition: TensorDimensions.h:87
EIGEN_DEVICE_FUNC Sizes()
Definition: TensorDimensions.h:95
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t IndexOfColMajor(const array< DenseIndex, Base::count > &indices) const
Definition: TensorDimensions.h:117
static const ptrdiff_t count
Definition: TensorDimensions.h:89
internal::numeric_list< std::ptrdiff_t, Indices... > Base
Definition: TensorDimensions.h:86
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t rank() const
Definition: TensorDimensions.h:91
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t IndexOfRowMajor(const array< DenseIndex, Base::count > &indices) const
Definition: TensorDimensions.h:122
Sizes & operator=(const T &)
Definition: TensorDimensions.h:107
EIGEN_DEVICE_FUNC Sizes(DenseIndex...)
Definition: TensorDimensions.h:101
EIGEN_DEVICE_FUNC Sizes(std::initializer_list< std::ptrdiff_t >)
Definition: TensorDimensions.h:102
EIGEN_DEVICE_FUNC Sizes(const array< DenseIndex, Base::count > &)
Definition: TensorDimensions.h:97
Definition: Meta.h:305
static constexpr Index value
Definition: Meta.h:306
Definition: TensorDimensions.h:38
static const std::ptrdiff_t value
Definition: TensorDimensions.h:39
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index run(const Index, const Dimensions &)
Definition: TensorDimensions.h:76
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index run(const Index index, const Dimensions &dimensions)
Definition: TensorDimensions.h:66
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index run(array< Index, NumIndices > const &, const Dimensions &)
Definition: TensorDimensions.h:58
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index run(array< Index, NumIndices > const &indices, const Dimensions &dimensions)
Definition: TensorDimensions.h:45
Definition: MoreMeta.h:202
Definition: Meta.h:205
Definition: MoreMeta.h:33
constexpr static std::size_t count
Definition: MoreMeta.h:34
std::conditional_t<(sizeof(I1)< sizeof(I2)), I2, I1 > type
Definition: XprHelper.h:146
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool run(Dims1 &, Dims2 &)
Definition: TensorDimensions.h:316
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool run(Dims1 &dims1, Dims2 &dims2)
Definition: TensorDimensions.h:309
Definition: TensorDimensions.h:304
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool run(Dims1 &, Dims2 &)
Definition: TensorDimensions.h:305
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index run(array< Index, NumIndices > const &indices, array< Index, NumIndices > const &)
Definition: TensorDimensions.h:152
Definition: TensorDimensions.h:138
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index run(array< Index, NumIndices > const &indices, array< Index, NumIndices > const &dimensions)
Definition: TensorDimensions.h:139
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index run(array< Index, NumIndices > const &indices, std::vector< DenseIndex > const &)
Definition: TensorDimensions.h:268
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index run(array< Index, NumIndices > const &indices, std::vector< DenseIndex > const &dimensions)
Definition: TensorDimensions.h:255