From 845aa94569e9e06655749596d96a3f0c7d11e9eb Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Wed, 18 Nov 2026 13:47:51 +0100 Subject: [PATCH] Reduce both extrema in one tree in the complex minmax kernel The complex `dpnp.minmax` kernel reduced the minimum and the maximum with two `custom_reduce_over_group` calls separated by a barrier. In the coverage build, the OpenCL CPU compiler intermittently corrupts its heap in `ReduceCrossBarrierValuesPass` while building this kernel (CMPLRLLVM-79305), which kills the coverage job. Fold both extrema in one tree over a local memory of twice the work-group size, with one barrier per step for both. --- dpnp/libtensor/include/kernels/reductions.hpp | 53 ++++++++++++++++--- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/dpnp/libtensor/include/kernels/reductions.hpp b/dpnp/libtensor/include/kernels/reductions.hpp index bda7231658f..133ddbd2911 100644 --- a/dpnp/libtensor/include/kernels/reductions.hpp +++ b/dpnp/libtensor/include/kernels/reductions.hpp @@ -3432,12 +3432,13 @@ struct can_use_minmax_group_reducer can_use_reduce_over_group::value); }; -/* @brief Reduces both extrema over a work-group with - * custom_reduce_over_group */ +/* @brief Reduces both extrema over a work-group in one tree in local memory, + * as custom_reduce_over_group does for one value */ template struct CustomMinMaxGroupReducer { private: + // minima in [0, wgs), maxima in [wgs, 2 * wgs) SlmT local_mem_; public: @@ -3446,12 +3447,48 @@ struct CustomMinMaxGroupReducer template void operator()(const GroupT &wg, argT &red_min, argT &red_max) const { - red_min = - su_ns::custom_reduce_over_group(wg, local_mem_, red_min, MinOpT()); - // local memory is reused for the maximum + // the tuning of custom_reduce_over_group + static constexpr std::uint32_t low_sz = 8u; + static constexpr std::uint32_t high_sz = 1024u; + + const std::uint32_t wgs = wg.get_local_linear_range(); + const std::uint32_t lid = wg.get_local_linear_id(); + const MinOpT min_op{}; + const MaxOpT max_op{}; + + // one tree for both: a tree each crashed the OpenCL CPU compiler in + // its barrier pass (CMPLRLLVM-79305) + local_mem_[lid] = red_min; + local_mem_[wgs + lid] = red_max; sycl::group_barrier(wg, sycl::memory_scope::work_group); - red_max = - su_ns::custom_reduce_over_group(wg, local_mem_, red_max, MaxOpT()); + + std::uint32_t n_witems = wgs; +#pragma unroll + for (std::uint32_t sz = high_sz; sz >= low_sz; sz >>= 1) { + if (n_witems >= sz) { + const std::uint32_t step = (n_witems + 1) >> 1; + if (lid < n_witems - step) { + local_mem_[lid] = + min_op(local_mem_[lid], local_mem_[step + lid]); + local_mem_[wgs + lid] = max_op( + local_mem_[wgs + lid], local_mem_[wgs + step + lid]); + } + sycl::group_barrier(wg, sycl::memory_scope::work_group); + n_witems = step; + } + } + + argT min_over_wg = local_mem_[0]; + argT max_over_wg = local_mem_[wgs]; + if (wg.leader()) { + for (std::uint32_t i = 1; i < n_witems; ++i) { + min_over_wg = min_op(min_over_wg, local_mem_[i]); + max_over_wg = max_op(max_over_wg, local_mem_[wgs + i]); + } + } + + red_min = sycl::group_broadcast(wg, min_over_wg, 0); + red_max = sycl::group_broadcast(wg, max_over_wg, 0); } }; @@ -3633,7 +3670,7 @@ sycl::event } else { using SlmT = sycl::local_accessor; - SlmT local_memory = SlmT(localRange, cgh); + SlmT local_memory = SlmT(sycl::range<1>{2 * wg}, cgh); using GroupReducerT = CustomMinMaxGroupReducer;