Loading...
Searching...
No Matches
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
13namespace Eigen {
14
15namespace internal {
16template<typename XprType, template <class> class MakePointer_>
17struct traits<TensorEvalToOp<XprType, MakePointer_> >
18{
19 // Type promotion to handle the case where the types of the lhs and the rhs are different.
20 typedef typename XprType::Scalar Scalar;
21 typedef traits<XprType> XprTraits;
22 typedef typename XprTraits::StorageKind StorageKind;
23 typedef typename XprTraits::Index Index;
24 typedef typename XprType::Nested Nested;
25 typedef typename remove_reference<Nested>::type _Nested;
26 static const int NumDimensions = XprTraits::NumDimensions;
27 static const int Layout = XprTraits::Layout;
28
29 enum {
30 Flags = 0
31 };
32 template <class T>
33 struct MakePointer {
34 // Intermediate typedef to workaround MSVC issue.
35 typedef MakePointer_<T> MakePointerT;
36 typedef typename MakePointerT::Type Type;
37 };
38};
39
40template<typename XprType, template <class> class MakePointer_>
41struct eval<TensorEvalToOp<XprType, MakePointer_>, Eigen::Dense>
42{
43 typedef const TensorEvalToOp<XprType, MakePointer_>& type;
44};
45
46template<typename XprType, template <class> class MakePointer_>
47struct nested<TensorEvalToOp<XprType, MakePointer_>, 1, typename eval<TensorEvalToOp<XprType, MakePointer_> >::type>
48{
49 typedef TensorEvalToOp<XprType, MakePointer_> type;
50};
51
52} // end namespace internal
53
54
55
56
57template<typename XprType, template <class> class MakePointer_>
58class TensorEvalToOp : public TensorBase<TensorEvalToOp<XprType, MakePointer_>, ReadOnlyAccessors>
59{
60 public:
61 typedef typename Eigen::internal::traits<TensorEvalToOp>::Scalar Scalar;
62 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
63 typedef typename internal::remove_const<typename XprType::CoeffReturnType>::type CoeffReturnType;
64 typedef typename MakePointer_<CoeffReturnType>::Type PointerType;
65 typedef typename Eigen::internal::nested<TensorEvalToOp>::type Nested;
66 typedef typename Eigen::internal::traits<TensorEvalToOp>::StorageKind StorageKind;
67 typedef typename Eigen::internal::traits<TensorEvalToOp>::Index Index;
68
69 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvalToOp(PointerType buffer, const XprType& expr)
70 : m_xpr(expr), m_buffer(buffer) {}
71
72 EIGEN_DEVICE_FUNC
73 const typename internal::remove_all<typename XprType::Nested>::type&
74 expression() const { return m_xpr; }
75
76 EIGEN_DEVICE_FUNC PointerType buffer() const { return m_buffer; }
77
78 protected:
79 typename XprType::Nested m_xpr;
80 PointerType m_buffer;
81};
82
83
84
85template<typename ArgType, typename Device, template <class> class MakePointer_>
86struct TensorEvaluator<const TensorEvalToOp<ArgType, MakePointer_>, Device>
87{
88 typedef TensorEvalToOp<ArgType, MakePointer_> XprType;
89 typedef typename ArgType::Scalar Scalar;
90 typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
91 typedef typename XprType::Index Index;
92 typedef typename internal::remove_const<typename XprType::CoeffReturnType>::type CoeffReturnType;
93 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
94 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
95
96 enum {
97 IsAligned = TensorEvaluator<ArgType, Device>::IsAligned,
98 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
99 Layout = TensorEvaluator<ArgType, Device>::Layout,
100 CoordAccess = false, // to be implemented
101 RawAccess = true
102 };
103
104 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
105 : m_impl(op.expression(), device), m_device(device),
106 m_buffer(op.buffer()), m_op(op), m_expression(op.expression())
107 { }
108
109 // Used for accessor extraction in SYCL Managed TensorMap:
110 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const XprType& op() const {
111 return m_op;
112 }
113
114 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ~TensorEvaluator() {
115 }
116
117 typedef typename internal::traits<const TensorEvalToOp<ArgType, MakePointer_> >::template MakePointer<CoeffReturnType>::Type DevicePointer;
118 EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
119
120 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(DevicePointer scalar) {
121 EIGEN_UNUSED_VARIABLE(scalar);
122 eigen_assert(scalar == NULL);
123 return m_impl.evalSubExprsIfNeeded(m_buffer);
124 }
125
126 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalScalar(Index i) {
127 m_buffer[i] = m_impl.coeff(i);
128 }
129 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void evalPacket(Index i) {
130 internal::pstoret<CoeffReturnType, PacketReturnType, Aligned>(m_buffer + i, m_impl.template packet<TensorEvaluator<ArgType, Device>::IsAligned ? Aligned : Unaligned>(i));
131 }
132
133 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
134 m_impl.cleanup();
135 }
136
137 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
138 {
139 return m_buffer[index];
140 }
141
142 template<int LoadMode>
143 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
144 {
145 return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
146 }
147
148 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
149 // We assume that evalPacket or evalScalar is called to perform the
150 // assignment and account for the cost of the write here.
151 return m_impl.costPerCoeff(vectorized) +
152 TensorOpCost(0, sizeof(CoeffReturnType), 0, vectorized, PacketSize);
153 }
154
155 EIGEN_DEVICE_FUNC DevicePointer data() const { return m_buffer; }
156 ArgType expression() const { return m_expression; }
157
159 const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
161 const Device& device() const{return m_device;}
162
163 private:
164 TensorEvaluator<ArgType, Device> m_impl;
165 const Device& m_device;
166 DevicePointer m_buffer;
167 const XprType& m_op;
168 const ArgType m_expression;
169};
170
171
172} // end namespace Eigen
173
174#endif // EIGEN_CXX11_TENSOR_TENSOR_EVAL_TO_H
The tensor base class.
Definition TensorForwardDeclarations.h:29
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The tensor evaluator class.
Definition TensorEvaluator.h:27
const Device & device() const
required by sycl in order to construct sycl buffer from raw pointer
Definition TensorEvaluator.h:112