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