|
| 1 | +#ifndef PFClusterProducer_plugins_alpaka_PFMultiDepthClusterizerHelper_h |
| 2 | +#define PFClusterProducer_plugins_alpaka_PFMultiDepthClusterizerHelper_h |
| 3 | + |
| 4 | +/** |
| 5 | + * @file PFMultiDepthClusterizerHelper.h |
| 6 | + * @brief Warp-level utility functions for particle flow multi-depth clustering. |
| 7 | + * |
| 8 | + * This header provides basic warp-synchronous operations used in clustering algorithms, |
| 9 | + * including bitwise manipulations (least/most significant set bits) and masked |
| 10 | + * warp-exclusive sum computations. |
| 11 | + */ |
| 12 | + |
| 13 | +#include "HeterogeneousCore/AlpakaInterface/interface/config.h" |
| 14 | + |
| 15 | +#include "RecoParticleFlow/PFClusterProducer/interface/alpaka/PFMultiDepthClusterWarpIntrinsics.h" |
| 16 | + |
| 17 | +namespace ALPAKA_ACCELERATOR_NAMESPACE { |
| 18 | + |
| 19 | + using namespace cms::alpakatools; |
| 20 | + |
| 21 | + /** |
| 22 | + * @brief Returns the position of the least significant set bit in a mask. |
| 23 | + * |
| 24 | + * @tparam TAcc Alpaka accelerator type. |
| 25 | + * |
| 26 | + * @param acc Alpaka accelerator instance. |
| 27 | + * @param x Input bitmask. |
| 28 | + * |
| 29 | + * @return Index of least significant 1 bit (0-based). (or -1 if x == 0). |
| 30 | + */ |
| 31 | + template< typename TAcc > |
| 32 | + ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE unsigned int get_ls1b_idx(TAcc const& acc, const int x) { |
| 33 | + const int pos = alpaka::ffs(acc, x); |
| 34 | + return static_cast<unsigned int>(pos - 1); |
| 35 | + } |
| 36 | + |
| 37 | +/** |
| 38 | + * @brief Clears the least significant set bit in a mask. |
| 39 | + * |
| 40 | + * @tparam TAcc Alpaka accelerator type. |
| 41 | + * |
| 42 | + * @param acc Alpaka accelerator instance. |
| 43 | + * @param x Input bitmask. |
| 44 | + * |
| 45 | + * @return Bitmask with least significant 1 bit cleared. |
| 46 | + */ |
| 47 | + |
| 48 | + template< typename TAcc > |
| 49 | + ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE unsigned int erase_ls1b(TAcc const& acc, const unsigned int x) { |
| 50 | + return (x & (x-1)); |
| 51 | + } |
| 52 | + |
| 53 | +/** |
| 54 | + * @brief Returns the position of the most significant set bit in a mask. |
| 55 | + * |
| 56 | + * @tparam TAcc Alpaka accelerator type. |
| 57 | + * |
| 58 | + * @param acc Alpaka accelerator instance. |
| 59 | + * @param x Input bitmask. |
| 60 | + * |
| 61 | + * @return Index of most significant 1 bit (0-based). (or -1 if x == 0) |
| 62 | + */ |
| 63 | + |
| 64 | + template< typename TAcc > |
| 65 | + ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE unsigned int get_ms1b_idx(TAcc const& acc, const unsigned int x) { |
| 66 | + constexpr unsigned int size = sizeof(unsigned int)-1; |
| 67 | + const int pos = size - cms::alpakatools::clz(acc, x); |
| 68 | + return pos - 1; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @brief Performs warp-level exclusive prefix sum under a custom lane mask. |
| 73 | + * |
| 74 | + * @tparam TAcc Alpaka accelerator type. |
| 75 | + * @tparam accum If true, broadcast total accumulated value to lowest active lane. |
| 76 | + * |
| 77 | + * @param acc Alpaka accelerator instance. |
| 78 | + * @param mask Active lane mask. |
| 79 | + * @param val Value to include in the prefix sum. |
| 80 | + * @param lane_idx Current thread's lane index. |
| 81 | + * |
| 82 | + * @return Exclusive prefix sum value for the current lane. |
| 83 | + */ |
| 84 | + |
| 85 | + template <typename TAcc, bool all = true, typename = std::enable_if_t<alpaka::isAccelerator<TAcc>>> |
| 86 | + ALPAKA_FN_ACC ALPAKA_FN_INLINE unsigned int warp_exclusive_sum(TAcc const& acc, const unsigned int mask, unsigned int val, const unsigned int lane_idx) { |
| 87 | + if ( mask == 0x0 ) return 0; |
| 88 | + |
| 89 | + const unsigned int w_extent = alpaka::warp::getSize(acc); |
| 90 | + // |
| 91 | + unsigned int local_offset = 0; |
| 92 | + // |
| 93 | + CMS_UNROLL_LOOP |
| 94 | + for (unsigned int j = 1; j < w_extent; j *= 2) { |
| 95 | + const auto n = warp::shfl_up_mask(acc, mask, val, j, w_extent); |
| 96 | + if (lane_idx >= j) local_offset += n; |
| 97 | + } |
| 98 | + // |
| 99 | + warp::syncWarpThreads_mask(acc, mask); |
| 100 | + |
| 101 | + if constexpr (!all) { |
| 102 | + return local_offset; |
| 103 | + } else { |
| 104 | + // Compute the lowest and the highest valid lane index in the mask: |
| 105 | + const unsigned low_lane_idx = get_ls1b_idx(acc, mask); |
| 106 | + const unsigned high_lane_idx = get_ms1b_idx(acc, mask); |
| 107 | + |
| 108 | + // send last lane value (total tile offset) to lane idx = low_lane_idx: |
| 109 | + const unsigned active_mask = 1 | (1 << high_lane_idx); |
| 110 | + const unsigned x = warp::shfl_mask(acc, active_mask, local_offset + val, high_lane_idx, w_extent); |
| 111 | + // |
| 112 | + if (lane_idx == low_lane_idx) local_offset = x; |
| 113 | + |
| 114 | + warp::syncWarpThreads_mask(acc, mask); |
| 115 | + } |
| 116 | + return local_offset; |
| 117 | + } |
| 118 | +/** |
| 119 | + * @brief Returns logical index for a given physical lane index based on custom lane mask. |
| 120 | + * |
| 121 | + * @tparam TAcc Alpaka accelerator type. |
| 122 | + * |
| 123 | + * @param acc Alpaka accelerator instance. |
| 124 | + * @param mask Input bitmask. |
| 125 | + * @param lane_idx imput phys. lane index |
| 126 | + * |
| 127 | + * @return Index of the lane in the mask |
| 128 | + */ |
| 129 | + |
| 130 | + template< typename TAcc > |
| 131 | + ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE unsigned int get_logical_lane_idx(TAcc const& acc, const unsigned int mask, const unsigned int lane_idx) { |
| 132 | + const auto lane_mask = mask & ((1 << lane_idx) - 1); |
| 133 | + return alpaka::popcount(acc, lane_mask); // Count 1s below current lane |
| 134 | + } |
| 135 | + |
| 136 | +/** |
| 137 | + * @brief Returns logical index for a given physical lane index based on custom lane mask. |
| 138 | + * |
| 139 | + * @tparam TAcc Alpaka accelerator type. |
| 140 | + * |
| 141 | + * @param acc Alpaka accelerator instance. |
| 142 | + * @param mask Input bitmask. |
| 143 | + * @param lane_idx imput phys. lane index |
| 144 | + * |
| 145 | + * @return Index of the lane in the mask |
| 146 | + */ |
| 147 | + |
| 148 | + template< typename TAcc > |
| 149 | + ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE unsigned int get_high_neighbor_logical_lane_idx(TAcc const& acc, const unsigned int active_mask, const unsigned int custom_mask, const unsigned int lane_idx) { |
| 150 | + // Zero out all bits <= lid |
| 151 | + const auto zeroed_lowbit_mask = custom_mask & (active_mask << (lane_idx+1)); |
| 152 | + // Just in case if the mask is exactly zero (may happen!): |
| 153 | + return zeroed_lowbit_mask == 0x0 ? lane_idx : get_ls1b_idx(acc, zeroed_lowbit_mask); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @brief generic warp reduction |
| 158 | + * |
| 159 | + * @tparam TAcc Alpaka accelerator type. |
| 160 | + * |
| 161 | + * @param acc Alpaka accelerator instance. |
| 162 | + * @param mask Input bitmask. |
| 163 | + * @param in imput value to reduce |
| 164 | + * @param f reducer |
| 165 | + * |
| 166 | + * @return return reduced value (propagated to all lanes in the mask by default) |
| 167 | + */ |
| 168 | + |
| 169 | + template< typename TAcc, typename reduce_t, typename reducer_t > |
| 170 | + ALPAKA_FN_ACC ALPAKA_FN_INLINE reduce_t warp_reduce(TAcc const& acc, unsigned int mask, reduce_t const in, const reducer_t f, bool all = true) |
| 171 | + { |
| 172 | + unsigned int const warpExtent = alpaka::warp::getSize(acc); |
| 173 | + // |
| 174 | + reduce_t result = static_cast<reduce_t>(0); |
| 175 | + |
| 176 | + for (unsigned int offset = warpExtent / 2; offset > 0; offset /= 2) { |
| 177 | + result = f(result, warp::shfl_down_mask(acc, mask, result, offset, warpExtent)); |
| 178 | + } |
| 179 | + |
| 180 | + if (all) result = warp::shfl_mask(acc, mask, result, 0, warpExtent); |
| 181 | + |
| 182 | + return result; |
| 183 | + } |
| 184 | + |
| 185 | +} |
| 186 | + |
| 187 | +#endif |
| 188 | + |
0 commit comments