TensorStorage.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) 2013 Christian Seiler <christian@iwakd.de>
5 // Copyright (C) 2014-2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_CXX11_TENSOR_TENSORSTORAGE_H
12 #define EIGEN_CXX11_TENSOR_TENSORSTORAGE_H
13 
14 #ifdef EIGEN_TENSOR_STORAGE_CTOR_PLUGIN
15 #define EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN EIGEN_TENSOR_STORAGE_CTOR_PLUGIN;
16 #else
17 #define EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN
18 #endif
19 
20 // IWYU pragma: private
21 #include "./InternalHeaderCheck.h"
22 
23 namespace Eigen {
24 
37 template <typename T, typename Dimensions, int Options>
38 class TensorStorage;
39 
40 // Pure fixed-size storage
41 template <typename T, typename FixedDimensions, int Options_>
43  private:
44  static constexpr std::size_t Size = FixedDimensions::total_size;
45 
46  // Allocate an array of size at least one to prevent compiler warnings.
47  static constexpr std::size_t MinSize = max_n_1<Size>::size;
49 
50  public:
52 
54  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T* data() const { return m_data; }
55 
56  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const FixedDimensions dimensions() const { return FixedDimensions(); }
57 
59 };
60 
61 // pure dynamic
62 template <typename T, typename IndexType, int NumIndices_, int Options_>
63 class TensorStorage<T, DSizes<IndexType, NumIndices_>, Options_> {
64  public:
65  typedef IndexType Index;
68 
69  EIGEN_DEVICE_FUNC TensorStorage() : m_data(0), m_dimensions() {
70  if (NumIndices_ == 0) {
71  m_data = internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(1);
72  }
73  }
75  : m_data(internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(size)), m_dimensions(dimensions) {
77  }
78 
79  template <typename... DenseIndex>
80  EIGEN_DEVICE_FUNC TensorStorage(DenseIndex... indices) : m_dimensions(indices...) {
81  m_data = internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(internal::array_prod(m_dimensions));
82  }
83 
86  internal::array_prod(other.m_dimensions))),
87  m_dimensions(other.m_dimensions) {
89  }
91  if (this != &other) {
92  Self tmp(other);
93  this->swap(tmp);
94  }
95  return *this;
96  }
97 
98  EIGEN_DEVICE_FUNC TensorStorage(Self&& other) : TensorStorage() { *this = std::move(other); }
99 
101  numext::swap(m_data, other.m_data);
102  numext::swap(m_dimensions, other.m_dimensions);
103  return *this;
104  }
105 
107  internal::conditional_aligned_delete_auto<T, (Options_ & DontAlign) == 0>(m_data,
108  internal::array_prod(m_dimensions));
109  }
110  EIGEN_DEVICE_FUNC void swap(Self& other) {
111  numext::swap(m_data, other.m_data);
112  numext::swap(m_dimensions, other.m_dimensions);
113  }
114 
115  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
116 
118  const Index currentSz = internal::array_prod(m_dimensions);
119  if (size != currentSz) {
120  internal::conditional_aligned_delete_auto<T, (Options_ & DontAlign) == 0>(m_data, currentSz);
121  if (size)
122  m_data = internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(size);
123  else if (NumIndices_ == 0) {
124  m_data = internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(1);
125  } else
126  m_data = 0;
128  }
129  m_dimensions = nbDimensions;
130  }
131 
133  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T* data() const { return m_data; }
134 
135  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
136 
137  private:
140 };
141 
142 } // end namespace Eigen
143 
144 #endif // EIGEN_CXX11_TENSOR_TENSORSTORAGE_H
#define EIGEN_ALIGN_MAX
Definition: ConfigureVectorization.h:146
#define EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(X)
Definition: DenseStorage.h:20
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
#define EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN
Definition: TensorStorage.h:17
EIGEN_DEVICE_FUNC Self & operator=(const Self &other)
Definition: TensorStorage.h:90
EIGEN_DEVICE_FUNC TensorStorage(Self &&other)
Definition: TensorStorage.h:98
EIGEN_DEVICE_FUNC ~TensorStorage()
Definition: TensorStorage.h:106
EIGEN_DEVICE_FUNC TensorStorage()
Definition: TensorStorage.h:69
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions & dimensions() const
Definition: TensorStorage.h:115
EIGEN_DEVICE_FUNC void swap(Self &other)
Definition: TensorStorage.h:110
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T * data()
Definition: TensorStorage.h:132
DSizes< IndexType, NumIndices_ > Dimensions
Definition: TensorStorage.h:66
EIGEN_DEVICE_FUNC Self & operator=(Self &&other)
Definition: TensorStorage.h:100
EIGEN_DEVICE_FUNC TensorStorage(DenseIndex... indices)
Definition: TensorStorage.h:80
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const
Definition: TensorStorage.h:135
TensorStorage< T, DSizes< IndexType, NumIndices_ >, Options_ > Self
Definition: TensorStorage.h:67
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T * data() const
Definition: TensorStorage.h:133
EIGEN_DEVICE_FUNC void resize(Index size, const array< Index, NumIndices_ > &nbDimensions)
Definition: TensorStorage.h:117
EIGEN_DEVICE_FUNC TensorStorage(const Self &other)
Definition: TensorStorage.h:84
EIGEN_DEVICE_FUNC TensorStorage(Index size, const array< Index, NumIndices_ > &dimensions)
Definition: TensorStorage.h:74
Stores the data of a tensor.
Definition: TensorStorage.h:42
EIGEN_ALIGN_MAX T m_data[MinSize]
Definition: TensorStorage.h:48
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex size() const
Definition: TensorStorage.h:58
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const FixedDimensions dimensions() const
Definition: TensorStorage.h:56
static constexpr std::size_t Size
Definition: TensorStorage.h:44
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T * data()
Definition: TensorStorage.h:53
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T * data() const
Definition: TensorStorage.h:54
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorStorage()
Definition: TensorStorage.h:51
static constexpr std::size_t MinSize
Definition: TensorStorage.h:47
@ DontAlign
Definition: Constants.h:324
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
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
EIGEN_DEVICE_FUNC void smart_copy(const T *start, const T *end, T *target)
Definition: Memory.h:569
EIGEN_DEVICE_FUNC T * conditional_aligned_new_auto(std::size_t size)
Definition: Memory.h:476
EIGEN_STRONG_INLINE void swap(T &a, T &b)
Definition: Meta.h:536
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
std::array< T, N > array
Definition: EmulateArray.h:231
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex
Definition: Meta.h:75
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::enable_if_t< std::is_base_of< DenseBase< std::decay_t< DerivedA > >, std::decay_t< DerivedA > >::value &&std::is_base_of< DenseBase< std::decay_t< DerivedB > >, std::decay_t< DerivedB > >::value, void > swap(DerivedA &&a, DerivedB &&b)
Definition: DenseBase.h:655
Definition: Eigen_Colamd.h:49
Definition: TensorDimensions.h:161
Definition: TensorMeta.h:32