Loading...
Searching...
No Matches
TensorChipping.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_CHIPPING_H
11#define EIGEN_CXX11_TENSOR_TENSOR_CHIPPING_H
12
13namespace Eigen {
14
15namespace internal {
16template<DenseIndex DimId, typename XprType>
17struct traits<TensorChippingOp<DimId, XprType> > : public traits<XprType>
18{
19 typedef typename XprType::Scalar Scalar;
20 typedef traits<XprType> XprTraits;
21 typedef typename XprTraits::StorageKind StorageKind;
22 typedef typename XprTraits::Index Index;
23 typedef typename XprType::Nested Nested;
24 typedef typename remove_reference<Nested>::type _Nested;
25 static const int NumDimensions = XprTraits::NumDimensions - 1;
26 static const int Layout = XprTraits::Layout;
27};
28
29template<DenseIndex DimId, typename XprType>
30struct eval<TensorChippingOp<DimId, XprType>, Eigen::Dense>
31{
32 typedef const TensorChippingOp<DimId, XprType>& type;
33};
34
35template<DenseIndex DimId, typename XprType>
36struct nested<TensorChippingOp<DimId, XprType>, 1, typename eval<TensorChippingOp<DimId, XprType> >::type>
37{
38 typedef TensorChippingOp<DimId, XprType> type;
39};
40
41template <DenseIndex DimId>
42struct DimensionId
43{
44 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DimensionId(DenseIndex dim) {
45 EIGEN_ONLY_USED_FOR_DEBUG(dim);
46 eigen_assert(dim == DimId);
47 }
48 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex actualDim() const {
49 return DimId;
50 }
51};
52template <>
53struct DimensionId<Dynamic>
54{
55 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DimensionId(DenseIndex dim) : actual_dim(dim) {
56 eigen_assert(dim >= 0);
57 }
58 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex actualDim() const {
59 return actual_dim;
60 }
61 private:
62 const DenseIndex actual_dim;
63};
64
65
66} // end namespace internal
67
71template <DenseIndex DimId, typename XprType>
72class TensorChippingOp : public TensorBase<TensorChippingOp<DimId, XprType> > {
73 public:
74 typedef typename Eigen::internal::traits<TensorChippingOp>::Scalar Scalar;
75 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
76 typedef typename XprType::CoeffReturnType CoeffReturnType;
77 typedef typename Eigen::internal::nested<TensorChippingOp>::type Nested;
78 typedef typename Eigen::internal::traits<TensorChippingOp>::StorageKind StorageKind;
79 typedef typename Eigen::internal::traits<TensorChippingOp>::Index Index;
80
81 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorChippingOp(const XprType& expr, const Index offset, const Index dim)
82 : m_xpr(expr), m_offset(offset), m_dim(dim) {
83 }
84
85 EIGEN_DEVICE_FUNC
86 const Index offset() const { return m_offset; }
87 EIGEN_DEVICE_FUNC
88 const Index dim() const { return m_dim.actualDim(); }
89
90 EIGEN_DEVICE_FUNC
91 const typename internal::remove_all<typename XprType::Nested>::type&
92 expression() const { return m_xpr; }
93
94 EIGEN_DEVICE_FUNC
95 EIGEN_STRONG_INLINE TensorChippingOp& operator = (const TensorChippingOp& other)
96 {
98 Assign assign(*this, other);
99 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
100 return *this;
101 }
102
103 template<typename OtherDerived>
104 EIGEN_DEVICE_FUNC
105 EIGEN_STRONG_INLINE TensorChippingOp& operator = (const OtherDerived& other)
106 {
108 Assign assign(*this, other);
109 internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
110 return *this;
111 }
112
113 protected:
114 typename XprType::Nested m_xpr;
115 const Index m_offset;
116 const internal::DimensionId<DimId> m_dim;
117};
118
119
120// Eval as rvalue
121template<DenseIndex DimId, typename ArgType, typename Device>
122struct TensorEvaluator<const TensorChippingOp<DimId, ArgType>, Device>
123{
124 typedef TensorChippingOp<DimId, ArgType> XprType;
125 static const int NumInputDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
126 static const int NumDims = NumInputDims-1;
127 typedef typename XprType::Index Index;
128 typedef DSizes<Index, NumDims> Dimensions;
129 typedef typename XprType::Scalar Scalar;
130 typedef typename XprType::CoeffReturnType CoeffReturnType;
131 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
132 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
133
134
135 enum {
136 // Alignment can't be guaranteed at compile time since it depends on the
137 // slice offsets.
138 IsAligned = false,
139 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
140 Layout = TensorEvaluator<ArgType, Device>::Layout,
141 CoordAccess = false, // to be implemented
142 RawAccess = false
143 };
144
145 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
146 : m_impl(op.expression(), device), m_dim(op.dim()), m_device(device)
147 {
148 EIGEN_STATIC_ASSERT((NumInputDims >= 1), YOU_MADE_A_PROGRAMMING_MISTAKE);
149 eigen_assert(NumInputDims > m_dim.actualDim());
150
151 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
152 eigen_assert(op.offset() < input_dims[m_dim.actualDim()]);
153
154 int j = 0;
155 for (int i = 0; i < NumInputDims; ++i) {
156 if (i != m_dim.actualDim()) {
157 m_dimensions[j] = input_dims[i];
158 ++j;
159 }
160 }
161
162 m_stride = 1;
163 m_inputStride = 1;
164 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
165 for (int i = 0; i < m_dim.actualDim(); ++i) {
166 m_stride *= input_dims[i];
167 m_inputStride *= input_dims[i];
168 }
169 } else {
170 for (int i = NumInputDims-1; i > m_dim.actualDim(); --i) {
171 m_stride *= input_dims[i];
172 m_inputStride *= input_dims[i];
173 }
174 }
175 m_inputStride *= input_dims[m_dim.actualDim()];
176 m_inputOffset = m_stride * op.offset();
177 }
178
179 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
180
181 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
182 m_impl.evalSubExprsIfNeeded(NULL);
183 return true;
184 }
185
186 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
187 m_impl.cleanup();
188 }
189
190 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
191 {
192 return m_impl.coeff(srcCoeff(index));
193 }
194
195 template<int LoadMode>
196 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
197 {
198 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
199 eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
200
201 if ((static_cast<int>(Layout) == static_cast<int>(ColMajor) && m_dim.actualDim() == 0) ||
202 (static_cast<int>(Layout) == static_cast<int>(RowMajor) && m_dim.actualDim() == NumInputDims-1)) {
203 // m_stride is equal to 1, so let's avoid the integer division.
204 eigen_assert(m_stride == 1);
205 Index inputIndex = index * m_inputStride + m_inputOffset;
206 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
207 for (int i = 0; i < PacketSize; ++i) {
208 values[i] = m_impl.coeff(inputIndex);
209 inputIndex += m_inputStride;
210 }
211 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
212 return rslt;
213 } else if ((static_cast<int>(Layout) == static_cast<int>(ColMajor) && m_dim.actualDim() == NumInputDims - 1) ||
214 (static_cast<int>(Layout) == static_cast<int>(RowMajor) && m_dim.actualDim() == 0)) {
215 // m_stride is aways greater than index, so let's avoid the integer division.
216 eigen_assert(m_stride > index);
217 return m_impl.template packet<LoadMode>(index + m_inputOffset);
218 } else {
219 const Index idx = index / m_stride;
220 const Index rem = index - idx * m_stride;
221 if (rem + PacketSize <= m_stride) {
222 Index inputIndex = idx * m_inputStride + m_inputOffset + rem;
223 return m_impl.template packet<LoadMode>(inputIndex);
224 } else {
225 // Cross the stride boundary. Fallback to slow path.
226 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
227 for (int i = 0; i < PacketSize; ++i) {
228 values[i] = coeff(index);
229 ++index;
230 }
231 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
232 return rslt;
233 }
234 }
235 }
236
237 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost
238 costPerCoeff(bool vectorized) const {
239 double cost = 0;
240 if ((static_cast<int>(Layout) == static_cast<int>(ColMajor) &&
241 m_dim.actualDim() == 0) ||
242 (static_cast<int>(Layout) == static_cast<int>(RowMajor) &&
243 m_dim.actualDim() == NumInputDims - 1)) {
244 cost += TensorOpCost::MulCost<Index>() + TensorOpCost::AddCost<Index>();
245 } else if ((static_cast<int>(Layout) == static_cast<int>(ColMajor) &&
246 m_dim.actualDim() == NumInputDims - 1) ||
247 (static_cast<int>(Layout) == static_cast<int>(RowMajor) &&
248 m_dim.actualDim() == 0)) {
249 cost += TensorOpCost::AddCost<Index>();
250 } else {
251 cost += 3 * TensorOpCost::MulCost<Index>() + TensorOpCost::DivCost<Index>() +
252 3 * TensorOpCost::AddCost<Index>();
253 }
254
255 return m_impl.costPerCoeff(vectorized) +
256 TensorOpCost(0, 0, cost, vectorized, PacketSize);
257 }
258
259 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType* data() const {
260 CoeffReturnType* result = const_cast<CoeffReturnType*>(m_impl.data());
261 if (((static_cast<int>(Layout) == static_cast<int>(ColMajor) && m_dim.actualDim() == NumDims) ||
262 (static_cast<int>(Layout) == static_cast<int>(RowMajor) && m_dim.actualDim() == 0)) &&
263 result) {
264 return result + m_inputOffset;
265 } else {
266 return NULL;
267 }
268 }
269
270 protected:
271 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index srcCoeff(Index index) const
272 {
273 Index inputIndex;
274 if ((static_cast<int>(Layout) == static_cast<int>(ColMajor) && m_dim.actualDim() == 0) ||
275 (static_cast<int>(Layout) == static_cast<int>(RowMajor) && m_dim.actualDim() == NumInputDims-1)) {
276 // m_stride is equal to 1, so let's avoid the integer division.
277 eigen_assert(m_stride == 1);
278 inputIndex = index * m_inputStride + m_inputOffset;
279 } else if ((static_cast<int>(Layout) == static_cast<int>(ColMajor) && m_dim.actualDim() == NumInputDims-1) ||
280 (static_cast<int>(Layout) == static_cast<int>(RowMajor) && m_dim.actualDim() == 0)) {
281 // m_stride is aways greater than index, so let's avoid the integer division.
282 eigen_assert(m_stride > index);
283 inputIndex = index + m_inputOffset;
284 } else {
285 const Index idx = index / m_stride;
286 inputIndex = idx * m_inputStride + m_inputOffset;
287 index -= idx * m_stride;
288 inputIndex += index;
289 }
290 return inputIndex;
291 }
292
293 Dimensions m_dimensions;
294 Index m_stride;
295 Index m_inputOffset;
296 Index m_inputStride;
297 TensorEvaluator<ArgType, Device> m_impl;
298 const internal::DimensionId<DimId> m_dim;
299 const Device& m_device;
300};
301
302
303// Eval as lvalue
304template<DenseIndex DimId, typename ArgType, typename Device>
305struct TensorEvaluator<TensorChippingOp<DimId, ArgType>, Device>
306 : public TensorEvaluator<const TensorChippingOp<DimId, ArgType>, Device>
307{
308 typedef TensorEvaluator<const TensorChippingOp<DimId, ArgType>, Device> Base;
309 typedef TensorChippingOp<DimId, ArgType> XprType;
310 static const int NumInputDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
311 static const int NumDims = NumInputDims-1;
312 typedef typename XprType::Index Index;
313 typedef DSizes<Index, NumDims> Dimensions;
314 typedef typename XprType::Scalar Scalar;
315 typedef typename XprType::CoeffReturnType CoeffReturnType;
316 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
317 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
318
319 enum {
320 IsAligned = false,
321 PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
322 RawAccess = false
323 };
324
325 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
326 : Base(op, device)
327 { }
328
329 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
330 {
331 return this->m_impl.coeffRef(this->srcCoeff(index));
332 }
333
334 template <int StoreMode> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
335 void writePacket(Index index, const PacketReturnType& x)
336 {
337 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
338
339 if ((static_cast<int>(this->Layout) == static_cast<int>(ColMajor) && this->m_dim.actualDim() == 0) ||
340 (static_cast<int>(this->Layout) == static_cast<int>(RowMajor) && this->m_dim.actualDim() == NumInputDims-1)) {
341 // m_stride is equal to 1, so let's avoid the integer division.
342 eigen_assert(this->m_stride == 1);
343 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
344 internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
345 Index inputIndex = index * this->m_inputStride + this->m_inputOffset;
346 for (int i = 0; i < PacketSize; ++i) {
347 this->m_impl.coeffRef(inputIndex) = values[i];
348 inputIndex += this->m_inputStride;
349 }
350 } else if ((static_cast<int>(this->Layout) == static_cast<int>(ColMajor) && this->m_dim.actualDim() == NumInputDims-1) ||
351 (static_cast<int>(this->Layout) == static_cast<int>(RowMajor) && this->m_dim.actualDim() == 0)) {
352 // m_stride is aways greater than index, so let's avoid the integer division.
353 eigen_assert(this->m_stride > index);
354 this->m_impl.template writePacket<StoreMode>(index + this->m_inputOffset, x);
355 } else {
356 const Index idx = index / this->m_stride;
357 const Index rem = index - idx * this->m_stride;
358 if (rem + PacketSize <= this->m_stride) {
359 const Index inputIndex = idx * this->m_inputStride + this->m_inputOffset + rem;
360 this->m_impl.template writePacket<StoreMode>(inputIndex, x);
361 } else {
362 // Cross stride boundary. Fallback to slow path.
363 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
364 internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
365 for (int i = 0; i < PacketSize; ++i) {
366 this->coeffRef(index) = values[i];
367 ++index;
368 }
369 }
370 }
371 }
372};
373
374
375} // end namespace Eigen
376
377#endif // EIGEN_CXX11_TENSOR_TENSOR_CHIPPING_H
Definition TensorAssign.h:56
The tensor base class.
Definition TensorForwardDeclarations.h:29
Definition TensorChipping.h:72
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
const int Dynamic
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