Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
TensorGenerator.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2015 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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSOR_TENSOR_GENERATOR_H
12#define EIGEN_TENSOR_TENSOR_GENERATOR_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20template <typename Generator, typename XprType>
21struct traits<TensorGeneratorOp<Generator, XprType> > : public traits<XprType> {
22 typedef typename XprType::Scalar Scalar;
23 typedef traits<XprType> XprTraits;
24 typedef typename XprTraits::StorageKind StorageKind;
25 typedef typename XprTraits::Index Index;
26 static constexpr int NumDimensions = XprTraits::NumDimensions;
27 static constexpr int Layout = XprTraits::Layout;
28 typedef typename XprTraits::PointerType PointerType;
29};
30
31template <typename Generator, typename XprType>
32struct eval<TensorGeneratorOp<Generator, XprType>, Eigen::Dense> {
33 typedef const TensorGeneratorOp<Generator, XprType>& type;
34};
35
36} // end namespace internal
37
43template <typename Generator, typename XprType>
44class TensorGeneratorOp : public TensorBase<TensorGeneratorOp<Generator, XprType>, ReadOnlyAccessors> {
45 public:
46 typedef typename Eigen::internal::traits<TensorGeneratorOp>::Scalar Scalar;
47 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
48 typedef typename XprType::CoeffReturnType CoeffReturnType;
49 typedef typename Eigen::internal::ref_selector<TensorGeneratorOp>::type Nested;
50 typedef typename Eigen::internal::traits<TensorGeneratorOp>::StorageKind StorageKind;
51 typedef typename Eigen::internal::traits<TensorGeneratorOp>::Index Index;
52
53 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorGeneratorOp(const XprType& expr, const Generator& generator)
54 : m_xpr(expr), m_generator(generator) {}
55
56 EIGEN_DEVICE_FUNC const Generator& generator() const { return m_generator; }
57
58 EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
59
60 protected:
61 typename XprType::Nested m_xpr;
62 const Generator m_generator;
63};
64
65// Eval as rvalue
66template <typename Generator, typename ArgType, typename Device>
67struct TensorEvaluator<const TensorGeneratorOp<Generator, ArgType>, Device> {
69 typedef typename XprType::Index Index;
70 typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
71 static constexpr int NumDims = internal::array_size<Dimensions>::value;
72 typedef typename XprType::Scalar Scalar;
73 typedef typename XprType::CoeffReturnType CoeffReturnType;
74 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
75 typedef StorageMemory<CoeffReturnType, Device> Storage;
76 typedef typename Storage::Type EvaluatorPointerType;
77 static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
78 enum {
79 IsAligned = false,
80 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
81 BlockAccess = true,
82 PreferBlockAccess = true,
83 CoordAccess = false, // to be implemented
84 RawAccess = false
85 };
86
87 typedef internal::TensorIntDivisor<Index> IndexDivisor;
88
89 //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
90 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
91 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
92
93 typedef typename internal::TensorMaterializedBlock<CoeffReturnType, NumDims, Layout, Index> TensorBlock;
94 //===--------------------------------------------------------------------===//
95
96 EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
97 : m_device(device), m_generator(op.generator()) {
98 TensorEvaluator<ArgType, Device> argImpl(op.expression(), device);
99 m_dimensions = argImpl.dimensions();
100
101 EIGEN_IF_CONSTEXPR (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
102 m_strides[0] = 1;
103 EIGEN_UNROLL_LOOP
104 for (int i = 1; i < NumDims; ++i) {
105 m_strides[i] = m_strides[i - 1] * m_dimensions[i - 1];
106 if (m_strides[i] != 0) m_fast_strides[i] = IndexDivisor(m_strides[i]);
107 }
108 } else {
109 m_strides[NumDims - 1] = 1;
110 EIGEN_UNROLL_LOOP
111 for (int i = NumDims - 2; i >= 0; --i) {
112 m_strides[i] = m_strides[i + 1] * m_dimensions[i + 1];
113 if (m_strides[i] != 0) m_fast_strides[i] = IndexDivisor(m_strides[i]);
114 }
115 }
116 }
117
118 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
119
120 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) { return true; }
121 EIGEN_STRONG_INLINE void cleanup() {}
122
123 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
124 array<Index, NumDims> coords;
125 extract_coordinates(index, coords);
126 return m_generator(coords);
127 }
128
129 template <int LoadMode>
130 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
131 const int packetSize = PacketType<CoeffReturnType, Device>::size;
132 eigen_assert(index + packetSize - 1 < dimensions().TotalSize());
133
134 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
135 std::remove_const_t<CoeffReturnType> values[packetSize];
136 for (int i = 0; i < packetSize; ++i) {
137 values[i] = coeff(index + i);
138 }
139 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
140 return rslt;
141 }
142
143 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const {
144 const size_t target_size = m_device.firstLevelCacheSize();
145 // TODO(ezhulenev): Generator should have a cost.
146 return internal::TensorBlockResourceRequirements::skewed<Scalar>(target_size);
147 }
148
149 struct BlockIteratorState {
150 Index stride;
151 Index span;
152 Index size;
153 Index count;
154 };
155
156 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
157 bool /*root_of_expr_ast*/ = false) const {
158 static constexpr bool is_col_major = static_cast<int>(Layout) == static_cast<int>(ColMajor);
159
160 // Compute spatial coordinates for the first block element.
161 array<Index, NumDims> coords;
162 extract_coordinates(desc.offset(), coords);
163 array<Index, NumDims> initial_coords = coords;
164
165 // Offset in the output block buffer.
166 Index offset = 0;
167
168 // Initialize output block iterator state. Dimensions in this array are
169 // always in inner_most -> outer_most order (col major layout).
170 array<BlockIteratorState, NumDims> it;
171 for (int i = 0; i < NumDims; ++i) {
172 const int dim = is_col_major ? i : NumDims - 1 - i;
173 it[i].size = desc.dimension(dim);
174 it[i].stride = i == 0 ? 1 : (it[i - 1].size * it[i - 1].stride);
175 it[i].span = it[i].stride * (it[i].size - 1);
176 it[i].count = 0;
177 }
178 eigen_assert(it[0].stride == 1);
179
180 // Prepare storage for the materialized generator result.
181 const typename TensorBlock::Storage block_storage = TensorBlock::prepareStorage(desc, scratch);
182
183 CoeffReturnType* block_buffer = block_storage.data();
184
185 static constexpr int packet_size = PacketType<CoeffReturnType, Device>::size;
186
187 static constexpr int inner_dim = is_col_major ? 0 : NumDims - 1;
188 const Index inner_dim_size = it[0].size;
189 const Index inner_dim_vectorized = inner_dim_size - packet_size;
190
191 while (it[NumDims - 1].count < it[NumDims - 1].size) {
192 Index i = 0;
193 // Generate data for the vectorized part of the inner-most dimension.
194 for (; i <= inner_dim_vectorized; i += packet_size) {
195 for (Index j = 0; j < packet_size; ++j) {
196 array<Index, NumDims> j_coords = coords; // Break loop dependence.
197 j_coords[inner_dim] += j;
198 *(block_buffer + offset + i + j) = m_generator(j_coords);
199 }
200 coords[inner_dim] += packet_size;
201 }
202 // Finalize non-vectorized part of the inner-most dimension.
203 for (; i < inner_dim_size; ++i) {
204 *(block_buffer + offset + i) = m_generator(coords);
205 coords[inner_dim]++;
206 }
207 coords[inner_dim] = initial_coords[inner_dim];
208
209 // For the 1d tensor we need to generate only one inner-most dimension.
210 EIGEN_IF_CONSTEXPR (NumDims == 1) break;
211
212 // Update offset.
213 for (i = 1; i < NumDims; ++i) {
214 if (++it[i].count < it[i].size) {
215 offset += it[i].stride;
216 coords[is_col_major ? i : NumDims - 1 - i]++;
217 break;
218 }
219 if (i != NumDims - 1) it[i].count = 0;
220 coords[is_col_major ? i : NumDims - 1 - i] = initial_coords[is_col_major ? i : NumDims - 1 - i];
221 offset -= it[i].span;
222 }
223 }
224
225 return block_storage.AsTensorMaterializedBlock();
226 }
227
228 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool) const {
229 // TODO(rmlarsen): This is just a placeholder. Define interface to make
230 // generators return their cost.
231 return TensorOpCost(0, 0, TensorOpCost::AddCost<Scalar>() + TensorOpCost::MulCost<Scalar>());
232 }
233
234 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return nullptr; }
235
236 protected:
237 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void extract_coordinates(Index index, array<Index, NumDims>& coords) const {
238 EIGEN_IF_CONSTEXPR (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
239 for (int i = NumDims - 1; i > 0; --i) {
240 const Index idx = index / m_fast_strides[i];
241 index -= idx * m_strides[i];
242 coords[i] = idx;
243 }
244 coords[0] = index;
245 } else {
246 for (int i = 0; i < NumDims - 1; ++i) {
247 const Index idx = index / m_fast_strides[i];
248 index -= idx * m_strides[i];
249 coords[i] = idx;
250 }
251 coords[NumDims - 1] = index;
252 }
253 }
254
255 const Device EIGEN_DEVICE_REF m_device;
256 Dimensions m_dimensions;
257 array<Index, NumDims> m_strides;
258 array<IndexDivisor, NumDims> m_fast_strides;
259 Generator m_generator;
260};
261
262} // end namespace Eigen
263
264#endif // EIGEN_TENSOR_TENSOR_GENERATOR_H
The tensor base class.
Definition TensorForwardDeclarations.h:69
Tensor generator class.
Definition TensorGenerator.h:44
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47