okami.mom/strata

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

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

main
16.4 KiB422 linesraw
1//==---- dpcpp_extensions.h ------------------*- 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
9#ifndef __DPCT_DPCPP_EXTENSIONS_H__
10#define __DPCT_DPCPP_EXTENSIONS_H__
11
12#include <stdexcept>
13
14#ifdef SYCL_EXT_ONEAPI_USER_DEFINED_REDUCTIONS
15#include <sycl/ext/oneapi/experimental/user_defined_reductions.hpp>
16#endif
17
18#include "functional.h"
19
20namespace dpct {
21namespace group {
22namespace detail {
23
24template <typename... _Args>
25constexpr auto __reduce_over_group(_Args... __args) {
26  return sycl::reduce_over_group(__args...);
27}
28
29template <typename... _Args> constexpr auto __group_broadcast(_Args... __args) {
30  return sycl::group_broadcast(__args...);
31}
32
33template <typename... _Args>
34constexpr auto __exclusive_scan_over_group(_Args... __args) {
35  return sycl::exclusive_scan_over_group(__args...);
36}
37
38template <typename... _Args>
39constexpr auto __inclusive_scan_over_group(_Args... __args) {
40  return sycl::inclusive_scan_over_group(__args...);
41}
42
43} // end namespace detail
44
45/// Perform an exclusive scan over the values of inputs from all work-items in
46/// the group using the operator binary_op, which must be one of the SYCL 2020
47/// group algorithms library function objects.
48///
49/// \param item A work-item in a group.
50/// \param inputs Pointer to the input data for the scan operation.
51/// \param outputs Pointer to the location where scan results will be stored.
52/// \param init initial value of the scan result.
53/// \param binary_op functor that implements the binary operation used to
54/// perform the scan.
55template <typename Item, typename T, class BinaryOperation,
56          int VALUES_PER_THREAD>
57__dpct_inline__ void
58exclusive_scan(const Item &item, T (&inputs)[VALUES_PER_THREAD],
59               T (&outputs)[VALUES_PER_THREAD], T init,
60               BinaryOperation binary_op) {
61  T result = inputs[0];
62
63#pragma unroll
64  for (int i = 1; i < VALUES_PER_THREAD; ++i) {
65    result = binary_op(result, inputs[i]);
66  }
67
68  T exclusive_result =
69      detail::__exclusive_scan_over_group(item.get_group(), result, binary_op);
70
71  T input = inputs[0];
72  if (item.get_local_linear_id() == 0) {
73    outputs[0] = init;
74  } else {
75    outputs[0] = exclusive_result;
76  }
77
78#pragma unroll
79  for (int i = 1; i < VALUES_PER_THREAD; ++i) {
80    T output = binary_op(input, outputs[i - 1]);
81    input = inputs[i];
82    outputs[i] = output;
83  }
84}
85
86/// Perform an exclusive scan over the values of input from all work-items in
87/// the group using the operator binary_op, which must be one of the SYCL 2020
88/// group algorithms library function objects.
89///
90/// \param item A work-item in a group.
91/// \param input Input data for the scan operation.
92/// \param init initial value of the scan result.
93/// \param binary_op functor that implements the binary operation used to
94/// perform the scan. \param group_aggregate group-wide aggregate of all inputs
95/// in the work-items of the group. \returns exclusive scan of the first i
96/// work-items where item is the i-th work item.
97template <typename Item, typename T, class BinaryOperation>
98__dpct_inline__ T
99exclusive_scan(const Item &item, T input, T init, BinaryOperation binary_op,
100               T &group_aggregate) {
101  T output = detail::__exclusive_scan_over_group(item.get_group(), input, init,
102                                                 binary_op);
103  if (item.get_local_linear_id() == item.get_local_range().size() - 1) {
104    group_aggregate = binary_op(output, input);
105  }
106
107  group_aggregate = detail::__group_broadcast(
108      item.get_group(), group_aggregate, item.get_local_range().size() - 1);
109  return output;
110}
111
112/// Perform an exclusive scan over the values of input from all work-items in
113/// the group using the operator binary_op, which must be one of the SYCL 2020
114/// group algorithms library function objects.
115///
116/// \param item A work-item in a group.
117/// \param input Input data for the scan operation.
118/// \param binary_op functor that implements the binary operation used to
119/// perform the scan. \param prefix_callback_op functor invoked by the first
120/// work-item in the group that returns the
121///        initial value in the resulting scan of the work-items in the group.
122/// \returns exclusive scan of the input elements assigned to work-items in the
123/// group.
124template <typename Item, typename T, class BinaryOperation,
125          class GroupPrefixCallbackOperation>
126__dpct_inline__ T
127exclusive_scan(const Item &item, T input, BinaryOperation binary_op,
128               GroupPrefixCallbackOperation &prefix_callback_op) {
129  T group_aggregate;
130
131  T output =
132      detail::__exclusive_scan_over_group(item.get_group(), input, binary_op);
133  if (item.get_local_linear_id() == item.get_local_range().size() - 1) {
134    group_aggregate = binary_op(output, input);
135  }
136
137  group_aggregate = detail::__group_broadcast(
138      item.get_group(), group_aggregate, item.get_local_range().size() - 1);
139
140  T group_prefix = prefix_callback_op(group_aggregate);
141  if (item.get_local_linear_id() == 0) {
142    output = group_prefix;
143  } else {
144    output = binary_op(group_prefix, output);
145  }
146
147  return output;
148}
149
150/// Perform a reduction of the data elements assigned to all threads in the
151/// group.
152///
153/// \param item A work-item in a group.
154/// \param inputs Pointer to the input data for the reduce operation.
155/// \param binary_op functor that implements the binary operation used to
156/// perform the scan. \returns value of the reduction using binary_op
157template <typename Item, typename T, class BinaryOperation,
158          int VALUES_PER_THREAD>
159__dpct_inline__ T
160reduce(Item item, T (&inputs)[VALUES_PER_THREAD], BinaryOperation binary_op) {
161  T result = inputs[0];
162
163#pragma unroll
164  for (int i = 1; i < VALUES_PER_THREAD; i++) {
165    result = binary_op(result, inputs[i]);
166  }
167  return detail::__reduce_over_group(item.get_group(), result, binary_op);
168}
169
170/// Perform a reduction on a limited number of the work items in a subgroup
171///
172/// \param item A work-item in a group.
173/// \param value value per work item which is to be reduced
174/// \param items_to_reduce num work items at the start of the subgroup to reduce
175/// \param binary_op functor that implements the binary operation used to
176/// perform the scan. \returns value of the reduction using binary_op
177template <typename Item, typename T, class BinaryOperation>
178__dpct_inline__
179typename ::std::enable_if_t<sycl::has_known_identity_v<BinaryOperation, T>, T>
180reduce_over_partial_group(const Item &item, const T &value,
181                          const ::std::uint16_t &items_to_reduce,
182                          BinaryOperation binary_op) {
183  T value_temp = (item.get_local_linear_id() < items_to_reduce)
184                     ? value
185                     : sycl::known_identity_v<BinaryOperation, T>;
186  return detail::__reduce_over_group(item.get_sub_group(), value_temp,
187                                     binary_op);
188}
189
190/// Perform an inclusive scan over the values of inputs from all work-items in
191/// the group using the operator binary_op, which must be one of the SYCL 2020
192/// group algorithms library function objects.
193///
194/// \param item A work-item in a group.
195/// \param inputs Pointer to the input data for the scan operation.
196/// \param outputs Pointer to the location where scan results will be stored.
197/// \param binary_op functor that implements the binary operation used to
198/// perform the scan. \returns inclusive scan of the input elements assigned to
199/// work-items in the group.
200template <typename Item, typename T, class BinaryOperation,
201          int VALUES_PER_THREAD>
202__dpct_inline__ void
203inclusive_scan(const Item &item, T (&inputs)[VALUES_PER_THREAD],
204               T (&outputs)[VALUES_PER_THREAD], BinaryOperation binary_op) {
205  T result = inputs[0];
206
207#pragma unroll
208  for (int i = 1; i < VALUES_PER_THREAD; ++i) {
209    result = binary_op(result, inputs[i]);
210  }
211
212  T exclusive_result =
213      detail::__exclusive_scan_over_group(item.get_group(), result, binary_op);
214
215  if (item.get_local_linear_id() == 0) {
216    outputs[0] = inputs[0];
217  } else {
218    outputs[0] = binary_op(inputs[0], exclusive_result);
219  }
220
221#pragma unroll
222  for (int i = 1; i < VALUES_PER_THREAD; ++i) {
223    outputs[i] = binary_op(inputs[i], outputs[i - 1]);
224  }
225}
226
227/// Perform an inclusive scan over the values of inputs from all work-items in
228/// the group using the operator binary_op, which must be one of the SYCL 2020
229/// group algorithms library function objects.
230///
231/// \param item A work-item in a group.
232/// \param input Pointer to the input data for the scan operation.
233/// \param binary_op functor that implements the binary operation used to
234/// perform the scan. \param group_aggregate group-wide aggregate of all inputs
235/// in the work-items of the group. \returns inclusive scan of the input
236/// elements assigned to work-items in the group.
237template <typename Item, typename T, class BinaryOperation>
238__dpct_inline__ T inclusive_scan(const Item &item, T input,
239                                                BinaryOperation binary_op,
240                                                T &group_aggregate) {
241  T output =
242      detail::__inclusive_scan_over_group(item.get_group(), input, binary_op);
243  if (item.get_local_linear_id() == item.get_local_range().size() - 1) {
244    group_aggregate = output;
245  }
246
247  group_aggregate = detail::__group_broadcast(
248      item.get_group(), group_aggregate, item.get_local_range().size() - 1);
249  return output;
250}
251
252/// Perform an inclusive scan over the values of input from all work-items in
253/// the group using the operator binary_op, which must be one of the SYCL 2020
254/// group algorithms library function objects.
255///
256/// \param item A work-item in a group.
257/// \param input Input data for the scan operation.
258/// \param binary_op functor that implements the binary operation used to
259/// perform the scan. \param prefix_callback_op functor invoked by the first
260/// work-item in the group that returns the
261///        initial value in the resulting scan of the work-items in the group.
262/// \returns inclusive scan of the input elements assigned to work-items in the
263/// group.
264template <typename Item, typename T, class BinaryOperation,
265          class GroupPrefixCallbackOperation>
266__dpct_inline__ T
267inclusive_scan(const Item &item, T input, BinaryOperation binary_op,
268               GroupPrefixCallbackOperation &prefix_callback_op) {
269  T group_aggregate;
270
271  T output = inclusive_scan(item, input, binary_op, group_aggregate);
272  T group_prefix = prefix_callback_op(group_aggregate);
273
274  return binary_op(group_prefix, output);
275}
276
277} // namespace group
278
279namespace device {
280
281namespace detail {
282
283template <typename... _Args> constexpr auto __joint_reduce(_Args... __args) {
284  return sycl::joint_reduce(__args...);
285}
286
287} // namespace detail
288
289/// Perform a reduce on each of the segments specified within data stored on
290/// the device.
291///
292/// \param queue Command queue used to access device used for reduction
293/// \param inputs Pointer to the data elements on the device to be reduced
294/// \param outputs Pointer to the storage where the reduced value for each
295/// segment will be stored \param segment_count number of segments to be reduced
296/// \param begin_offsets Pointer to the set of indices that are the first
297/// element in each segment \param end_offsets Pointer to the set of indices
298/// that are one past the last element in each segment \param binary_op functor
299/// that implements the binary operation used to perform the scan. \param init
300/// initial value of the reduction for each segment.
301template <int GROUP_SIZE, typename T, typename OffsetT, class BinaryOperation>
302void segmented_reduce(sycl::queue queue, T *inputs, T *outputs,
303                      size_t segment_count, OffsetT *begin_offsets,
304                      OffsetT *end_offsets, BinaryOperation binary_op, T init) {
305
306  sycl::range<1> global_size(segment_count * GROUP_SIZE);
307  sycl::range<1> local_size(GROUP_SIZE);
308
309  queue.submit([&](sycl::handler &cgh) {
310    cgh.parallel_for(
311        sycl::nd_range<1>(global_size, local_size), [=](sycl::nd_item<1> item) {
312          OffsetT segment_begin = begin_offsets[item.get_group_linear_id()];
313          OffsetT segment_end = end_offsets[item.get_group_linear_id()];
314          if (segment_begin == segment_end) {
315            if (item.get_local_linear_id() == 0) {
316              outputs[item.get_group_linear_id()] = init;
317            }
318            return;
319          }
320
321          sycl::multi_ptr<T, sycl::access::address_space::global_space>
322              input_ptr = inputs;
323          T group_aggregate = detail::__joint_reduce(
324              item.get_group(), input_ptr + segment_begin,
325              input_ptr + segment_end, init, binary_op);
326
327          if (item.get_local_linear_id() == 0) {
328            outputs[item.get_group_linear_id()] = group_aggregate;
329          }
330        });
331  });
332}
333
334
335#ifdef SYCL_EXT_ONEAPI_USER_DEFINED_REDUCTIONS
336
337namespace experimental {
338namespace detail {
339template <typename _Tp, typename... _Ts> struct __is_any {
340  constexpr static bool value = std::disjunction_v<
341      std::is_same<std::remove_cv_t<_Tp>, std::remove_cv_t<_Ts>>...>;
342};
343
344template <typename _Tp, typename _Bp> struct __in_native_op_list {
345  constexpr static bool value =
346      __is_any<_Bp, sycl::plus<_Tp>, sycl::bit_or<_Tp>, sycl::bit_xor<_Tp>,
347               sycl::bit_and<_Tp>, sycl::maximum<_Tp>, sycl::minimum<_Tp>,
348               sycl::multiplies<_Tp>>::value;
349};
350
351template <typename _Tp, typename _Bp> struct __is_native_op {
352  constexpr static bool value = __in_native_op_list<_Tp, _Bp>::value ||
353                                __in_native_op_list<void, _Bp>::value;
354};
355
356} // namespace detail
357
358/// Perform a reduce on each of the segments specified within data stored on
359/// the device. Compared with dpct::device::segmented_reduce, this experimental
360/// feature support user define reductions.
361///
362/// \param queue Command queue used to access device used for reduction
363/// \param inputs Pointer to the data elements on the device to be reduced
364/// \param outputs Pointer to the storage where the reduced value for each
365/// segment will be stored \param segment_count number of segments to be reduced
366/// \param begin_offsets Pointer to the set of indices that are the first
367/// element in each segment \param end_offsets Pointer to the set of indices
368/// that are one past the last element in each segment \param binary_op functor
369/// that implements the binary operation used to perform the scan. \param init
370/// initial value of the reduction for each segment.
371template <int GROUP_SIZE, typename T, typename OffsetT, class BinaryOperation>
372void segmented_reduce(sycl::queue queue, T *inputs, T *outputs,
373                      size_t segment_count, OffsetT *begin_offsets,
374                      OffsetT *end_offsets, BinaryOperation binary_op, T init) {
375
376  sycl::range<1> global_size(segment_count * GROUP_SIZE);
377  sycl::range<1> local_size(GROUP_SIZE);
378
379  if constexpr (!detail::__is_native_op<T, BinaryOperation>::value) {
380    queue.submit([&](sycl::handler &cgh) {
381      size_t temp_memory_size = GROUP_SIZE * sizeof(T);
382      auto scratch = sycl::local_accessor<std::byte, 1>(temp_memory_size, cgh);
383      cgh.parallel_for(
384          sycl::nd_range<1>(global_size, local_size),
385          [=](sycl::nd_item<1> item) {
386            OffsetT segment_begin = begin_offsets[item.get_group_linear_id()];
387            OffsetT segment_end = end_offsets[item.get_group_linear_id()];
388            if (segment_begin == segment_end) {
389              if (item.get_local_linear_id() == 0) {
390                outputs[item.get_group_linear_id()] = init;
391              }
392              return;
393            }
394            // Create a handle that associates the group with an allocation it
395            // can use
396            auto handle =
397                sycl::ext::oneapi::experimental::group_with_scratchpad(
398                    item.get_group(),
399                    sycl::span(&scratch[0], temp_memory_size));
400            T group_aggregate = sycl::ext::oneapi::experimental::joint_reduce(
401                handle, inputs + segment_begin, inputs + segment_end, init,
402                binary_op);
403            if (item.get_local_linear_id() == 0) {
404              outputs[item.get_group_linear_id()] = group_aggregate;
405            }
406          });
407    });
408  } else {
409    dpct::device::segmented_reduce<GROUP_SIZE>(queue, inputs, outputs,
410                                               segment_count, begin_offsets,
411                                               end_offsets, binary_op, init);
412  }
413}
414} // namespace experimental
415
416#endif // SYCL_EXT_ONEAPI_USER_DEFINED_REDUCTIONS
417
418
419} // namespace device
420} // namespace dpct
421
422#endif