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