TensorGenerator.h
Go to the documentation of this file.
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 namespace Eigen {
14 
22 namespace internal {
23 template<typename Generator, typename XprType>
24 struct traits<TensorGeneratorOp<Generator, XprType> > : public traits<XprType>
25 {
26  typedef typename XprType::Scalar Scalar;
28  typedef typename XprTraits::StorageKind StorageKind;
29  typedef typename XprTraits::Index Index;
30  typedef typename XprType::Nested Nested;
32  static const int NumDimensions = XprTraits::NumDimensions;
33  static const int Layout = XprTraits::Layout;
34 };
35 
36 template<typename Generator, typename XprType>
37 struct eval<TensorGeneratorOp<Generator, XprType>, Eigen::Dense>
38 {
40 };
41 
42 template<typename Generator, typename XprType>
43 struct nested<TensorGeneratorOp<Generator, XprType>, 1, typename eval<TensorGeneratorOp<Generator, XprType> >::type>
44 {
46 };
47 
48 } // end namespace internal
49 
50 
51 
52 template<typename Generator, typename XprType>
53 class TensorGeneratorOp : public TensorBase<TensorGeneratorOp<Generator, XprType>, ReadOnlyAccessors>
54 {
55  public:
58  typedef typename XprType::CoeffReturnType CoeffReturnType;
62 
63  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorGeneratorOp(const XprType& expr, const Generator& generator)
64  : m_xpr(expr), m_generator(generator) {}
65 
66  EIGEN_DEVICE_FUNC
67  const Generator& generator() const { return m_generator; }
68 
69  EIGEN_DEVICE_FUNC
71  expression() const { return m_xpr; }
72 
73  protected:
74  typename XprType::Nested m_xpr;
75  const Generator m_generator;
76 };
77 
78 
79 // Eval as rvalue
80 template<typename Generator, typename ArgType, typename Device>
81 struct TensorEvaluator<const TensorGeneratorOp<Generator, ArgType>, Device>
82 {
84  typedef typename XprType::Index Index;
86  static const int NumDims = internal::array_size<Dimensions>::value;
87  typedef typename XprType::Scalar Scalar;
90  enum {
91  IsAligned = false,
93  BlockAccess = false,
95  CoordAccess = false, // to be implemented
96  RawAccess = false
97  };
98 
99  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
100  : m_generator(op.generator())
101  {
103  m_dimensions = impl.dimensions();
104 
105  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
106  m_strides[0] = 1;
107  for (int i = 1; i < NumDims; ++i) {
108  m_strides[i] = m_strides[i - 1] * m_dimensions[i - 1];
109  }
110  } else {
111  m_strides[NumDims - 1] = 1;
112  for (int i = NumDims - 2; i >= 0; --i) {
113  m_strides[i] = m_strides[i + 1] * m_dimensions[i + 1];
114  }
115  }
116  }
117 
118  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
119 
120  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
121  return true;
122  }
123  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
124  }
125 
126  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
127  {
128  array<Index, NumDims> coords;
129  extract_coordinates(index, coords);
130  return m_generator(coords);
131  }
132 
133  template<int LoadMode>
134  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
135  {
137  EIGEN_STATIC_ASSERT((packetSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
138  eigen_assert(index+packetSize-1 < dimensions().TotalSize());
139 
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 TensorOpCost
149  costPerCoeff(bool) const {
150  // TODO(rmlarsen): This is just a placeholder. Define interface to make
151  // generators return their cost.
152  return TensorOpCost(0, 0, TensorOpCost::AddCost<Scalar>() +
153  TensorOpCost::MulCost<Scalar>());
154  }
155 
156  EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
157 
158  protected:
159  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
160  void extract_coordinates(Index index, array<Index, NumDims>& coords) const {
161  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
162  for (int i = NumDims - 1; i > 0; --i) {
163  const Index idx = index / m_strides[i];
164  index -= idx * m_strides[i];
165  coords[i] = idx;
166  }
167  coords[0] = index;
168  } else {
169  for (int i = 0; i < NumDims - 1; ++i) {
170  const Index idx = index / m_strides[i];
171  index -= idx * m_strides[i];
172  coords[i] = idx;
173  }
174  coords[NumDims-1] = index;
175  }
176  }
177 
180  Generator m_generator;
181 };
182 
183 } // end namespace Eigen
184 
185 #endif // EIGEN_CXX11_TENSOR_TENSOR_GENERATOR_H
Eigen::TensorEvaluator::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorEvaluator.h:54
Eigen::TensorEvaluator::device
const Device & device() const
required by sycl in order to construct sycl buffer from raw pointer
Definition: TensorEvaluator.h:114
Eigen
Definition: common.h:73
Eigen::internal::nested< TensorGeneratorOp< Generator, XprType >, 1, typename eval< TensorGeneratorOp< Generator, XprType > >::type >::type
TensorGeneratorOp< Generator, XprType > type
Definition: TensorGenerator.h:45
Eigen::array< Index, NumDims >
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::m_dimensions
Dimensions m_dimensions
Definition: TensorGenerator.h:178
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::data
EIGEN_DEVICE_FUNC Scalar * data() const
Definition: TensorGenerator.h:156
Eigen::internal::nested
Definition: TensorTraits.h:170
Eigen::TensorEvaluator::PacketAccess
@ PacketAccess
Definition: TensorEvaluator.h:42
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:579
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::PacketReturnType
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorGenerator.h:89
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::TensorEvaluator
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorGenerator.h:99
Eigen::PacketType::type
internal::packet_traits< Scalar >::type type
Definition: TensorMeta.h:51
Eigen::TensorGeneratorOp::m_xpr
XprType::Nested m_xpr
Definition: TensorGenerator.h:74
Eigen::internal::remove_all::type
T type
Definition: Meta.h:78
Scalar
SCALAR Scalar
Definition: common.h:84
Eigen::TensorGeneratorOp::generator
const EIGEN_DEVICE_FUNC Generator & generator() const
Definition: TensorGenerator.h:67
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorGenerator.h:126
Eigen::internal::traits< TensorGeneratorOp< Generator, XprType > >::Index
XprTraits::Index Index
Definition: TensorGenerator.h:29
EIGEN_ALIGN_MAX
#define EIGEN_ALIGN_MAX
Definition: Macros.h:757
Eigen::internal::traits< TensorGeneratorOp< Generator, XprType > >::XprTraits
traits< XprType > XprTraits
Definition: TensorGenerator.h:27
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::Dimensions
TensorEvaluator< ArgType, Device >::Dimensions Dimensions
Definition: TensorGenerator.h:85
Eigen::internal::eval< TensorGeneratorOp< Generator, XprType >, Eigen::Dense >::type
const typedef TensorGeneratorOp< Generator, XprType > & type
Definition: TensorGenerator.h:39
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::evalSubExprsIfNeeded
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar *)
Definition: TensorGenerator.h:120
Eigen::internal::true_type
Definition: Meta.h:54
Eigen::internal::remove_reference::type
T type
Definition: Meta.h:66
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::Index
XprType::Index Index
Definition: TensorGenerator.h:84
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::Scalar
XprType::Scalar Scalar
Definition: TensorGenerator.h:87
Eigen::internal::traits< TensorGeneratorOp< Generator, XprType > >::_Nested
remove_reference< Nested >::type _Nested
Definition: TensorGenerator.h:31
Eigen::TensorGeneratorOp::Index
Eigen::internal::traits< TensorGeneratorOp >::Index Index
Definition: TensorGenerator.h:61
Eigen::internal::traits< TensorGeneratorOp< Generator, XprType > >::StorageKind
XprTraits::StorageKind StorageKind
Definition: TensorGenerator.h:28
Eigen::TensorGeneratorOp
Tensor generator class.
Definition: TensorForwardDeclarations.h:58
Eigen::internal::unpacket_traits
Definition: XprHelper.h:158
Eigen::TensorEvaluator::IsAligned
@ IsAligned
Definition: TensorEvaluator.h:41
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:494
Eigen::TensorGeneratorOp::Scalar
Eigen::internal::traits< TensorGeneratorOp >::Scalar Scalar
Definition: TensorGenerator.h:56
Eigen::Map< Matrix< Scalar, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> >
Eigen::TensorBase
The tensor base class.
Definition: TensorBase.h:829
Eigen::TensorGeneratorOp::m_generator
const Generator m_generator
Definition: TensorGenerator.h:75
Eigen::TensorGeneratorOp::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorGenerator.h:58
Eigen::TensorEvaluator::Layout
@ Layout
Definition: TensorEvaluator.h:43
Eigen::TensorGeneratorOp::RealScalar
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorGenerator.h:57
Eigen::internal::array_size
Definition: EmulateArray.h:203
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::packet
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
Definition: TensorGenerator.h:134
Eigen::TensorGeneratorOp::TensorGeneratorOp
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorGeneratorOp(const XprType &expr, const Generator &generator)
Definition: TensorGenerator.h:63
Eigen::TensorGeneratorOp::StorageKind
Eigen::internal::traits< TensorGeneratorOp >::StorageKind StorageKind
Definition: TensorGenerator.h:60
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::cleanup
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup()
Definition: TensorGenerator.h:123
Eigen::internal::traits< TensorGeneratorOp< Generator, XprType > >::Nested
XprType::Nested Nested
Definition: TensorGenerator.h:30
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
EIGEN_STATIC_ASSERT
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:124
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::costPerCoeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool) const
Definition: TensorGenerator.h:149
Eigen::TensorGeneratorOp::Nested
Eigen::internal::nested< TensorGeneratorOp >::type Nested
Definition: TensorGenerator.h:59
Eigen::TensorEvaluator::coeff
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorEvaluator.h:66
Eigen::TensorGeneratorOp::expression
const EIGEN_DEVICE_FUNC internal::remove_all< typename XprType::Nested >::type & expression() const
Definition: TensorGenerator.h:71
Eigen::TensorEvaluator
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:28
internal
Definition: BandTriangularSolver.h:13
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::XprType
TensorGeneratorOp< Generator, ArgType > XprType
Definition: TensorGenerator.h:83
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::extract_coordinates
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void extract_coordinates(Index index, array< Index, NumDims > &coords) const
Definition: TensorGenerator.h:160
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::m_strides
array< Index, NumDims > m_strides
Definition: TensorGenerator.h:179
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::CoeffReturnType
XprType::CoeffReturnType CoeffReturnType
Definition: TensorGenerator.h:88
Eigen::ColMajor
@ ColMajor
Definition: Constants.h:320
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::m_generator
Generator m_generator
Definition: TensorGenerator.h:180
Eigen::TensorEvaluator< const TensorGeneratorOp< Generator, ArgType >, Device >::dimensions
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Dimensions & dimensions() const
Definition: TensorGenerator.h:118
Eigen::internal::eval
Definition: XprHelper.h:312
Eigen::internal::traits< TensorGeneratorOp< Generator, XprType > >::Scalar
XprType::Scalar Scalar
Definition: TensorGenerator.h:26
Eigen::TensorOpCost
Definition: TensorCostModel.h:25
Eigen::GenericNumTraits::Real
T Real
Definition: NumTraits.h:100
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Eigen::Dense
Definition: Constants.h:491


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:06:45