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 #include "./InternalHeaderCheck.h"
21 
22 namespace Eigen {
23 
36 template<typename T, typename Dimensions, int Options> class TensorStorage;
37 
38 
39 // Pure fixed-size storage
40 template<typename T, typename FixedDimensions, int Options_>
42 {
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  EIGEN_STRONG_INLINE TensorStorage() {
53  }
54 
56  EIGEN_STRONG_INLINE T *data() { return m_data; }
58  EIGEN_STRONG_INLINE const T *data() const { return m_data; }
59 
61  EIGEN_STRONG_INLINE const FixedDimensions dimensions() const { return FixedDimensions(); }
62 
64  EIGEN_STRONG_INLINE DenseIndex size() const { return Size; }
65 };
66 
67 
68 // pure dynamic
69 template<typename T, typename IndexType, int NumIndices_, int Options_>
70 class TensorStorage<T, DSizes<IndexType, NumIndices_>, Options_>
71 {
72  public:
73  typedef IndexType Index;
76 
77  EIGEN_DEVICE_FUNC TensorStorage() : m_data(0), m_dimensions() {
78  if (NumIndices_ == 0) {
79  m_data = internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(1);
80  }
81  }
82  EIGEN_DEVICE_FUNC TensorStorage(internal::constructor_without_unaligned_array_assert)
83  : m_data(0), m_dimensions(internal::template repeat<NumIndices_, Index>(0)) {}
85  : m_data(internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(size)), m_dimensions(dimensions)
87 
88  template <typename... DenseIndex>
89  EIGEN_DEVICE_FUNC TensorStorage(DenseIndex... indices) : m_dimensions(indices...) {
90  m_data = internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(internal::array_prod(m_dimensions));
91  }
92 
94  : m_data(internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(internal::array_prod(other.m_dimensions)))
95  , m_dimensions(other.m_dimensions)
96  {
98  }
100  {
101  if (this != &other) {
102  Self tmp(other);
103  this->swap(tmp);
104  }
105  return *this;
106  }
107 
109  {
110  *this = std::move(other);
111  }
112 
114  {
115  numext::swap(m_data, other.m_data);
116  numext::swap(m_dimensions, other.m_dimensions);
117  return *this;
118  }
119 
120  EIGEN_DEVICE_FUNC ~TensorStorage() { internal::conditional_aligned_delete_auto<T,(Options_&DontAlign)==0>(m_data, internal::array_prod(m_dimensions)); }
122  { numext::swap(m_data,other.m_data); numext::swap(m_dimensions,other.m_dimensions); }
123 
124  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const {return m_dimensions;}
125 
127  {
128  const Index currentSz = internal::array_prod(m_dimensions);
129  if(size != currentSz)
130  {
131  internal::conditional_aligned_delete_auto<T,(Options_&DontAlign)==0>(m_data, currentSz);
132  if (size)
133  m_data = internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(size);
134  else if (NumIndices_ == 0) {
135  m_data = internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(1);
136  }
137  else
138  m_data = 0;
140  }
141  m_dimensions = nbDimensions;
142  }
143 
144  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T *data() { return m_data; }
145  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T *data() const { return m_data; }
146 
147  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
148 
149  private:
152 };
153 
154 } // end namespace Eigen
155 
156 #endif // EIGEN_CXX11_TENSOR_TENSORSTORAGE_H
#define EIGEN_ALIGN_MAX
#define EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(X)
#define EIGEN_DEVICE_FUNC
#define EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN
Definition: TensorStorage.h:17
TensorStorage(internal::constructor_without_unaligned_array_assert)
Definition: TensorStorage.h:82
TensorStorage(Index size, const array< Index, NumIndices_ > &dimensions)
Definition: TensorStorage.h:84
void resize(Index size, const array< Index, NumIndices_ > &nbDimensions)
TensorStorage< T, DSizes< IndexType, NumIndices_ >, Options_ > Self
Definition: TensorStorage.h:75
EIGEN_ALIGN_MAX T m_data[MinSize]
Definition: TensorStorage.h:48
const T * data() const
Definition: TensorStorage.h:58
const FixedDimensions dimensions() const
Definition: TensorStorage.h:61
static constexpr std::size_t Size
Definition: TensorStorage.h:44
static constexpr std::size_t MinSize
Definition: TensorStorage.h:47
DenseIndex size() const
Definition: TensorStorage.h:64
T * conditional_aligned_new_auto(std::size_t size)
constexpr array< t, n > repeat(t v)
void smart_copy(const T *start, const T *end, T *target)
constexpr auto array_prod(const array< T, N > &arr) -> decltype(array_reduce< product_op, T, N >(arr, static_cast< T >(1)))
void swap(scoped_array< T > &a, scoped_array< T > &b)
std::ptrdiff_t array_prod(const Sizes< Indices... > &)
void swap(T &a, T &b)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
std::array< T, N > array
EIGEN_DEFAULT_DENSE_INDEX_TYPE DenseIndex