TensorReduction.h
Go to the documentation of this file.
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 
14 // clang is incompatible with the CUDA syntax wrt making a kernel a class friend,
15 // so we'll use a macro to make clang happy.
16 #ifndef KERNEL_FRIEND
17 #if defined(__clang__) && (defined(__CUDA__) || defined(__HIP__))
18 #define KERNEL_FRIEND friend __global__ EIGEN_HIP_LAUNCH_BOUNDS_1024
19 #else
20 #define KERNEL_FRIEND friend
21 #endif
22 #endif
23 
24 #include "./InternalHeaderCheck.h"
25 
26 namespace Eigen {
27 
28 
36 namespace internal {
37  template<typename Op, typename Dims, typename XprType,template <class> class MakePointer_ >
38  struct traits<TensorReductionOp<Op, Dims, XprType, MakePointer_> >
39  : traits<XprType>
40 {
41  typedef traits<XprType> XprTraits;
42  typedef typename XprTraits::Scalar Scalar;
43  typedef typename XprTraits::StorageKind StorageKind;
44  typedef typename XprTraits::Index Index;
45  typedef typename XprType::Nested Nested;
46  static constexpr int NumDimensions = XprTraits::NumDimensions - array_size<Dims>::value;
47  static constexpr int Layout = XprTraits::Layout;
48  typedef typename XprTraits::PointerType PointerType;
49 
50  template <class T> struct MakePointer {
51  // Intermediate typedef to workaround MSVC issue.
52  typedef MakePointer_<T> MakePointerT;
53  typedef typename MakePointerT::Type Type;
54  };
55 };
56 
57 template<typename Op, typename Dims, typename XprType, template <class> class MakePointer_>
58 struct eval<TensorReductionOp<Op, Dims, XprType, MakePointer_>, Eigen::Dense>
59 {
60  typedef const TensorReductionOp<Op, Dims, XprType, MakePointer_>& type;
61 };
62 
63 template<typename Op, typename Dims, typename XprType, template <class> class MakePointer_>
64 struct nested<TensorReductionOp<Op, Dims, XprType, MakePointer_>, 1, typename eval<TensorReductionOp<Op, Dims, XprType, MakePointer_> >::type>
65 {
66  typedef TensorReductionOp<Op, Dims, XprType, MakePointer_> type;
67 };
68 
69 
70 template <typename OutputDims> struct DimInitializer {
71  template <typename InputDims, typename ReducedDims> EIGEN_DEVICE_FUNC
72  static void run(const InputDims& input_dims,
73  const array<bool, internal::array_size<InputDims>::value>& reduced,
74  OutputDims* output_dims, ReducedDims* reduced_dims) {
75  const int NumInputDims = internal::array_size<InputDims>::value;
76  int outputIndex = 0;
77  int reduceIndex = 0;
78  for (int i = 0; i < NumInputDims; ++i) {
79  if (reduced[i]) {
80  (*reduced_dims)[reduceIndex] = input_dims[i];
81  ++reduceIndex;
82  } else {
83  (*output_dims)[outputIndex] = input_dims[i];
84  ++outputIndex;
85  }
86  }
87  }
88 };
89 
90 template <> struct DimInitializer<Sizes<> > {
91  template <typename InputDims, typename Index, size_t Rank> EIGEN_DEVICE_FUNC
92  static void run(const InputDims& input_dims, const array<bool, Rank>&,
93  Sizes<>*, array<Index, Rank>* reduced_dims) {
94  const int NumInputDims = internal::array_size<InputDims>::value;
95  for (int i = 0; i < NumInputDims; ++i) {
96  (*reduced_dims)[i] = input_dims[i];
97  }
98  }
99 };
100 
101 
102 template <typename ReducedDims, int NumTensorDims, int Layout>
103 struct are_inner_most_dims {
104  static const bool value = false;
105 };
106 template <typename ReducedDims, int NumTensorDims, int Layout>
107 struct preserve_inner_most_dims {
108  static const bool value = false;
109 };
110 
111 template <typename ReducedDims, int NumTensorDims>
112 struct are_inner_most_dims<ReducedDims, NumTensorDims, ColMajor>{
113  static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
114  static const bool tmp2 = index_statically_eq<ReducedDims>(0, 0);
115  static const bool tmp3 = index_statically_eq<ReducedDims>(array_size<ReducedDims>::value-1, array_size<ReducedDims>::value-1);
116  static const bool value = tmp1 & tmp2 & tmp3;
117 };
118 template <typename ReducedDims, int NumTensorDims>
119 struct are_inner_most_dims<ReducedDims, NumTensorDims, RowMajor>{
120  static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
121  static const bool tmp2 = index_statically_eq<ReducedDims>(0, NumTensorDims - array_size<ReducedDims>::value);
122  static const bool tmp3 = index_statically_eq<ReducedDims>(array_size<ReducedDims>::value - 1, NumTensorDims - 1);
123  static const bool value = tmp1 & tmp2 & tmp3;
124 
125 };
126 template <typename ReducedDims, int NumTensorDims>
127 struct preserve_inner_most_dims<ReducedDims, NumTensorDims, ColMajor>{
128  static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
129  static const bool tmp2 = index_statically_gt<ReducedDims>(0, 0);
130  static const bool value = tmp1 & tmp2;
131 
132 };
133 template <typename ReducedDims, int NumTensorDims>
134 struct preserve_inner_most_dims<ReducedDims, NumTensorDims, RowMajor>{
135  static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
136  static const bool tmp2 = index_statically_lt<ReducedDims>(array_size<ReducedDims>::value - 1, NumTensorDims - 1);
137  static const bool value = tmp1 & tmp2;
138 };
139 
140 
141 template <int DimIndex, typename Self, typename Op>
142 struct GenericDimReducer {
143  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self& self, typename Self::Index firstIndex, Op& reducer, typename Self::CoeffReturnType* accum) {
144  EIGEN_STATIC_ASSERT((DimIndex > 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
145  for (int j = 0; j < self.m_reducedDims[DimIndex]; ++j) {
146  const typename Self::Index input = firstIndex + j * self.m_reducedStrides[DimIndex];
147  GenericDimReducer<DimIndex-1, Self, Op>::reduce(self, input, reducer, accum);
148  }
149  }
150 };
151 template <typename Self, typename Op>
152 struct GenericDimReducer<0, Self, Op> {
153  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self& self, typename Self::Index firstIndex, Op& reducer, typename Self::CoeffReturnType* accum) {
154  for (int j = 0; j < self.m_reducedDims[0]; ++j) {
155  const typename Self::Index input = firstIndex + j * self.m_reducedStrides[0];
156  reducer.reduce(self.m_impl.coeff(input), accum);
157  }
158  }
159 };
160 template <typename Self, typename Op>
161 struct GenericDimReducer<-1, Self, Op> {
162  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self& self, typename Self::Index index, Op& reducer, typename Self::CoeffReturnType* accum) {
163  reducer.reduce(self.m_impl.coeff(index), accum);
164  }
165 };
166 
167 template <typename Self, typename Op, bool Vectorizable = (Self::InputPacketAccess && Self::ReducerTraits::PacketAccess),
168  bool UseTreeReduction = (!Self::ReducerTraits::IsStateful &&
169  !Self::ReducerTraits::IsExactlyAssociative &&
170  // GPU threads can quickly run out of stack space
171  // for moderately sized inputs.
172  !Self::RunningOnGPU
173  )>
174 struct InnerMostDimReducer {
175  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename Self::CoeffReturnType reduce(const Self& self, typename Self::Index firstIndex, typename Self::Index numValuesToReduce, Op& reducer) {
176  typename Self::CoeffReturnType accum = reducer.initialize();
177  for (typename Self::Index j = 0; j < numValuesToReduce; ++j) {
178  reducer.reduce(self.m_impl.coeff(firstIndex + j), &accum);
179  }
180  return reducer.finalize(accum);
181  }
182 };
183 
184 template <typename Self, typename Op>
185 struct InnerMostDimReducer<Self, Op, true, false> {
186  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename Self::CoeffReturnType reduce(const Self& self, typename Self::Index firstIndex, typename Self::Index numValuesToReduce, Op& reducer0) {
187  using Index = typename Self::Index;
188  constexpr Index packetSize = internal::unpacket_traits<typename Self::PacketReturnType>::size;
189  Index start = 0;
190  typename Self::PacketReturnType paccum0 = reducer0.template initializePacket<typename Self::PacketReturnType>();
191  if (!Self::ReducerTraits::IsStateful && numValuesToReduce >= 4*packetSize) {
192  const Index VectorizedSize4 = (numValuesToReduce / (4*packetSize)) * (4*packetSize);
193  typename Self::PacketReturnType paccum1 = reducer0.template initializePacket<typename Self::PacketReturnType>();
194  typename Self::PacketReturnType paccum2 = reducer0.template initializePacket<typename Self::PacketReturnType>();
195  typename Self::PacketReturnType paccum3 = reducer0.template initializePacket<typename Self::PacketReturnType>();
196  const Index offset0 = firstIndex;
197  const Index offset1 = firstIndex + packetSize;
198  const Index offset2 = firstIndex + 2*packetSize;
199  const Index offset3 = firstIndex + 3*packetSize;
200  for (Index j = 0; j < VectorizedSize4; j += 4*packetSize) {
201  reducer0.reducePacket(self.m_impl.template packet<Unaligned>(offset0 + j), &paccum0);
202  reducer0.reducePacket(self.m_impl.template packet<Unaligned>(offset1 + j), &paccum1);
203  reducer0.reducePacket(self.m_impl.template packet<Unaligned>(offset2 + j), &paccum2);
204  reducer0.reducePacket(self.m_impl.template packet<Unaligned>(offset3 + j), &paccum3);
205  }
206  reducer0.reducePacket(paccum1, &paccum0);
207  reducer0.reducePacket(paccum2, &paccum0);
208  reducer0.reducePacket(paccum3, &paccum0);
209  start = VectorizedSize4;
210  }
211  if (start <= (numValuesToReduce - packetSize)) {
212  const Index VectorizedSize = (numValuesToReduce / packetSize) * packetSize;
213  for (Index j = start; j < VectorizedSize; j += packetSize) {
214  reducer0.reducePacket(self.m_impl.template packet<Unaligned>(firstIndex + j), &paccum0);
215  }
216  start = VectorizedSize;
217  }
218  typename Self::CoeffReturnType accum = reducer0.initialize();
219  for (Index j = start; j < numValuesToReduce; ++j) {
220  reducer0.reduce(self.m_impl.coeff(firstIndex + j), &accum);
221  }
222  return reducer0.finalizeBoth(accum, paccum0);
223  }
224 };
225 
226 
227 #if !defined(EIGEN_HIPCC)
228 
229 // The following implements tree-based reduction, which improves the accuracy
230 // of sum and mean reductions, since each of the n inputs only participates in
231 // O(log n) additions.
232 template <typename T>
233 EIGEN_DEVICE_FUNC inline Index LeafSize() { return 1024; }
234 template <>
235 EIGEN_DEVICE_FUNC inline Index LeafSize<half>() { return 200; }
236 template <>
238 
239 template <typename Self, typename Op>
240 struct InnerMostDimReducer<Self, Op, false, true> {
241  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename Self::CoeffReturnType
242  reduce(const Self& self, typename Self::Index firstIndex,
243  typename Self::Index numValuesToReduce, Op& reducer) {
244  const Index kLeafSize = LeafSize<typename Self::CoeffReturnType>();
245  typename Self::CoeffReturnType accum = reducer.initialize();
246  if (numValuesToReduce > kLeafSize) {
247  const typename Self::Index half = numValuesToReduce / 2;
248  // Recursively reduce the two halves.
249  reducer.reduce(reduce(self, firstIndex, half, reducer), &accum);
250  reducer.reduce(
251  reduce(self, firstIndex + half, numValuesToReduce - half, reducer),
252  &accum);
253  return reducer.finalize(accum);
254  } else {
255  return InnerMostDimReducer<Self, Op, false, false>::reduce(self, firstIndex, numValuesToReduce, reducer);
256  }
257  }
258 };
259 
260 template <typename Self, typename Op>
261 struct InnerMostDimReducer<Self, Op, true, true> {
262  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename Self::CoeffReturnType
263  reduce(const Self& self, typename Self::Index firstIndex,
264  typename Self::Index numValuesToReduce, Op& reducer) {
265  const Index kLeafSize = LeafSize<typename Self::CoeffReturnType>();
266  const typename Self::Index packetSize =
267  internal::unpacket_traits<typename Self::PacketReturnType>::size;
268  typename Self::CoeffReturnType accum = reducer.initialize();
269  if (numValuesToReduce > packetSize * kLeafSize) {
270  // Make sure the split point is aligned on a packet boundary.
271  const typename Self::Index split =
272  packetSize *
273  divup(firstIndex + divup(numValuesToReduce, typename Self::Index(2)),
274  packetSize);
275  const typename Self::Index num_left =
276  numext::mini(split - firstIndex, numValuesToReduce);
277  reducer.reduce(reduce(self, firstIndex, num_left, reducer), &accum);
278  if (num_left < numValuesToReduce) {
279  reducer.reduce(
280  reduce(self, split, numValuesToReduce - num_left, reducer), &accum);
281  }
282  return reducer.finalize(accum);
283  } else {
284  return InnerMostDimReducer<Self, Op, true, false>::reduce(self, firstIndex, numValuesToReduce, reducer);
285  }
286  }
287 };
288 #endif
289 
290 template <int DimIndex, typename Self, typename Op, bool vectorizable = (Self::InputPacketAccess && Self::ReducerTraits::PacketAccess)>
291 struct InnerMostDimPreserver {
292  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self&, typename Self::Index, Op&, typename Self::PacketReturnType*) {
293  eigen_assert(false && "should never be called");
294  }
295 };
296 
297 template <int DimIndex, typename Self, typename Op>
298 struct InnerMostDimPreserver<DimIndex, Self, Op, true> {
299  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self& self, typename Self::Index firstIndex, Op& reducer, typename Self::PacketReturnType* accum) {
300  EIGEN_STATIC_ASSERT((DimIndex > 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
301  for (typename Self::Index j = 0; j < self.m_reducedDims[DimIndex]; ++j) {
302  const typename Self::Index input = firstIndex + j * self.m_reducedStrides[DimIndex];
303  InnerMostDimPreserver<DimIndex-1, Self, Op>::reduce(self, input, reducer, accum);
304  }
305  }
306 };
307 
308 template <typename Self, typename Op>
309 struct InnerMostDimPreserver<0, Self, Op, true> {
310  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self& self, typename Self::Index firstIndex, Op& reducer0, typename Self::PacketReturnType* accum0) {
311  using Index = typename Self::Index;
312  const Index stride = self.m_reducedStrides[0];
313  const Index size = self.m_reducedDims[0];
314  if (!Self::ReducerTraits::IsStateful && size >= 16) {
315  const Index unrolled_size4 = (size / 4) * 4;
316  typename Self::PacketReturnType accum1 = reducer0.template initializePacket<typename Self::PacketReturnType>();
317  typename Self::PacketReturnType accum2 = reducer0.template initializePacket<typename Self::PacketReturnType>();
318  typename Self::PacketReturnType accum3 = reducer0.template initializePacket<typename Self::PacketReturnType>();
319  for (Index j = 0; j < unrolled_size4; j += 4) {
320  const Index input0 = firstIndex + j * stride;
321  reducer0.reducePacket(self.m_impl.template packet<Unaligned>(input0), accum0);
322  const Index input1 = firstIndex + (j+1) * stride;
323  reducer0.reducePacket(self.m_impl.template packet<Unaligned>(input1), &accum1);
324  const Index input2 = firstIndex + (j+2) * stride;
325  reducer0.reducePacket(self.m_impl.template packet<Unaligned>(input2), &accum2);
326  const Index input3 = firstIndex + (j+3) * stride;
327  reducer0.reducePacket(self.m_impl.template packet<Unaligned>(input3), &accum3);
328  }
329  reducer0.reducePacket(accum1, accum0);
330  reducer0.reducePacket(accum2, accum0);
331  reducer0.reducePacket(accum3, accum0);
332  for (Index j = unrolled_size4; j < size; ++j) {
333  Index input = firstIndex + j * stride;
334  reducer0.reducePacket(self.m_impl.template packet<Unaligned>(input), accum0);
335  }
336  } else {
337  for (Index j = 0; j < size; ++j) {
338  Index input = firstIndex + j * stride;
339  reducer0.reducePacket(self.m_impl.template packet<Unaligned>(input), accum0);
340  }
341  }
342  }
343 };
344 template <typename Self, typename Op>
345 struct InnerMostDimPreserver<-1, Self, Op, true> {
346  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void reduce(const Self&, typename Self::Index, Op&, typename Self::PacketReturnType*) {
347  eigen_assert(false && "should never be called");
348  }
349 };
350 
351 // Default full reducer
352 template <typename Self, typename Op, typename Device, bool Vectorizable = (Self::InputPacketAccess && Self::ReducerTraits::PacketAccess)>
353 struct FullReducer {
354  static constexpr bool HasOptimizedImplementation = false;
355 
356  static EIGEN_DEVICE_FUNC void run(const Self& self, Op& reducer, const Device&, typename Self::EvaluatorPointerType output) {
357  const typename Self::Index num_coeffs = array_prod(self.m_impl.dimensions());
358  *output = InnerMostDimReducer<Self, Op, Vectorizable>::reduce(self, 0, num_coeffs, reducer);
359  }
360 };
361 
362 
363 #ifdef EIGEN_USE_THREADS
364 // Multithreaded full reducers
365 template <typename Self, typename Op,
366  bool Vectorizable = (Self::InputPacketAccess && Self::ReducerTraits::PacketAccess)>
367 struct FullReducerShard {
368  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void run(const Self& self, typename Self::Index firstIndex,
369  typename Self::Index numValuesToReduce, Op& reducer,
370  typename Self::CoeffReturnType* output) {
371  *output = InnerMostDimReducer<Self, Op, Vectorizable>::reduce(
372  self, firstIndex, numValuesToReduce, reducer);
373  }
374 };
375 
376 // Multithreaded full reducer
377 template <typename Self, typename Op, bool Vectorizable>
378 struct FullReducer<Self, Op, ThreadPoolDevice, Vectorizable> {
379  static constexpr bool HasOptimizedImplementation = !Self::ReducerTraits::IsStateful;
380  static constexpr Index PacketSize =
381  unpacket_traits<typename Self::PacketReturnType>::size;
382 
383  // launch one reducer per thread and accumulate the result.
384  static void run(const Self& self, Op& reducer, const ThreadPoolDevice& device,
385  typename Self::CoeffReturnType* output) {
386  typedef typename Self::Index Index;
387  const Index num_coeffs = array_prod(self.m_impl.dimensions());
388  if (num_coeffs == 0) {
389  *output = reducer.finalize(reducer.initialize());
390  return;
391  }
392  const TensorOpCost cost =
393  self.m_impl.costPerCoeff(Vectorizable) +
394  TensorOpCost(0, 0, internal::functor_traits<Op>::Cost, Vectorizable,
395  PacketSize);
397  num_coeffs, cost, device.numThreads());
398  if (num_threads == 1) {
399  *output =
400  InnerMostDimReducer<Self, Op, Vectorizable>::reduce(self, 0, num_coeffs, reducer);
401  return;
402  }
403  const Index blocksize = num_coeffs / num_threads;
404  const Index numblocks = blocksize > 0 ? num_coeffs / blocksize : 0;
405  eigen_assert(num_coeffs >= numblocks * blocksize);
406 
407  Barrier barrier(internal::convert_index<unsigned int>(numblocks));
408  MaxSizeVector<typename Self::CoeffReturnType> shards(numblocks, reducer.initialize());
409  for (Index i = 0; i < numblocks; ++i) {
410  device.enqueue_with_barrier(&barrier, &FullReducerShard<Self, Op, Vectorizable>::run,
411  self, i * blocksize, blocksize, reducer,
412  &shards[i]);
413  }
414  typename Self::CoeffReturnType finalShard;
415  if (numblocks * blocksize < num_coeffs) {
416  finalShard = InnerMostDimReducer<Self, Op, Vectorizable>::reduce(
417  self, numblocks * blocksize, num_coeffs - numblocks * blocksize,
418  reducer);
419  } else {
420  finalShard = reducer.initialize();
421  }
422  barrier.Wait();
423 
424  for (Index i = 0; i < numblocks; ++i) {
425  reducer.reduce(shards[i], &finalShard);
426  }
427  *output = reducer.finalize(finalShard);
428  }
429 };
430 
431 #endif
432 
433 
434 // Default inner reducer
435 template <typename Self, typename Op, typename Device>
436 struct InnerReducer {
437  static constexpr bool HasOptimizedImplementation = false;
438 
439  EIGEN_DEVICE_FUNC static bool run(const Self&, Op&, const Device&, typename Self::CoeffReturnType*, typename Self::Index, typename Self::Index) {
440  eigen_assert(false && "Not implemented");
441  return true;
442  }
443 };
444 
445 // Default outer reducer
446 template <typename Self, typename Op, typename Device>
447 struct OuterReducer {
448  static constexpr bool HasOptimizedImplementation = false;
449 
450  EIGEN_DEVICE_FUNC static bool run(const Self&, Op&, const Device&, typename Self::CoeffReturnType*, typename Self::Index, typename Self::Index) {
451  eigen_assert(false && "Not implemented");
452  return true;
453  }
454 };
455 
456 #ifdef EIGEN_USE_SYCL
457 // Default Generic reducer
458 template <typename Self, typename Op, typename Device>
459 struct GenericReducer {
460  static constexpr bool HasOptimizedImplementation = false;
461 
462  EIGEN_DEVICE_FUNC static bool run(const Self&, Op&, const Device&, typename Self::CoeffReturnType*, typename Self::Index, typename Self::Index) {
463  eigen_assert(false && "Not implemented");
464  return true;
465  }
466 };
467 #endif
468 
469 #if defined(EIGEN_USE_GPU) && (defined(EIGEN_GPUCC))
470 template <int B, int N, typename S, typename R, typename I_>
471 __global__ EIGEN_HIP_LAUNCH_BOUNDS_1024 void FullReductionKernel(R, const S, I_, typename S::CoeffReturnType*, unsigned int*);
472 
473 
474 #if defined(EIGEN_HAS_GPU_FP16)
475 template <typename S, typename R, typename I_>
476 __global__ EIGEN_HIP_LAUNCH_BOUNDS_1024 void ReductionInitFullReduxKernelHalfFloat(R, const S, I_, internal::packet_traits<half>::type*);
477 template <int B, int N, typename S, typename R, typename I_>
478 __global__ EIGEN_HIP_LAUNCH_BOUNDS_1024 void FullReductionKernelHalfFloat(R, const S, I_, half*, internal::packet_traits<half>::type*);
479 template <int NPT, typename S, typename R, typename I_>
480 __global__ EIGEN_HIP_LAUNCH_BOUNDS_1024 void InnerReductionKernelHalfFloat(R, const S, I_, I_, half*);
481 
482 #endif
483 
484 template <int NPT, typename S, typename R, typename I_>
485 __global__ EIGEN_HIP_LAUNCH_BOUNDS_1024 void InnerReductionKernel(R, const S, I_, I_, typename S::CoeffReturnType*);
486 
487 template <int NPT, typename S, typename R, typename I_>
488 __global__ EIGEN_HIP_LAUNCH_BOUNDS_1024 void OuterReductionKernel(R, const S, I_, I_, typename S::CoeffReturnType*);
489 #endif
490 
499 template <typename Op, typename CoeffReturnType>
500 struct ReductionReturnType {
501 #if defined(EIGEN_USE_SYCL)
502  typedef std::remove_const_t<decltype(std::declval<Op>().initialize())> type;
503 #else
504  typedef std::remove_const_t<CoeffReturnType> type;
505 #endif
506 };
507 
508 } // end namespace internal
509 
510 
511 template <typename Op, typename Dims, typename XprType, template <class> class MakePointer_>
512 class TensorReductionOp : public TensorBase<TensorReductionOp<Op, Dims, XprType, MakePointer_>, ReadOnlyAccessors> {
513  public:
514  typedef typename Eigen::internal::traits<TensorReductionOp>::Scalar Scalar;
516  typedef std::remove_const_t<typename XprType::CoeffReturnType> CoeffReturnType;
517  typedef typename Eigen::internal::nested<TensorReductionOp>::type Nested;
518  typedef typename Eigen::internal::traits<TensorReductionOp>::StorageKind StorageKind;
519  typedef typename Eigen::internal::traits<TensorReductionOp>::Index Index;
520 
521  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
522  TensorReductionOp(const XprType& expr, const Dims& dims) : m_expr(expr), m_dims(dims)
523  { }
524  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
525  TensorReductionOp(const XprType& expr, const Dims& dims, const Op& reducer) : m_expr(expr), m_dims(dims), m_reducer(reducer)
526  { }
527 
528  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
529  const XprType& expression() const { return m_expr; }
530  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
531  const Dims& dims() const { return m_dims; }
532  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
533  const Op& reducer() const { return m_reducer; }
534 
535  protected:
536  typename XprType::Nested m_expr;
537  const Dims m_dims;
538  const Op m_reducer;
539 };
540 
541 template<typename ArgType, typename Device>
543 
544 // Eval as rvalue
545 template<typename Op, typename Dims, typename ArgType, template <class> class MakePointer_, typename Device>
546 struct TensorReductionEvaluatorBase<const TensorReductionOp<Op, Dims, ArgType, MakePointer_>, Device>
547 {
548  typedef internal::reducer_traits<Op, Device> ReducerTraits;
549  typedef Dims ReducedDims;
551  typedef typename XprType::Index Index;
552  typedef ArgType ChildType;
554  static constexpr int NumInputDims = internal::array_size<InputDimensions>::value;
555  static constexpr int NumReducedDims = internal::array_size<Dims>::value;
556  static constexpr int NumOutputDims = NumInputDims - NumReducedDims;
557  typedef std::conditional_t<NumOutputDims==0, Sizes<>, DSizes<Index, NumOutputDims> > Dimensions;
558  typedef typename XprType::Scalar Scalar;
560  static constexpr bool InputPacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess;
561  typedef typename internal::ReductionReturnType<Op, typename XprType::CoeffReturnType>::type CoeffReturnType;
563  static constexpr Index PacketSize = PacketType<CoeffReturnType, Device>::size;
564 
565  typedef typename Eigen::internal::traits<XprType>::PointerType TensorPointerType;
568 
569  // Subset of strides of the input tensor for the non-reduced dimensions.
570  // Indexed by output dimensions.
571  static constexpr int NumPreservedStrides = max_n_1<NumOutputDims>::size;
572 
573  // For full reductions
574 #if defined(EIGEN_USE_GPU) && (defined(EIGEN_GPUCC))
575  static constexpr bool RunningOnGPU = internal::is_same<Device, Eigen::GpuDevice>::value;
576  static constexpr bool RunningOnSycl = false;
577 #elif defined(EIGEN_USE_SYCL)
578 static constexpr bool RunningOnSycl = internal::is_same<internal::remove_all_t<Device>, Eigen::SyclDevice>::value;
579 static constexpr bool RunningOnGPU = false;
580 #else
581  static constexpr bool RunningOnGPU = false;
582  static constexpr bool RunningOnSycl = false;
583 #endif
584 
585  static constexpr int Layout = TensorEvaluator<ArgType, Device>::Layout;
586  enum {
587  IsAligned = false,
588  PacketAccess = Self::InputPacketAccess && ReducerTraits::PacketAccess,
589  BlockAccess = false,
590  PreferBlockAccess = true,
591  CoordAccess = false, // to be implemented
592  RawAccess = false
593  };
594 
595  typedef std::remove_const_t<Scalar> ScalarNoConst;
596 
597  //===- Tensor block evaluation strategy (see TensorBlock.h) -------------===//
598  typedef internal::TensorBlockNotImplemented TensorBlock;
599  //===--------------------------------------------------------------------===//
600 
601  static constexpr bool ReducingInnerMostDims = internal::are_inner_most_dims<Dims, NumInputDims, Layout>::value;
602  static constexpr bool PreservingInnerMostDims = internal::preserve_inner_most_dims<Dims, NumInputDims, Layout>::value;
603  static constexpr bool RunningFullReduction = (NumOutputDims==0);
604 
605  EIGEN_STRONG_INLINE TensorReductionEvaluatorBase(const XprType& op, const Device& device)
606  : m_impl(op.expression(), device),
607  m_reducer(op.reducer()),
608  m_result(NULL),
609  m_device(device) {
610  EIGEN_STATIC_ASSERT((NumInputDims >= NumReducedDims), YOU_MADE_A_PROGRAMMING_MISTAKE);
611  EIGEN_STATIC_ASSERT((!ReducingInnerMostDims | !PreservingInnerMostDims | (NumReducedDims == NumInputDims)),
612  YOU_MADE_A_PROGRAMMING_MISTAKE);
613 
614  // Build the bitmap indicating if an input dimension is reduced or not.
615  for (int i = 0; i < NumInputDims; ++i) {
616  m_reduced[i] = false;
617  }
618  for (int i = 0; i < NumReducedDims; ++i) {
619  eigen_assert(op.dims()[i] >= 0);
620  eigen_assert(op.dims()[i] < NumInputDims);
621  m_reduced[op.dims()[i]] = true;
622  }
623 
624  const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
625  internal::DimInitializer<Dimensions>::run(input_dims, m_reduced, &m_dimensions, &m_reducedDims);
626 
627  // Precompute output strides.
628  if (NumOutputDims > 0) {
629  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
630  m_outputStrides[0] = 1;
631  for (int i = 1; i < NumOutputDims; ++i) {
632  m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
633  m_fastOutputStrides[i] = internal::TensorIntDivisor<Index>(m_outputStrides[i]);
634  }
635  } else {
636  m_outputStrides[static_cast<size_t>(NumOutputDims - 1)] = 1;
637  for (int i = NumOutputDims - 2; i >= 0; --i) {
638  m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
639  m_fastOutputStrides[i] = internal::TensorIntDivisor<Index>(m_outputStrides[i]);
640  }
641  }
642  }
643 
644  // Precompute input strides.
645  if (NumInputDims > 0) {
646  array<Index, NumInputDims> input_strides;
647  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
648  input_strides[0] = 1;
649  for (int i = 1; i < NumInputDims; ++i) {
650  input_strides[i] = input_strides[i-1] * input_dims[i-1];
651  }
652  } else {
653  input_strides.back() = 1;
654  for (int i = NumInputDims - 2; i >= 0; --i) {
655  input_strides[i] = input_strides[i + 1] * input_dims[i + 1];
656  }
657  }
658 
659  int outputIndex = 0;
660  int reduceIndex = 0;
661  for (int i = 0; i < NumInputDims; ++i) {
662  if (m_reduced[i]) {
663  m_reducedStrides[reduceIndex] = input_strides[i];
664  ++reduceIndex;
665  } else {
666  m_preservedStrides[outputIndex] = input_strides[i];
667  m_output_to_input_dim_map[outputIndex] = i;
668  ++outputIndex;
669  }
670  }
671  }
672 
673  // Special case for full reductions
674  if (NumOutputDims == 0) {
675  m_preservedStrides[0] = internal::array_prod(input_dims);
676  }
677 
678  m_numValuesToReduce =
679  NumOutputDims == 0
680  ? internal::array_prod(input_dims)
681  : (static_cast<int>(Layout) == static_cast<int>(ColMajor))
682  ? m_preservedStrides[0]
683  : m_preservedStrides[static_cast<size_t>(NumOutputDims - 1)];
684  }
685 
686  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
687 
688  EIGEN_STRONG_INLINE
690  // Use the FullReducer if possible.
691  if ((RunningFullReduction && RunningOnSycl) ||(RunningFullReduction &&
692  internal::FullReducer<Self, Op, Device>::HasOptimizedImplementation &&
693  ((RunningOnGPU && (m_device.majorDeviceVersion() >= 3)) ||
694  !RunningOnGPU))) {
695  bool need_assign = false;
696  if (!data) {
697  m_result = static_cast<EvaluatorPointerType>(m_device.get((CoeffReturnType*)m_device.allocate_temp(sizeof(CoeffReturnType))));
698  data = m_result;
699  need_assign = true;
700  }
701  Op reducer(m_reducer);
702  internal::FullReducer<Self, Op, Device>::run(*this, reducer, m_device, data);
703  return need_assign;
704  }
705 
706  // Attempt to use an optimized reduction.
707  else if ((RunningOnGPU && (m_device.majorDeviceVersion() >= 3)) || (RunningOnSycl)) {
708  bool reducing_inner_dims = true;
709  for (int i = 0; i < NumReducedDims; ++i) {
710  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
711  reducing_inner_dims &= m_reduced[i];
712  } else {
713  reducing_inner_dims &= m_reduced[NumInputDims - 1 - i];
714  }
715  }
716  if (internal::InnerReducer<Self, Op, Device>::HasOptimizedImplementation &&
717  (reducing_inner_dims || ReducingInnerMostDims)) {
718  const Index num_values_to_reduce = internal::array_prod(m_reducedDims);
719  const Index num_coeffs_to_preserve = internal::array_prod(m_dimensions);
720  if (!data) {
721  if ((num_coeffs_to_preserve < 1024 && num_values_to_reduce > num_coeffs_to_preserve && num_values_to_reduce > 128) || (RunningOnSycl)) {
722  data = static_cast<EvaluatorPointerType>(m_device.get((CoeffReturnType*)m_device.allocate_temp(sizeof(CoeffReturnType) * num_coeffs_to_preserve)));
723  m_result = data;
724  }
725  else {
726  return true;
727  }
728  }
729  Op reducer(m_reducer);
730  // For SYCL this if always return false
731  if (internal::InnerReducer<Self, Op, Device>::run(*this, reducer, m_device, data, num_values_to_reduce, num_coeffs_to_preserve)) {
732  if (m_result) {
733  m_device.deallocate_temp(m_result);
734  m_result = NULL;
735  }
736  return true;
737  } else {
738  return (m_result != NULL);
739  }
740  }
741 
742  bool preserving_inner_dims = true;
743  for (int i = 0; i < NumReducedDims; ++i) {
744  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
745  preserving_inner_dims &= m_reduced[NumInputDims - 1 - i];
746  } else {
747  preserving_inner_dims &= m_reduced[i];
748  }
749  }
750  if (internal::OuterReducer<Self, Op, Device>::HasOptimizedImplementation &&
751  preserving_inner_dims) {
752  const Index num_values_to_reduce = internal::array_prod(m_reducedDims);
753  const Index num_coeffs_to_preserve = internal::array_prod(m_dimensions);
754  if (!data) {
755  if ((num_coeffs_to_preserve < 1024 && num_values_to_reduce > num_coeffs_to_preserve && num_values_to_reduce > 32) || (RunningOnSycl)) {
756  data = static_cast<EvaluatorPointerType>(m_device.get((CoeffReturnType*)m_device.allocate_temp(sizeof(CoeffReturnType) * num_coeffs_to_preserve)));
757  m_result = data;
758  }
759  else {
760  return true;
761  }
762  }
763  Op reducer(m_reducer);
764  // For SYCL this if always return false
765  if (internal::OuterReducer<Self, Op, Device>::run(*this, reducer, m_device, data, num_values_to_reduce, num_coeffs_to_preserve)) {
766  if (m_result) {
767  m_device.deallocate_temp(m_result);
768  m_result = NULL;
769  }
770  return true;
771  } else {
772  return (m_result != NULL);
773  }
774  }
775  #if defined(EIGEN_USE_SYCL)
776  // If there is no Optimised version for SYCL, the reduction expression
777  // must break into two subexpression and use the SYCL generic Reducer on the device.
778  if(RunningOnSycl) {
779  const Index num_values_to_reduce = internal::array_prod(m_reducedDims);
780  const Index num_coeffs_to_preserve = internal::array_prod(m_dimensions);
781  if (!data) {
782  data = static_cast<EvaluatorPointerType>(m_device.get((CoeffReturnType*)m_device.allocate_temp(sizeof(CoeffReturnType) * num_coeffs_to_preserve)));
783  m_result = data;
784  }
785  Op reducer(m_reducer);
786  internal::GenericReducer<Self, Op, Device>::run(*this, reducer, m_device, data, num_values_to_reduce, num_coeffs_to_preserve);
787  return (m_result != NULL);
788  }
789  #endif
790  }
791  return true;
792  }
793 
794 #ifdef EIGEN_USE_THREADS
795  template <typename EvalSubExprsCallback>
796  EIGEN_STRONG_INLINE
797  void
798  evalSubExprsIfNeededAsync(EvaluatorPointerType data,
799  EvalSubExprsCallback done) {
800  m_impl.evalSubExprsIfNeededAsync(NULL, [this, data, done](bool) {
801  done(evalSubExprsIfNeededCommon(data));
802  });
803  }
804 #endif
805 
806  EIGEN_STRONG_INLINE
808  m_impl.evalSubExprsIfNeeded(NULL);
809  return evalSubExprsIfNeededCommon(data);
810  }
811 
812  EIGEN_STRONG_INLINE void cleanup() {
813  m_impl.cleanup();
814  if (m_result) {
815  m_device.deallocate_temp(m_result);
816  m_result = NULL;
817  }
818  }
819 
820  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
821  {
822  if (( RunningFullReduction || RunningOnGPU) && m_result ) {
823  return *(m_result + index);
824  }
825  Op reducer(m_reducer);
826  if (ReducingInnerMostDims || RunningFullReduction) {
827  const Index num_values_to_reduce =
828  (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? m_preservedStrides[0] : m_preservedStrides[NumPreservedStrides - 1];
829  return internal::InnerMostDimReducer<Self, Op>::reduce(*this, firstInput(index),
830  num_values_to_reduce, reducer);
831  } else {
832  typename Self::CoeffReturnType accum = reducer.initialize();
833  internal::GenericDimReducer<NumReducedDims-1, Self, Op>::reduce(*this, firstInput(index), reducer, &accum);
834  return reducer.finalize(accum);
835  }
836  }
837 
838  // TODO(bsteiner): provide a more efficient implementation.
839  template<int LoadMode>
840  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
841  {
842  eigen_assert(index + PacketSize - 1 < Index(internal::array_prod(dimensions())));
843 
844  if (RunningOnGPU && m_result) {
845  return internal::pload<PacketReturnType>(m_result + index);
846  }
847 
848  EIGEN_ALIGN_MAX std::remove_const_t<CoeffReturnType> values[PacketSize];
849  if (ReducingInnerMostDims) {
850  const Index num_values_to_reduce =
851  (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? m_preservedStrides[0] : m_preservedStrides[NumPreservedStrides - 1];
852  const Index firstIndex = firstInput(index);
853  for (Index i = 0; i < PacketSize; ++i) {
854  Op reducer(m_reducer);
855  values[i] = internal::InnerMostDimReducer<Self, Op>::reduce(*this, firstIndex + i * num_values_to_reduce,
856  num_values_to_reduce, reducer);
857  }
858  } else if (PreservingInnerMostDims) {
859  const Index firstIndex = firstInput(index);
860  const int innermost_dim = (static_cast<int>(Layout) == static_cast<int>(ColMajor)) ? 0 : NumOutputDims - 1;
861  // TBD: extend this the the n innermost dimensions that we preserve.
862  if (((firstIndex % m_dimensions[innermost_dim]) + PacketSize - 1) < m_dimensions[innermost_dim]) {
863  Op reducer(m_reducer);
864  typename Self::PacketReturnType accum = reducer.template initializePacket<typename Self::PacketReturnType>();
865  internal::InnerMostDimPreserver<NumReducedDims-1, Self, Op>::reduce(*this, firstIndex, reducer, &accum);
866  return reducer.finalizePacket(accum);
867  } else {
868  for (int i = 0; i < PacketSize; ++i) {
869  values[i] = coeff(index + i);
870  }
871  }
872  } else {
873  for (int i = 0; i < PacketSize; ++i) {
874  values[i] = coeff(index + i);
875  }
876  }
877  PacketReturnType rslt = internal::pload<PacketReturnType>(values);
878  return rslt;
879  }
880 
881  // Must be called after evalSubExprsIfNeeded().
882  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
883  if (RunningFullReduction && m_result) {
884  return TensorOpCost(sizeof(CoeffReturnType), 0, 0, vectorized, PacketSize);
885  } else {
886  const Index num_values_to_reduce = internal::array_prod(m_reducedDims);
887  const double compute_cost = num_values_to_reduce * internal::functor_traits<Op>::Cost;
888  return m_impl.costPerCoeff(vectorized) * num_values_to_reduce +
889  TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
890  }
891  }
892 
893  EIGEN_DEVICE_FUNC EvaluatorPointerType data() const { return m_result; }
894  EIGEN_DEVICE_FUNC const TensorEvaluator<ArgType, Device>& impl() const { return m_impl; }
895  EIGEN_DEVICE_FUNC const Device& device() const { return m_device; }
896 
897  private:
898  template <int, typename, typename> friend struct internal::GenericDimReducer;
899  template <typename, typename, bool, bool> friend struct internal::InnerMostDimReducer;
900  template <int, typename, typename, bool> friend struct internal::InnerMostDimPreserver;
901  template <typename S, typename O, typename D, bool V> friend struct internal::FullReducer;
902 #ifdef EIGEN_USE_THREADS
903  template <typename S, typename O, bool V> friend struct internal::FullReducerShard;
904 #endif
905 #if defined(EIGEN_USE_GPU) && (defined(EIGEN_GPUCC))
906  template <int B, int N, typename S, typename R, typename I_> KERNEL_FRIEND void internal::FullReductionKernel(R, const S, I_, typename S::CoeffReturnType*, unsigned int*);
907 #if defined(EIGEN_HAS_GPU_FP16)
908  template <typename S, typename R, typename I_> KERNEL_FRIEND void internal::ReductionInitFullReduxKernelHalfFloat(R, const S, I_, internal::packet_traits<Eigen::half>::type*);
909  template <int B, int N, typename S, typename R, typename I_> KERNEL_FRIEND void internal::FullReductionKernelHalfFloat(R, const S, I_, half*, internal::packet_traits<Eigen::half>::type*);
910  template <int NPT, typename S, typename R, typename I_> KERNEL_FRIEND void internal::InnerReductionKernelHalfFloat(R, const S, I_, I_, half*);
911 #endif
912  template <int NPT, typename S, typename R, typename I_> KERNEL_FRIEND void internal::InnerReductionKernel(R, const S, I_, I_, typename S::CoeffReturnType*);
913 
914  template <int NPT, typename S, typename R, typename I_> KERNEL_FRIEND void internal::OuterReductionKernel(R, const S, I_, I_, typename S::CoeffReturnType*);
915 #endif
916 
917 #if defined(EIGEN_USE_SYCL)
918  template < typename Evaluator_, typename Op__> friend class TensorSycl::internal::GenericNondeterministicReducer;
919  // SYCL need the Generic reducer for the case the recution algorithm is neither inner, outer, and full reducer
920  template <typename, typename, typename> friend struct internal::GenericReducer;
921 #endif
922 
923 
924  template <typename S, typename O, typename D> friend struct internal::InnerReducer;
925 
926  struct BlockIteratorState {
930  };
931 
932  // Returns the Index in the input tensor of the first value that needs to be
933  // used to compute the reduction at output index "index".
934  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index firstInput(Index index) const {
935  if (ReducingInnerMostDims) {
936  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
937  return index * m_preservedStrides[0];
938  } else {
939  return index * m_preservedStrides[NumPreservedStrides - 1];
940  }
941  }
942  // TBD: optimize the case where we preserve the innermost dimensions.
943  Index startInput = 0;
944  if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
945  for (int i = NumOutputDims - 1; i > 0; --i) {
946  // This is index_i in the output tensor.
947  const Index idx = index / m_outputStrides[i];
948  startInput += idx * m_preservedStrides[i];
949  index -= idx * m_outputStrides[i];
950  }
951  if (PreservingInnerMostDims) {
952  eigen_assert(m_preservedStrides[0] == 1);
953  startInput += index;
954  } else {
955  startInput += index * m_preservedStrides[0];
956  }
957  } else {
958  for (int i = 0; i < NumOutputDims - 1; ++i) {
959  // This is index_i in the output tensor.
960  const Index idx = index / m_outputStrides[i];
961  startInput += idx * m_preservedStrides[i];
962  index -= idx * m_outputStrides[i];
963  }
964  if (PreservingInnerMostDims) {
965  eigen_assert(m_preservedStrides[NumPreservedStrides - 1] == 1);
966  startInput += index;
967  } else {
968  startInput += index * m_preservedStrides[NumPreservedStrides - 1];
969  }
970  }
971  return startInput;
972  }
973 
974  // Bitmap indicating if an input dimension is reduced or not.
976  // Dimensions of the output of the operation.
978  // Precomputed strides for the output tensor.
982  // Map from output to input dimension index.
984  // How many values go into each reduction
986 
987  // Subset of strides of the input tensor for the reduced dimensions.
988  // Indexed by reduced dimensions.
990  // Size of the input dimensions that are reduced.
991  // Indexed by reduced dimensions.
993 
994  // Evaluator for the input expression.
996 
997  // Operation to apply for computing the reduction.
999 
1001 
1003 };
1004 
1005 template<typename Op, typename Dims, typename ArgType, template <class> class MakePointer_, typename Device>
1006 struct TensorEvaluator<const TensorReductionOp<Op, Dims, ArgType, MakePointer_>, Device>
1007 : public TensorReductionEvaluatorBase<const TensorReductionOp<Op, Dims, ArgType, MakePointer_>, Device> {
1009  EIGEN_STRONG_INLINE TensorEvaluator(const typename Base::XprType& op, const Device& device) : Base(op, device){}
1010 };
1011 
1012 
1013 template<typename Op, typename Dims, typename ArgType, template <class> class MakePointer_>
1014 struct TensorEvaluator<const TensorReductionOp<Op, Dims, ArgType, MakePointer_>, Eigen::SyclDevice>
1015 : public TensorReductionEvaluatorBase<const TensorReductionOp<Op, Dims, ArgType, MakePointer_>, Eigen::SyclDevice> {
1016 
1018  EIGEN_STRONG_INLINE TensorEvaluator(const typename Base::XprType& op, const Eigen::SyclDevice& device) : Base(op, device){}
1019  // The coeff function in the base the recursive method which is not an standard layout and cannot be used in the SYCL kernel
1020  //Therefore the coeff function should be overridden by for SYCL kernel
1021  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename Base::CoeffReturnType coeff(typename Base::Index index) const {
1022  return *(this->data() + index);
1023  }
1024  // The packet function in the base the recursive method which is not an standard layout and cannot be used in the SYCL kernel
1025  //Therefore the packet function should be overridden by for SYCL kernel
1026  template<int LoadMode>
1027  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename Base::PacketReturnType packet(typename Base::Index index) const {
1028  return internal::pload<typename Base::PacketReturnType>(this->data() + index);
1029  }
1030 };
1031 
1032 } // end namespace Eigen
1033 
1034 #endif // EIGEN_CXX11_TENSOR_TENSOR_REDUCTION_H
int i
#define EIGEN_ALIGN_MAX
#define EIGEN_DEVICE_FUNC
#define EIGEN_HIP_LAUNCH_BOUNDS_1024
#define eigen_assert(x)
int data[]
#define EIGEN_STATIC_ASSERT(X, MSG)
#define EIGEN_DEVICE_REF
Definition: TensorMacros.h:36
#define KERNEL_FRIEND
The tensor base class.
static int numThreads(double output_size, const TensorOpCost &cost_per_coeff, int max_threads)
Eigen::internal::traits< TensorReductionOp >::Scalar Scalar
std::remove_const_t< typename XprType::CoeffReturnType > CoeffReturnType
Eigen::internal::nested< TensorReductionOp >::type Nested
Eigen::internal::traits< TensorReductionOp >::StorageKind StorageKind
Eigen::internal::traits< TensorReductionOp >::Index Index
const XprType & expression() const
const Op & reducer() const
TensorReductionOp(const XprType &expr, const Dims &dims, const Op &reducer)
Eigen::NumTraits< Scalar >::Real RealScalar
TensorReductionOp(const XprType &expr, const Dims &dims)
const Dims & dims() const
Type
Index LeafSize< half >()
constexpr auto array_prod(const array< T, N > &arr) -> decltype(array_reduce< product_op, T, N >(arr, static_cast< T >(1)))
Index LeafSize< bfloat16 >()
EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
std::array< T, N > array
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
EIGEN_ALWAYS_INLINE T divup(const X x, const Y y)
Definition: TensorMeta.h:32
internal::packet_traits< Scalar >::type type
Definition: TensorMeta.h:55
SparseMat::Index size
TensorReductionEvaluatorBase< const TensorReductionOp< Op, Dims, ArgType, MakePointer_ >, Eigen::SyclDevice > Base
TensorReductionEvaluatorBase< const TensorReductionOp< Op, Dims, ArgType, MakePointer_ >, Device > Base
A cost model used to limit the number of threads used for evaluating tensor expression.
const Dimensions & dimensions() const
EvaluatorPointerType data() const
TensorReductionEvaluatorBase< const TensorReductionOp< Op, Dims, ArgType, MakePointer_ >, Device > Self
std::conditional_t< NumOutputDims==0, Sizes<>, DSizes< Index, NumOutputDims > > Dimensions
internal::ReductionReturnType< Op, typename XprType::CoeffReturnType >::type CoeffReturnType
std::ptrdiff_t j