Loading...
Searching...
No Matches
TensorReduction.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// Copyright (C) 2016 Mehdi Goli, Codeplay Software Ltd <eigen@codeplay.com>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_CXX11_TENSOR_TENSOR_REDUCTION_H
12#define EIGEN_CXX11_TENSOR_TENSOR_REDUCTION_H
13
14namespace Eigen {
15
16namespace internal {
17 template<typename Op, typename Dims, typename XprType,template <class> class MakePointer_ >
18 struct traits<TensorReductionOp<Op, Dims, XprType, MakePointer_> >
19 : traits<XprType>
20{
21 typedef traits<XprType> XprTraits;
22 typedef typename XprTraits::Scalar Scalar;
23 typedef typename XprTraits::StorageKind StorageKind;
24 typedef typename XprTraits::Index Index;
25 typedef typename XprType::Nested Nested;
26 static const int NumDimensions = XprTraits::NumDimensions - array_size<Dims>::value;
27 static const int Layout = XprTraits::Layout;
28
29 template <class T> struct MakePointer {
30 // Intermediate typedef to workaround MSVC issue.
31 typedef MakePointer_<T> MakePointerT;
32 typedef typename MakePointerT::Type Type;
33 };
34};
35
36template<typename Op, typename Dims, typename XprType, template <class> class MakePointer_>
37struct eval<TensorReductionOp<Op, Dims, XprType, MakePointer_>, Eigen::Dense>
38{
39 typedef const TensorReductionOp<Op, Dims, XprType, MakePointer_>& type;
40};
41
42template<typename Op, typename Dims, typename XprType, template <class> class MakePointer_>
43struct nested<TensorReductionOp<Op, Dims, XprType, MakePointer_>, 1, typename eval<TensorReductionOp<Op, Dims, XprType, MakePointer_> >::type>
44{
45 typedef TensorReductionOp<Op, Dims, XprType, MakePointer_> type;
46};
47
48
49template <typename OutputDims> struct DimInitializer {
50 template <typename InputDims, typename ReducedDims> EIGEN_DEVICE_FUNC
51 static void run(const InputDims& input_dims,
52 const array<bool, internal::array_size<InputDims>::value>& reduced,
53 OutputDims* output_dims, ReducedDims* reduced_dims) {
54 const int NumInputDims = internal::array_size<InputDims>::value;
55 int outputIndex = 0;
56 int reduceIndex = 0;
57 for (int i = 0; i < NumInputDims; ++i) {
58 if (reduced[i]) {
59 (*reduced_dims)[reduceIndex] = input_dims[i];
60 ++reduceIndex;
61 } else {
62 (*output_dims)[outputIndex] = input_dims[i];
63 ++outputIndex;
64 }
65 }
66 }
67};
68
69template <> struct DimInitializer<Sizes<> > {
70 template <typename InputDims, typename Index, size_t Rank> EIGEN_DEVICE_FUNC
71 static void run(const InputDims& input_dims, const array<bool, Rank>&,
72 Sizes<>*, array<Index, Rank>* reduced_dims) {
73 const int NumInputDims = internal::array_size<InputDims>::value;
74 for (int i = 0; i < NumInputDims; ++i) {
75 (*reduced_dims)[i] = input_dims[i];
76 }
77 }
78};
79
80
81template <typename ReducedDims, int NumTensorDims, int Layout>
82struct are_inner_most_dims {
83 static const bool value = false;
84};
85template <typename ReducedDims, int NumTensorDims, int Layout>
86struct preserve_inner_most_dims {
87 static const bool value = false;
88};
89
90#if EIGEN_HAS_CONSTEXPR && EIGEN_HAS_VARIADIC_TEMPLATES
91template <typename ReducedDims, int NumTensorDims>
92struct are_inner_most_dims<ReducedDims, NumTensorDims, ColMajor>{
93 static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
94 static const bool tmp2 = index_statically_eq<ReducedDims>(0, 0);
95 static const bool tmp3 = index_statically_eq<ReducedDims>(array_size<ReducedDims>::value-1, array_size<ReducedDims>::value-1);
96 static const bool value = tmp1 & tmp2 & tmp3;
97};
98template <typename ReducedDims, int NumTensorDims>
99struct are_inner_most_dims<ReducedDims, NumTensorDims, RowMajor>{
100 static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
101 static const bool tmp2 = index_statically_eq<ReducedDims>(0, NumTensorDims - array_size<ReducedDims>::value);
102 static const bool tmp3 = index_statically_eq<ReducedDims>(array_size<ReducedDims>::value - 1, NumTensorDims - 1);
103 static const bool value = tmp1 & tmp2 & tmp3;
104
105};
106template <typename ReducedDims, int NumTensorDims>
107struct preserve_inner_most_dims<ReducedDims, NumTensorDims, ColMajor>{
108 static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
109 static const bool tmp2 = index_statically_gt<ReducedDims>(0, 0);
110 static const bool value = tmp1 & tmp2;
111
112};
113template <typename ReducedDims, int NumTensorDims>
114struct preserve_inner_most_dims<ReducedDims, NumTensorDims, RowMajor>{
115 static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
116 static const bool tmp2 = index_statically_lt<ReducedDims>(array_size<ReducedDims>::value - 1, NumTensorDims - 1);
117 static const bool value = tmp1 & tmp2;
118};
119#endif
120
121
122template <int DimIndex, typename Self, typename Op>
123struct GenericDimReducer {
124 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self& self, typename Self::Index firstIndex, Op& reducer, typename Self::CoeffReturnType* accum) {
125 EIGEN_STATIC_ASSERT((DimIndex > 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
126 for (int j = 0; j < self.m_reducedDims[DimIndex]; ++j) {
127 const typename Self::Index input = firstIndex + j * self.m_reducedStrides[DimIndex];
128 GenericDimReducer<DimIndex-1, Self, Op>::reduce(self, input, reducer, accum);
129 }
130 }
131};
132template <typename Self, typename Op>
133struct GenericDimReducer<0, Self, Op> {
134 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self& self, typename Self::Index firstIndex, Op& reducer, typename Self::CoeffReturnType* accum) {
135 for (int j = 0; j < self.m_reducedDims[0]; ++j) {
136 const typename Self::Index input = firstIndex + j * self.m_reducedStrides[0];
137 reducer.reduce(self.m_impl.coeff(input), accum);
138 }
139 }
140};
141template <typename Self, typename Op>
142struct GenericDimReducer<-1, Self, Op> {
143 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self& self, typename Self::Index index, Op& reducer, typename Self::CoeffReturnType* accum) {
144 reducer.reduce(self.m_impl.coeff(index), accum);
145 }
146};
147
148template <typename Self, typename Op, bool Vectorizable = (Self::InputPacketAccess & Op::PacketAccess)>
149struct InnerMostDimReducer {
150 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename Self::CoeffReturnType reduce(const Self& self, typename Self::Index firstIndex, typename Self::Index numValuesToReduce, Op& reducer) {
151 typename Self::CoeffReturnType accum = reducer.initialize();
152 for (typename Self::Index j = 0; j < numValuesToReduce; ++j) {
153 reducer.reduce(self.m_impl.coeff(firstIndex + j), &accum);
154 }
155 return reducer.finalize(accum);
156 }
157};
158
159template <typename Self, typename Op>
160struct InnerMostDimReducer<Self, Op, true> {
161 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename Self::CoeffReturnType reduce(const Self& self, typename Self::Index firstIndex, typename Self::Index numValuesToReduce, Op& reducer) {
162 const int packetSize = internal::unpacket_traits<typename Self::PacketReturnType>::size;
163 const typename Self::Index VectorizedSize = (numValuesToReduce / packetSize) * packetSize;
164 typename Self::PacketReturnType p = reducer.template initializePacket<typename Self::PacketReturnType>();
165 for (typename Self::Index j = 0; j < VectorizedSize; j += packetSize) {
166 reducer.reducePacket(self.m_impl.template packet<Unaligned>(firstIndex + j), &p);
167 }
168 typename Self::CoeffReturnType accum = reducer.initialize();
169 for (typename Self::Index j = VectorizedSize; j < numValuesToReduce; ++j) {
170 reducer.reduce(self.m_impl.coeff(firstIndex + j), &accum);
171 }
172 return reducer.finalizeBoth(accum, p);
173 }
174};
175
176template <int DimIndex, typename Self, typename Op, bool vectorizable = (Self::InputPacketAccess & Op::PacketAccess)>
177struct InnerMostDimPreserver {
178 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self&, typename Self::Index, Op&, typename Self::PacketReturnType*) {
179 eigen_assert(false && "should never be called");
180 }
181};
182
183template <int DimIndex, typename Self, typename Op>
184struct InnerMostDimPreserver<DimIndex, Self, Op, true> {
185 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self& self, typename Self::Index firstIndex, Op& reducer, typename Self::PacketReturnType* accum) {
186 EIGEN_STATIC_ASSERT((DimIndex > 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
187 for (typename Self::Index j = 0; j < self.m_reducedDims[DimIndex]; ++j) {
188 const typename Self::Index input = firstIndex + j * self.m_reducedStrides[DimIndex];
189 InnerMostDimPreserver<DimIndex-1, Self, Op>::reduce(self, input, reducer, accum);
190 }
191 }
192};
193
194template <typename Self, typename Op>
195struct InnerMostDimPreserver<0, Self, Op, true> {
196 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self& self, typename Self::Index firstIndex, Op& reducer, typename Self::PacketReturnType* accum) {
197 for (typename Self::Index j = 0; j < self.m_reducedDims[0]; ++j) {
198 const typename Self::Index input = firstIndex + j * self.m_reducedStrides[0];
199 reducer.reducePacket(self.m_impl.template packet<Unaligned>(input), accum);
200 }
201 }
202};
203template <typename Self, typename Op>
204struct InnerMostDimPreserver<-1, Self, Op, true> {
205 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self&, typename Self::Index, Op&, typename Self::PacketReturnType*) {
206 eigen_assert(false && "should never be called");
207 }
208};
209
210// Default full reducer
211template <typename Self, typename Op, typename Device, bool Vectorizable = (Self::InputPacketAccess & Op::PacketAccess)>
212struct FullReducer {
213 static const bool HasOptimizedImplementation = false;
214
215 static EIGEN_DEVICE_FUNC void run(const Self& self, Op& reducer, const Device&, typename Self::CoeffReturnType* output) {
216 const typename Self::Index num_coeffs = array_prod(self.m_impl.dimensions());
217 *output = InnerMostDimReducer<Self, Op, Vectorizable>::reduce(self, 0, num_coeffs, reducer);
218 }
219};
220
221
222#ifdef EIGEN_USE_THREADS
223// Multithreaded full reducers
224template <typename Self, typename Op,
225 bool Vectorizable = (Self::InputPacketAccess & Op::PacketAccess)>
226struct FullReducerShard {
227 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(const Self& self, typename Self::Index firstIndex,
228 typename Self::Index numValuesToReduce, Op& reducer,
229 typename Self::CoeffReturnType* output) {
230 *output = InnerMostDimReducer<Self, Op, Vectorizable>::reduce(
231 self, firstIndex, numValuesToReduce, reducer);
232 }
233};
234
235// Multithreaded full reducer
236template <typename Self, typename Op, bool Vectorizable>
237struct FullReducer<Self, Op, ThreadPoolDevice, Vectorizable> {
238 static const bool HasOptimizedImplementation = !Op::IsStateful;
239 static const int PacketSize =
240 unpacket_traits<typename Self::PacketReturnType>::size;
241
242 // launch one reducer per thread and accumulate the result.
243 static void run(const Self& self, Op& reducer, const ThreadPoolDevice& device,
244 typename Self::CoeffReturnType* output) {
245 typedef typename Self::Index Index;
246 const Index num_coeffs = array_prod(self.m_impl.dimensions());
247 if (num_coeffs == 0) {
248 *output = reducer.finalize(reducer.initialize());
249 return;
250 }
251 const TensorOpCost cost =
252 self.m_impl.costPerCoeff(Vectorizable) +
253 TensorOpCost(0, 0, internal::functor_traits<Op>::Cost, Vectorizable,
254 PacketSize);
255 const int num_threads = TensorCostModel<ThreadPoolDevice>::numThreads(
256 num_coeffs, cost, device.numThreads());
257 if (num_threads == 1) {
258 *output =
259 InnerMostDimReducer<Self, Op, Vectorizable>::reduce(self, 0, num_coeffs, reducer);
260 return;
261 }
262 const Index blocksize =
263 std::floor<Index>(static_cast<float>(num_coeffs) / num_threads);
264 const Index numblocks = blocksize > 0 ? num_coeffs / blocksize : 0;
265 eigen_assert(num_coeffs >= numblocks * blocksize);
266
267 Barrier barrier(internal::convert_index<unsigned int>(numblocks));
268 MaxSizeVector<typename Self::CoeffReturnType> shards(numblocks, reducer.initialize());
269 for (Index i = 0; i < numblocks; ++i) {
270 device.enqueue_with_barrier(&barrier, &FullReducerShard<Self, Op, Vectorizable>::run,
271 self, i * blocksize, blocksize, reducer,
272 &shards[i]);
273 }
274 typename Self::CoeffReturnType finalShard;
275 if (numblocks * blocksize < num_coeffs) {
276 finalShard = InnerMostDimReducer<Self, Op, Vectorizable>::reduce(
277 self, numblocks * blocksize, num_coeffs - numblocks * blocksize,
278 reducer);
279 } else {
280 finalShard = reducer.initialize();
281 }
282 barrier.Wait();
283
284 for (Index i = 0; i < numblocks; ++i) {
285 reducer.reduce(shards[i], &finalShard);
286 }
287 *output = reducer.finalize(finalShard);
288 }
289};
290
291#endif
292
293
294// Default inner reducer
295template <typename Self, typename Op, typename Device>
296struct InnerReducer {
297 static const bool HasOptimizedImplementation = false;
298
299 EIGEN_DEVICE_FUNC static bool run(const Self&, Op&, const Device&, typename Self::CoeffReturnType*, typename Self::Index, typename Self::Index) {
300 eigen_assert(false && "Not implemented");
301 return true;
302 }
303};
304
305// Default outer reducer
306template <typename Self, typename Op, typename Device>
307struct OuterReducer {
308 static const bool HasOptimizedImplementation = false;
309
310 EIGEN_DEVICE_FUNC static bool run(const Self&, Op&, const Device&, typename Self::CoeffReturnType*, typename Self::Index, typename Self::Index) {
311 eigen_assert(false && "Not implemented");
312 return true;
313 }
314};
315
316
317#if defined(EIGEN_USE_GPU) && defined(__CUDACC__)
318template <int B, int N, typename S, typename R, typename I>
319__global__ void FullReductionKernel(R, const S, I, typename S::CoeffReturnType*, unsigned int*);
320
321
322#ifdef EIGEN_HAS_CUDA_FP16
323template <typename S, typename R, typename I>
324__global__ void ReductionInitFullReduxKernelHalfFloat(R, const S, I, half2*);
325template <int B, int N, typename S, typename R, typename I>
326__global__ void FullReductionKernelHalfFloat(R, const S, I, half*, half2*);
327template <int NPT, typename S, typename R, typename I>
328__global__ void InnerReductionKernelHalfFloat(R, const S, I, I, half*);
329
330#endif
331
332template <int NPT, typename S, typename R, typename I>
333__global__ void InnerReductionKernel(R, const S, I, I, typename S::CoeffReturnType*);
334
335template <int NPT, typename S, typename R, typename I>
336__global__ void OuterReductionKernel(R, const S, I, I, typename S::CoeffReturnType*);
337#endif
338
339} // end namespace internal
340
347template <typename Op, typename Dims, typename XprType, template <class> class MakePointer_>
348class TensorReductionOp : public TensorBase<TensorReductionOp<Op, Dims, XprType, MakePointer_>, ReadOnlyAccessors> {
349 public:
350 typedef typename Eigen::internal::traits<TensorReductionOp>::Scalar Scalar;
351 typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
352 typedef typename internal::remove_const<typename XprType::CoeffReturnType>::type CoeffReturnType;
353 typedef typename Eigen::internal::nested<TensorReductionOp>::type Nested;
354 typedef typename Eigen::internal::traits<TensorReductionOp>::StorageKind StorageKind;
355 typedef typename Eigen::internal::traits<TensorReductionOp>::Index Index;
356
357 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
358 TensorReductionOp(const XprType& expr, const Dims& dims) : m_expr(expr), m_dims(dims)
359 { }
360 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
361 TensorReductionOp(const XprType& expr, const Dims& dims, const Op& reducer) : m_expr(expr), m_dims(dims), m_reducer(reducer)
362 { }
363
364 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
365 const XprType& expression() const { return m_expr; }
366 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
367 const Dims& dims() const { return m_dims; }
368 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
369 const Op& reducer() const { return m_reducer; }
370
371 protected:
372 typename XprType::Nested m_expr;
373 const Dims m_dims;
374 const Op m_reducer;
375};
376
377
378// Eval as rvalue
379template<typename Op, typename Dims, typename ArgType, template <class> class MakePointer_, typename Device>
380struct TensorEvaluator<const TensorReductionOp<Op, Dims, ArgType, MakePointer_>, Device>
381{
383 typedef typename XprType::Index Index;
384 typedef ArgType ChildType;
385 typedef typename TensorEvaluator<ArgType, Device>::Dimensions InputDimensions;
386 static const int NumInputDims = internal::array_size<InputDimensions>::value;
387 static const int NumReducedDims = internal::array_size<Dims>::value;
388 static const int NumOutputDims = NumInputDims - NumReducedDims;
389 typedef typename internal::conditional<NumOutputDims==0, Sizes<>, DSizes<Index, NumOutputDims> >::type Dimensions;
390 typedef typename XprType::Scalar Scalar;
392 static const bool InputPacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess;
393 typedef typename internal::remove_const<typename XprType::CoeffReturnType>::type CoeffReturnType;
394 typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
395 static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
396
397 enum {
398 IsAligned = false,
399 PacketAccess = Self::InputPacketAccess && Op::PacketAccess,
400 Layout = TensorEvaluator<ArgType, Device>::Layout,
401 CoordAccess = false, // to be implemented
402 RawAccess = false
403 };
404
405 static const bool ReducingInnerMostDims = internal::are_inner_most_dims<Dims, NumInputDims, Layout>::value;
406 static const bool PreservingInnerMostDims = internal::preserve_inner_most_dims<Dims, NumInputDims, Layout>::value;
407 static const bool RunningFullReduction = (NumOutputDims==0);
408
409 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
410 : m_impl(op.expression(), device), m_reducer(op.reducer()), m_result(NULL), m_device(device), m_xpr_dims(op.dims())
411 {
412 EIGEN_STATIC_ASSERT((NumInputDims >= NumReducedDims), YOU_MADE_A_PROGRAMMING_MISTAKE);
413 EIGEN_STATIC_ASSERT((!ReducingInnerMostDims | !PreservingInnerMostDims | (NumReducedDims == NumInputDims)),
414 YOU_MADE_A_PROGRAMMING_MISTAKE);
415
416 // Build the bitmap indicating if an input dimension is reduced or not.
417 for (int i = 0; i < NumInputDims; ++i) {
418 m_reduced[i] = false;
419 }
420 for (int i = 0; i < NumReducedDims; ++i) {
421 eigen_assert(op.dims()[i] >= 0);
422 eigen_assert(op.dims()[i] < NumInputDims);
423 m_reduced[op.dims()[i]] = true;
424 }
425
426 const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
427 internal::DimInitializer<Dimensions>::run(input_dims, m_reduced, &m_dimensions, &m_reducedDims);
428
429 // Precompute output strides.
430 if (NumOutputDims > 0) {
431 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
432 m_outputStrides[0] = 1;
433 for (int i = 1; i < NumOutputDims; ++i) {
434 m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
435 }
436 } else {
437 m_outputStrides.back() = 1;
438 for (int i = NumOutputDims - 2; i >= 0; --i) {
439 m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
440 }
441 }
442 }
443
444 // Precompute input strides.
445 if (NumInputDims > 0) {
446 array<Index, NumInputDims> input_strides;
447 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
448 input_strides[0] = 1;
449 for (int i = 1; i < NumInputDims; ++i) {
450 input_strides[i] = input_strides[i-1] * input_dims[i-1];
451 }
452 } else {
453 input_strides.back() = 1;
454 for (int i = NumInputDims - 2; i >= 0; --i) {
455 input_strides[i] = input_strides[i + 1] * input_dims[i + 1];
456 }
457 }
458
459 int outputIndex = 0;
460 int reduceIndex = 0;
461 for (int i = 0; i < NumInputDims; ++i) {
462 if (m_reduced[i]) {
463 m_reducedStrides[reduceIndex] = input_strides[i];
464 ++reduceIndex;
465 } else {
466 m_preservedStrides[outputIndex] = input_strides[i];
467 ++outputIndex;
468 }
469 }
470 }
471
472 // Special case for full reductions
473 if (NumOutputDims == 0) {
474 m_preservedStrides[0] = internal::array_prod(input_dims);
475 }
476 }
477
478 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
479
480 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool evalSubExprsIfNeeded(typename MakePointer_<CoeffReturnType>::Type data) {
481 m_impl.evalSubExprsIfNeeded(NULL);
482
483 // Use the FullReducer if possible.
484 if ((RunningFullReduction && RunningOnSycl) ||(RunningFullReduction &&
485 internal::FullReducer<Self, Op, Device>::HasOptimizedImplementation &&
486 ((RunningOnGPU && (m_device.majorDeviceVersion() >= 3)) ||
487 !RunningOnGPU))) {
488 bool need_assign = false;
489 if (!data) {
490 m_result = static_cast<CoeffReturnType*>(m_device.allocate(sizeof(CoeffReturnType)));
491 data = m_result;
492 need_assign = true;
493 }
494 Op reducer(m_reducer);
495 internal::FullReducer<Self, Op, Device>::run(*this, reducer, m_device, data);
496 return need_assign;
497 }
498 else if(RunningOnSycl){
499 const Index num_values_to_reduce = internal::array_prod(m_reducedDims);
500 const Index num_coeffs_to_preserve = static_cast<Index>(internal::array_prod(m_dimensions));
501 if (!data) {
502 data = static_cast<CoeffReturnType*>(m_device.allocate(sizeof(CoeffReturnType) * num_coeffs_to_preserve));
503 m_result = data;
504 }
505 Op reducer(m_reducer);
506 internal::InnerReducer<Self, Op, Device>::run(*this, reducer, m_device, data, num_values_to_reduce, num_coeffs_to_preserve);
507 return (m_result != NULL);
508 }
509
510 // Attempt to use an optimized reduction.
511 else if (RunningOnGPU && (m_device.majorDeviceVersion() >= 3)) {
512 bool reducing_inner_dims = true;
513 for (int i = 0; i < NumReducedDims; ++i) {
514 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
515 reducing_inner_dims &= m_reduced[i];
516 } else {
517 reducing_inner_dims &= m_reduced[NumInputDims - 1 - i];
518 }
519 }
520 if (internal::InnerReducer<Self, Op, Device>::HasOptimizedImplementation &&
521 (reducing_inner_dims || ReducingInnerMostDims)) {
522 const Index num_values_to_reduce = internal::array_prod(m_reducedDims);
523 const Index num_coeffs_to_preserve = internal::array_prod(m_dimensions);
524 if (!data) {
525 if (num_coeffs_to_preserve < 1024 && num_values_to_reduce > num_coeffs_to_preserve && num_values_to_reduce > 128) {
526 data = static_cast<CoeffReturnType*>(m_device.allocate(sizeof(CoeffReturnType) * num_coeffs_to_preserve));
527 m_result = data;
528 }
529 else {
530 return true;
531 }
532 }
533 Op reducer(m_reducer);
534 if (internal::InnerReducer<Self, Op, Device>::run(*this, reducer, m_device, data, num_values_to_reduce, num_coeffs_to_preserve)) {
535 if (m_result) {
536 m_device.deallocate(m_result);
537 m_result = NULL;
538 }
539 return true;
540 } else {
541 return (m_result != NULL);
542 }
543 }
544
545 bool preserving_inner_dims = true;
546 for (int i = 0; i < NumReducedDims; ++i) {
547 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
548 preserving_inner_dims &= m_reduced[NumInputDims - 1 - i];
549 } else {
550 preserving_inner_dims &= m_reduced[i];
551 }
552 }
553 if (internal::OuterReducer<Self, Op, Device>::HasOptimizedImplementation &&
554 preserving_inner_dims) {
555 const Index num_values_to_reduce = internal::array_prod(m_reducedDims);
556 const Index num_coeffs_to_preserve = internal::array_prod(m_dimensions);
557 if (!data) {
558 if (num_coeffs_to_preserve < 1024 && num_values_to_reduce > num_coeffs_to_preserve && num_values_to_reduce > 32) {
559 data = static_cast<CoeffReturnType*>(m_device.allocate(sizeof(CoeffReturnType) * num_coeffs_to_preserve));
560 m_result = data;
561 }
562 else {
563 return true;
564 }
565 }
566 Op reducer(m_reducer);
567 if (internal::OuterReducer<Self, Op, Device>::run(*this, reducer, m_device, data, num_values_to_reduce, num_coeffs_to_preserve)) {
568 if (m_result) {
569 m_device.deallocate(m_result);
570 m_result = NULL;
571 }
572 return true;
573 } else {
574 return (m_result != NULL);
575 }
576 }
577 }
578 return true;
579 }
580
581 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
582 m_impl.cleanup();
583 if (m_result) {
584 m_device.deallocate(m_result);
585 m_result = NULL;
586 }
587 }
588
589 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
590 {
591 if ((RunningOnSycl || RunningFullReduction || RunningOnGPU) && m_result) {
592 return *(m_result + index);
593 }
594 Op reducer(m_reducer);
595 if (ReducingInnerMostDims || RunningFullReduction) {
596 const Index num_values_to_reduce =
597 (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? m_preservedStrides[0] : m_preservedStrides[NumPreservedStrides - 1];
598 return internal::InnerMostDimReducer<Self, Op>::reduce(*this, firstInput(index),
599 num_values_to_reduce, reducer);
600 } else {
601 typename Self::CoeffReturnType accum = reducer.initialize();
602 internal::GenericDimReducer<NumReducedDims-1, Self, Op>::reduce(*this, firstInput(index), reducer, &accum);
603 return reducer.finalize(accum);
604 }
605 }
606
607 // TODO(bsteiner): provide a more efficient implementation.
608 template<int LoadMode>
609 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
610 {
611 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
612 eigen_assert(index + PacketSize - 1 < Index(internal::array_prod(dimensions())));
613
614 if (RunningOnGPU && m_result) {
615 return internal::pload<PacketReturnType>(m_result + index);
616 }
617
618 EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
619 if (ReducingInnerMostDims) {
620 const Index num_values_to_reduce =
621 (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? m_preservedStrides[0] : m_preservedStrides[NumPreservedStrides - 1];
622 const Index firstIndex = firstInput(index);
623 for (Index i = 0; i < PacketSize; ++i) {
624 Op reducer(m_reducer);
625 values[i] = internal::InnerMostDimReducer<Self, Op>::reduce(*this, firstIndex + i * num_values_to_reduce,
626 num_values_to_reduce, reducer);
627 }
628 } else if (PreservingInnerMostDims) {
629 const Index firstIndex = firstInput(index);
630 const int innermost_dim = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? 0 : NumOutputDims - 1;
631 // TBD: extend this the the n innermost dimensions that we preserve.
632 if (((firstIndex % m_dimensions[innermost_dim]) + PacketSize - 1) < m_dimensions[innermost_dim]) {
633 Op reducer(m_reducer);
634 typename Self::PacketReturnType accum = reducer.template initializePacket<typename Self::PacketReturnType>();
635 internal::InnerMostDimPreserver<NumReducedDims-1, Self, Op>::reduce(*this, firstIndex, reducer, &accum);
636 return reducer.finalizePacket(accum);
637 } else {
638 for (int i = 0; i < PacketSize; ++i) {
639 values[i] = coeff(index + i);
640 }
641 }
642 } else {
643 for (int i = 0; i < PacketSize; ++i) {
644 values[i] = coeff(index + i);
645 }
646 }
647 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
648 return rslt;
649 }
650
651 // Must be called after evalSubExprsIfNeeded().
652 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
653 if (RunningFullReduction && m_result) {
654 return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
655 } else {
656 const Index num_values_to_reduce = internal::array_prod(m_reducedDims);
657 const double compute_cost = num_values_to_reduce * internal::functor_traits<Op>::Cost;
658 return m_impl.costPerCoeff(vectorized) * num_values_to_reduce +
659 TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
660 }
661 }
662
663 EIGEN_DEVICE_FUNC typename MakePointer_<Scalar>::Type data() const { return m_result; }
665 const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
667 const Device& device() const{return m_device;}
669 const Dims& xprDims() const {return m_xpr_dims;}
670
671
672 private:
673 template <int, typename, typename> friend struct internal::GenericDimReducer;
674 template <typename, typename, bool> friend struct internal::InnerMostDimReducer;
675 template <int, typename, typename, bool> friend struct internal::InnerMostDimPreserver;
676 template <typename S, typename O, typename D, bool V> friend struct internal::FullReducer;
677#ifdef EIGEN_USE_THREADS
678 template <typename S, typename O, bool V> friend struct internal::FullReducerShard;
679#endif
680#if defined(EIGEN_USE_GPU) && defined(__CUDACC__)
681 template <int B, int N, typename S, typename R, typename I> friend __global__ void internal::FullReductionKernel(R, const S, I, typename S::CoeffReturnType*, unsigned int*);
682#ifdef EIGEN_HAS_CUDA_FP16
683 template <typename S, typename R, typename I> friend __global__ void internal::ReductionInitFullReduxKernelHalfFloat(R, const S, I, half2*);
684 template <int B, int N, typename S, typename R, typename I> friend __global__ void internal::FullReductionKernelHalfFloat(R, const S, I, half*, half2*);
685 template <int NPT, typename S, typename R, typename I> friend __global__ void internal::InnerReductionKernelHalfFloat(R, const S, I, I, half*);
686#endif
687 template <int NPT, typename S, typename R, typename I> friend __global__ void internal::InnerReductionKernel(R, const S, I, I, typename S::CoeffReturnType*);
688
689 template <int NPT, typename S, typename R, typename I> friend __global__ void internal::OuterReductionKernel(R, const S, I, I, typename S::CoeffReturnType*);
690#endif
691
692 template <typename S, typename O, typename D> friend struct internal::InnerReducer;
693
694 // Returns the Index in the input tensor of the first value that needs to be
695 // used to compute the reduction at output index "index".
696 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index firstInput(Index index) const {
697 if (ReducingInnerMostDims) {
698 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
699 return index * m_preservedStrides[0];
700 } else {
701 return index * m_preservedStrides[NumPreservedStrides - 1];
702 }
703 }
704 // TBD: optimize the case where we preserve the innermost dimensions.
705 Index startInput = 0;
706 if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
707 for (int i = NumOutputDims - 1; i > 0; --i) {
708 // This is index_i in the output tensor.
709 const Index idx = index / m_outputStrides[i];
710 startInput += idx * m_preservedStrides[i];
711 index -= idx * m_outputStrides[i];
712 }
713 if (PreservingInnerMostDims) {
714 eigen_assert(m_preservedStrides[0] == 1);
715 startInput += index;
716 } else {
717 startInput += index * m_preservedStrides[0];
718 }
719 } else {
720 for (int i = 0; i < NumOutputDims - 1; ++i) {
721 // This is index_i in the output tensor.
722 const Index idx = index / m_outputStrides[i];
723 startInput += idx * m_preservedStrides[i];
724 index -= idx * m_outputStrides[i];
725 }
726 if (PreservingInnerMostDims) {
727 eigen_assert(m_preservedStrides[NumPreservedStrides - 1] == 1);
728 startInput += index;
729 } else {
730 startInput += index * m_preservedStrides[NumPreservedStrides - 1];
731 }
732 }
733 return startInput;
734 }
735
736 // Bitmap indicating if an input dimension is reduced or not.
737 array<bool, NumInputDims> m_reduced;
738 // Dimensions of the output of the operation.
739 Dimensions m_dimensions;
740 // Precomputed strides for the output tensor.
741 array<Index, NumOutputDims> m_outputStrides;
742 // Subset of strides of the input tensor for the non-reduced dimensions.
743 // Indexed by output dimensions.
744 static const int NumPreservedStrides = max_n_1<NumOutputDims>::size;
745 array<Index, NumPreservedStrides> m_preservedStrides;
746
747 // Subset of strides of the input tensor for the reduced dimensions.
748 // Indexed by reduced dimensions.
749 array<Index, NumReducedDims> m_reducedStrides;
750 // Size of the input dimensions that are reduced.
751 // Indexed by reduced dimensions.
752 array<Index, NumReducedDims> m_reducedDims;
753
754 // Evaluator for the input expression.
755 TensorEvaluator<ArgType, Device> m_impl;
756
757 // Operation to apply for computing the reduction.
758 Op m_reducer;
759
760 // For full reductions
761#if defined(EIGEN_USE_GPU) && defined(__CUDACC__)
762 static const bool RunningOnGPU = internal::is_same<Device, Eigen::GpuDevice>::value;
763 static const bool RunningOnSycl = false;
764#elif defined(EIGEN_USE_SYCL)
765static const bool RunningOnSycl = internal::is_same<typename internal::remove_all<Device>::type, Eigen::SyclDevice>::value;
766static const bool RunningOnGPU = false;
767#else
768 static const bool RunningOnGPU = false;
769 static const bool RunningOnSycl = false;
770#endif
771 typename MakePointer_<CoeffReturnType>::Type m_result;
772
773 const Device& m_device;
774 const Dims& m_xpr_dims;
775};
776
777} // end namespace Eigen
778
779#endif // EIGEN_CXX11_TENSOR_TENSOR_REDUCTION_H
The tensor base class.
Definition TensorForwardDeclarations.h:29
Tensor reduction class.
Definition TensorReduction.h:348
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