Eigen-unsupported  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
TensorEvalTo.h
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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
24 namespace internal {
25 template<typename XprType, template <class> class MakePointer_>
26 struct traits<TensorEvalToOp<XprType, MakePointer_> >
27 {
28  // Type promotion to handle the case where the types of the lhs and the rhs are different.
29  typedef typename XprType::Scalar Scalar;
30  typedef traits<XprType> XprTraits;
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;
37  typedef typename MakePointer_<Scalar>::Type PointerType;
38 
39  enum {
40  Flags = 0
41  };
42  template <class T>
43  struct MakePointer {
44  // Intermediate typedef to workaround MSVC issue.
45  typedef MakePointer_<T> MakePointerT;
46  typedef typename MakePointerT::Type Type;
47 
48 
49  };
50 };
51 
52 template<typename XprType, template <class> class MakePointer_>
53 struct eval<TensorEvalToOp<XprType, MakePointer_>, Eigen::Dense>
54 {
55  typedef const TensorEvalToOp<XprType, MakePointer_>& type;
56 };
57 
58 template<typename XprType, template <class> class MakePointer_>
59 struct nested<TensorEvalToOp<XprType, MakePointer_>, 1, typename eval<TensorEvalToOp<XprType, MakePointer_> >::type>
60 {
61  typedef TensorEvalToOp<XprType, MakePointer_> type;
62 };
63 
64 } // end namespace internal
65 
66 
67 
68 
69 template<typename XprType, template <class> class MakePointer_>
70 class TensorEvalToOp : public TensorBase<TensorEvalToOp<XprType, MakePointer_>, ReadOnlyAccessors>
71 {
72  public:
73  typedef typename Eigen::internal::traits<TensorEvalToOp>::Scalar Scalar;
74  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
75  typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
76  typedef typename MakePointer_<CoeffReturnType>::Type PointerType;
77  typedef typename Eigen::internal::nested<TensorEvalToOp>::type Nested;
78  typedef typename Eigen::internal::traits<TensorEvalToOp>::StorageKind StorageKind;
79  typedef typename Eigen::internal::traits<TensorEvalToOp>::Index Index;
80 
81  static constexpr int NumDims = Eigen::internal::traits<TensorEvalToOp>::NumDimensions;
82 
83  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvalToOp(PointerType buffer, const XprType& expr)
84  : m_xpr(expr), m_buffer(buffer) {}
85 
86  EIGEN_DEVICE_FUNC
87  const internal::remove_all_t<typename XprType::Nested>&
88  expression() const { return m_xpr; }
89 
90  EIGEN_DEVICE_FUNC PointerType buffer() const { return m_buffer; }
91 
92  protected:
93  typename XprType::Nested m_xpr;
94  PointerType m_buffer;
95 };
96 
97 
98 
99 template<typename ArgType, typename Device, template <class> class MakePointer_>
100 struct TensorEvaluator<const TensorEvalToOp<ArgType, MakePointer_>, Device>
101 {
102  typedef TensorEvalToOp<ArgType, MakePointer_> XprType;
103  typedef typename ArgType::Scalar Scalar;
104  typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
105  typedef typename XprType::Index Index;
106  typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
107  typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
108  static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
109  typedef typename Eigen::internal::traits<XprType>::PointerType TensorPointerType;
110  typedef StorageMemory<CoeffReturnType, Device> Storage;
111  typedef typename Storage::Type EvaluatorPointerType;
112  enum {
113  IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
114  PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
115  BlockAccess = true,
116  PreferBlockAccess = false,
117  CoordAccess = false, // to be implemented
118  RawAccess = true
119  };
120 
121  static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
122  static constexpr int NumDims = internal::traits<ArgType>::NumDimensions;
123 
124  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
125  typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
126  typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
127 
128  typedef typename TensorEvaluator<const ArgType, Device>::TensorBlock
129  ArgTensorBlock;
130 
131  typedef internal::TensorBlockAssignment<
132  CoeffReturnType, NumDims, typename ArgTensorBlock::XprType, Index>
133  TensorBlockAssignment;
134  //===--------------------------------------------------------------------===//
135 
136  EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
137  : m_impl(op.expression(), device), m_buffer(device.get(op.buffer())), m_expression(op.expression()){}
138 
139 
140  EIGEN_STRONG_INLINE ~TensorEvaluator() {
141  }
142 
143 
144  EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
145 
146  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType scalar) {
147  EIGEN_UNUSED_VARIABLE(scalar);
148  eigen_assert(scalar == NULL);
149  return m_impl.evalSubExprsIfNeeded(m_buffer);
150  }
151 
152 #ifdef EIGEN_USE_THREADS
153  template <typename EvalSubExprsCallback>
154  EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(
155  EvaluatorPointerType scalar, EvalSubExprsCallback done) {
156  EIGEN_UNUSED_VARIABLE(scalar);
157  eigen_assert(scalar == NULL);
158  m_impl.evalSubExprsIfNeededAsync(m_buffer, std::move(done));
159  }
160 #endif
161 
162  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) {
163  m_buffer[i] = m_impl.coeff(i);
164  }
165  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) {
166  internal::pstoret<CoeffReturnType, PacketReturnType, Aligned>(m_buffer + i, m_impl.template packet<TensorEvaluator<ArgType, Device>::IsAligned ? Aligned : Unaligned>(i));
167  }
168 
169  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
170  internal::TensorBlockResourceRequirements getResourceRequirements() const {
171  return m_impl.getResourceRequirements();
172  }
173 
174  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalBlock(
175  TensorBlockDesc& desc, TensorBlockScratch& scratch) {
176  // Add `m_buffer` as destination buffer to the block descriptor.
177  desc.template AddDestinationBuffer<Layout>(
178  /*dst_base=*/m_buffer + desc.offset(),
179  /*dst_strides=*/internal::strides<Layout>(m_impl.dimensions()));
180 
181  ArgTensorBlock block =
182  m_impl.block(desc, scratch, /*root_of_expr_ast=*/true);
183 
184  // If block was evaluated into a destination buffer, there is no need to do
185  // an assignment.
186  if (block.kind() != internal::TensorBlockKind::kMaterializedInOutput) {
187  TensorBlockAssignment::Run(
188  TensorBlockAssignment::target(
189  desc.dimensions(), internal::strides<Layout>(m_impl.dimensions()),
190  m_buffer, desc.offset()),
191  block.expr());
192  }
193  block.cleanup();
194  }
195 
196  EIGEN_STRONG_INLINE void cleanup() {
197  m_impl.cleanup();
198  }
199 
200  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
201  {
202  return m_buffer[index];
203  }
204 
205  template<int LoadMode>
206  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
207  {
208  return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
209  }
210 
211  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
212  // We assume that evalPacket or evalScalar is called to perform the
213  // assignment and account for the cost of the write here.
214  return m_impl.costPerCoeff(vectorized) +
215  TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
216  }
217 
218  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_buffer; }
219  ArgType expression() const { return m_expression; }
220  #ifdef EIGEN_USE_SYCL
221  // binding placeholder accessors to a command group handler for SYCL
222  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
223  m_impl.bind(cgh);
224  m_buffer.bind(cgh);
225  }
226  #endif
227 
228 
229  private:
230  TensorEvaluator<ArgType, Device> m_impl;
231  EvaluatorPointerType m_buffer;
232  const ArgType m_expression;
233 };
234 
235 
236 } // end namespace Eigen
237 
238 #endif // EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index