blob: 934c5409e7dc97f325ce9d2baf9e339369c4b6c7 [file]
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
//
// 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/.
// SPDX-License-Identifier: MPL-2.0
#ifndef EIGEN_TENSOR_TENSOR_EVAL_TO_H
#define EIGEN_TENSOR_TENSOR_EVAL_TO_H
// IWYU pragma: private
#include "./InternalHeaderCheck.h"
namespace Eigen {
namespace internal {
template <typename XprType, template <class> class MakePointer_>
struct traits<TensorEvalToOp<XprType, MakePointer_> > {
using Scalar = typename XprType::Scalar;
using XprTraits = traits<XprType>;
using StorageKind = typename XprTraits::StorageKind;
using Index = typename XprTraits::Index;
static constexpr int NumDimensions = XprTraits::NumDimensions;
static constexpr int Layout = XprTraits::Layout;
using PointerType = typename MakePointer_<Scalar>::Type;
enum { Flags = 0 };
template <class T>
struct MakePointer {
using Type = typename MakePointer_<T>::Type;
};
};
template <typename XprType, template <class> class MakePointer_>
struct eval<TensorEvalToOp<XprType, MakePointer_>, Eigen::Dense> {
using type = const TensorEvalToOp<XprType, MakePointer_>&;
};
} // end namespace internal
template <typename XprType, template <class> class MakePointer_>
class TensorEvalToOp : public TensorBase<TensorEvalToOp<XprType, MakePointer_>, ReadOnlyAccessors> {
public:
using Scalar = typename Eigen::internal::traits<TensorEvalToOp>::Scalar;
using RealScalar = typename Eigen::NumTraits<Scalar>::Real;
using CoeffReturnType = std::remove_const_t<typename XprType::CoeffReturnType>;
using PointerType = typename MakePointer_<CoeffReturnType>::Type;
using Nested = typename Eigen::internal::ref_selector<TensorEvalToOp>::type;
using StorageKind = typename Eigen::internal::traits<TensorEvalToOp>::StorageKind;
using Index = typename Eigen::internal::traits<TensorEvalToOp>::Index;
static constexpr int NumDims = Eigen::internal::traits<TensorEvalToOp>::NumDimensions;
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvalToOp(PointerType buffer, const XprType& expr)
: m_xpr(expr), m_buffer(buffer) {}
EIGEN_DEVICE_FUNC const internal::remove_all_t<typename XprType::Nested>& expression() const { return m_xpr; }
EIGEN_DEVICE_FUNC PointerType buffer() const { return m_buffer; }
protected:
typename XprType::Nested m_xpr;
PointerType m_buffer;
};
template <typename ArgType, typename Device, template <class> class MakePointer_>
struct TensorEvaluator<const TensorEvalToOp<ArgType, MakePointer_>, Device> {
using XprType = TensorEvalToOp<ArgType, MakePointer_>;
using Scalar = typename ArgType::Scalar;
using Dimensions = typename TensorEvaluator<ArgType, Device>::Dimensions;
using Index = typename XprType::Index;
using CoeffReturnType = std::remove_const_t<typename XprType::CoeffReturnType>;
using PacketReturnType = typename PacketType<CoeffReturnType, Device>::type;
static constexpr int PacketSize = PacketType<CoeffReturnType, Device>::size;
using TensorPointerType = typename Eigen::internal::traits<XprType>::PointerType;
using Storage = StorageMemory<CoeffReturnType, Device>;
using EvaluatorPointerType = typename Storage::Type;
enum {
IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
BlockAccess = true,
PreferBlockAccess = false,
CoordAccess = false, // to be implemented
RawAccess = true
};
static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
static constexpr int NumDims = internal::traits<ArgType>::NumDimensions;
//===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
using TensorBlockDesc = internal::TensorBlockDescriptor<NumDims, Index>;
using TensorBlockScratch = internal::TensorBlockScratchAllocator<Device>;
using ArgTensorBlock = typename TensorEvaluator<const ArgType, Device>::TensorBlock;
using TensorBlockAssignment =
internal::TensorBlockAssignment<CoeffReturnType, NumDims, typename ArgTensorBlock::XprType, Index>;
//===--------------------------------------------------------------------===//
EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
: m_impl(op.expression(), device), m_buffer(device.get(op.buffer())), m_expression(op.expression()) {}
EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(EvaluatorPointerType scalar) {
EIGEN_UNUSED_VARIABLE(scalar);
eigen_assert(scalar == nullptr);
return m_impl.evalSubExprsIfNeeded(m_buffer);
}
#ifdef EIGEN_USE_THREADS
template <typename EvalSubExprsCallback>
EIGEN_STRONG_INLINE void evalSubExprsIfNeededAsync(EvaluatorPointerType scalar, EvalSubExprsCallback done) {
EIGEN_UNUSED_VARIABLE(scalar);
eigen_assert(scalar == nullptr);
m_impl.evalSubExprsIfNeededAsync(m_buffer, std::move(done));
}
#endif
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) const { m_buffer[i] = m_impl.coeff(i); }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) const {
internal::pstoret<CoeffReturnType, PacketReturnType, Aligned>(
m_buffer + i, m_impl.template packet < TensorEvaluator<ArgType, Device>::IsAligned ? Aligned : Unaligned > (i));
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements() const {
return m_impl.getResourceRequirements();
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalBlock(TensorBlockDesc& desc, TensorBlockScratch& scratch) {
// Add `m_buffer` as destination buffer to the block descriptor.
desc.template AddDestinationBuffer<Layout>(
/*dst_base=*/m_buffer + desc.offset(),
/*dst_strides=*/internal::strides<Layout>(m_impl.dimensions()));
ArgTensorBlock block = m_impl.block(desc, scratch, /*root_of_expr_ast=*/true);
// If block was evaluated into a destination buffer, there is no need to do
// an assignment.
if (block.kind() != internal::TensorBlockKind::kMaterializedInOutput) {
TensorBlockAssignment::Run(
TensorBlockAssignment::target(desc.dimensions(), internal::strides<Layout>(m_impl.dimensions()), m_buffer,
desc.offset()),
block.expr());
}
block.cleanup();
}
EIGEN_STRONG_INLINE void cleanup() { m_impl.cleanup(); }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const { return m_buffer[index]; }
template <int LoadMode>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const {
return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
}
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
// We assume that evalPacket or evalScalar is called to perform the
// assignment and account for the cost of the write here.
return m_impl.costPerCoeff(vectorized) + TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
}
EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_buffer; }
ArgType expression() const { return m_expression; }
private:
TensorEvaluator<ArgType, Device> m_impl;
EvaluatorPointerType m_buffer;
const ArgType m_expression;
};
} // end namespace Eigen
#endif // EIGEN_TENSOR_TENSOR_EVAL_TO_H