11 #ifndef EIGEN_CXX11_TENSOR_TENSOR_REDUCTION_H
12 #define EIGEN_CXX11_TENSOR_TENSOR_REDUCTION_H
24 template<
typename Op,
typename Dims,
typename XprType,
template <
class>
class MakePointer_ >
32 typedef typename XprType::Nested
Nested;
34 static const int Layout = XprTraits::Layout;
43 template<
typename Op,
typename Dims,
typename XprType,
template <
class>
class MakePointer_>
49 template<
typename Op,
typename Dims,
typename XprType,
template <
class>
class MakePointer_>
57 template <
typename InputDims,
typename ReducedDims> EIGEN_DEVICE_FUNC
58 static void run(
const InputDims& input_dims,
60 OutputDims* output_dims, ReducedDims* reduced_dims) {
64 for (
int i = 0; i < NumInputDims; ++i) {
66 (*reduced_dims)[reduceIndex] = input_dims[i];
69 (*output_dims)[outputIndex] = input_dims[i];
77 template <
typename InputDims,
typename Index,
size_t Rank> EIGEN_DEVICE_FUNC
81 for (
int i = 0; i < NumInputDims; ++i) {
82 (*reduced_dims)[i] = input_dims[i];
88 template <
typename ReducedDims,
int NumTensorDims,
int Layout>
90 static const bool value =
false;
92 template <
typename ReducedDims,
int NumTensorDims,
int Layout>
94 static const bool value =
false;
97 #if EIGEN_HAS_CONSTEXPR && EIGEN_HAS_VARIADIC_TEMPLATES
98 template <
typename ReducedDims,
int NumTensorDims>
100 static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
101 static const bool tmp2 = index_statically_eq<ReducedDims>(0, 0);
103 static const bool value = tmp1 & tmp2 & tmp3;
105 template <
typename ReducedDims,
int NumTensorDims>
106 struct are_inner_most_dims<ReducedDims, NumTensorDims,
RowMajor>{
107 static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
108 static const bool tmp2 = index_statically_eq<ReducedDims>(0, NumTensorDims - array_size<ReducedDims>::value);
109 static const bool tmp3 = index_statically_eq<ReducedDims>(array_size<ReducedDims>::value - 1, NumTensorDims - 1);
110 static const bool value = tmp1 & tmp2 & tmp3;
113 template <
typename ReducedDims,
int NumTensorDims>
114 struct preserve_inner_most_dims<ReducedDims, NumTensorDims,
ColMajor>{
115 static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
116 static const bool tmp2 = index_statically_gt<ReducedDims>(0, 0);
117 static const bool value = tmp1 & tmp2;
120 template <
typename ReducedDims,
int NumTensorDims>
121 struct preserve_inner_most_dims<ReducedDims, NumTensorDims,
RowMajor>{
122 static const bool tmp1 = indices_statically_known_to_increase<ReducedDims>();
123 static const bool tmp2 = index_statically_lt<ReducedDims>(array_size<ReducedDims>::value - 1, NumTensorDims - 1);
124 static const bool value = tmp1 & tmp2;
129 template <
int DimIndex,
typename Self,
typename Op>
133 for (
int j = 0; j <
self.m_reducedDims[DimIndex]; ++j) {
134 const typename Self::Index input = firstIndex + j *
self.m_reducedStrides[DimIndex];
139 template <
typename Self,
typename Op>
142 for (
int j = 0; j <
self.m_reducedDims[0]; ++j) {
143 const typename Self::Index input = firstIndex + j *
self.m_reducedStrides[0];
144 reducer.reduce(
self.m_impl.coeff(input), accum);
148 template <
typename Self,
typename Op>
151 reducer.reduce(
self.m_impl.coeff(index), accum);
155 template <
typename Self,
typename Op,
bool Vectorizable = (Self::InputPacketAccess & Op::PacketAccess)>
158 typename Self::CoeffReturnType accum = reducer.initialize();
159 for (
typename Self::Index j = 0; j < numValuesToReduce; ++j) {
160 reducer.reduce(
self.m_impl.coeff(firstIndex + j), &accum);
162 return reducer.finalize(accum);
166 template <
typename Self,
typename Op>
170 const typename Self::Index VectorizedSize = (numValuesToReduce / packetSize) * packetSize;
171 typename Self::PacketReturnType p = reducer.template initializePacket<typename Self::PacketReturnType>();
172 for (
typename Self::Index j = 0; j < VectorizedSize; j += packetSize) {
173 reducer.reducePacket(
self.m_impl.template packet<Unaligned>(firstIndex + j), &p);
175 typename Self::CoeffReturnType accum = reducer.initialize();
176 for (
typename Self::Index j = VectorizedSize; j < numValuesToReduce; ++j) {
177 reducer.reduce(
self.m_impl.coeff(firstIndex + j), &accum);
179 return reducer.finalizeBoth(accum, p);
183 template <
int DimIndex,
typename Self,
typename Op,
bool vectorizable = (Self::InputPacketAccess & Op::PacketAccess)>
190 template <
int DimIndex,
typename Self,
typename Op>
194 for (
typename Self::Index j = 0; j <
self.m_reducedDims[DimIndex]; ++j) {
195 const typename Self::Index input = firstIndex + j *
self.m_reducedStrides[DimIndex];
201 template <
typename Self,
typename Op>
204 for (
typename Self::Index j = 0; j <
self.m_reducedDims[0]; ++j) {
205 const typename Self::Index input = firstIndex + j *
self.m_reducedStrides[0];
206 reducer.reducePacket(
self.m_impl.template packet<Unaligned>(input), accum);
210 template <
typename Self,
typename Op>
218 template <
typename Self,
typename Op,
typename Device,
bool Vectorizable = (Self::InputPacketAccess & Op::PacketAccess)>
222 static EIGEN_DEVICE_FUNC
void run(
const Self&
self, Op& reducer,
const Device&,
typename Self::CoeffReturnType* output) {
229 #ifdef EIGEN_USE_THREADS
231 template <
typename Self,
typename Op,
232 bool Vectorizable = (Self::InputPacketAccess & Op::PacketAccess)>
233 struct FullReducerShard {
235 typename Self::Index numValuesToReduce, Op& reducer,
236 typename Self::CoeffReturnType* output) {
238 self, firstIndex, numValuesToReduce, reducer);
243 template <
typename Self,
typename Op,
bool Vectorizable>
244 struct FullReducer<Self, Op, ThreadPoolDevice, Vectorizable> {
245 static const bool HasOptimizedImplementation = !Op::IsStateful;
246 static const int PacketSize =
250 static void run(
const Self&
self, Op& reducer,
const ThreadPoolDevice& device,
251 typename Self::CoeffReturnType* output) {
254 if (num_coeffs == 0) {
255 *output = reducer.finalize(reducer.initialize());
258 const TensorOpCost cost =
259 self.m_impl.costPerCoeff(Vectorizable) +
263 num_coeffs, cost, device.numThreads());
264 if (num_threads == 1) {
269 const Index blocksize =
270 std::floor<Index>(
static_cast<float>(num_coeffs) / num_threads);
271 const Index numblocks = blocksize > 0 ? num_coeffs / blocksize : 0;
274 Barrier barrier(internal::convert_index<unsigned int>(numblocks));
275 MaxSizeVector<typename Self::CoeffReturnType> shards(numblocks, reducer.initialize());
276 for (
Index i = 0; i < numblocks; ++i) {
278 self, i * blocksize, blocksize, reducer,
281 typename Self::CoeffReturnType finalShard;
282 if (numblocks * blocksize < num_coeffs) {
284 self, numblocks * blocksize, num_coeffs - numblocks * blocksize,
287 finalShard = reducer.initialize();
291 for (
Index i = 0; i < numblocks; ++i) {
292 reducer.reduce(shards[i], &finalShard);
294 *output = reducer.finalize(finalShard);
302 template <
typename Self,
typename Op,
typename Device>
306 EIGEN_DEVICE_FUNC
static bool run(
const Self&, Op&,
const Device&,
typename Self::CoeffReturnType*,
typename Self::Index,
typename Self::Index) {
313 template <
typename Self,
typename Op,
typename Device>
317 EIGEN_DEVICE_FUNC
static bool run(
const Self&, Op&,
const Device&,
typename Self::CoeffReturnType*,
typename Self::Index,
typename Self::Index) {
324 #if defined(EIGEN_USE_GPU) && defined(__CUDACC__)
325 template <
int B,
int N,
typename S,
typename R,
typename I>
326 __global__
void FullReductionKernel(R,
const S, I,
typename S::CoeffReturnType*,
unsigned int*);
329 #ifdef EIGEN_HAS_CUDA_FP16
330 template <
typename S,
typename R,
typename I>
331 __global__
void ReductionInitFullReduxKernelHalfFloat(R,
const S, I, half2*);
332 template <
int B,
int N,
typename S,
typename R,
typename I>
333 __global__
void FullReductionKernelHalfFloat(R,
const S, I,
half*, half2*);
334 template <
int NPT,
typename S,
typename R,
typename I>
335 __global__
void InnerReductionKernelHalfFloat(R,
const S, I, I,
half*);
339 template <
int NPT,
typename S,
typename R,
typename I>
340 __global__
void InnerReductionKernel(R,
const S, I, I,
typename S::CoeffReturnType*);
342 template <
int NPT,
typename S,
typename R,
typename I>
343 __global__
void OuterReductionKernel(R,
const S, I, I,
typename S::CoeffReturnType*);
349 template <
typename Op,
typename Dims,
typename XprType,
template <
class>
class MakePointer_>
350 class TensorReductionOp :
public TensorBase<TensorReductionOp<Op, Dims, XprType, MakePointer_>, ReadOnlyAccessors> {
381 template<
typename Op,
typename Dims,
typename ArgType,
template <
class>
class MakePointer_,
typename Device>
390 static const int NumOutputDims = NumInputDims - NumReducedDims;
409 static const bool RunningFullReduction = (NumOutputDims==0);
415 EIGEN_STATIC_ASSERT((!ReducingInnerMostDims | !PreservingInnerMostDims | (NumReducedDims == NumInputDims)),
416 YOU_MADE_A_PROGRAMMING_MISTAKE);
419 for (
int i = 0; i < NumInputDims; ++i) {
420 m_reduced[i] =
false;
422 for (
int i = 0; i < NumReducedDims; ++i) {
425 m_reduced[op.
dims()[i]] =
true;
432 if (NumOutputDims > 0) {
434 m_outputStrides[0] = 1;
435 for (
int i = 1; i < NumOutputDims; ++i) {
436 m_outputStrides[i] = m_outputStrides[i - 1] * m_dimensions[i - 1];
439 m_outputStrides.back() = 1;
440 for (
int i = NumOutputDims - 2; i >= 0; --i) {
441 m_outputStrides[i] = m_outputStrides[i + 1] * m_dimensions[i + 1];
447 if (NumInputDims > 0) {
450 input_strides[0] = 1;
451 for (
int i = 1; i < NumInputDims; ++i) {
452 input_strides[i] = input_strides[i-1] * input_dims[i-1];
455 input_strides.
back() = 1;
456 for (
int i = NumInputDims - 2; i >= 0; --i) {
457 input_strides[i] = input_strides[i + 1] * input_dims[i + 1];
463 for (
int i = 0; i < NumInputDims; ++i) {
465 m_reducedStrides[reduceIndex] = input_strides[i];
468 m_preservedStrides[outputIndex] = input_strides[i];
475 if (NumOutputDims == 0) {
483 m_impl.evalSubExprsIfNeeded(NULL);
486 if ((RunningFullReduction && RunningOnSycl) ||(RunningFullReduction &&
488 ((RunningOnGPU && (
m_device.majorDeviceVersion() >= 3)) ||
490 bool need_assign =
false;
496 Op reducer(m_reducer);
500 else if(RunningOnSycl){
507 Op reducer(m_reducer);
509 return (m_result != NULL);
513 else if (RunningOnGPU && (
m_device.majorDeviceVersion() >= 3)) {
514 bool reducing_inner_dims =
true;
515 for (
int i = 0; i < NumReducedDims; ++i) {
517 reducing_inner_dims &= m_reduced[i];
519 reducing_inner_dims &= m_reduced[NumInputDims - 1 - i];
523 (reducing_inner_dims || ReducingInnerMostDims)) {
527 if (num_coeffs_to_preserve < 1024 && num_values_to_reduce > num_coeffs_to_preserve && num_values_to_reduce > 128) {
535 Op reducer(m_reducer);
543 return (m_result != NULL);
547 bool preserving_inner_dims =
true;
548 for (
int i = 0; i < NumReducedDims; ++i) {
550 preserving_inner_dims &= m_reduced[NumInputDims - 1 - i];
552 preserving_inner_dims &= m_reduced[i];
556 preserving_inner_dims) {
560 if (num_coeffs_to_preserve < 1024 && num_values_to_reduce > num_coeffs_to_preserve && num_values_to_reduce > 32) {
568 Op reducer(m_reducer);
576 return (m_result != NULL);
593 if ((RunningOnSycl || RunningFullReduction || RunningOnGPU) && m_result) {
594 return *(m_result + index);
596 Op reducer(m_reducer);
597 if (ReducingInnerMostDims || RunningFullReduction) {
598 const Index num_values_to_reduce =
599 (
static_cast<int>(
Layout) ==
static_cast<int>(
ColMajor)) ? m_preservedStrides[0] : m_preservedStrides[NumPreservedStrides - 1];
601 num_values_to_reduce, reducer);
605 return reducer.finalize(accum);
610 template<
int LoadMode>
616 if (RunningOnGPU && m_result) {
617 return internal::pload<PacketReturnType>(m_result + index);
621 if (ReducingInnerMostDims) {
622 const Index num_values_to_reduce =
623 (
static_cast<int>(
Layout) ==
static_cast<int>(
ColMajor)) ? m_preservedStrides[0] : m_preservedStrides[NumPreservedStrides - 1];
624 const Index firstIndex = firstInput(index);
625 for (
Index i = 0; i < PacketSize; ++i) {
626 Op reducer(m_reducer);
628 num_values_to_reduce, reducer);
630 }
else if (PreservingInnerMostDims) {
631 const Index firstIndex = firstInput(index);
632 const int innermost_dim = (
static_cast<int>(
Layout) ==
static_cast<int>(
ColMajor)) ? 0 : NumOutputDims - 1;
634 if (((firstIndex % m_dimensions[innermost_dim]) + PacketSize - 1) < m_dimensions[innermost_dim]) {
635 Op reducer(m_reducer);
636 typename Self::PacketReturnType accum = reducer.template initializePacket<typename Self::PacketReturnType>();
638 return reducer.finalizePacket(accum);
640 for (
int i = 0; i < PacketSize; ++i) {
641 values[i] =
coeff(index + i);
645 for (
int i = 0; i < PacketSize; ++i) {
646 values[i] =
coeff(index + i);
655 if (RunningFullReduction && m_result) {
660 return m_impl.costPerCoeff(vectorized) * num_values_to_reduce +
661 TensorOpCost(0, 0, compute_cost, vectorized, PacketSize);
671 const Dims&
xprDims()
const {
return m_xpr_dims;}
679 #ifdef EIGEN_USE_THREADS
680 template <
typename S,
typename O,
bool V>
friend struct internal::FullReducerShard;
682 #if defined(EIGEN_USE_GPU) && defined(__CUDACC__)
683 template <
int B,
int N,
typename S,
typename R,
typename I>
friend void internal::FullReductionKernel(R,
const S, I,
typename S::CoeffReturnType*,
unsigned int*);
684 #ifdef EIGEN_HAS_CUDA_FP16
685 template <
typename S,
typename R,
typename I>
friend void internal::ReductionInitFullReduxKernelHalfFloat(R,
const S, I, half2*);
686 template <
int B,
int N,
typename S,
typename R,
typename I>
friend void internal::FullReductionKernelHalfFloat(R,
const S, I,
half*, half2*);
687 template <
int NPT,
typename S,
typename R,
typename I>
friend void internal::InnerReductionKernelHalfFloat(R,
const S, I, I,
half*);
689 template <
int NPT,
typename S,
typename R,
typename I>
friend void internal::InnerReductionKernel(R,
const S, I, I,
typename S::CoeffReturnType*);
691 template <
int NPT,
typename S,
typename R,
typename I>
friend void internal::OuterReductionKernel(R,
const S, I, I,
typename S::CoeffReturnType*);
699 if (ReducingInnerMostDims) {
701 return index * m_preservedStrides[0];
703 return index * m_preservedStrides[NumPreservedStrides - 1];
707 Index startInput = 0;
709 for (
int i = NumOutputDims - 1; i > 0; --i) {
711 const Index idx = index / m_outputStrides[i];
712 startInput += idx * m_preservedStrides[i];
713 index -= idx * m_outputStrides[i];
715 if (PreservingInnerMostDims) {
719 startInput += index * m_preservedStrides[0];
722 for (
int i = 0; i < NumOutputDims - 1; ++i) {
724 const Index idx = index / m_outputStrides[i];
725 startInput += idx * m_preservedStrides[i];
726 index -= idx * m_outputStrides[i];
728 if (PreservingInnerMostDims) {
729 eigen_assert(m_preservedStrides[NumPreservedStrides - 1] == 1);
732 startInput += index * m_preservedStrides[NumPreservedStrides - 1];
763 #if defined(EIGEN_USE_GPU) && defined(__CUDACC__)
765 static const bool RunningOnSycl =
false;
766 #elif defined(EIGEN_USE_SYCL)
768 static const bool RunningOnGPU =
false;
770 static const bool RunningOnGPU =
false;
771 static const bool RunningOnSycl =
false;
781 #endif // EIGEN_CXX11_TENSOR_TENSOR_REDUCTION_H