TensorForcedEval.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_FORCED_EVAL_H
11 #define EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H
12 
13 namespace Eigen {
14 
22 namespace internal {
23 template<typename XprType>
25 {
26  // Type promotion to handle the case where the types of the lhs and the rhs are different.
27  typedef typename XprType::Scalar Scalar;
30  typedef typename traits<XprType>::Index Index;
31  typedef typename XprType::Nested Nested;
33  static const int NumDimensions = XprTraits::NumDimensions;
34  static const int Layout = XprTraits::Layout;
35  typedef typename XprTraits::PointerType PointerType;
36 
37  enum {
38  Flags = 0
39  };
40 };
41 
42 template<typename XprType>
44 {
46 };
47 
48 template<typename XprType>
50 {
52 };
53 
54 } // end namespace internal
55 
56 
57 
58 template<typename XprType>
59 class TensorForcedEvalOp : public TensorBase<TensorForcedEvalOp<XprType>, ReadOnlyAccessors>
60 {
61  public:
68 
70  : m_xpr(expr) {}
71 
74  expression() const { return m_xpr; }
75 
76  protected:
77  typename XprType::Nested m_xpr;
78 };
79 
80 namespace internal {
81 template <typename Device, typename CoeffReturnType>
83  template <typename StorageType>
84 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void operator()(Index numValues, StorageType m_buffer) {
85  // Initialize non-trivially constructible types.
87  for (Index i = 0; i < numValues; ++i) new (m_buffer + i) CoeffReturnType();
88  }
89 }
90 };
91 
92 // SYCL does not support non-integral types
93 // having new (m_buffer + i) CoeffReturnType() causes the following compiler error for SYCL Devices
94 // no matching function for call to 'operator new'
95 template <typename CoeffReturnType>
96 struct non_integral_type_placement_new<Eigen::SyclDevice, CoeffReturnType> {
97  template <typename StorageType>
99 }
100 };
101 } // end namespace internal
102 
103 template<typename ArgType_, typename Device>
104 struct TensorEvaluator<const TensorForcedEvalOp<ArgType_>, Device>
105 {
108  typedef typename ArgType::Scalar Scalar;
110  typedef typename XprType::Index Index;
113  static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
117 
118  enum {
119  IsAligned = true,
122  PreferBlockAccess = false,
124  RawAccess = true
125  };
126 
127  static const int NumDims = internal::traits<ArgType>::NumDimensions;
128 
129  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
132 
133  typedef typename internal::TensorMaterializedBlock<CoeffReturnType, NumDims,
134  Layout, Index>
136  //===--------------------------------------------------------------------===//
137 
138  TensorEvaluator(const XprType& op, const Device& device)
139  : m_impl(op.expression(), device), m_op(op.expression()),
140  m_device(device), m_buffer(NULL)
141  { }
142 
143  EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
144 
145  EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType) {
146  const Index numValues = internal::array_prod(m_impl.dimensions());
147  m_buffer = m_device.get((CoeffReturnType*)m_device.allocate_temp(numValues * sizeof(CoeffReturnType)));
148 
150 
152  EvalTo evalToTmp(m_device.get(m_buffer), m_op);
153 
155  const EvalTo, typename internal::remove_const<Device>::type,
158  run(evalToTmp, m_device);
159 
160  return true;
161  }
162 
163 #ifdef EIGEN_USE_THREADS
164  template <typename EvalSubExprsCallback>
165  EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(
166  EvaluatorPointerType, EvalSubExprsCallback done) {
167  const Index numValues = internal::array_prod(m_impl.dimensions());
168  m_buffer = m_device.get((CoeffReturnType*)m_device.allocate_temp(
169  numValues * sizeof(CoeffReturnType)));
171  EvalTo;
172  EvalTo evalToTmp(m_device.get(m_buffer), m_op);
173 
174  auto on_done = std::bind([](EvalSubExprsCallback done_) { done_(true); },
175  std::move(done));
177  const EvalTo, typename internal::remove_const<Device>::type,
178  decltype(on_done),
181  runAsync(evalToTmp, m_device, std::move(on_done));
182  }
183 #endif
184 
186  m_device.deallocate_temp(m_buffer);
187  m_buffer = NULL;
188  }
189 
190  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
191  {
192  return m_buffer[index];
193  }
194 
195  template<int LoadMode>
196  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
197  {
198  return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
199  }
200 
204  }
205 
207  block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
208  bool /*root_of_expr_ast*/ = false) const {
209  assert(m_buffer != NULL);
210  return TensorBlock::materialize(m_buffer, m_impl.dimensions(), desc, scratch);
211  }
212 
214  return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
215  }
216 
218  EvaluatorPointerType data() const { return m_buffer; }
219 
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_buffer.bind(cgh);
224  m_impl.bind(cgh);
225  }
226 #endif
227  private:
229  const ArgType m_op;
231  EvaluatorPointerType m_buffer;
232 };
233 
234 
235 } // end namespace Eigen
236 
237 #endif // EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H
internal::TensorBlockScratchAllocator< Device > TensorBlockScratch
SCALAR Scalar
Definition: bench_gemm.cpp:46
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes< Indices... > &)
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
Eigen::internal::traits< TensorForcedEvalOp >::Index Index
internal::TensorBlockDescriptor< NumDims, Index > TensorBlockDesc
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
internal::TensorMaterializedBlock< CoeffReturnType, NumDims, Layout, Index > TensorBlock
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorForcedEvalOp(const XprType &expr)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EvaluatorPointerType data() const
EIGEN_DEVICE_FUNC const internal::remove_all< typename XprType::Nested >::type & expression() const
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
A cost model used to limit the number of threads used for evaluating tensor expression.
Eigen::internal::traits< TensorForcedEvalOp >::Scalar Scalar
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void operator()(Index, StorageType)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const
Eigen::NumTraits< Scalar >::Real RealScalar
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:77
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void operator()(Index numValues, StorageType m_buffer)
Eigen::internal::nested< TensorForcedEvalOp >::type Nested
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
#define NULL
Definition: ccolamd.c:609
internal::remove_const< typename XprType::CoeffReturnType >::type CoeffReturnType
The tensor base class.
Definition: TensorBase.h:973
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen::internal::traits< TensorForcedEvalOp >::StorageKind StorageKind
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType)
#define EIGEN_DEVICE_REF
Definition: TensorMacros.h:50
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlockResourceRequirements any()
Definition: TensorBlock.h:155
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc &desc, TensorBlockScratch &scratch, bool=false) const
Eigen::internal::traits< XprType >::PointerType TensorPointerType


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:36:59