Loading...
Searching...
No Matches
TensorForcedEval.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_FORCED_EVAL_H
11#define EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H
12
13namespace Eigen {
14
15namespace internal {
16template<typename XprType, template <class> class MakePointer_>
17struct traits<TensorForcedEvalOp<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 traits<XprType>::StorageKind StorageKind;
23 typedef typename traits<XprType>::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> struct MakePointer {
33 // Intermediate typedef to workaround MSVC issue.
34 typedef MakePointer_<T> MakePointerT;
35 typedef typename MakePointerT::Type Type;
36 };
37};
38
39template<typename XprType, template <class> class MakePointer_>
40struct eval<TensorForcedEvalOp<XprType, MakePointer_>, Eigen::Dense>
41{
42 typedef const TensorForcedEvalOp<XprType, MakePointer_>& type;
43};
44
45template<typename XprType, template <class> class MakePointer_>
46struct nested<TensorForcedEvalOp<XprType, MakePointer_>, 1, typename eval<TensorForcedEvalOp<XprType, MakePointer_> >::type>
47{
48 typedef TensorForcedEvalOp<XprType, MakePointer_> type;
49};
50
51} // end namespace internal
52
53
54
55// FIXME use proper doxygen documentation (e.g. \tparam MakePointer_)
56
63
71template<typename XprType, template <class> class MakePointer_>
72class TensorForcedEvalOp : public TensorBase<TensorForcedEvalOp<XprType, MakePointer_>, ReadOnlyAccessors>
73{
74 public:
75 typedef typename Eigen::internal::traits<TensorForcedEvalOp>::Scalar Scalar;
76 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
77 typedef typename internal::remove_const<typename XprType::CoeffReturnType>::type CoeffReturnType;
78 typedef typename Eigen::internal::nested<TensorForcedEvalOp>::type Nested;
79 typedef typename Eigen::internal::traits<TensorForcedEvalOp>::StorageKind StorageKind;
80 typedef typename Eigen::internal::traits<TensorForcedEvalOp>::Index Index;
81
82 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorForcedEvalOp(const XprType& expr)
83 : m_xpr(expr) {}
84
85 EIGEN_DEVICE_FUNC
86 const typename internal::remove_all<typename XprType::Nested>::type&
87 expression() const { return m_xpr; }
88
89 protected:
90 typename XprType::Nested m_xpr;
91};
92
93
94template<typename ArgType, typename Device, template <class> class MakePointer_>
95struct TensorEvaluator<const TensorForcedEvalOp<ArgType, MakePointer_>, Device>
96{
98 typedef typename ArgType::Scalar Scalar;
99 typedef typename TensorEvaluator<ArgType, Device>::Dimensions Dimensions;
100 typedef typename XprType::Index Index;
101 typedef typename XprType::CoeffReturnType CoeffReturnType;
102 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
103 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
104
105 enum {
106 IsAligned = true,
107 PacketAccess = (PacketSize > 1),
108 Layout = TensorEvaluator<ArgType, Device>::Layout,
109 RawAccess = true
110 };
111
112 EIGEN_DEVICE_FUNC TensorEvaluator(const XprType& op, const Device& device)
114 : m_impl(op.expression(), device), m_op(op.expression()), m_device(device), m_buffer(NULL)
115 { }
116
117 EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
118
119 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(CoeffReturnType*) {
120 const Index numValues = internal::array_prod(m_impl.dimensions());
121 m_buffer = (CoeffReturnType*)m_device.allocate(numValues * sizeof(CoeffReturnType));
122 // Should initialize the memory in case we're dealing with non POD types.
123 if (NumTraits<CoeffReturnType>::RequireInitialization) {
124 for (Index i = 0; i < numValues; ++i) {
125 new(m_buffer+i) CoeffReturnType();
126 }
127 }
128 typedef TensorEvalToOp< const typename internal::remove_const<ArgType>::type > EvalTo;
129 EvalTo evalToTmp(m_buffer, m_op);
130 const bool PacketAccess = internal::IsVectorizable<Device, const ArgType>::value;
131 internal::TensorExecutor<const EvalTo, typename internal::remove_const<Device>::type, PacketAccess>::run(evalToTmp, m_device);
132 return true;
133 }
134 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
135 m_device.deallocate(m_buffer);
136 m_buffer = NULL;
137 }
138
139 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
140 {
141 return m_buffer[index];
142 }
143
144 template<int LoadMode>
145 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
146 {
147 return internal::ploadt<PacketReturnType, LoadMode>(m_buffer + index);
148 }
149
150 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
151 return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
152 }
153
154 EIGEN_DEVICE_FUNC typename MakePointer<Scalar>::Type data() const { return m_buffer; }
155
157 const TensorEvaluator<ArgType, Device>& impl() { return m_impl; }
159 const Device& device() const{return m_device;}
160 private:
161 TensorEvaluator<ArgType, Device> m_impl;
162 const ArgType m_op;
163 const Device& m_device;
164 typename MakePointer<CoeffReturnType>::Type m_buffer;
165};
166
167
168} // end namespace Eigen
169
170#endif // EIGEN_CXX11_TENSOR_TENSOR_FORCED_EVAL_H
The tensor base class.
Definition TensorForwardDeclarations.h:29
Tensor reshaping class.
Definition TensorForcedEval.h:73
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