okami.mom/strata

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

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

main
65.2 KiB1566 linesraw
1//==---- group_utils.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
9#ifndef __DPCT_GROUP_UTILS_HPP__
10#define __DPCT_GROUP_UTILS_HPP__
11
12#include <iterator>
13#include <stdexcept>
14#include <sycl/sycl.hpp>
15
16#include "dpct.hpp"
17#include "dpl_extras/functional.h"
18
19#include "detail/group_utils_detail.hpp"
20namespace dpct {
21namespace group {
22
23/// Rearranging data partitioned across a work-group.
24///
25/// \tparam T The type of the data elements.
26/// \tparam ElementsPerWorkItem The number of data elements assigned to a
27/// work-item.
28template <typename T, size_t ElementsPerWorkItem> class exchange {
29public:
30  static size_t get_local_memory_size(size_t group_threads) {
31    size_t padding_values =
32        (INSERT_PADDING)
33            ? ((group_threads * ElementsPerWorkItem) >> LOG_LOCAL_MEMORY_BANKS)
34            : 0;
35    return (group_threads * ElementsPerWorkItem + padding_values) * sizeof(T);
36  }
37
38  exchange(uint8_t *local_memory) : _local_memory(local_memory) {}
39
40  // TODO: Investigate if padding is required for performance,
41  // and if specializations are required for specific target hardware.
42  static size_t adjust_by_padding(size_t offset) {
43
44    if constexpr (INSERT_PADDING) {
45      offset = detail::shr_add(offset, LOG_LOCAL_MEMORY_BANKS, offset);
46    }
47    return offset;
48  }
49
50  struct blocked_offset {
51    template <typename Item> size_t operator()(Item item, size_t i) {
52      size_t offset = item.get_local_linear_id() * ElementsPerWorkItem + i;
53      return adjust_by_padding(offset);
54    }
55  };
56
57  struct striped_offset {
58    template <typename Item> size_t operator()(Item item, size_t i) {
59      size_t offset = i * item.get_group().get_local_linear_range() +
60                      item.get_local_linear_id();
61      return adjust_by_padding(offset);
62    }
63  };
64
65  struct sub_group_striped_offset {
66    template <typename Item> size_t operator()(Item item, size_t i) {
67      auto sg = item.get_sub_group();
68      size_t wg_size = item.get_group().get_local_linear_range();
69      size_t sub_group_sliced_items =
70          std::min<size_t>(sg.get_local_linear_range(), wg_size);
71      size_t offset = (sg.get_group_linear_id() * sub_group_sliced_items *
72                       ElementsPerWorkItem) +
73                      (i * sub_group_sliced_items) + sg.get_local_linear_id();
74      return adjust_by_padding(offset);
75    }
76  };
77
78  template <typename Iterator> struct scatter_offset {
79    Iterator begin;
80    scatter_offset(const int (&ranks)[ElementsPerWorkItem]) {
81      begin = std::begin(ranks);
82    }
83    template <typename Item> size_t operator()(Item item, size_t i) const {
84      // iterator i is expected to be within bounds [0,VALUES_PER_THREAD)
85      return adjust_by_padding(begin[i]);
86    }
87  };
88
89  template <typename Item, typename offsetFunctorTypeFW,
90            typename offsetFunctorTypeRV>
91  [[deprecated("Please use blocked_to_striped, "
92               "striped_to_blocked, scatter_to_blocked, scatter_to_striped "
93               "instead")]] __dpct_inline__ void
94  helper_exchange(Item item, T (&keys)[ElementsPerWorkItem],
95                  offsetFunctorTypeFW &offset_functor_fw,
96                  offsetFunctorTypeRV &offset_functor_rv) {
97    helper_exchange(item, keys, keys, offset_functor_fw, offset_functor_rv);
98  }
99
100  /// Inplace rearrange elements from blocked order to striped order.
101  ///
102  /// Suppose 512 integer data elements partitioned across 128 work-items, where
103  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
104  /// blocked \p input across the work-group is:
105  ///
106  ///   {[0, 1, 2, 3], [4, 5, 6, 7], ..., [508, 509, 510, 511]}.
107  ///
108  /// The striped order output is:
109  ///
110  ///   {[0, 128, 256, 384], [1, 129, 257, 385], ..., [127, 255, 383, 511]}.
111  ///
112  /// \tparam Item The work-item identifier type.
113  /// \param item The work-item identifier.
114  /// \param input The input data of each work-item.
115  template <typename Item>
116  __dpct_inline__ void blocked_to_striped(Item item,
117                                          T (&input)[ElementsPerWorkItem]) {
118    striped_offset get_striped_offset;
119    blocked_offset get_blocked_offset;
120    helper_exchange(item, input, input, get_blocked_offset, get_striped_offset);
121  }
122
123  /// Inplace rearrange elements from striped order to blocked order.
124  ///
125  /// Suppose 512 integer data elements partitioned across 128 work-items, where
126  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
127  /// striped \p input across the work-group is:
128  ///
129  ///   { [0, 128, 256, 384], [1, 129, 257, 385], ..., [127, 255, 383, 511] }.
130  ///
131  /// The blocked order output is:
132  ///
133  ///   { [0, 1, 2, 3], [4, 5, 6, 7], ..., [508, 509, 510, 511] }.
134  ///
135  /// \tparam Item The work-item identifier type.
136  /// \param item The work-item identifier.
137  /// \param input The input data of each work-item.
138  template <typename Item>
139  __dpct_inline__ void striped_to_blocked(Item item,
140                                          T (&input)[ElementsPerWorkItem]) {
141    blocked_offset get_blocked_offset;
142    striped_offset get_striped_offset;
143    helper_exchange(item, input, input, get_striped_offset, get_blocked_offset);
144  }
145
146  /// Rearrange elements from blocked order to striped order.
147  ///
148  /// Suppose 512 integer data elements partitioned across 128 work-items, where
149  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
150  /// blocked \p input across the work-group is:
151  ///
152  ///   { [0, 1, 2, 3], [4, 5, 6, 7], ..., [508, 509, 510, 511] }.
153  ///
154  /// The striped order output is:
155  ///
156  ///   { [0, 128, 256, 384], [1, 129, 257, 385], ..., [127, 255, 383, 511] }.
157  ///
158  /// \tparam Item The work-item identifier type.
159  /// \param item The work-item identifier.
160  /// \param input The input data of each work-item.
161  /// \param output The corresponding output data of each work-item.
162  template <typename Item>
163  __dpct_inline__ void blocked_to_striped(Item item,
164                                          T (&input)[ElementsPerWorkItem],
165                                          T (&output)[ElementsPerWorkItem]) {
166    striped_offset get_striped_offset;
167    blocked_offset get_blocked_offset;
168    helper_exchange(item, input, output, get_blocked_offset,
169                    get_striped_offset);
170  }
171
172  /// Rearrange elements from striped order to blocked order.
173  ///
174  /// Suppose 512 integer data elements partitioned across 128 work-items, where
175  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
176  /// striped \p input across the work-group is:
177  ///
178  ///   { [0, 128, 256, 384], [1, 129, 257, 385], ..., [127, 255, 383, 511] }.
179  ///
180  /// The blocked order output is:
181  ///
182  ///   { [0, 1, 2, 3], [4, 5, 6, 7], ..., [508, 509, 510, 511] }.
183  ///
184  /// \tparam Item The work-item identifier type.
185  /// \param item The work-item identifier.
186  /// \param input The input data of each work-item.
187  /// \param output The corresponding output data of each work-item.
188  template <typename Item>
189  __dpct_inline__ void striped_to_blocked(Item item,
190                                          T (&input)[ElementsPerWorkItem],
191                                          T (&output)[ElementsPerWorkItem]) {
192    blocked_offset get_blocked_offset;
193    striped_offset get_striped_offset;
194    helper_exchange(item, input, output, get_striped_offset,
195                    get_blocked_offset);
196  }
197
198  /// Inplace exchanges data items annotated by rank into blocked arrangement.
199  ///
200  /// Suppose 512 integer data elements partitioned across 128 work-items, where
201  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
202  /// striped \p input across the work-group is:
203  ///
204  ///   { [0, 128, 256, 384], [1, 129, 257, 385], ..., [127, 255, 383, 511] }.
205  ///
206  /// The rank across the work-group is:
207  ///
208  ///   { [0, 1, 2, 3], [4, 5, 6, 7], ..., [508, 509, 510, 511] }.
209  ///
210  /// The blocked order output is:
211  ///
212  ///   { [0, 1, 2, 3], [4, 5, 6, 7], ..., [508, 509, 510, 511] }.
213  ///
214  /// \tparam Item The work-item identifier type.
215  /// \param item The work-item identifier.
216  /// \param input The input data of each work-item.
217  /// \param ranks The corresponding rank annotation of each work-item.
218  template <typename Item>
219  __dpct_inline__ void scatter_to_blocked(Item item,
220                                          T (&input)[ElementsPerWorkItem],
221                                          int (&ranks)[ElementsPerWorkItem]) {
222    scatter_offset<const int *> get_scatter_offset(ranks);
223    blocked_offset get_blocked_offset;
224    helper_exchange(item, input, input, get_scatter_offset, get_blocked_offset);
225  }
226
227  /// Inplace exchanges data items annotated by rank into striped arrangement.
228  ///
229  /// Suppose 512 integer data elements partitioned across 128 work-items, where
230  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
231  /// blocked \p input across the work-group is:
232  ///
233  ///   { [0, 1, 2, 3], [4, 5, 6, 7], ..., [508, 509, 510, 511] }.
234  ///
235  /// The rank across the work-group is:
236  ///
237  ///   { [16, 20, 24, 28], [32, 36, 40, 44], ..., [499, 503, 507, 511] }.
238  ///
239  /// The striped order output of each work-item will be:
240  ///
241  ///   { [0, 128, 256, 384], [1, 129, 257, 385], ..., [127, 255, 383, 511] }.
242  ///
243  /// \tparam Item The work-item identifier type.
244  /// \param item The work-item identifier.
245  /// \param input The input data of each work-item.
246  /// \param ranks The corresponding rank annotation of each work-item.
247  template <typename Item>
248  __dpct_inline__ void scatter_to_striped(Item item,
249                                          T (&input)[ElementsPerWorkItem],
250                                          int (&ranks)[ElementsPerWorkItem]) {
251    scatter_offset<const int *> get_scatter_offset(ranks);
252    striped_offset get_striped_offset;
253    helper_exchange(item, input, input, get_scatter_offset, get_striped_offset);
254  }
255
256  /// Rearrange elements from blocked order to sub_group striped order.
257  ///
258  /// Suppose 512 integer data elements partitioned across 128 work-items, where
259  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
260  /// blocked \p input across the work-group is:
261  ///
262  ///   { [0, 1, 2, 3], [4, 5, 6, 7], ..., [508, 509, 510, 511] }.
263  ///
264  /// The sub_group striped order output (with sub_group size 2) is:
265  ///
266  ///   { [0, 4, 1, 5], [2, 6, 3, 7], [8, 12, 9, 13], [10, 14, 11, 15], ...
267  ///     , [506, 510, 507, 511] }.
268  ///
269  /// \tparam Item The work-item identifier type.
270  /// \param item The work-item identifier.
271  /// \param input The input data of each work-item.
272  /// \param output The corresponding output data of each work-item.
273  template <typename Item>
274  __dpct_inline__ void
275  blocked_to_sub_group_striped(Item item, T (&input)[ElementsPerWorkItem],
276                               T (&output)[ElementsPerWorkItem]) {
277    blocked_offset get_blocked_offset;
278    sub_group_striped_offset get_sub_group_striped_offset;
279    helper_exchange(item, input, output, get_blocked_offset,
280                    get_sub_group_striped_offset);
281  }
282
283  /// Rearrange elements from sub_group striped order to blocked order.
284  ///
285  /// Suppose 512 integer data elements partitioned across 128 work-items, where
286  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
287  /// sub_group striped \p input across the work-group is:
288  ///
289  ///   { [0, 4, 1, 5], [2, 6, 3, 7], [8, 12, 9, 13], [10, 14, 11, 15], ...
290  ///     , [506, 510, 507, 511] }.
291  ///
292  /// The blocked order output is:
293  ///
294  ///   { [0, 1, 2, 3], [4, 5, 6, 7], ..., [508, 509, 510, 511] }.
295  ///
296  /// \tparam Item The work-item identifier type.
297  /// \param item The work-item identifier.
298  /// \param input The input data of each work-item.
299  /// \param output The corresponding output data of each work-item.
300  template <typename Item>
301  __dpct_inline__ void
302  sub_group_striped_to_blocked(Item item, T (&input)[ElementsPerWorkItem],
303                               T (&output)[ElementsPerWorkItem]) {
304    blocked_offset get_blocked_offset;
305    sub_group_striped_offset get_sub_group_striped_offset;
306    helper_exchange(item, input, output, get_sub_group_striped_offset,
307                    get_blocked_offset);
308  }
309
310private:
311  template <typename Item, typename offsetFunctorTypeFW,
312            typename offsetFunctorTypeRV>
313  __dpct_inline__ void helper_exchange(Item item,
314                                       T (&input)[ElementsPerWorkItem],
315                                       T (&output)[ElementsPerWorkItem],
316                                       offsetFunctorTypeFW &offset_functor_fw,
317                                       offsetFunctorTypeRV &offset_functor_rv) {
318    T *buffer = reinterpret_cast<T *>(_local_memory);
319#pragma unroll
320    for (size_t i = 0; i < ElementsPerWorkItem; i++) {
321      size_t offset = offset_functor_fw(item, i);
322      buffer[offset] = input[i];
323    }
324    sycl::group_barrier(item.get_group());
325#pragma unroll
326    for (size_t i = 0; i < ElementsPerWorkItem; i++) {
327      size_t offset = offset_functor_rv(item, i);
328      output[i] = buffer[offset];
329    }
330  }
331
332  static constexpr int LOG_LOCAL_MEMORY_BANKS = 4;
333  static constexpr bool INSERT_PADDING =
334      (ElementsPerWorkItem > 4) &&
335      (detail::power_of_two<ElementsPerWorkItem>::VALUE);
336
337  uint8_t *_local_memory;
338};
339
340/// The work-group wide radix sort to sort integer data elements
341/// assigned to all work-items in the work-group.
342///
343/// \tparam T The type of the data elements.
344/// \tparam ElementsPerWorkItem The number of data elements assigned to
345/// a work-item.
346/// \tparam RADIX_BITS The number of radix bits per digit place.
347template <typename T, int ElementsPerWorkItem, int RADIX_BITS = 4>
348class group_radix_sort {
349  uint8_t *_local_memory;
350
351public:
352  group_radix_sort(uint8_t *local_memory) : _local_memory(local_memory) {}
353
354  static size_t get_local_memory_size(size_t group_threads) {
355    size_t ranks_size =
356        detail::radix_rank<RADIX_BITS>::get_local_memory_size(group_threads);
357    size_t exchange_size =
358        exchange<T, ElementsPerWorkItem>::get_local_memory_size(group_threads);
359    return sycl::max(ranks_size, exchange_size);
360  }
361
362private:
363  template <typename Item, bool DESCENDING>
364  __dpct_inline__ void
365  helper_sort(const Item &item, T (&keys)[ElementsPerWorkItem],
366              int begin_bit = 0, int end_bit = 8 * sizeof(T),
367              bool is_striped = false) {
368    using UnsignedT =
369        typename std::conditional<std::is_same<T, sycl::half>::value, uint16_t,
370                                  uint32_t>::type;
371    UnsignedT(&unsigned_keys)[ElementsPerWorkItem] =
372        reinterpret_cast<UnsignedT(&)[ElementsPerWorkItem]>(keys);
373
374#pragma unroll
375    for (int i = 0; i < ElementsPerWorkItem; ++i) {
376      unsigned_keys[i] = detail::traits<T>::twiddle_in(unsigned_keys[i]);
377    }
378
379    for (int i = begin_bit; i < end_bit; i += RADIX_BITS) {
380      int pass_bits = sycl::min(RADIX_BITS, end_bit - begin_bit);
381
382      int ranks[ElementsPerWorkItem];
383      detail::radix_rank<RADIX_BITS, DESCENDING>(_local_memory)
384          .template rank_keys<Item, UnsignedT, ElementsPerWorkItem>(
385              item, unsigned_keys, ranks, i, pass_bits);
386
387      sycl::group_barrier(item.get_group());
388
389      bool last_iter = i + RADIX_BITS >= end_bit;
390      if (last_iter && is_striped) {
391        exchange<T, ElementsPerWorkItem>(_local_memory)
392            .scatter_to_striped(item, keys, ranks);
393
394      } else {
395        exchange<T, ElementsPerWorkItem>(_local_memory)
396            .scatter_to_blocked(item, keys, ranks);
397      }
398
399      sycl::group_barrier(item.get_group());
400    }
401
402#pragma unroll
403    for (int i = 0; i < ElementsPerWorkItem; ++i) {
404      unsigned_keys[i] = detail::traits<T>::twiddle_out(unsigned_keys[i]);
405    }
406  }
407
408public:
409  /// Performs an ascending work-group wide radix sort over a blocked
410  /// arrangement of input elements.
411  ///
412  /// Suppose 512 integer data elements partitioned across 128 work-items, where
413  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
414  /// \p input across the work-group is:
415  ///
416  ///   { [0,511,1,510], [2,509,3,508], [4,507,5,506], ..., [254,257,255,256] }.
417  ///
418  /// The ascending order output is:
419  ///
420  ///   { [0,1,2,3], [4,5,6,7], [8,9,10,11], ..., [508,509,510,511] }.
421  ///
422  /// \tparam Item The work-item identifier type.
423  /// \param item The work-item identifier.
424  /// \param input The input data of each work-item.
425  /// \param begin_bit The beginning (least-significant) bit index needed for
426  /// key comparison.
427  /// \param end_bit The past-the-end (most-significant) bit
428  /// index needed for key comparison.
429  template <typename Item>
430  __dpct_inline__ void sort(const Item &item, T (&input)[ElementsPerWorkItem],
431                            int begin_bit = 0, int end_bit = 8 * sizeof(T)) {
432    helper_sort<Item, /*DESCENDING=*/false>(item, input, begin_bit, end_bit);
433  }
434
435  /// Performs an descending work-group wide radix sort over a blocked
436  /// arrangement of input elements.
437  ///
438  /// Suppose 512 integer data elements partitioned across 128 work-items, where
439  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
440  /// \p input across the work-group is:
441  ///
442  ///   { [0,511,1,510], [2,509,3,508], [4,507,5,506], ..., [254,257,255,256] }.
443  ///
444  /// The descending order output is:
445  ///
446  ///   { [511,510,509,508], [11,10,9,8], [7,6,5,4], ..., [3,2,1,0] }.
447  ///
448  /// \tparam Item The work-item identifier type.
449  /// \param item The work-item identifier.
450  /// \param input The input data of each work-item.
451  /// \param begin_bit The beginning (least-significant) bit index needed for
452  /// key comparison.
453  /// \param end_bit The past-the-end (most-significant) bit
454  /// index needed for key comparison.
455  template <typename Item>
456  __dpct_inline__ void
457  sort_descending(const Item &item, T (&input)[ElementsPerWorkItem],
458                  int begin_bit = 0, int end_bit = 8 * sizeof(T)) {
459    helper_sort<Item, /*DESCENDING=*/true>(item, input, begin_bit, end_bit);
460  }
461
462  /// Performs an ascending radix sort across a blocked arrangement of input
463  /// elements, leaving them in a striped arrangement.
464  ///
465  /// Suppose 512 integer data elements partitioned across 128 work-items, where
466  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
467  /// \p input across the work-group is:
468  ///
469  ///   { [0,511,1,510], [2,509,3,508], [4,507,5,506], ..., [254,257,255,256] }.
470  ///
471  /// The corresponding output of each work-item will be:
472  ///
473  ///   { [0,128,256,384], [1,129,257,385], [2,130,258,386], ...,
474  ///   [127,255,383,511] }.
475  ///
476  /// \tparam Item The work-item identifier type.
477  /// \param item The work-item identifier.
478  /// \param input The input data of each work-item.
479  /// \param begin_bit The beginning (least-significant) bit index needed for
480  /// key comparison.
481  /// \param end_bit The past-the-end (most-significant) bit
482  /// index needed for key comparison.
483  template <typename Item>
484  __dpct_inline__ void
485  sort_blocked_to_striped(const Item &item, T (&input)[ElementsPerWorkItem],
486                          int begin_bit = 0, int end_bit = 8 * sizeof(T)) {
487    helper_sort<Item, /*DESCENDING=*/false>(item, input, begin_bit, end_bit,
488                                            /*is_striped=*/true);
489  }
490
491  /// Performs an descending radix sort across a blocked arrangement of input
492  /// elements, leaving them in a striped arrangement.
493  ///
494  /// Suppose 512 integer data elements partitioned across 128 work-items, where
495  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
496  /// \p input across the work-group is:
497  ///
498  ///   { [0,511,1,510], [2,509,3,508], [4,507,5,506], ..., [254,257,255,256] }.
499  ///
500  /// The descending striped order output is:
501  ///
502  ///   { [0,128,256,384], [1,129,257,385], [2,130,258,386], ...,
503  ///   [127,255,383,511] }.
504  ///
505  /// \tparam Item The work-item identifier type.
506  /// \param item The work-item identifier.
507  /// \param input The input data of each work-item.
508  /// \param begin_bit The beginning (least-significant) bit index needed for
509  /// key comparison.
510  /// \param end_bit The past-the-end (most-significant) bit
511  /// index needed for key comparison.
512  template <typename Item>
513  __dpct_inline__ void sort_descending_blocked_to_striped(
514      const Item &item, T (&input)[ElementsPerWorkItem], int begin_bit = 0,
515      int end_bit = 8 * sizeof(T)) {
516    helper_sort<Item, /*DESCENDING=*/true>(item, input, begin_bit, end_bit,
517                                           /*is_striped=*/true);
518  }
519};
520
521/// The work-group wide radix sort to sort integer data elements assigned to
522/// all work-items in the work-group.
523///
524/// \tparam T The type of the data elements.
525/// \tparam ElementsPerWorkItem The number of data elements assigned to
526/// a work-item.
527/// \tparam DECENDING The value indicating if data elements are sorted in
528/// decending order.
529template <typename T, int ElementsPerWorkItem, bool DESCENDING = false>
530class [[deprecated("Please use group_radix_sort instead")]] radix_sort {
531  using SorterT = group_radix_sort<T, ElementsPerWorkItem>;
532  SorterT _sorter;
533
534public:
535  static size_t get_local_memory_size(size_t group_threads) {
536    return SorterT::get_local_memory_size(group_threads);
537  }
538
539  radix_sort(uint8_t * local_memory) : _sorter(local_memory) {}
540
541  template <typename Item>
542  __dpct_inline__ void helper_sort(
543      const Item &item, T(&keys)[ElementsPerWorkItem], int begin_bit = 0,
544      int end_bit = 8 * sizeof(T), bool is_striped = false) {
545    if constexpr (DESCENDING) {
546      if (is_striped)
547        _sorter.sort_descending_blocked_to_striped(item, keys, begin_bit,
548                                                   end_bit);
549      else
550        _sorter.sort_descending(item, keys, begin_bit, end_bit);
551    } else {
552      if (is_striped)
553        _sorter.sort_blocked_to_striped(item, keys, begin_bit, end_bit);
554      else
555        _sorter.sort(item, keys, begin_bit, end_bit);
556    }
557  }
558
559  template <typename Item>
560  __dpct_inline__ void sort_blocked(
561      const Item &item, T(&keys)[ElementsPerWorkItem], int begin_bit = 0,
562      int end_bit = 8 * sizeof(T)) {
563    helper_sort(item, keys, begin_bit, end_bit, false);
564  }
565
566  template <typename Item>
567  __dpct_inline__ void sort_blocked_to_striped(
568      const Item &item, T(&keys)[ElementsPerWorkItem], int begin_bit = 0,
569      int end_bit = 8 * sizeof(T)) {
570    helper_sort(item, keys, begin_bit, end_bit, true);
571  }
572};
573
574/// Load linear segment items into block format across threads
575/// Helper for Block Load
576enum load_algorithm {
577  BLOCK_LOAD_DIRECT,
578  BLOCK_LOAD_STRIPED,
579};
580
581// loads a linear segment of workgroup items into a blocked arrangement.
582template <size_t ITEMS_PER_WORK_ITEM, typename InputT, typename InputIteratorT,
583          typename Item>
584[[deprecated("Please use load_direct_blocked instead")]]
585__dpct_inline__ void load_blocked(const Item &item, InputIteratorT block_itr,
586                                  InputT (&items)[ITEMS_PER_WORK_ITEM]) {
587
588  // This implementation does not take in account range loading across
589  // workgroup items To-do: Decide whether range loading is required for group
590  // loading
591  size_t linear_tid = item.get_local_linear_id();
592  uint32_t workgroup_offset = linear_tid * ITEMS_PER_WORK_ITEM;
593#pragma unroll
594  for (size_t idx = 0; idx < ITEMS_PER_WORK_ITEM; idx++) {
595    items[idx] = block_itr[workgroup_offset + idx];
596  }
597}
598
599// loads a linear segment of workgroup items into a striped arrangement.
600template <size_t ITEMS_PER_WORK_ITEM, typename InputT, typename InputIteratorT,
601          typename Item>
602[[deprecated("Please use load_direct_striped instead")]]
603__dpct_inline__ void load_striped(const Item &item, InputIteratorT block_itr,
604                                  InputT (&items)[ITEMS_PER_WORK_ITEM]) {
605
606  // This implementation does not take in account range loading across
607  // workgroup items To-do: Decide whether range loading is required for group
608  // loading
609  size_t linear_tid = item.get_local_linear_id();
610  size_t group_work_items = item.get_local_range().size();
611#pragma unroll
612  for (size_t idx = 0; idx < ITEMS_PER_WORK_ITEM; idx++) {
613    items[idx] = block_itr[linear_tid + (idx * group_work_items)];
614  }
615}
616
617/// Load a linear segment of elements into a blocked arrangement across the
618/// work-group.
619///
620/// \tparam T The data type to load.
621/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
622/// onto each work-item.
623/// \tparam InputIteratorT  The random-access iterator type for input \iterator.
624/// \tparam ItemT The sycl::nd_item index space class.
625/// \param item The calling work-item.
626/// \param input_iter The work-group's base input iterator for loading from.
627/// \param data Data to load.
628template <typename T, size_t ElementsPerWorkItem, typename InputIteratorT,
629          typename ItemT>
630__dpct_inline__ void load_direct_blocked(const ItemT &item,
631                                         InputIteratorT input_iter,
632                                         T (&data)[ElementsPerWorkItem]) {
633  size_t work_item_id = item.get_local_linear_id();
634#pragma unroll
635  for (size_t i = 0; i < ElementsPerWorkItem; i++)
636    data[i] = input_iter[(work_item_id * ElementsPerWorkItem) + i];
637}
638
639/// Load a linear segment of elements into a striped arrangement across the
640/// work-group.
641///
642/// \tparam T The data type to load.
643/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
644/// onto each work-item.
645/// \tparam InputIteratorT  The random-access iterator type for input \iterator.
646/// \tparam ItemT The sycl::nd_item index space class.
647/// \param item The calling work-item.
648/// \param input_iter The work-group's base input iterator for loading from.
649/// \param data Data to load.
650template <typename T, int ElementsPerWorkItem, typename InputIteratorT,
651          typename ItemT>
652__dpct_inline__ void load_direct_striped(const ItemT &item,
653                                         InputIteratorT input_iter,
654                                         T (&data)[ElementsPerWorkItem]) {
655  size_t work_group_size = item.get_group().get_local_linear_range();
656  size_t work_item_id = item.get_local_linear_id();
657#pragma unroll
658  for (size_t i = 0; i < ElementsPerWorkItem; i++)
659    data[i] = input_iter[work_item_id + i * work_group_size];
660}
661
662/// Load a linear segment of elements into a sub_group striped arrangement
663/// across the work-group.
664///
665/// \tparam T The data type to load.
666/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
667/// onto each work-item.
668/// \tparam InputIteratorT  The random-access iterator type for input \iterator.
669/// \tparam ItemT The sycl::nd_item index space class.
670/// \param item The calling work-item.
671/// \param input_iter The work-group's base input iterator for loading from.
672/// \param data Data to load.
673template <typename T, int ElementsPerWorkItem, typename InputIteratorT,
674          typename ItemT>
675__dpct_inline__ void
676load_direct_sub_group_striped(const ItemT &item, InputIteratorT input_iter,
677                              T (&data)[ElementsPerWorkItem]) {
678  auto sg = item.get_sub_group();
679  size_t wg_size = item.get_group().get_local_linear_range();
680  size_t sub_group_sliced_items =
681      std::min<size_t>(sg.get_local_linear_range(), wg_size);
682
683#pragma unroll
684  for (size_t i = 0; i < ElementsPerWorkItem; i++) {
685    size_t offset = (sg.get_group_linear_id() * sub_group_sliced_items *
686                     ElementsPerWorkItem) +
687                    (i * sub_group_sliced_items) + sg.get_local_linear_id();
688    data[i] = input_iter[offset];
689  }
690}
691
692/// Load a linear segment of elements into a blocked arrangement across the
693/// work-group, guarded by range.
694///
695/// \tparam T The data type to load.
696/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
697/// onto each work-item.
698/// \tparam InputIteratorT  The random-access iterator type for input \iterator.
699/// \tparam ItemT The sycl::nd_item index space class.
700/// \param item The calling work-item.
701/// \param input_iter The work-group's base input iterator for loading from.
702/// \param data Data to load.
703/// \param valid_items Number of valid items to load
704template <typename T, size_t ElementsPerWorkItem, typename InputIteratorT,
705          typename ItemT>
706__dpct_inline__ void
707load_direct_blocked(const ItemT &item, InputIteratorT input_iter,
708                    T (&data)[ElementsPerWorkItem], int valid_items) {
709  size_t work_item_id = item.get_local_linear_id();
710#pragma unroll
711  for (size_t i = 0; i < ElementsPerWorkItem; i++)
712    if ((work_item_id * ElementsPerWorkItem) + i < valid_items)
713      data[i] = input_iter[(work_item_id * ElementsPerWorkItem) + i];
714}
715
716/// Load a linear segment of elements into a striped arrangement across the
717/// work-group, guarded by range.
718///
719/// \tparam T The data type to load.
720/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
721/// onto each work-item.
722/// \tparam InputIteratorT  The random-access iterator type for input \iterator.
723/// \tparam ItemT The sycl::nd_item index space class.
724/// \param item The calling work-item.
725/// \param input_iter The work-group's base input iterator for loading from.
726/// \param data Data to load.
727/// \param valid_items Number of valid items to load
728template <typename T, int ElementsPerWorkItem, typename InputIteratorT,
729          typename ItemT>
730__dpct_inline__ void
731load_direct_striped(const ItemT &item, InputIteratorT input_iter,
732                    T (&data)[ElementsPerWorkItem], int valid_items) {
733  size_t work_group_size = item.get_group().get_local_linear_range();
734  size_t work_item_id = item.get_local_linear_id();
735#pragma unroll
736  for (size_t i = 0; i < ElementsPerWorkItem; i++)
737    if (work_item_id + (i * work_group_size) < valid_items)
738      data[i] = input_iter[work_item_id + i * work_group_size];
739}
740
741/// Load a linear segment of elements into a sub_group striped arrangement
742/// across the work-group, guarded by range.
743///
744/// \tparam T The data type to load.
745/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
746/// onto each work-item.
747/// \tparam InputIteratorT  The random-access iterator type for input \iterator.
748/// \tparam ItemT The sycl::nd_item index space class.
749/// \param item The calling work-item.
750/// \param input_iter The work-group's base input iterator for loading from.
751/// \param data Data to load.
752/// \param valid_items Number of valid items to load.
753template <typename T, int ElementsPerWorkItem, typename InputIteratorT,
754          typename ItemT>
755__dpct_inline__ void
756load_direct_sub_group_striped(const ItemT &item, InputIteratorT input_iter,
757                              T (&data)[ElementsPerWorkItem], int valid_items) {
758  auto sg = item.get_sub_group();
759  size_t wg_size = item.get_group().get_local_linear_range();
760  size_t sub_group_sliced_items =
761      std::min<size_t>(sg.get_local_linear_range(), wg_size);
762
763#pragma unroll
764  for (size_t i = 0; i < ElementsPerWorkItem; i++) {
765    size_t offset = (sg.get_group_linear_id() * sub_group_sliced_items *
766                     ElementsPerWorkItem) +
767                    (i * sub_group_sliced_items) + sg.get_local_linear_id();
768    if (offset < valid_items) {
769      data[i] = input_iter[offset];
770    }
771  }
772}
773
774/// Load a linear segment of elements into a blocked arrangement across the
775/// work-group, guarded by range.
776///
777/// \tparam T The data type to load.
778/// \tparam DefaultT The type of default value to assign out-of-bound items.
779/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
780/// onto each work-item.
781/// \tparam InputIteratorT  The random-access iterator type for input \iterator.
782/// \tparam ItemT The sycl::nd_item index space class.
783/// \param item The calling work-item.
784/// \param input_iter The work-group's base input iterator for loading from.
785/// \param data Data to load.
786/// \param valid_items Number of valid items to load
787/// \param default_value Default value to assign out-of-bound items.
788template <typename T, typename DefaultT, size_t ElementsPerWorkItem,
789          typename InputIteratorT, typename ItemT>
790__dpct_inline__ void
791load_direct_blocked(const ItemT &item, InputIteratorT input_iter,
792                    T (&data)[ElementsPerWorkItem], int valid_items,
793                    DefaultT default_value) {
794#pragma unroll
795  for (int i = 0; i < ElementsPerWorkItem; i++) {
796    data[i] = default_value;
797  }
798  load_direct_blocked(item, input_iter, data, valid_items);
799}
800
801/// Load a linear segment of elements into a striped arrangement across the
802/// work-group, guarded by range.
803///
804/// \tparam T The data type to load.
805/// \tparam DefaultT The type of default value to assign out-of-bound items.
806/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
807/// onto each work-item.
808/// \tparam InputIteratorT  The random-access iterator type for input \iterator.
809/// \tparam ItemT The sycl::nd_item index space class.
810/// \param item The calling work-item.
811/// \param input_iter The work-group's base input iterator for loading from.
812/// \param data Data to load.
813/// \param valid_items Number of valid items to load
814/// \param default_value Default value to assign out-of-bound items.
815template <typename T, typename DefaultT, int ElementsPerWorkItem,
816          typename InputIteratorT, typename ItemT>
817__dpct_inline__ void
818load_direct_striped(const ItemT &item, InputIteratorT input_iter,
819                    T (&data)[ElementsPerWorkItem], int valid_items,
820                    DefaultT default_value) {
821#pragma unroll
822  for (int i = 0; i < ElementsPerWorkItem; i++) {
823    data[i] = default_value;
824  }
825  load_direct_striped(item, input_iter, data, valid_items);
826}
827
828/// Load a linear segment of elements into a sub_group striped arrangement
829/// across the work-group, guarded by range.
830///
831/// \tparam T The data type to load.
832/// \tparam DefaultT The type of default value to assign out-of-bound items.
833/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
834/// onto each work-item.
835/// \tparam InputIteratorT  The random-access iterator type for input \iterator.
836/// \tparam ItemT The sycl::nd_item index space class.
837/// \param item The calling work-item.
838/// \param input_iter The work-group's base input iterator for loading from.
839/// \param data Data to load.
840/// \param valid_items Number of valid items to load.
841/// \param default_value Default value to assign out-of-bound items.
842template <typename T, typename DefaultT, int ElementsPerWorkItem,
843          typename InputIteratorT, typename ItemT>
844__dpct_inline__ void
845load_direct_sub_group_striped(const ItemT &item, InputIteratorT input_iter,
846                              T (&data)[ElementsPerWorkItem], int valid_items,
847                              DefaultT default_value) {
848#pragma unroll
849  for (int i = 0; i < ElementsPerWorkItem; i++) {
850    data[i] = default_value;
851  }
852  load_direct_sub_group_striped(item, input_iter, data, valid_items);
853}
854
855/// Store a blocked arrangement of items across a work-group into a linear
856/// segment of items.
857///
858/// \tparam T The data type to store.
859/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
860/// onto each work-item.
861/// \tparam OutputIteratorT  The random-access iterator type for output.
862/// \iterator.
863/// \tparam ItemT The sycl::nd_item index space class.
864/// \param item The calling work-item.
865/// \param output_iter The work-group's base output iterator for writing.
866/// \param data Data to store.
867template <typename T, size_t ElementsPerWorkItem, typename OutputIteratorT,
868          typename ItemT>
869__dpct_inline__ void store_direct_blocked(const ItemT &item,
870                                          OutputIteratorT output_iter,
871                                          T (&data)[ElementsPerWorkItem]) {
872  size_t work_item_id = item.get_local_linear_id();
873  OutputIteratorT work_item_iter =
874      output_iter + (work_item_id * ElementsPerWorkItem);
875#pragma unroll
876  for (size_t i = 0; i < ElementsPerWorkItem; i++)
877    work_item_iter[i] = data[i];
878}
879
880/// Store a striped arrangement of items across a work-group into a linear
881/// segment of items.
882///
883/// \tparam T The data type to store.
884/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
885/// onto each work-item.
886/// \tparam OutputIteratorT  The random-access iterator type for output.
887/// \iterator.
888/// \tparam ItemT The sycl::nd_item index space class.
889/// \param item The calling work-item.
890/// \param output_iter The work-group's base output iterator for writing.
891/// \param data Data to store.
892template <typename T, size_t ElementsPerWorkItem, typename OutputIteratorT,
893          typename ItemT>
894__dpct_inline__ void store_direct_striped(const ItemT &item,
895                                          OutputIteratorT output_iter,
896                                          T (&data)[ElementsPerWorkItem]) {
897  size_t work_group_size = item.get_group().get_local_linear_range();
898  size_t work_item_id = item.get_local_linear_id();
899  OutputIteratorT work_item_iter = output_iter + work_item_id;
900#pragma unroll
901  for (size_t i = 0; i < ElementsPerWorkItem; i++)
902    work_item_iter[i * work_group_size] = data[i];
903}
904
905/// Store a sub_group striped arrangement of items across a work-group into a
906/// linear segment of items.
907///
908/// \tparam T The data type to store.
909/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
910/// onto each work-item.
911/// \tparam OutputIteratorT  The random-access iterator type for output.
912/// \iterator.
913/// \tparam ItemT The sycl::nd_item index space class.
914/// \param item The calling work-item.
915/// \param output_iter The work-group's base output iterator for writing.
916/// \param data Data to store.
917template <typename T, size_t ElementsPerWorkItem, typename OutputIteratorT,
918          typename ItemT>
919__dpct_inline__ void
920store_direct_sub_group_striped(const ItemT &item, OutputIteratorT output_iter,
921                               T (&data)[ElementsPerWorkItem]) {
922  auto sg = item.get_sub_group();
923  size_t wg_size = item.get_group().get_local_linear_range();
924  size_t sub_group_sliced_items =
925      std::min<size_t>(sg.get_local_linear_range(), wg_size);
926
927  OutputIteratorT work_item_iter =
928      output_iter +
929      sg.get_group_linear_id() * sub_group_sliced_items * ElementsPerWorkItem +
930      sg.get_local_linear_id();
931
932#pragma unroll
933  for (size_t i = 0; i < ElementsPerWorkItem; i++) {
934    work_item_iter[i * sub_group_sliced_items] = data[i];
935  }
936}
937
938/// Store a blocked arrangement of items across a work-group into a linear
939/// segment of items, guarded by range.
940///
941/// \tparam T The data type to store.
942/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
943/// onto each work-item.
944/// \tparam OutputIteratorT  The random-access iterator type for output.
945/// \iterator.
946/// \tparam ItemT The sycl::nd_item index space class.
947/// \param item The calling work-item.
948/// \param output_iter The work-group's base output iterator for writing.
949/// \param data Data to store.
950/// \param valid_items Number of valid items to load
951template <typename T, size_t ElementsPerWorkItem, typename OutputIteratorT,
952          typename ItemT>
953__dpct_inline__ void
954store_direct_blocked(const ItemT &item, OutputIteratorT output_iter,
955                     T (&data)[ElementsPerWorkItem], size_t valid_items) {
956  size_t work_item_id = item.get_local_linear_id();
957  OutputIteratorT work_item_iter =
958      output_iter + (work_item_id * ElementsPerWorkItem);
959#pragma unroll
960  for (size_t i = 0; i < ElementsPerWorkItem; i++)
961    if (i + (work_item_id * ElementsPerWorkItem) < valid_items)
962      work_item_iter[i] = data[i];
963}
964
965/// Store a striped arrangement of items across a work-group into a linear
966/// segment of items, guarded by range.
967///
968/// \tparam T The data type to store.
969/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
970/// onto each work-item.
971/// \tparam OutputIteratorT  The random-access iterator type for output.
972/// \iterator.
973/// \tparam ItemT The sycl::nd_item index space class.
974/// \param item The calling work-item.
975/// \param output_iter The work-group's base output iterator for writing.
976/// \param items Data to store.
977/// \param valid_items Number of valid items to load
978template <typename T, size_t ElementsPerWorkItem, typename OutputIteratorT,
979          typename ItemT>
980__dpct_inline__ void
981store_direct_striped(const ItemT &item, OutputIteratorT output_iter,
982                     T (&data)[ElementsPerWorkItem], size_t valid_items) {
983  size_t work_group_size = item.get_group().get_local_linear_range();
984  size_t work_item_id = item.get_local_linear_id();
985  OutputIteratorT work_item_iter = output_iter + work_item_id;
986#pragma unroll
987  for (size_t i = 0; i < ElementsPerWorkItem; i++)
988    if ((i * work_group_size) + work_item_id < valid_items)
989      work_item_iter[i * work_group_size] = data[i];
990}
991
992/// Store a sub_group striped arrangement of items across a work-group into a
993/// linear segment of items.
994///
995/// \tparam T The data type to store.
996/// \tparam ElementsPerWorkItem The number of consecutive elements partitioned
997/// onto each work-item.
998/// \tparam OutputIteratorT  The random-access iterator type for output.
999/// \iterator.
1000/// \tparam ItemT The sycl::nd_item index space class.
1001/// \param item The calling work-item.
1002/// \param output_iter The work-group's base output iterator for writing.
1003/// \param data Data to store.
1004/// \param valid_items Number of valid items to load
1005template <typename T, size_t ElementsPerWorkItem, typename OutputIteratorT,
1006          typename ItemT>
1007__dpct_inline__ void
1008store_direct_sub_group_striped(const ItemT &item, OutputIteratorT output_iter,
1009                               T (&data)[ElementsPerWorkItem],
1010                               size_t valid_items) {
1011  auto sg = item.get_sub_group();
1012  size_t wg_size = item.get_group().get_local_linear_range();
1013  size_t sub_group_sliced_items =
1014      std::min<size_t>(sg.get_local_linear_range(), wg_size);
1015  size_t sub_group_offset =
1016      sg.get_group_linear_id() * sub_group_sliced_items * ElementsPerWorkItem;
1017  OutputIteratorT work_item_iter =
1018      output_iter + sub_group_offset + sg.get_local_linear_id();
1019
1020#pragma unroll
1021  for (size_t i = 0; i < ElementsPerWorkItem; i++) {
1022    if (sub_group_offset + sg.get_local_linear_id() +
1023            i * sub_group_sliced_items <
1024        valid_items) {
1025      work_item_iter[i * sub_group_sliced_items] = data[i];
1026    }
1027  }
1028}
1029
1030// loads a linear segment of workgroup items into a subgroup striped
1031// arrangement. Created as free function until exchange mechanism is
1032// implemented.
1033// To-do: inline this function with BLOCK_LOAD_WARP_TRANSPOSE mechanism
1034template <size_t ITEMS_PER_WORK_ITEM, typename InputT, typename InputIteratorT,
1035          typename Item>
1036__dpct_inline__ void
1037uninitialized_load_subgroup_striped(const Item &item, InputIteratorT block_itr,
1038                                    InputT (&items)[ITEMS_PER_WORK_ITEM]) {
1039
1040  // This implementation does not take in account range loading across
1041  // workgroup items To-do: Decide whether range loading is required for group
1042  // loading
1043  // This implementation uses unintialized memory for loading linear segments
1044  // into warp striped arrangement.
1045  uint32_t subgroup_offset = item.get_sub_group().get_local_linear_id();
1046  uint32_t subgroup_size = item.get_sub_group().get_local_linear_range();
1047  uint32_t subgroup_idx = item.get_sub_group().get_group_linear_id();
1048  uint32_t initial_offset =
1049      (subgroup_idx * ITEMS_PER_WORK_ITEM * subgroup_size) + subgroup_offset;
1050#pragma unroll
1051  for (size_t idx = 0; idx < ITEMS_PER_WORK_ITEM; idx++) {
1052    new (&items[idx]) InputT(block_itr[initial_offset + (idx * subgroup_size)]);
1053  }
1054}
1055// template parameters :
1056// ITEMS_PER_WORK_ITEM: size_t variable controlling the number of items per
1057// thread/work_item
1058// ALGORITHM: load_algorithm variable controlling the type of load operation.
1059// InputT: type for input sequence.
1060// InputIteratorT:  input iterator type
1061// Item : typename parameter resembling sycl::nd_item<3> .
1062template <size_t ITEMS_PER_WORK_ITEM, load_algorithm ALGORITHM, typename InputT,
1063          typename InputIteratorT, typename Item>
1064class [[deprecated(
1065    "Please use dpct::group::group_load instead")]] workgroup_load {
1066public:
1067  static size_t get_local_memory_size(size_t group_work_items) { return 0; }
1068  workgroup_load(uint8_t *local_memory) : _local_memory(local_memory) {}
1069
1070  __dpct_inline__ void load(const Item &item, InputIteratorT block_itr,
1071                            InputT (&items)[ITEMS_PER_WORK_ITEM]) {
1072
1073    if constexpr (ALGORITHM == BLOCK_LOAD_DIRECT) {
1074      load_blocked<ITEMS_PER_WORK_ITEM>(item, block_itr, items);
1075    } else if constexpr (ALGORITHM == BLOCK_LOAD_STRIPED) {
1076      load_striped<ITEMS_PER_WORK_ITEM>(item, block_itr, items);
1077    }
1078  }
1079
1080private:
1081  uint8_t *_local_memory;
1082};
1083
1084/// Enumerates alternative algorithms for dpct::group::group_load to read a
1085/// linear segment of data from memory into a blocked arrangement across a
1086/// work-group.
1087enum class group_load_algorithm {
1088  /// A blocked arrangement of data is read directly from memory.
1089  blocked,
1090
1091  /// A striped arrangement of data is read directly from memory.
1092  striped,
1093
1094  /// A striped arrangement of data is read directly from memory and transposed
1095  /// into a blocked arrangement.
1096  transpose,
1097
1098  /// A sub_group striped arrangement of data is read directly from memory and
1099  /// transposed into a blocked arrangement.
1100  sub_group_transpose,
1101};
1102
1103/// Provide methods for loading a linear segment of items from memory into a
1104/// blocked arrangement across a work-group.
1105///
1106/// \tparam T The input data type.
1107/// \tparam ElementsPerWorkItem The number of data elements assigned to a
1108/// work-item.
1109/// \tparam LoadAlgorithm The data movement strategy, default is blocked.
1110template <typename T, size_t ElementsPerWorkItem,
1111          group_load_algorithm LoadAlgorithm = group_load_algorithm::blocked>
1112class group_load {
1113public:
1114  static size_t get_local_memory_size(size_t work_group_size) {
1115    if constexpr ((LoadAlgorithm == group_load_algorithm::transpose) ||
1116                  (LoadAlgorithm ==
1117                   group_load_algorithm::sub_group_transpose)) {
1118      return dpct::group::exchange<
1119          T, ElementsPerWorkItem>::get_local_memory_size(work_group_size);
1120    }
1121    return 0;
1122  }
1123  group_load(uint8_t *local_memory) : _local_memory(local_memory) {}
1124
1125  /// Load a linear segment of items from memory.
1126  ///
1127  /// Suppose 512 integer data elements partitioned across 128 work-items, where
1128  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
1129  /// \p input across the work-group is:
1130  ///
1131  ///   1, 2, 3, 4, 5, 6, 7, ..., 508, 509, 510, 511.
1132  ///
1133  /// The blocked order \p data of each work-item will be:
1134  ///
1135  ///   {[0,1,2,3], [4,5,6,7], ..., [508,509,510,511]}.
1136  ///
1137  /// The striped order \p output of each work-item will be:
1138  ///
1139  ///   {[0,128,256,384], [1,129,257,385], ..., [127,255,383,511]}.
1140  ///
1141  /// \tparam ItemT The sycl::nd_item index space class.
1142  /// \tparam InputIteratorT The random-access iterator type for input
1143  /// \iterator.
1144  /// \param item The work-item identifier.
1145  /// \param input_iter The work-group's base input iterator for loading from.
1146  /// \param data The data to load.
1147  template <typename ItemT, typename InputIteratorT>
1148  __dpct_inline__ void load(const ItemT &item, InputIteratorT input_iter,
1149                            T (&data)[ElementsPerWorkItem]) {
1150    if constexpr (LoadAlgorithm == group_load_algorithm::blocked) {
1151      load_direct_blocked<T, ElementsPerWorkItem, InputIteratorT, ItemT>(
1152          item, input_iter, data);
1153    } else if constexpr (LoadAlgorithm == group_load_algorithm::striped) {
1154      load_direct_striped<T, ElementsPerWorkItem, InputIteratorT, ItemT>(
1155          item, input_iter, data);
1156    } else if constexpr (LoadAlgorithm == group_load_algorithm::transpose) {
1157      load_direct_striped<T, ElementsPerWorkItem, InputIteratorT, ItemT>(
1158          item, input_iter, data);
1159      dpct::group::exchange<T, ElementsPerWorkItem>(_local_memory)
1160          .striped_to_blocked(item, data, data);
1161    } else if constexpr (LoadAlgorithm ==
1162                         group_load_algorithm::sub_group_transpose) {
1163      load_direct_sub_group_striped<T, ElementsPerWorkItem, InputIteratorT,
1164                                    ItemT>(item, input_iter, data);
1165      dpct::group::exchange<T, ElementsPerWorkItem>(_local_memory)
1166          .sub_group_striped_to_blocked(item, data, data);
1167    }
1168  }
1169
1170  /// Load a linear segment of items from memory, guarded by range.
1171  ///
1172  /// Suppose 512 integer data elements partitioned across 128 work-items, where
1173  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and
1174  /// valid_items is 5, the \p input across the work-group is:
1175  ///
1176  ///   0, 1, 2, 3, 4, 5, 6, 7, ..., 508, 509, 510, 511.
1177  ///
1178  /// The blocked order \p data of each work-item will be:
1179  ///
1180  ///   {[0,1,2,3], [4,?,?,?], ..., [?,?,?,?]}.
1181  ///
1182  /// The striped order \p output of each work-item will be:
1183  ///
1184  ///   {[0,?,?,?], [1,?,?,?], [2,?,?,?], [3,?,?,?] ..., [?,?,?,?]}.
1185  ///
1186  /// \tparam ItemT The sycl::nd_item index space class.
1187  /// \tparam InputIteratorT The random-access iterator type for input
1188  /// \iterator.
1189  /// \param item The work-item identifier.
1190  /// \param input_iter The work-group's base input iterator for loading from.
1191  /// \param data The data to load.
1192  /// \param valid_items Number of valid items to load
1193  template <typename ItemT, typename InputIteratorT>
1194  __dpct_inline__ void load(const ItemT &item, InputIteratorT input_iter,
1195                            T (&data)[ElementsPerWorkItem], int valid_items) {
1196    if constexpr (LoadAlgorithm == group_load_algorithm::blocked) {
1197      load_direct_blocked<T, ElementsPerWorkItem, InputIteratorT, ItemT>(
1198          item, input_iter, data, valid_items);
1199    } else if constexpr (LoadAlgorithm == group_load_algorithm::striped) {
1200      load_direct_striped<T, ElementsPerWorkItem, InputIteratorT, ItemT>(
1201          item, input_iter, data, valid_items);
1202    } else if constexpr (LoadAlgorithm == group_load_algorithm::transpose) {
1203      load_direct_striped<T, ElementsPerWorkItem, InputIteratorT, ItemT>(
1204          item, input_iter, data, valid_items);
1205      dpct::group::exchange<T, ElementsPerWorkItem>(_local_memory)
1206          .striped_to_blocked(item, data, data);
1207    } else if constexpr (LoadAlgorithm ==
1208                         group_load_algorithm::sub_group_transpose) {
1209      load_direct_sub_group_striped<T, ElementsPerWorkItem, InputIteratorT,
1210                                    ItemT>(item, input_iter, data, valid_items);
1211      dpct::group::exchange<T, ElementsPerWorkItem>(_local_memory)
1212          .sub_group_striped_to_blocked(item, data, data);
1213    }
1214  }
1215
1216  /// Load a linear segment of items from memory, guarded by range.
1217  ///
1218  /// Suppose 512 integer data elements partitioned across 128 work-items, where
1219  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and
1220  /// valid_items is 5, the \p input across the work-group is:
1221  ///
1222  ///   0, 1, 2, 3, 4, 5, 6, 7, ..., 508, 509, 510, 511.
1223  ///
1224  /// The blocked order \p data of each work-item will be:
1225  ///
1226  ///   {[0,1,2,3], [4,?,?,?], ..., [?,?,?,?]}.
1227  ///
1228  /// The striped order \p output of each work-item will be:
1229  ///
1230  ///   {[0,?,?,?], [1,?,?,?], [2,?,?,?], [3,?,?,?] ..., [?,?,?,?]}.
1231  ///
1232  /// \tparam ItemT The sycl::nd_item index space class.
1233  /// \tparam InputIteratorT The random-access iterator type for input
1234  /// \iterator.
1235  /// \tparam DefaultT The type of default value to assign out-of-bound items.
1236  /// \param item The work-item identifier.
1237  /// \param input_iter The work-group's base input iterator for loading from.
1238  /// \param data The data to load.
1239  /// \param valid_items Number of valid items to load.
1240  /// \param default_value Default value to assign out-of-bound items.
1241  template <typename ItemT, typename InputIteratorT, typename DefaultT>
1242  __dpct_inline__ void load(const ItemT &item, InputIteratorT input_iter,
1243                            T (&data)[ElementsPerWorkItem], int valid_items,
1244                            DefaultT default_value) {
1245    if constexpr (LoadAlgorithm == group_load_algorithm::blocked) {
1246      load_direct_blocked<T, DefaultT, ElementsPerWorkItem, InputIteratorT,
1247                          ItemT>(item, input_iter, data, valid_items,
1248                                 default_value);
1249    } else if constexpr (LoadAlgorithm == group_load_algorithm::striped) {
1250      load_direct_striped<T, DefaultT, ElementsPerWorkItem, InputIteratorT,
1251                          ItemT>(item, input_iter, data, valid_items,
1252                                 default_value);
1253    } else if constexpr (LoadAlgorithm == group_load_algorithm::transpose) {
1254      load_direct_striped<T, DefaultT, ElementsPerWorkItem, InputIteratorT,
1255                          ItemT>(item, input_iter, data, valid_items,
1256                                 default_value);
1257      dpct::group::exchange<T, ElementsPerWorkItem>(_local_memory)
1258          .striped_to_blocked(item, data, data);
1259    } else if constexpr (LoadAlgorithm ==
1260                         group_load_algorithm::sub_group_transpose) {
1261      load_direct_sub_group_striped<T, DefaultT, ElementsPerWorkItem,
1262                                    InputIteratorT, ItemT>(
1263          item, input_iter, data, valid_items, default_value);
1264      dpct::group::exchange<T, ElementsPerWorkItem>(_local_memory)
1265          .sub_group_striped_to_blocked(item, data, data);
1266    }
1267  }
1268
1269private:
1270  uint8_t *_local_memory;
1271};
1272
1273/// Enumerates alternative algorithms for dpct::group::group_load to write a
1274/// blocked arrangement of items across a work-group to a linear segment of
1275/// memory.
1276enum class group_store_algorithm {
1277  /// A blocked arrangement of data is written directly to memory.
1278  blocked,
1279
1280  /// A striped arrangement of data is written directly to memory.
1281  striped,
1282
1283  /// A blocked arrangement of data transposed into a striped arrangement and
1284  /// then written directly to memory.
1285  transpose,
1286
1287  /// A blocked arrangement of data transposed into a sub_group striped
1288  /// arrangement and then written directly to memory.
1289  sub_group_transpose,
1290};
1291
1292/// Provide methods for writing a blocked arrangement of elements partitioned
1293/// across a work-group to a linear segment of memory.
1294///
1295/// \tparam T The output data type.
1296/// \tparam ElementsPerWorkItem The number of data elements assigned to a
1297/// work-item.
1298/// \tparam StoreAlgorithm The data movement strategy, default is blocked.
1299template <typename T, size_t ElementsPerWorkItem,
1300          group_store_algorithm StoreAlgorithm = group_store_algorithm::blocked>
1301class group_store {
1302public:
1303  static size_t get_local_memory_size(size_t work_group_size) {
1304    if constexpr ((StoreAlgorithm == group_store_algorithm::transpose) ||
1305                  (StoreAlgorithm ==
1306                   group_store_algorithm::sub_group_transpose)) {
1307      return dpct::group::exchange<
1308          T, ElementsPerWorkItem>::get_local_memory_size(work_group_size);
1309    }
1310    return 0;
1311  }
1312  group_store(uint8_t *local_memory) : _local_memory(local_memory) {}
1313
1314  /// Store items into a linear segment of memory.
1315  ///
1316  /// Suppose 512 integer data elements partitioned across 128 work-items, where
1317  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and the
1318  /// \p input across the work-group is:
1319  ///
1320  ///   {[0,1,2,3], [4,5,6,7], ..., [508,509,510,511]}.
1321  ///
1322  /// The blocked order \p output will be:
1323  ///
1324  ///   1, 2, 3, 4, 5, 6, 7, ..., 508, 509, 510, 511.
1325  ///
1326  /// The striped order \p output will be:
1327  ///
1328  ///   0, 128, 256, 384, 1, 129, 257, 385, ..., 127, 255, 383, 511.
1329  ///
1330  /// \tparam ItemT The sycl::nd_item index space class.
1331  /// \tparam OutputIteratorT The random-access iterator type for \p output
1332  /// iterator.
1333  /// \param item The work-item identifier.
1334  /// \param input The input data of each work-item.
1335  /// \param data The data to store.
1336  template <typename ItemT, typename OutputIteratorT>
1337  __dpct_inline__ void store(const ItemT &item, OutputIteratorT output_iter,
1338                             T (&data)[ElementsPerWorkItem]) {
1339    if constexpr (StoreAlgorithm == group_store_algorithm::blocked) {
1340      store_direct_blocked<T, ElementsPerWorkItem, OutputIteratorT, ItemT>(
1341          item, output_iter, data);
1342    } else if constexpr (StoreAlgorithm == group_store_algorithm::striped) {
1343      store_direct_striped<T, ElementsPerWorkItem, OutputIteratorT, ItemT>(
1344          item, output_iter, data);
1345    } else if constexpr (StoreAlgorithm == group_store_algorithm::transpose) {
1346      dpct::group::exchange<T, ElementsPerWorkItem>(_local_memory)
1347          .blocked_to_striped(item, data, data);
1348      store_direct_striped<T, ElementsPerWorkItem, OutputIteratorT, ItemT>(
1349          item, output_iter, data);
1350    } else if constexpr (StoreAlgorithm ==
1351                         group_store_algorithm::sub_group_transpose) {
1352      dpct::group::exchange<T, ElementsPerWorkItem>(_local_memory)
1353          .blocked_to_sub_group_striped(item, data, data);
1354      store_direct_sub_group_striped<T, ElementsPerWorkItem, OutputIteratorT,
1355                                     ItemT>(item, output_iter, data);
1356    }
1357  }
1358
1359  /// Store items into a linear segment of memory, guarded by range.
1360  ///
1361  /// Suppose 512 integer data elements partitioned across 128 work-items, where
1362  /// each work-item owns 4 ( \p ElementsPerWorkItem ) data elements and
1363  /// \p valid_items is 5, the \p output across the work-group is:
1364  ///
1365  ///   {[0,0,0,0], [0,0,0,0], ..., [0,0,0,0]}.
1366  ///
1367  /// The blocked order \p output will be:
1368  ///
1369  ///   0, 1, 2, 3, 4, 5, 0, 0, ..., 0, 0, 0, 0.
1370  ///
1371  /// The striped order \p output will be:
1372  ///
1373  ///   0, 4, 8, 12, 16, 0, 0, 0, ..., 0, 0, 0, 0.
1374  ///
1375  /// \tparam ItemT The sycl::nd_item index space class.
1376  /// \tparam OutputIteratorT The random-access iterator type for \p output
1377  /// iterator.
1378  /// \param item The work-item identifier.
1379  /// \param input The input data of each work-item.
1380  /// \param data The data to store.
1381  /// \param valid_items Number of valid items to load
1382  template <typename ItemT, typename OutputIteratorT>
1383  __dpct_inline__ void store(const ItemT &item, OutputIteratorT output_iter,
1384                             T (&data)[ElementsPerWorkItem],
1385                             size_t valid_items) {
1386    if constexpr (StoreAlgorithm == group_store_algorithm::blocked) {
1387      store_direct_blocked<T, ElementsPerWorkItem, OutputIteratorT, ItemT>(
1388          item, output_iter, data, valid_items);
1389    } else if constexpr (StoreAlgorithm == group_store_algorithm::striped) {
1390      store_direct_striped<T, ElementsPerWorkItem, OutputIteratorT, ItemT>(
1391          item, output_iter, data, valid_items);
1392    } else if constexpr (StoreAlgorithm == group_store_algorithm::transpose) {
1393      dpct::group::exchange<T, ElementsPerWorkItem>(_local_memory)
1394          .blocked_to_striped(item, data, data);
1395      store_direct_striped<T, ElementsPerWorkItem, OutputIteratorT, ItemT>(
1396          item, output_iter, data, valid_items);
1397    } else if constexpr (StoreAlgorithm ==
1398                         group_store_algorithm::sub_group_transpose) {
1399      dpct::group::exchange<T, ElementsPerWorkItem>(_local_memory)
1400          .blocked_to_sub_group_striped(item, data, data);
1401      store_direct_sub_group_striped<T, ElementsPerWorkItem, OutputIteratorT,
1402                                     ItemT>(item, output_iter, data,
1403                                            valid_items);
1404    }
1405  }
1406
1407private:
1408  uint8_t *_local_memory;
1409};
1410
1411/// The work-group wide shuffle operations that allow work-items to exchange
1412/// data elements with other work-items within the same work-group.
1413///
1414/// \tparam T The type of the data elements.
1415/// \tparam group_dim_0 The first dimension size of the work-group.
1416/// \tparam group_dim_1 The second dimension size of the work-group.
1417/// \tparam group_dim_2 The third dimension size of the work-group.
1418template <typename T, int group_dim_0, int group_dim_1 = 1, int group_dim_2 = 1>
1419class group_shuffle {
1420  T *_local_memory = nullptr;
1421  static constexpr size_t group_work_items =
1422      group_dim_0 * group_dim_1 * group_dim_2;
1423
1424public:
1425  static constexpr size_t get_local_memory_size(size_t work_group_size) {
1426    return sizeof(T) * work_group_size;
1427  }
1428  group_shuffle(uint8_t *local_memory) : _local_memory((T *)local_memory) {}
1429
1430  /// Selects a value from a work-item at a given distance in the work-group
1431  /// and stores the value in the output.
1432  ///
1433  /// \tparam ItemT The work-item identifier type.
1434  /// \param item The work-item identifier.
1435  /// \param input The input from the calling work-item.
1436  /// \param output The output where the selected data will be stored.
1437  /// \param distance The distance of work-items to look ahead or behind in the
1438  /// work-group.
1439  template <typename ItemT>
1440  __dpct_inline__ void select(const ItemT &item, T input, T &output,
1441                              int distance = 1) {
1442    auto g = item.get_group();
1443    size_t id = g.get_local_linear_id();
1444    _local_memory[id] = input;
1445
1446    sycl::group_barrier(g, sycl::memory_scope::work_group);
1447
1448    const int target_id = static_cast<int>(id) + distance;
1449    if ((target_id >= 0) && (target_id < group_work_items)) {
1450      output = _local_memory[static_cast<size_t>(target_id)];
1451    }
1452  }
1453  /// Selects a value from a work-item at a given distance in the work-group
1454  /// and stores the value in the output, using a wrapped index to handle
1455  /// overflow.
1456  ///
1457  /// \tparam ItemT The work-item identifier type.
1458  /// \param item The work-item identifier.
1459  /// \param input The input data to be selected.
1460  /// \param output The output where the selected data will be stored.
1461  /// \param distance The number of work-items to look ahead in the
1462  /// work-group.
1463  template <typename ItemT>
1464  __dpct_inline__ void select2(const ItemT &item, T input, T &output,
1465                               unsigned int distance = 1) {
1466    auto g = item.get_group();
1467    size_t id = g.get_local_linear_id();
1468    _local_memory[id] = input;
1469
1470    sycl::group_barrier(g, sycl::memory_scope::work_group);
1471
1472    unsigned int offset = id + distance;
1473    if (offset >= group_work_items)
1474      offset -= group_work_items;
1475
1476    output = _local_memory[offset];
1477  }
1478  /// Performs a shuffle operation to move data to the right across the
1479  /// work-items, shifting elements in a work-item array by one position to the
1480  /// right.
1481  ///
1482  /// \tparam ElementsPerWorkItem The number of data elements per work-item.
1483  /// \tparam ItemT The work-item identifier type.
1484  /// \param item The work-item identifier.
1485  /// \param input The input data to be shuffled.
1486  /// \param output The array that will store the shuffle result.
1487  template <int ElementsPerWorkItem, typename ItemT>
1488  __dpct_inline__ void shuffle_right(const ItemT &item,
1489                                     T (&input)[ElementsPerWorkItem],
1490                                     T (&output)[ElementsPerWorkItem]) {
1491    auto g = item.get_group();
1492    size_t id = g.get_local_linear_id();
1493    _local_memory[id] = input[ElementsPerWorkItem - 1];
1494
1495    sycl::group_barrier(g, sycl::memory_scope::work_group);
1496
1497#pragma unroll
1498    for (int index = ElementsPerWorkItem - 1; index > 0; --index)
1499      output[index] = input[index - 1];
1500
1501    if (id > 0)
1502      output[0] = _local_memory[id - 1];
1503  }
1504  /// Performs a shuffle operation to move data to the right across the
1505  /// work-items, storing the suffix of the group after the shuffle operation.
1506  ///
1507  /// \tparam ElementsPerWorkItem The number of data elements per work-item.
1508  /// \tparam ItemT The work-item identifier type.
1509  /// \param item The work-item identifier.
1510  /// \param input The input data to be shuffled.
1511  /// \param output The array that will store the shuffle result.
1512  /// \param group_suffix The suffix of the group after the shuffle.
1513  template <int ElementsPerWorkItem, typename ItemT>
1514  __dpct_inline__ void
1515  shuffle_right(const ItemT &item, T (&input)[ElementsPerWorkItem],
1516                T (&output)[ElementsPerWorkItem], T &group_suffix) {
1517    shuffle_right(item, input, output);
1518    group_suffix = _local_memory[group_work_items - 1];
1519  }
1520  /// Performs a shuffle operation to move data to the left across the
1521  /// work-items, shifting elements in a work-item array by one position to the
1522  /// left.
1523  ///
1524  /// \tparam ElementsPerWorkItem The number of data elements per work-item.
1525  /// \tparam ItemT The work-item identifier type.
1526  /// \param item The work-item identifier.
1527  /// \param input The input data to be shuffled.
1528  /// \param output The array that will store the shuffle result.
1529  template <int ElementsPerWorkItem, typename ItemT>
1530  __dpct_inline__ void shuffle_left(const ItemT &item,
1531                                    T (&input)[ElementsPerWorkItem],
1532                                    T (&output)[ElementsPerWorkItem]) {
1533    auto g = item.get_group();
1534    size_t id = g.get_local_linear_id();
1535    _local_memory[id] = input[0];
1536
1537    sycl::group_barrier(g, sycl::memory_scope::work_group);
1538
1539#pragma unroll
1540    for (int index = 0; index < ElementsPerWorkItem - 1; index++)
1541      output[index] = input[index + 1];
1542
1543    if (id < group_work_items - 1)
1544      output[ElementsPerWorkItem - 1] = _local_memory[id + 1];
1545  }
1546  /// Performs a shuffle operation to move data to the left across the
1547  /// work-items, storing the prefix of the group before the shuffle operation.
1548  ///
1549  /// \tparam ElementsPerWorkItem The number of data elements per work-item.
1550  /// \tparam ItemT The work-item identifier type.
1551  /// \param item The work-item identifier.
1552  /// \param input The input data to be shuffled.
1553  /// \param output The array that will store the shuffle result.
1554  /// \param group_prefix The prefix of the group before the shuffle.
1555  template <int ElementsPerWorkItem, typename ItemT>
1556  __dpct_inline__ void
1557  shuffle_left(const ItemT &item, T (&input)[ElementsPerWorkItem],
1558               T (&output)[ElementsPerWorkItem], T &group_prefix) {
1559    shuffle_left(item, input, output);
1560    group_prefix = _local_memory[0];
1561  }
1562};
1563} // namespace group
1564} // namespace dpct
1565
1566#endif // __DPCT_GROUP_UTILS_HPP__