okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

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