Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
TensorStorage.h
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// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_TENSOR_TENSORSTORAGE_H
13#define EIGEN_TENSOR_TENSORSTORAGE_H
14
15#ifdef EIGEN_TENSOR_STORAGE_CTOR_PLUGIN
16#define EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN EIGEN_TENSOR_STORAGE_CTOR_PLUGIN;
17#else
18#define EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN
19#endif
20
21// IWYU pragma: private
22#include "./InternalHeaderCheck.h"
23
24namespace Eigen {
25
37template <typename T, typename Dimensions, int Options>
38class TensorStorage;
39
40// Pure fixed-size storage
41template <typename T, typename FixedDimensions, int Options_>
42class TensorStorage {
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;
48 EIGEN_ALIGN_MAX T m_data[MinSize];
49
50 public:
51 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorStorage() {}
52
53 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T* data() { return m_data; }
54 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T* data() const { return m_data; }
55
56 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const FixedDimensions dimensions() const { return FixedDimensions(); }
57
58 constexpr EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex size() const { return Size; }
59};
60
61// pure dynamic
62template <typename T, typename IndexType, int NumIndices_, int Options_>
63class TensorStorage<T, DSizes<IndexType, NumIndices_>, Options_> {
64 public:
65 typedef IndexType Index;
66 typedef DSizes<IndexType, NumIndices_> Dimensions;
67 typedef TensorStorage<T, DSizes<IndexType, NumIndices_>, Options_> Self;
68
69 EIGEN_DEVICE_FUNC TensorStorage() : m_data(0), m_dimensions() {
70 EIGEN_IF_CONSTEXPR (NumIndices_ == 0) {
71 m_data = internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(1);
72 }
73 }
74 EIGEN_DEVICE_FUNC TensorStorage(Index size, const array<Index, NumIndices_>& dimensions)
75 : m_data(internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(size)), m_dimensions(dimensions) {
76 EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN
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
84 EIGEN_DEVICE_FUNC TensorStorage(const Self& other)
85 : m_data(internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(
86 internal::array_prod(other.m_dimensions))),
87 m_dimensions(other.m_dimensions) {
88 internal::smart_copy(other.m_data, other.m_data + internal::array_prod(other.m_dimensions), m_data);
89 }
90 EIGEN_DEVICE_FUNC Self& operator=(const Self& other) {
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
100 EIGEN_DEVICE_FUNC Self& operator=(Self&& other) {
101 numext::swap(m_data, other.m_data);
102 numext::swap(m_dimensions, other.m_dimensions);
103 return *this;
104 }
105
106 EIGEN_DEVICE_FUNC ~TensorStorage() {
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
117 EIGEN_DEVICE_FUNC void resize(Index size, const array<Index, NumIndices_>& nbDimensions) {
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 EIGEN_IF_CONSTEXPR (NumIndices_ == 0) {
124 m_data = internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(1);
125 } else
126 m_data = 0;
127 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
128 }
129 m_dimensions = nbDimensions;
130 }
131
132 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T* data() { return m_data; }
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:
138 T* m_data;
139 Dimensions m_dimensions;
140};
141
142} // end namespace Eigen
143
144#endif // EIGEN_TENSOR_TENSORSTORAGE_H
Namespace containing all symbols from the Eigen library.