10#ifndef EIGEN_CXX11_TENSOR_TENSOR_GENERATOR_H
11#define EIGEN_CXX11_TENSOR_TENSOR_GENERATOR_H
16template<
typename Generator,
typename XprType>
17struct traits<TensorGeneratorOp<Generator, XprType> > :
public traits<XprType>
19 typedef typename XprType::Scalar Scalar;
20 typedef traits<XprType> XprTraits;
21 typedef typename XprTraits::StorageKind StorageKind;
22 typedef typename XprTraits::Index
Index;
23 typedef typename XprType::Nested Nested;
24 typedef typename remove_reference<Nested>::type _Nested;
25 static const int NumDimensions = XprTraits::NumDimensions;
26 static const int Layout = XprTraits::Layout;
27 typedef typename XprTraits::PointerType PointerType;
30template<
typename Generator,
typename XprType>
31struct eval<TensorGeneratorOp<Generator, XprType>, Eigen::Dense>
33 typedef const TensorGeneratorOp<Generator, XprType>& type;
36template<
typename Generator,
typename XprType>
37struct nested<TensorGeneratorOp<Generator, XprType>, 1, typename eval<TensorGeneratorOp<Generator, XprType> >::type>
39 typedef TensorGeneratorOp<Generator, XprType> type;
49template <
typename Generator,
typename XprType>
50class TensorGeneratorOp :
public TensorBase<TensorGeneratorOp<Generator, XprType>, ReadOnlyAccessors> {
52 typedef typename Eigen::internal::traits<TensorGeneratorOp>::Scalar Scalar;
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;
59 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorGeneratorOp(
const XprType& expr,
const Generator& generator)
60 : m_xpr(expr), m_generator(generator) {}
63 const Generator& generator()
const {
return m_generator; }
66 const typename internal::remove_all<typename XprType::Nested>::type&
67 expression()
const {
return m_xpr; }
70 typename XprType::Nested m_xpr;
71 const Generator m_generator;
76template<
typename Generator,
typename ArgType,
typename Device>
80 typedef typename XprType::Index
Index;
81 typedef typename TensorEvaluator<ArgType, Device>::Dimensions
Dimensions;
82 static const int NumDims = internal::array_size<Dimensions>::value;
83 typedef typename XprType::Scalar
Scalar;
85 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
86 typedef StorageMemory<CoeffReturnType, Device> Storage;
87 typedef typename Storage::Type EvaluatorPointerType;
90 PacketAccess = (PacketType<CoeffReturnType, Device>::size > 1),
92 PreferBlockAccess =
true,
93 Layout = TensorEvaluator<ArgType, Device>::Layout,
98 typedef internal::TensorIntDivisor<Index> IndexDivisor;
101 typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
102 typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
104 typedef typename internal::TensorMaterializedBlock<CoeffReturnType, NumDims,
109 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
110 : m_device(device), m_generator(op.generator())
112 TensorEvaluator<ArgType, Device> argImpl(op.expression(), device);
113 m_dimensions = argImpl.dimensions();
115 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
118 for (
int i = 1; i < NumDims; ++i) {
119 m_strides[i] = m_strides[i - 1] * m_dimensions[i - 1];
120 if (m_strides[i] != 0) m_fast_strides[i] = IndexDivisor(m_strides[i]);
123 m_strides[NumDims - 1] = 1;
125 for (
int i = NumDims - 2; i >= 0; --i) {
126 m_strides[i] = m_strides[i + 1] * m_dimensions[i + 1];
127 if (m_strides[i] != 0) m_fast_strides[i] = IndexDivisor(m_strides[i]);
132 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
134 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType ) {
137 EIGEN_STRONG_INLINE
void cleanup() {
140 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const
142 array<Index, NumDims> coords;
143 extract_coordinates(index, coords);
144 return m_generator(coords);
147 template<
int LoadMode>
148 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const
150 const int packetSize = PacketType<CoeffReturnType, Device>::size;
151 EIGEN_STATIC_ASSERT((packetSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
152 eigen_assert(index+packetSize-1 < dimensions().TotalSize());
154 EIGEN_ALIGN_MAX
typename internal::remove_const<CoeffReturnType>::type values[packetSize];
155 for (
int i = 0; i < packetSize; ++i) {
156 values[i] = coeff(index+i);
158 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
162 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
163 internal::TensorBlockResourceRequirements getResourceRequirements()
const {
164 const size_t target_size = m_device.firstLevelCacheSize();
166 return internal::TensorBlockResourceRequirements::skewed<Scalar>(
170 struct BlockIteratorState {
177 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock
178 block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
179 bool =
false)
const {
180 static const bool is_col_major =
181 static_cast<int>(Layout) ==
static_cast<int>(
ColMajor);
184 array<Index, NumDims> coords;
185 extract_coordinates(desc.offset(), coords);
186 array<Index, NumDims> initial_coords = coords;
193 array<BlockIteratorState, NumDims> it;
194 for (
int i = 0; i < NumDims; ++i) {
195 const int dim = is_col_major ? i : NumDims - 1 - i;
196 it[i].size = desc.dimension(dim);
197 it[i].stride = i == 0 ? 1 : (it[i - 1].size * it[i - 1].stride);
198 it[i].span = it[i].stride * (it[i].size - 1);
201 eigen_assert(it[0].stride == 1);
204 const typename TensorBlock::Storage block_storage =
205 TensorBlock::prepareStorage(desc, scratch);
207 CoeffReturnType* block_buffer = block_storage.data();
209 static const int packet_size = PacketType<CoeffReturnType, Device>::size;
211 static const int inner_dim = is_col_major ? 0 : NumDims - 1;
212 const Index inner_dim_size = it[0].size;
213 const Index inner_dim_vectorized = inner_dim_size - packet_size;
215 while (it[NumDims - 1].count < it[NumDims - 1].size) {
218 for (; i <= inner_dim_vectorized; i += packet_size) {
219 for (Index j = 0; j < packet_size; ++j) {
220 array<Index, NumDims> j_coords = coords;
221 j_coords[inner_dim] += j;
222 *(block_buffer + offset + i + j) = m_generator(j_coords);
224 coords[inner_dim] += packet_size;
227 for (; i < inner_dim_size; ++i) {
228 *(block_buffer + offset + i) = m_generator(coords);
231 coords[inner_dim] = initial_coords[inner_dim];
234 if (NumDims == 1)
break;
237 for (i = 1; i < NumDims; ++i) {
238 if (++it[i].count < it[i].size) {
239 offset += it[i].stride;
240 coords[is_col_major ? i : NumDims - 1 - i]++;
243 if (i != NumDims - 1) it[i].count = 0;
244 coords[is_col_major ? i : NumDims - 1 - i] =
245 initial_coords[is_col_major ? i : NumDims - 1 - i];
246 offset -= it[i].span;
250 return block_storage.AsTensorMaterializedBlock();
253 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
254 costPerCoeff(
bool)
const {
257 return TensorOpCost(0, 0, TensorOpCost::AddCost<Scalar>() +
258 TensorOpCost::MulCost<Scalar>());
261 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return NULL; }
265 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void bind(cl::sycl::handler&)
const {}
269 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
270 void extract_coordinates(Index index, array<Index, NumDims>& coords)
const {
271 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
272 for (
int i = NumDims - 1; i > 0; --i) {
273 const Index idx = index / m_fast_strides[i];
274 index -= idx * m_strides[i];
279 for (
int i = 0; i < NumDims - 1; ++i) {
280 const Index idx = index / m_fast_strides[i];
281 index -= idx * m_strides[i];
284 coords[NumDims-1] = index;
288 const Device EIGEN_DEVICE_REF m_device;
289 Dimensions m_dimensions;
290 array<Index, NumDims> m_strides;
291 array<IndexDivisor, NumDims> m_fast_strides;
292 Generator m_generator;
The tensor base class.
Definition TensorForwardDeclarations.h:56
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:27