Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorEvalTo.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
0011 #define EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
0012 
0013 namespace Eigen {
0014 
0015 /** \class TensorForcedEval
0016   * \ingroup CXX11_Tensor_Module
0017   *
0018   * \brief Tensor reshaping class.
0019   *
0020   *
0021   */
0022 namespace internal {
0023 template<typename XprType, template <class> class MakePointer_>
0024 struct traits<TensorEvalToOp<XprType, MakePointer_> >
0025 {
0026   // Type promotion to handle the case where the types of the lhs and the rhs are different.
0027   typedef typename XprType::Scalar Scalar;
0028   typedef traits<XprType> XprTraits;
0029   typedef typename XprTraits::StorageKind StorageKind;
0030   typedef typename XprTraits::Index Index;
0031   typedef typename XprType::Nested Nested;
0032   typedef typename remove_reference<Nested>::type _Nested;
0033   static const int NumDimensions = XprTraits::NumDimensions;
0034   static const int Layout = XprTraits::Layout;
0035   typedef typename MakePointer_<Scalar>::Type PointerType;
0036 
0037   enum {
0038     Flags = 0
0039   };
0040   template <class T>
0041   struct MakePointer {
0042     // Intermediate typedef to workaround MSVC issue.
0043     typedef MakePointer_<T> MakePointerT;
0044     typedef typename MakePointerT::Type Type;
0045 
0046 
0047   };
0048 };
0049 
0050 template<typename XprType, template <class> class MakePointer_>
0051 struct eval<TensorEvalToOp<XprType, MakePointer_>, Eigen::Dense>
0052 {
0053   typedef const TensorEvalToOp<XprType, MakePointer_>& type;
0054 };
0055 
0056 template<typename XprType, template <class> class MakePointer_>
0057 struct nested<TensorEvalToOp<XprType, MakePointer_>, 1, typename eval<TensorEvalToOp<XprType, MakePointer_> >::type>
0058 {
0059   typedef TensorEvalToOp<XprType, MakePointer_> type;
0060 };
0061 
0062 }  // end namespace internal
0063 
0064 
0065 
0066 
0067 template<typename XprType, template <class> class MakePointer_>
0068 class TensorEvalToOp : public TensorBase<TensorEvalToOp<XprType, MakePointer_>, ReadOnlyAccessors>
0069 {
0070   public:
0071   typedef typename Eigen::internal::traits<TensorEvalToOp>::Scalar Scalar;
0072   typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
0073   typedef typename internal::remove_const<typename XprType::CoeffReturnType>::type CoeffReturnType;
0074   typedef typename MakePointer_<CoeffReturnType>::Type PointerType;
0075   typedef typename Eigen::internal::nested<TensorEvalToOp>::type Nested;
0076   typedef typename Eigen::internal::traits<TensorEvalToOp>::StorageKind StorageKind;
0077   typedef typename Eigen::internal::traits<TensorEvalToOp>::Index Index;
0078 
0079   static const int NumDims = Eigen::internal::traits<TensorEvalToOp>::NumDimensions;
0080 
0081   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvalToOp(PointerType buffer, const XprType& expr)
0082       : m_xpr(expr), m_buffer(buffer) {}
0083 
0084     EIGEN_DEVICE_FUNC
0085     const typename internal::remove_all<typename XprType::Nested>::type&
0086     expression() const { return m_xpr; }
0087 
0088     EIGEN_DEVICE_FUNC PointerType buffer() const { return m_buffer; }
0089 
0090   protected:
0091     typename XprType::Nested m_xpr;
0092     PointerType m_buffer;
0093 };
0094 
0095 
0096 
0097 template<typename ArgType, typename Device, template <class> class MakePointer_>
0098 struct TensorEvaluator<const TensorEvalToOp<ArgType, MakePointer_>, Device>
0099 {
0100   typedef TensorEvalToOp<ArgType, MakePointer_> XprType;
0101   typedef typename ArgType::Scalar Scalar;
0102   typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
0103   typedef typename XprType::Index Index;
0104   typedef typename internal::remove_const<typename XprType::CoeffReturnType>::type CoeffReturnType;
0105   typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
0106   static const int PacketSize = PacketType<CoeffReturnType, Device>::size;
0107   typedef typename Eigen::internal::traits<XprType>::PointerType TensorPointerType;
0108   typedef StorageMemory<CoeffReturnType, Device> Storage;
0109   typedef typename Storage::Type EvaluatorPointerType;
0110   enum {
0111     IsAligned         = TensorEvaluator<ArgType, Device>::IsAligned,
0112     PacketAccess      = TensorEvaluator<ArgType, Device>::PacketAccess,
0113     BlockAccess       = true,
0114     PreferBlockAccess = false,
0115     Layout            = TensorEvaluator<ArgType, Device>::Layout,
0116     CoordAccess       = false,  // to be implemented
0117     RawAccess         = true
0118   };
0119 
0120   static const int NumDims = internal::traits<ArgType>::NumDimensions;
0121 
0122   //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
0123   typedef internal::TensorBlockDescriptor<NumDims, Index> TensorBlockDesc;
0124   typedef internal::TensorBlockScratchAllocator<Device> TensorBlockScratch;
0125 
0126   typedef typename TensorEvaluator<const ArgType, Device>::TensorBlock
0127       ArgTensorBlock;
0128 
0129   typedef internal::TensorBlockAssignment<
0130       CoeffReturnType, NumDims, typename ArgTensorBlock::XprType, Index>
0131       TensorBlockAssignment;
0132   //===--------------------------------------------------------------------===//
0133 
0134   EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
0135       : m_impl(op.expression(), device), m_buffer(device.get(op.buffer())), m_expression(op.expression()){}
0136 
0137 
0138   EIGEN_STRONG_INLINE ~TensorEvaluator() {
0139   }
0140 
0141 
0142   EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
0143 
0144   EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType scalar) {
0145     EIGEN_UNUSED_VARIABLE(scalar);
0146     eigen_assert(scalar == NULL);
0147     return m_impl.evalSubExprsIfNeeded(m_buffer);
0148   }
0149 
0150 #ifdef EIGEN_USE_THREADS
0151   template <typename EvalSubExprsCallback>
0152   EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(
0153       EvaluatorPointerType scalar, EvalSubExprsCallback done) {
0154     EIGEN_UNUSED_VARIABLE(scalar);
0155     eigen_assert(scalar == NULL);
0156     m_impl.evalSubExprsIfNeededAsync(m_buffer, std::move(done));
0157   }
0158 #endif
0159 
0160   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) {
0161     m_buffer[i] = m_impl.coeff(i);
0162   }
0163   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) {
0164     internal::pstoret<CoeffReturnType, PacketReturnType, Aligned>(m_buffer + i, m_impl.template packet<TensorEvaluator<ArgType, Device>::IsAligned ? Aligned : Unaligned>(i));
0165   }
0166 
0167   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0168   internal::TensorBlockResourceRequirements getResourceRequirements() const {
0169     return m_impl.getResourceRequirements();
0170   }
0171 
0172   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalBlock(
0173       TensorBlockDesc& desc, TensorBlockScratch& scratch) {
0174     // Add `m_buffer` as destination buffer to the block descriptor.
0175     desc.template AddDestinationBuffer<Layout>(
0176         /*dst_base=*/m_buffer + desc.offset(),
0177         /*dst_strides=*/internal::strides<Layout>(m_impl.dimensions()));
0178 
0179     ArgTensorBlock block =
0180         m_impl.block(desc, scratch, /*root_of_expr_ast=*/true);
0181 
0182     // If block was evaluated into a destination buffer, there is no need to do
0183     // an assignment.
0184     if (block.kind() != internal::TensorBlockKind::kMaterializedInOutput) {
0185       TensorBlockAssignment::Run(
0186           TensorBlockAssignment::target(
0187               desc.dimensions(), internal::strides<Layout>(m_impl.dimensions()),
0188               m_buffer, desc.offset()),
0189           block.expr());
0190     }
0191     block.cleanup();
0192   }
0193 
0194   EIGEN_STRONG_INLINE void cleanup() {
0195     m_impl.cleanup();
0196   }
0197 
0198   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
0199   {
0200     return m_buffer[index];
0201   }
0202 
0203   template<int LoadMode>
0204   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
0205   {
0206     return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
0207   }
0208 
0209   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
0210     // We assume that evalPacket or evalScalar is called to perform the
0211     // assignment and account for the cost of the write here.
0212     return m_impl.costPerCoeff(vectorized) +
0213         TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
0214   }
0215 
0216   EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_buffer; }
0217   ArgType expression() const { return m_expression; }
0218   #ifdef EIGEN_USE_SYCL
0219   // binding placeholder accessors to a command group handler for SYCL
0220   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void bind(cl::sycl::handler &cgh) const {
0221     m_impl.bind(cgh);
0222     m_buffer.bind(cgh);
0223   }
0224   #endif
0225 
0226 
0227  private:
0228   TensorEvaluator<ArgType, Device> m_impl;
0229   EvaluatorPointerType m_buffer;
0230   const ArgType m_expression;
0231 };
0232 
0233 
0234 } // end namespace Eigen
0235 
0236 #endif // EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H