okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- group_utils_detail.hpp ---------------------*- C++----------------==// 2// 3// Copyright (C) Intel Corporation 4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 5// See https://llvm.org/LICENSE.txt for license information. 6// 7//===---------------------------------------------------------------------===// 8#ifndef __DPCT_DETAIL_GROUP_UTILS_DETAIL_HPP__ 9#define __DPCT_DETAIL_GROUP_UTILS_DETAIL_HPP__ 10 11#include <iterator> 12#include <stdexcept> 13#include <sycl/sycl.hpp> 14 15namespace dpct { 16 17namespace group { 18namespace detail { 19 20typedef uint16_t digit_counter_type ; 21typedef uint32_t packed_counter_type ; 22 23template < int N ,int CURRENT_VAL = N ,int COUNT = 0 > struct log2 { 24enum {VALUE = log2 < N , (CURRENT_VAL >>1 ),COUNT + 1 > ::VALUE }; 25}; 26 27template < int N ,int COUNT > struct log2 < N ,0 ,COUNT > { 28enum {VALUE = (1 << (COUNT - 1 )< N ) ?COUNT :COUNT - 1 }; 29}; 30 31template < int RADIX_BITS ,bool DESCENDING = false> class radix_rank { 32public : 33static size_t get_local_memory_size (size_t group_threads ) { 34return group_threads * PADDED_COUNTER_LANES * sizeof (packed_counter_type ); 35 } 36 37radix_rank (uint8_t * local_memory ) :_local_memory (local_memory ) {} 38 39template < typename Item ,typename KT ,int VALUES_PER_THREAD > 40__dpct_inline__ void 41rank_keys (const Item & item ,KT (& keys )[VALUES_PER_THREAD ], 42int (& ranks )[VALUES_PER_THREAD ],int current_bit ,int num_bits ) { 43 44digit_counter_type thread_prefixes [VALUES_PER_THREAD ]; 45digit_counter_type * digit_counters [VALUES_PER_THREAD ]; 46digit_counter_type * buffer = 47reinterpret_cast < digit_counter_type *> (_local_memory ); 48 49reset_local_memory (item ); 50 51item .barrier (sycl::access::fence_space::local_space ); 52 53#pragma unroll 54for (int i = 0 ;i < VALUES_PER_THREAD ;++ i ) { 55uint32_t digit = ::dpct::bfe (keys [i ],current_bit ,num_bits ); 56uint32_t sub_counter = digit >>LOG_COUNTER_LANES ; 57uint32_t counter_lane = digit & (COUNTER_LANES - 1 ); 58 59if (DESCENDING ) { 60sub_counter = PACKING_RATIO - 1 - sub_counter ; 61counter_lane = COUNTER_LANES - 1 - counter_lane ; 62 } 63 64digit_counters [i ]= 65& buffer [counter_lane * item .get_local_range ().size ()* PACKING_RATIO + 66item .get_local_linear_id ()* PACKING_RATIO + sub_counter ]; 67thread_prefixes [i ]= * digit_counters [i ]; 68* digit_counters [i ]= thread_prefixes [i ]+ 1 ; 69 } 70 71item .barrier (sycl::access::fence_space::local_space ); 72 73scan_counters (item ); 74 75item .barrier (sycl::access::fence_space::local_space ); 76 77for (int i = 0 ;i < VALUES_PER_THREAD ;++ i ) { 78ranks [i ]= thread_prefixes [i ]+ * digit_counters [i ]; 79 } 80 } 81 82private : 83template < typename Item > 84__dpct_inline__ void reset_local_memory (const Item & item ) { 85packed_counter_type * ptr = 86reinterpret_cast < packed_counter_type *> (_local_memory ); 87 88#pragma unroll 89for (int i = 0 ;i < PADDED_COUNTER_LANES ;++ i ) { 90ptr [i * item .get_local_range ().size ()+ item .get_local_linear_id ()]= 0 ; 91 } 92 } 93 94template < typename Item > 95__dpct_inline__ packed_counter_typeupsweep (const Item & item ) { 96packed_counter_type sum = 0 ; 97packed_counter_type * ptr = 98reinterpret_cast < packed_counter_type *> (_local_memory ); 99 100#pragma unroll 101for (int i = 0 ;i < PADDED_COUNTER_LANES ;i ++ ) { 102cached_segment [i ]= 103ptr [item .get_local_linear_id ()* PADDED_COUNTER_LANES + i ]; 104 } 105 106#pragma unroll 107for (int i = 0 ;i < PADDED_COUNTER_LANES ;++ i ) { 108sum += cached_segment [i ]; 109 } 110 111return sum ; 112 } 113 114template < typename Item > 115__dpct_inline__ void exclusive_downsweep (const Item & item , 116packed_counter_type raking_partial ) { 117packed_counter_type * ptr = 118reinterpret_cast < packed_counter_type *> (_local_memory ); 119packed_counter_type sum = raking_partial ; 120 121#pragma unroll 122for (int i = 0 ;i < PADDED_COUNTER_LANES ;++ i ) { 123packed_counter_type value = cached_segment [i ]; 124cached_segment [i ]= sum ; 125sum += value ; 126 } 127 128#pragma unroll 129for (int i = 0 ;i < PADDED_COUNTER_LANES ;++ i ) { 130ptr [item .get_local_linear_id ()* PADDED_COUNTER_LANES + i ]= 131cached_segment [i ]; 132 } 133 } 134 135struct prefix_callback { 136__dpct_inline__ packed_counter_type 137 operator()(packed_counter_type block_aggregate ) { 138packed_counter_type block_prefix = 0 ; 139 140#pragma unroll 141for (int packed = 1 ;packed < PACKING_RATIO ;packed ++ ) { 142block_prefix += block_aggregate 143 << (sizeof (digit_counter_type )* 8 * packed ); 144 } 145 146return block_prefix ; 147 } 148 }; 149 150template < typename Item > 151__dpct_inline__ void scan_counters (const Item & item ) { 152packed_counter_type raking_partial = upsweep (item ); 153 154prefix_callback callback ; 155packed_counter_type exclusive_partial = exclusive_scan ( 156item ,raking_partial , sycl::ext::oneapi::plus < packed_counter_type > (), 157callback ); 158 159exclusive_downsweep (item ,exclusive_partial ); 160 } 161 162private : 163static constexpr int PACKING_RATIO = 164sizeof (packed_counter_type ) /sizeof (digit_counter_type ); 165static constexpr int LOG_PACKING_RATIO = log2 < PACKING_RATIO > ::VALUE ; 166static constexpr int LOG_COUNTER_LANES = RADIX_BITS - LOG_PACKING_RATIO ; 167static constexpr int COUNTER_LANES = 1 <<LOG_COUNTER_LANES ; 168static constexpr int PADDED_COUNTER_LANES = COUNTER_LANES + 1 ; 169 170packed_counter_type cached_segment [PADDED_COUNTER_LANES ]; 171uint8_t * _local_memory ; 172}; 173 174template < typename T ,typename U > struct base_traits { 175 176static __dpct_inline__ U twiddle_in (U key ) { 177throw std::runtime_error ("Not implemented" ); 178 } 179static __dpct_inline__ U twiddle_out (U key ) { 180throw std::runtime_error ("Not implemented" ); 181 } 182}; 183 184template < typename U > struct base_traits < uint32_t ,U > { 185static __dpct_inline__ U twiddle_in (U key ) {return key ; } 186static __dpct_inline__ U twiddle_out (U key ) {return key ; } 187}; 188 189template < typename U > struct base_traits < int ,U > { 190static constexpr U HIGH_BIT = U (1 ) << ((sizeof (U )* 8 )- 1 ); 191static __dpct_inline__ U twiddle_in (U key ) {return key ^HIGH_BIT ; } 192static __dpct_inline__ U twiddle_out (U key ) {return key ^HIGH_BIT ; } 193}; 194 195template < typename U > struct base_traits < float ,U > { 196static constexpr U HIGH_BIT = U (1 ) << ((sizeof (U )* 8 )- 1 ); 197static __dpct_inline__ U twiddle_in (U key ) { 198U mask = (key & HIGH_BIT ) ?U (-1 ) :HIGH_BIT ; 199return key ^mask ; 200 } 201static __dpct_inline__ U twiddle_out (U key ) { 202U mask = (key & HIGH_BIT ) ?HIGH_BIT :U (-1 ); 203return key ^mask ; 204 } 205}; 206 207template < typename U > struct base_traits < sycl::half ,U > { 208static constexpr U HIGH_BIT = U (1 ) << ((sizeof (U )* 8 )- 1 ); 209static __dpct_inline__ U twiddle_in (U key ) { 210U mask = (key & HIGH_BIT ) ?U (-1 ) :HIGH_BIT ; 211return key ^mask ; 212 } 213static __dpct_inline__ U twiddle_out (U key ) { 214U mask = (key & HIGH_BIT ) ?HIGH_BIT :U (-1 ); 215return key ^mask ; 216 } 217}; 218 219template < typename T > struct traits :base_traits < T ,T > {}; 220template <> struct traits < uint32_t > :base_traits < uint32_t ,uint32_t > {}; 221template <> struct traits < int > :base_traits < int ,uint32_t > {}; 222template <> struct traits < float > :base_traits < float ,uint32_t > {}; 223template <> struct traits < sycl::half > :base_traits < sycl::half ,uint16_t > {}; 224 225template < int N > struct power_of_two { 226enum {VALUE = ((N & (N - 1 ))== 0 ) }; 227}; 228 229__dpct_inline__ uint32_t shr_add (uint32_t x ,uint32_t shift ,uint32_t addend ) { 230return (x >>shift )+ addend ; 231} 232 233}// namespace detail 234}// namespace group 235}// namespace dpct 236#endif // !__DPCT_DETAIL_GROUP_UTILS_DETAIL_HPP__