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 EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType /*data*/) { return true; }
123 EIGEN_STRONG_INLINE void cleanup() {}
124
125 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const {
126 array<Index, NumDims> coords;
127 extract_coordinates(index, coords);
128 return m_generator(coords);
129 }
130
131 template <int LoadMode>
132 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
133 const int packetSize = PacketType<CoeffReturnType, Device>::size;
134 eigen_assert(index + packetSize - 1 < dimensions().TotalSize());
135
136 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment)
137 std::remove_const_t<CoeffReturnType> values[packetSize];
138 for (int i = 0; i < packetSize; ++i) {
139 values[i] = coeff(index + i);
140 }
141 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
142 return rslt;
143 }
144
145 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const {
146 const size_t target_size = m_device.firstLevelCacheSize();
147 // TODO(ezhulenev): Generator should have a cost.
148 return internal::TensorBlockResourceRequirements::skewed<Scalar>(target_size);
149 }
150
151 struct BlockIteratorState {
152 Index stride;
153 Index span;
154 Index size;
155 Index count;
156 };
157
158 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
159 bool /*root_of_expr_ast*/ = false) const {
160 static constexpr bool is_col_major = static_cast<int>(Layout) == static_cast<int>(ColMajor);
161
162 // Compute spatial coordinates for the first block element.
163 array<Index, NumDims> coords;
164 extract_coordinates(desc.offset(), coords);
165 array<Index, NumDims> initial_coords = coords;
166
167 // Offset in the output block buffer.
168 Index offset = 0;
169
170 // Initialize output block iterator state. Dimensions in this array are
171 // always in inner_most -> outer_most order (col major layout).
172 array<BlockIteratorState, NumDims> it;
173 for (int i = 0; i < NumDims; ++i) {
174 const int dim = is_col_major ? i : NumDims - 1 - i;
175 it[i].size = desc.dimension(dim);
176 it[i].stride = i == 0 ? 1 : (it[i - 1].size * it[i - 1].stride);
177 it[i].span = it[i].stride * (it[i].size - 1);
178 it[i].count = 0;
179 }
180 eigen_assert(it[0].stride == 1);
181
182 // Prepare storage for the materialized generator result.
183 const typename TensorBlock::Storage block_storage = TensorBlock::prepareStorage(desc, scratch);
184
185 CoeffReturnType* block_buffer = block_storage.data();
186
187 static constexpr int packet_size = PacketType<CoeffReturnType, Device>::size;
188
189 static constexpr int inner_dim = is_col_major ? 0 : NumDims - 1;
190 const Index inner_dim_size = it[0].size;
191 const Index inner_dim_vectorized = inner_dim_size - packet_size;
192
193 while (it[NumDims - 1].count < it[NumDims - 1].size) {
194 Index i = 0;
195 // Generate data for the vectorized part of the inner-most dimension.
196 for (; i <= inner_dim_vectorized; i += packet_size) {
197 for (Index j = 0; j < packet_size; ++j) {
198 array<Index, NumDims> j_coords = coords; // Break loop dependence.
199 j_coords[inner_dim] += j;
200 *(block_buffer + offset + i + j) = m_generator(j_coords);
201 }
202 coords[inner_dim] += packet_size;
203 }
204 // Finalize non-vectorized part of the inner-most dimension.
205 for (; i < inner_dim_size; ++i) {
206 *(block_buffer + offset + i) = m_generator(coords);
207 coords[inner_dim]++;
208 }
209 coords[inner_dim] = initial_coords[inner_dim];
210
211 // For the 1d tensor we need to generate only one inner-most dimension.
212 EIGEN_IF_CONSTEXPR (NumDims == 1) break;
213
214 // Update offset.
215 for (i = 1; i < NumDims; ++i) {
216 if (++it[i].count < it[i].size) {
217 offset += it[i].stride;
218 coords[is_col_major ? i : NumDims - 1 - i]++;
219 break;
220 }
221 if (i != NumDims - 1) it[i].count = 0;
222 coords[is_col_major ? i : NumDims - 1 - i] = initial_coords[is_col_major ? i : NumDims - 1 - i];
223 offset -= it[i].span;
224 }
225 }
226
227 return block_storage.AsTensorMaterializedBlock();
228 }
229
230 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool) const {
231 // TODO(rmlarsen): This is just a placeholder. Define interface to make
232 // generators return their cost.
233 return TensorOpCost(0, 0, TensorOpCost::AddCost<Scalar>() + TensorOpCost::MulCost<Scalar>());
234 }
235
236 EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return nullptr; }
237
238 protected:
239 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void extract_coordinates(Index index, array<Index, NumDims>& coords) const {
240 EIGEN_IF_CONSTEXPR (NumDims == 0) return;
241 EIGEN_IF_CONSTEXPR (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
242 for (int i = NumDims - 1; i > 0; --i) {
243 const Index idx = index / m_fast_strides[i];
244 index -= idx * m_strides[i];
245 coords[i] = idx;
246 }
247 coords[0] = index;
248 } else {
249 for (int i = 0; i < NumDims - 1; ++i) {
250 const Index idx = index / m_fast_strides[i];
251 index -= idx * m_strides[i];
252 coords[i] = idx;
253 }
254 coords[NumDims - 1] = index;
255 }
256 }
257
258 const Device EIGEN_DEVICE_REF m_device;
259 Dimensions m_dimensions;
260 array<Index, NumDims> m_strides;
261 array<IndexDivisor, NumDims> m_fast_strides;
262 Generator m_generator;
263};
264
265} // end namespace Eigen
266
267#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