// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2014 Benoit Steiner // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. #ifndef EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H #define EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H // IWYU pragma: private #include "./InternalHeaderCheck.h" #include namespace Eigen { namespace internal { template struct traits> { // Type promotion to handle the case where the types of the lhs and the rhs are different. typedef typename XprType::Scalar Scalar; typedef traits XprTraits; typedef typename traits::StorageKind StorageKind; typedef typename traits::Index Index; typedef typename XprType::Nested Nested; typedef std::remove_reference_t Nested_; static constexpr int NumDimensions = XprTraits::NumDimensions; static constexpr int Layout = XprTraits::Layout; typedef typename XprTraits::PointerType PointerType; enum { Flags = 0 }; }; template struct eval, Eigen::Dense> { typedef const TensorForcedEvalOp& type; }; template struct nested, 1, typename eval>::type> { typedef TensorForcedEvalOp type; }; } // end namespace internal /** * \ingroup CXX11_Tensor_Module * * \brief Tensor reshaping class. */ template class TensorForcedEvalOp : public TensorBase, ReadOnlyAccessors> { public: typedef typename Eigen::internal::traits::Scalar Scalar; typedef typename Eigen::NumTraits::Real RealScalar; typedef std::remove_const_t CoeffReturnType; typedef typename Eigen::internal::nested::type Nested; typedef typename Eigen::internal::traits::StorageKind StorageKind; typedef typename Eigen::internal::traits::Index Index; EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorForcedEvalOp(const XprType& expr) : m_xpr(expr) {} EIGEN_DEVICE_FUNC const internal::remove_all_t& expression() const { return m_xpr; } protected: typename XprType::Nested m_xpr; }; namespace internal { template struct non_integral_type_placement_new { template EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void operator()(Index numValues, StorageType m_buffer) { // Initialize non-trivially constructible types. if (!internal::is_arithmetic::value) { for (Index i = 0; i < numValues; ++i) new (m_buffer + i) CoeffReturnType(); } } }; // SYCL does not support non-integral types // having new (m_buffer + i) CoeffReturnType() causes the following compiler error for SYCL Devices // no matching function for call to 'operator new' template struct non_integral_type_placement_new { template EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void operator()(Index, StorageType) {} }; } // end namespace internal template class DeviceTempPointerHolder { public: DeviceTempPointerHolder(const Device& device, size_t size) : device_(device), size_(size), ptr_(device.allocate_temp(size)) {} ~DeviceTempPointerHolder() { device_.deallocate_temp(ptr_); size_ = 0; ptr_ = nullptr; } void* ptr() { return ptr_; } private: Device device_; size_t size_; void* ptr_; }; template struct TensorEvaluator, Device> { typedef const internal::remove_all_t ArgType; typedef TensorForcedEvalOp XprType; typedef typename ArgType::Scalar Scalar; typedef typename TensorEvaluator::Dimensions Dimensions; typedef typename XprType::Index Index; typedef typename XprType::CoeffReturnType CoeffReturnType; typedef typename PacketType::type PacketReturnType; static constexpr int PacketSize = PacketType::size; typedef typename Eigen::internal::traits::PointerType TensorPointerType; typedef StorageMemory Storage; typedef typename Storage::Type EvaluatorPointerType; enum { IsAligned = true, PacketAccess = (PacketType::size > 1), BlockAccess = internal::is_arithmetic::value, PreferBlockAccess = false, RawAccess = true }; static constexpr int Layout = TensorEvaluator::Layout; static constexpr int NumDims = internal::traits::NumDimensions; //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===// typedef internal::TensorBlockDescriptor TensorBlockDesc; typedef internal::TensorBlockScratchAllocator TensorBlockScratch; typedef typename internal::TensorMaterializedBlock TensorBlock; //===--------------------------------------------------------------------===// TensorEvaluator(const XprType& op, const Device& device) : m_impl(op.expression(), device), m_op(op.expression()), m_device(device), m_buffer_holder(nullptr), m_buffer(nullptr) {} ~TensorEvaluator() { cleanup(); } EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); } EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType) { const Index numValues = internal::array_prod(m_impl.dimensions()); m_buffer_holder = std::make_shared>(m_device, numValues * sizeof(CoeffReturnType)); m_buffer = static_cast(m_buffer_holder->ptr()); internal::non_integral_type_placement_new()(numValues, m_buffer); typedef TensorEvalToOp> EvalTo; EvalTo evalToTmp(m_device.get(m_buffer), m_op); internal::TensorExecutor, /*Vectorizable=*/internal::IsVectorizable::value, /*Tiling=*/internal::IsTileable::value>::run(evalToTmp, m_device); return true; } #ifdef EIGEN_USE_THREADS template EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(EvaluatorPointerType, EvalSubExprsCallback done) { const Index numValues = internal::array_prod(m_impl.dimensions()); m_buffer_holder = std::make_shared>(m_device, numValues * sizeof(CoeffReturnType)); m_buffer = static_cast(m_buffer_holder->ptr()); typedef TensorEvalToOp> EvalTo; EvalTo evalToTmp(m_device.get(m_buffer), m_op); auto on_done = std::bind([](EvalSubExprsCallback done_) { done_(true); }, std::move(done)); internal::TensorAsyncExecutor< const EvalTo, std::remove_const_t, decltype(on_done), /*Vectorizable=*/internal::IsVectorizable::value, /*Tiling=*/internal::IsTileable::value>::runAsync(evalToTmp, m_device, std::move(on_done)); } #endif EIGEN_STRONG_INLINE void cleanup() { m_buffer_holder = nullptr; m_buffer = nullptr; } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_buffer[index]; } template EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const { return internal::ploadt(m_buffer + index); } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const { return internal::TensorBlockResourceRequirements::any(); } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch, bool /*root_of_expr_ast*/ = false) const { eigen_assert(m_buffer != nullptr); return TensorBlock::materialize(m_buffer, m_impl.dimensions(), desc, scratch); } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const { return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize); } EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE EvaluatorPointerType data() const { return m_buffer; } private: TensorEvaluator m_impl; const ArgType m_op; const Device EIGEN_DEVICE_REF m_device; std::shared_ptr> m_buffer_holder; EvaluatorPointerType m_buffer; // Cached copy of the value stored in m_buffer_holder. }; } // end namespace Eigen #endif // EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H