TensorEvalTo.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) 2014 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_EVAL_TO_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
25 namespace internal {
26 template <typename XprType, template <class> class MakePointer_>
27 struct traits<TensorEvalToOp<XprType, MakePointer_> > {
28  // Type promotion to handle the case where the types of the lhs and the rhs are different.
29  typedef typename XprType::Scalar Scalar;
31  typedef typename XprTraits::StorageKind StorageKind;
32  typedef typename XprTraits::Index Index;
33  typedef typename XprType::Nested Nested;
34  typedef std::remove_reference_t<Nested> Nested_;
35  static constexpr int NumDimensions = XprTraits::NumDimensions;
36  static constexpr int Layout = XprTraits::Layout;
38 
39  enum { Flags = 0 };
40  template <class T>
41  struct MakePointer {
42  // Intermediate typedef to workaround MSVC issue.
43  typedef MakePointer_<T> MakePointerT;
44  typedef typename MakePointerT::Type Type;
45  };
46 };
47 
48 template <typename XprType, template <class> class MakePointer_>
49 struct eval<TensorEvalToOp<XprType, MakePointer_>, Eigen::Dense> {
51 };
52 
53 template <typename XprType, template <class> class MakePointer_>
54 struct nested<TensorEvalToOp<XprType, MakePointer_>, 1, typename eval<TensorEvalToOp<XprType, MakePointer_> >::type> {
56 };
57 
58 } // end namespace internal
59 
60 template <typename XprType, template <class> class MakePointer_>
61 class TensorEvalToOp : public TensorBase<TensorEvalToOp<XprType, MakePointer_>, ReadOnlyAccessors> {
62  public:
65  typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
70 
72 
74  : m_xpr(expr), m_buffer(buffer) {}
75 
77 
79 
80  protected:
81  typename XprType::Nested m_xpr;
83 };
84 
85 template <typename ArgType, typename Device, template <class> class MakePointer_>
86 struct TensorEvaluator<const TensorEvalToOp<ArgType, MakePointer_>, Device> {
88  typedef typename ArgType::Scalar Scalar;
90  typedef typename XprType::Index Index;
91  typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
97  enum {
100  BlockAccess = true,
101  PreferBlockAccess = false,
102  CoordAccess = false, // to be implemented
103  RawAccess = true
104  };
105 
107  static constexpr int NumDims = internal::traits<ArgType>::NumDimensions;
108 
109  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
112 
114 
117  //===--------------------------------------------------------------------===//
118 
119  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
120  : m_impl(op.expression(), device), m_buffer(device.get(op.buffer())), m_expression(op.expression()) {}
121 
123 
124  EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
125 
127  EIGEN_UNUSED_VARIABLE(scalar);
128  eigen_assert(scalar == NULL);
129  return m_impl.evalSubExprsIfNeeded(m_buffer);
130  }
131 
132 #ifdef EIGEN_USE_THREADS
133  template <typename EvalSubExprsCallback>
134  EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(EvaluatorPointerType scalar, EvalSubExprsCallback done) {
135  EIGEN_UNUSED_VARIABLE(scalar);
136  eigen_assert(scalar == NULL);
137  m_impl.evalSubExprsIfNeededAsync(m_buffer, std::move(done));
138  }
139 #endif
140 
141  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) const { m_buffer[i] = m_impl.coeff(i); }
143  internal::pstoret<CoeffReturnType, PacketReturnType, Aligned>(
144  m_buffer + i, m_impl.template packet < TensorEvaluator<ArgType, Device>::IsAligned ? Aligned : Unaligned > (i));
145  }
146 
148  return m_impl.getResourceRequirements();
149  }
150 
152  // Add `m_buffer` as destination buffer to the block descriptor.
153  desc.template AddDestinationBuffer<Layout>(
154  /*dst_base=*/m_buffer + desc.offset(),
155  /*dst_strides=*/internal::strides<Layout>(m_impl.dimensions()));
156 
157  ArgTensorBlock block = m_impl.block(desc, scratch, /*root_of_expr_ast=*/true);
158 
159  // If block was evaluated into a destination buffer, there is no need to do
160  // an assignment.
162  TensorBlockAssignment::Run(
163  TensorBlockAssignment::target(desc.dimensions(), internal::strides<Layout>(m_impl.dimensions()), m_buffer,
164  desc.offset()),
165  block.expr());
166  }
167  block.cleanup();
168  }
169 
170  EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
171 
172  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_buffer[index]; }
173 
174  template <int LoadMode>
176  return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
177  }
178 
180  // We assume that evalPacket or evalScalar is called to perform the
181  // assignment and account for the cost of the write here.
182  return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
183  }
184 
185  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_buffer; }
186  ArgType expression() const { return m_expression; }
187 
188  private:
191  const ArgType m_expression;
192 };
193 
194 } // end namespace Eigen
195 
196 #endif // EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:966
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
SCALAR Scalar
Definition: bench_gemm.cpp:45
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:79
The tensor base class.
Definition: TensorBase.h:1026
Definition: TensorEvalTo.h:61
XprType::Nested m_xpr
Definition: TensorEvalTo.h:81
Eigen::internal::traits< TensorEvalToOp >::StorageKind StorageKind
Definition: TensorEvalTo.h:68
MakePointer_< CoeffReturnType >::Type PointerType
Definition: TensorEvalTo.h:66
EIGEN_DEVICE_FUNC const internal::remove_all_t< typename XprType::Nested > & expression() const
Definition: TensorEvalTo.h:76
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvalToOp(PointerType buffer, const XprType &expr)
Definition: TensorEvalTo.h:73
Eigen::NumTraits< Scalar >::Real RealScalar
Definition: TensorEvalTo.h:64
Eigen::internal::traits< TensorEvalToOp >::Index Index
Definition: TensorEvalTo.h:69
PointerType m_buffer
Definition: TensorEvalTo.h:82
EIGEN_DEVICE_FUNC PointerType buffer() const
Definition: TensorEvalTo.h:78
static constexpr int NumDims
Definition: TensorEvalTo.h:71
Eigen::internal::nested< TensorEvalToOp >::type Nested
Definition: TensorEvalTo.h:67
Eigen::internal::traits< TensorEvalToOp >::Scalar Scalar
Definition: TensorEvalTo.h:63
std::remove_const_t< typename XprType::CoeffReturnType > CoeffReturnType
Definition: TensorEvalTo.h:65
Definition: TensorCostModel.h:28
Definition: TensorBlock.h:1314
IndexType offset() const
Definition: TensorBlock.h:270
const Dimensions & dimensions() const
Definition: TensorBlock.h:271
Definition: TensorBlock.h:604
const XprType & expr() const
Definition: TensorBlock.h:621
TensorBlockKind kind() const
Definition: TensorBlock.h:617
void cleanup()
Definition: TensorBlock.h:626
@ Unaligned
Definition: Constants.h:235
@ Aligned
Definition: Constants.h:242
char char * op
Definition: level2_impl.h:374
@ kMaterializedInOutput
Definition: TensorBlock.h:559
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
Definition: Eigen_Colamd.h:49
Type
Type of JSON value.
Definition: rapidjson.h:513
Container::iterator get(Container &c, Position position)
Definition: stdlist_overload.cpp:29
Definition: Constants.h:519
T Real
Definition: NumTraits.h:183
Definition: TensorForwardDeclarations.h:25
Definition: TensorMeta.h:47
Definition: TensorForwardDeclarations.h:42
TensorEvaluator< ArgType, Device >::Dimensions Dimensions
Definition: TensorEvalTo.h:89
TensorEvaluator< ArgType, Device > m_impl
Definition: TensorEvalTo.h:189
PacketType< CoeffReturnType, Device >::type PacketReturnType
Definition: TensorEvalTo.h:92
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType scalar)
Definition: TensorEvalTo.h:126
TensorEvaluator< const ArgType, Device >::TensorBlock ArgTensorBlock
Definition: TensorEvalTo.h:113
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const
Definition: TensorEvalTo.h:147
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) const
Definition: TensorEvalTo.h:141
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalBlock(TensorBlockDesc &desc, TensorBlockScratch &scratch)
Definition: TensorEvalTo.h:151
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const
Definition: TensorEvalTo.h:185
EIGEN_STRONG_INLINE ~TensorEvaluator()
Definition: TensorEvalTo.h:122
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Definition: TensorEvalTo.h:172
internal::TensorBlockAssignment< CoeffReturnType, NumDims, typename ArgTensorBlock::XprType, Index > TensorBlockAssignment
Definition: TensorEvalTo.h:116
StorageMemory< CoeffReturnType, Device > Storage
Definition: TensorEvalTo.h:95
EIGEN_STRONG_INLINE TensorEvaluator(const XprType &op, const Device &device)
Definition: TensorEvalTo.h:119
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
Definition: TensorEvalTo.h:175
internal::TensorBlockScratchAllocator< Device > TensorBlockScratch
Definition: TensorEvalTo.h:111
EIGEN_DEVICE_FUNC const Dimensions & dimensions() const
Definition: TensorEvalTo.h:124
EIGEN_STRONG_INLINE void cleanup()
Definition: TensorEvalTo.h:170
std::remove_const_t< typename XprType::CoeffReturnType > CoeffReturnType
Definition: TensorEvalTo.h:91
internal::TensorBlockDescriptor< NumDims, Index > TensorBlockDesc
Definition: TensorEvalTo.h:110
Eigen::internal::traits< XprType >::PointerType TensorPointerType
Definition: TensorEvalTo.h:94
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) const
Definition: TensorEvalTo.h:142
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
Definition: TensorEvalTo.h:179
TensorEvalToOp< ArgType, MakePointer_ > XprType
Definition: TensorEvalTo.h:87
A cost model used to limit the number of threads used for evaluating tensor expression.
Definition: TensorEvaluator.h:31
static constexpr int Layout
Definition: TensorEvaluator.h:46
Storage::Type EvaluatorPointerType
Definition: TensorEvaluator.h:41
@ PacketAccess
Definition: TensorEvaluator.h:50
@ IsAligned
Definition: TensorEvaluator.h:49
static constexpr int PacketSize
Definition: TensorEvaluator.h:38
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc &desc, TensorBlockScratch &scratch, bool=false) const
Definition: TensorEvaluator.h:147
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
Definition: TensorEvaluator.h:100
const TensorEvalToOp< XprType, MakePointer_ > & type
Definition: TensorEvalTo.h:50
Definition: XprHelper.h:427
Definition: TensorTraits.h:152
ref_selector< T >::type type
Definition: TensorTraits.h:153
traits< XprType > XprTraits
Definition: TensorEvalTo.h:30
XprTraits::StorageKind StorageKind
Definition: TensorEvalTo.h:31
std::remove_reference_t< Nested > Nested_
Definition: TensorEvalTo.h:34
XprType::Nested Nested
Definition: TensorEvalTo.h:33
XprTraits::Index Index
Definition: TensorEvalTo.h:32
MakePointer_< Scalar >::Type PointerType
Definition: TensorEvalTo.h:37
XprType::Scalar Scalar
Definition: TensorEvalTo.h:29
Definition: ForwardDeclarations.h:21