11#ifndef EIGEN_CXX11_TENSOR_TENSOR_REDUCTION_H
12#define EIGEN_CXX11_TENSOR_TENSOR_REDUCTION_H
17 template<
typename Op,
typename Dims,
typename XprType,
template <
class>
class MakePointer_ >
18 struct traits<TensorReductionOp<Op, Dims, XprType, MakePointer_> >
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;
29 template <
class T>
struct MakePointer {
31 typedef MakePointer_<T> MakePointerT;
32 typedef typename MakePointerT::Type Type;
36template<
typename Op,
typename Dims,
typename XprType,
template <
class>
class MakePointer_>
37struct eval<TensorReductionOp<Op, Dims, XprType, MakePointer_>, Eigen::Dense>
39 typedef const TensorReductionOp<Op, Dims, XprType, MakePointer_>& type;
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>
45 typedef TensorReductionOp<Op, Dims, XprType, MakePointer_> type;
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;
57 for (
int i = 0; i < NumInputDims; ++i) {
59 (*reduced_dims)[reduceIndex] = input_dims[i];
62 (*output_dims)[outputIndex] = input_dims[i];
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];
81template <
typename ReducedDims,
int NumTensorDims,
int Layout>
82struct are_inner_most_dims {
83 static const bool value =
false;
85template <
typename ReducedDims,
int NumTensorDims,
int Layout>
86struct preserve_inner_most_dims {
87 static const bool value =
false;
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;
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;
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;
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;
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);
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);
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);
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);
155 return reducer.finalize(accum);
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);
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);
172 return reducer.finalizeBoth(accum, p);
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");
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);
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);
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");
211template <
typename Self,
typename Op,
typename Device,
bool Vectorizable = (Self::InputPacketAccess & Op::PacketAccess)>
213 static const bool HasOptimizedImplementation =
false;
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);
222#ifdef EIGEN_USE_THREADS
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);
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;
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());
251 const TensorOpCost cost =
252 self.m_impl.costPerCoeff(Vectorizable) +
253 TensorOpCost(0, 0, internal::functor_traits<Op>::Cost, Vectorizable,
255 const int num_threads = TensorCostModel<ThreadPoolDevice>::numThreads(
256 num_coeffs, cost, device.numThreads());
257 if (num_threads == 1) {
259 InnerMostDimReducer<Self, Op, Vectorizable>::reduce(self, 0, num_coeffs, reducer);
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);
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,
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,
280 finalShard = reducer.initialize();
284 for (
Index i = 0; i < numblocks; ++i) {
285 reducer.reduce(shards[i], &finalShard);
287 *output = reducer.finalize(finalShard);
295template <
typename Self,
typename Op,
typename Device>
297 static const bool HasOptimizedImplementation =
false;
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");
306template <
typename Self,
typename Op,
typename Device>
308 static const bool HasOptimizedImplementation =
false;
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");
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*);
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*);
332template <
int NPT,
typename S,
typename R,
typename I>
333__global__
void InnerReductionKernel(R,
const S, I, I,
typename S::CoeffReturnType*);
335template <
int NPT,
typename S,
typename R,
typename I>
336__global__
void OuterReductionKernel(R,
const S, I, I,
typename S::CoeffReturnType*);
347template <
typename Op,
typename Dims,
typename XprType,
template <
class>
class MakePointer_>
348class TensorReductionOp :
public TensorBase<TensorReductionOp<Op, Dims, XprType, MakePointer_>, ReadOnlyAccessors> {
350 typedef typename Eigen::internal::traits<TensorReductionOp>::Scalar Scalar;
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;
357 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
358 TensorReductionOp(
const XprType& expr,
const Dims& dims) : m_expr(expr), m_dims(dims)
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)
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; }
372 typename XprType::Nested m_expr;
379template<
typename Op,
typename Dims,
typename ArgType,
template <
class>
class MakePointer_,
typename Device>
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;
399 PacketAccess = Self::InputPacketAccess && Op::PacketAccess,
400 Layout = TensorEvaluator<ArgType, Device>::Layout,
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);
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())
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);
417 for (
int i = 0; i < NumInputDims; ++i) {
418 m_reduced[i] =
false;
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;
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);
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];
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];
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];
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];
461 for (
int i = 0; i < NumInputDims; ++i) {
463 m_reducedStrides[reduceIndex] = input_strides[i];
466 m_preservedStrides[outputIndex] = input_strides[i];
473 if (NumOutputDims == 0) {
474 m_preservedStrides[0] = internal::array_prod(input_dims);
478 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
480 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC
bool evalSubExprsIfNeeded(
typename MakePointer_<CoeffReturnType>::Type data) {
481 m_impl.evalSubExprsIfNeeded(NULL);
484 if ((RunningFullReduction && RunningOnSycl) ||(RunningFullReduction &&
485 internal::FullReducer<Self, Op, Device>::HasOptimizedImplementation &&
486 ((RunningOnGPU && (m_device.majorDeviceVersion() >= 3)) ||
488 bool need_assign =
false;
490 m_result =
static_cast<CoeffReturnType*
>(m_device.allocate(
sizeof(CoeffReturnType)));
494 Op reducer(m_reducer);
495 internal::FullReducer<Self, Op, Device>::run(*
this, reducer, m_device, data);
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));
502 data =
static_cast<CoeffReturnType*
>(m_device.allocate(
sizeof(CoeffReturnType) * num_coeffs_to_preserve));
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);
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];
517 reducing_inner_dims &= m_reduced[NumInputDims - 1 - i];
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);
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));
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)) {
536 m_device.deallocate(m_result);
541 return (m_result != NULL);
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];
550 preserving_inner_dims &= m_reduced[i];
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);
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));
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)) {
569 m_device.deallocate(m_result);
574 return (m_result != NULL);
581 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void cleanup() {
584 m_device.deallocate(m_result);
589 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const
591 if ((RunningOnSycl || RunningFullReduction || RunningOnGPU) && m_result) {
592 return *(m_result + index);
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);
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);
608 template<
int LoadMode>
609 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const
611 EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
612 eigen_assert(index + PacketSize - 1 < Index(internal::array_prod(dimensions())));
614 if (RunningOnGPU && m_result) {
615 return internal::pload<PacketReturnType>(m_result + index);
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);
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;
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);
638 for (
int i = 0; i < PacketSize; ++i) {
639 values[i] = coeff(index + i);
643 for (
int i = 0; i < PacketSize; ++i) {
644 values[i] = coeff(index + i);
647 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
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);
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);
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;}
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;
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*);
687 template <
int NPT,
typename S,
typename R,
typename I>
friend __global__
void internal::InnerReductionKernel(R,
const S, I, I,
typename S::CoeffReturnType*);
689 template <
int NPT,
typename S,
typename R,
typename I>
friend __global__
void internal::OuterReductionKernel(R,
const S, I, I,
typename S::CoeffReturnType*);
692 template <
typename S,
typename O,
typename D>
friend struct internal::InnerReducer;
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];
701 return index * m_preservedStrides[NumPreservedStrides - 1];
705 Index startInput = 0;
706 if (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
707 for (
int i = NumOutputDims - 1; i > 0; --i) {
709 const Index idx = index / m_outputStrides[i];
710 startInput += idx * m_preservedStrides[i];
711 index -= idx * m_outputStrides[i];
713 if (PreservingInnerMostDims) {
714 eigen_assert(m_preservedStrides[0] == 1);
717 startInput += index * m_preservedStrides[0];
720 for (
int i = 0; i < NumOutputDims - 1; ++i) {
722 const Index idx = index / m_outputStrides[i];
723 startInput += idx * m_preservedStrides[i];
724 index -= idx * m_outputStrides[i];
726 if (PreservingInnerMostDims) {
727 eigen_assert(m_preservedStrides[NumPreservedStrides - 1] == 1);
730 startInput += index * m_preservedStrides[NumPreservedStrides - 1];
737 array<bool, NumInputDims> m_reduced;
739 Dimensions m_dimensions;
741 array<Index, NumOutputDims> m_outputStrides;
744 static const int NumPreservedStrides = max_n_1<NumOutputDims>::size;
745 array<Index, NumPreservedStrides> m_preservedStrides;
749 array<Index, NumReducedDims> m_reducedStrides;
752 array<Index, NumReducedDims> m_reducedDims;
755 TensorEvaluator<ArgType, Device> m_impl;
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;
768 static const bool RunningOnGPU =
false;
769 static const bool RunningOnSycl =
false;
771 typename MakePointer_<CoeffReturnType>::Type m_result;
773 const Device& m_device;
774 const Dims& m_xpr_dims;
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