okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
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 : 30static size_t get_local_memory_size (size_t group_threads ) { 31size_t padding_values = 32 (INSERT_PADDING ) 33 ? ((group_threads * ElementsPerWorkItem ) >>LOG_LOCAL_MEMORY_BANKS ) 34 :0 ; 35return (group_threads * ElementsPerWorkItem + padding_values )* sizeof (T ); 36 } 37 38exchange (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. 42static size_t adjust_by_padding (size_t offset ) { 43 44if constexpr (INSERT_PADDING ) { 45offset = detail::shr_add (offset ,LOG_LOCAL_MEMORY_BANKS ,offset ); 46 } 47return offset ; 48 } 49 50struct blocked_offset { 51template < typename Item > size_t operator()(Item item ,size_t i ) { 52size_t offset = item .get_local_linear_id ()* ElementsPerWorkItem + i ; 53return adjust_by_padding (offset ); 54 } 55 }; 56 57struct striped_offset { 58template < typename Item > size_t operator()(Item item ,size_t i ) { 59size_t offset = i * item .get_group ().get_local_linear_range ()+ 60item .get_local_linear_id (); 61return adjust_by_padding (offset ); 62 } 63 }; 64 65struct sub_group_striped_offset { 66template < typename Item > size_t operator()(Item item ,size_t i ) { 67auto sg = item .get_sub_group (); 68size_t wg_size = item .get_group ().get_local_linear_range (); 69size_t sub_group_sliced_items = 70 std::min < size_t > (sg .get_local_linear_range (),wg_size ); 71size_t offset = (sg .get_group_linear_id ()* sub_group_sliced_items * 72ElementsPerWorkItem )+ 73 (i * sub_group_sliced_items )+ sg .get_local_linear_id (); 74return adjust_by_padding (offset ); 75 } 76 }; 77 78template < typename Iterator > struct scatter_offset { 79Iterator begin ; 80scatter_offset (const int (& ranks )[ElementsPerWorkItem ]) { 81begin = std::begin (ranks ); 82 } 83template < typename Item > size_t operator()(Item item ,size_t i )const { 84// iterator i is expected to be within bounds [0,VALUES_PER_THREAD) 85return adjust_by_padding (begin [i ]); 86 } 87 }; 88 89template < typename Item ,typename offsetFunctorTypeFW , 90typename offsetFunctorTypeRV > 91 [[deprecated ("Please use blocked_to_striped, " 92"striped_to_blocked, scatter_to_blocked, scatter_to_striped " 93"instead" )]]__dpct_inline__ void 94helper_exchange (Item item ,T (& keys )[ElementsPerWorkItem ], 95offsetFunctorTypeFW & offset_functor_fw , 96offsetFunctorTypeRV & offset_functor_rv ) { 97helper_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. 115template < typename Item > 116__dpct_inline__ void blocked_to_striped (Item item , 117T (& input )[ElementsPerWorkItem ]) { 118striped_offset get_striped_offset ; 119blocked_offset get_blocked_offset ; 120helper_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. 138template < typename Item > 139__dpct_inline__ void striped_to_blocked (Item item , 140T (& input )[ElementsPerWorkItem ]) { 141blocked_offset get_blocked_offset ; 142striped_offset get_striped_offset ; 143helper_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. 162template < typename Item > 163__dpct_inline__ void blocked_to_striped (Item item , 164T (& input )[ElementsPerWorkItem ], 165T (& output )[ElementsPerWorkItem ]) { 166striped_offset get_striped_offset ; 167blocked_offset get_blocked_offset ; 168helper_exchange (item ,input ,output ,get_blocked_offset , 169get_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. 188template < typename Item > 189__dpct_inline__ void striped_to_blocked (Item item , 190T (& input )[ElementsPerWorkItem ], 191T (& output )[ElementsPerWorkItem ]) { 192blocked_offset get_blocked_offset ; 193striped_offset get_striped_offset ; 194helper_exchange (item ,input ,output ,get_striped_offset , 195get_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. 218template < typename Item > 219__dpct_inline__ void scatter_to_blocked (Item item , 220T (& input )[ElementsPerWorkItem ], 221int (& ranks )[ElementsPerWorkItem ]) { 222scatter_offset < const int *> get_scatter_offset (ranks ); 223blocked_offset get_blocked_offset ; 224helper_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. 247template < typename Item > 248__dpct_inline__ void scatter_to_striped (Item item , 249T (& input )[ElementsPerWorkItem ], 250int (& ranks )[ElementsPerWorkItem ]) { 251scatter_offset < const int *> get_scatter_offset (ranks ); 252striped_offset get_striped_offset ; 253helper_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. 273template < typename Item > 274__dpct_inline__ void 275blocked_to_sub_group_striped (Item item ,T (& input )[ElementsPerWorkItem ], 276T (& output )[ElementsPerWorkItem ]) { 277blocked_offset get_blocked_offset ; 278sub_group_striped_offset get_sub_group_striped_offset ; 279helper_exchange (item ,input ,output ,get_blocked_offset , 280get_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. 300template < typename Item > 301__dpct_inline__ void 302sub_group_striped_to_blocked (Item item ,T (& input )[ElementsPerWorkItem ], 303T (& output )[ElementsPerWorkItem ]) { 304blocked_offset get_blocked_offset ; 305sub_group_striped_offset get_sub_group_striped_offset ; 306helper_exchange (item ,input ,output ,get_sub_group_striped_offset , 307get_blocked_offset ); 308 } 309 310private : 311template < typename Item ,typename offsetFunctorTypeFW , 312typename offsetFunctorTypeRV > 313__dpct_inline__ void helper_exchange (Item item , 314T (& input )[ElementsPerWorkItem ], 315T (& output )[ElementsPerWorkItem ], 316offsetFunctorTypeFW & offset_functor_fw , 317offsetFunctorTypeRV & offset_functor_rv ) { 318T * buffer = reinterpret_cast < T *> (_local_memory ); 319#pragma unroll 320for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) { 321size_t offset = offset_functor_fw (item ,i ); 322buffer [offset ]= input [i ]; 323 } 324 sycl::group_barrier (item .get_group ()); 325#pragma unroll 326for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) { 327size_t offset = offset_functor_rv (item ,i ); 328output [i ]= buffer [offset ]; 329 } 330 } 331 332static constexpr int LOG_LOCAL_MEMORY_BANKS = 4 ; 333static constexpr bool INSERT_PADDING = 334 (ElementsPerWorkItem > 4 )&& 335 (detail::power_of_two < ElementsPerWorkItem > ::VALUE ); 336 337uint8_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 { 349uint8_t * _local_memory ; 350 351public : 352group_radix_sort (uint8_t * local_memory ) :_local_memory (local_memory ) {} 353 354static size_t get_local_memory_size (size_t group_threads ) { 355size_t ranks_size = 356 detail::radix_rank < RADIX_BITS > ::get_local_memory_size (group_threads ); 357size_t exchange_size = 358exchange < T ,ElementsPerWorkItem > ::get_local_memory_size (group_threads ); 359return sycl::max (ranks_size ,exchange_size ); 360 } 361 362private : 363template < typename Item ,bool DESCENDING > 364__dpct_inline__ void 365helper_sort (const Item & item ,T (& keys )[ElementsPerWorkItem ], 366int begin_bit = 0 ,int end_bit = 8 * sizeof (T ), 367bool is_striped = false) { 368using UnsignedT = 369typename std::conditional < std::is_same < T , sycl::half > ::value ,uint16_t , 370uint32_t > ::type ; 371UnsignedT (& unsigned_keys )[ElementsPerWorkItem ]= 372reinterpret_cast < UnsignedT (& )[ElementsPerWorkItem ]> (keys ); 373 374#pragma unroll 375for (int i = 0 ;i < ElementsPerWorkItem ;++ i ) { 376unsigned_keys [i ]= detail::traits < T > ::twiddle_in (unsigned_keys [i ]); 377 } 378 379for (int i = begin_bit ;i < end_bit ;i += RADIX_BITS ) { 380int pass_bits = sycl::min (RADIX_BITS ,end_bit - begin_bit ); 381 382int ranks [ElementsPerWorkItem ]; 383 detail::radix_rank < RADIX_BITS ,DESCENDING > (_local_memory ) 384 .template rank_keys < Item ,UnsignedT ,ElementsPerWorkItem > ( 385item ,unsigned_keys ,ranks ,i ,pass_bits ); 386 387 sycl::group_barrier (item .get_group ()); 388 389bool last_iter = i + RADIX_BITS >=end_bit ; 390if (last_iter && is_striped ) { 391exchange < T ,ElementsPerWorkItem > (_local_memory ) 392 .scatter_to_striped (item ,keys ,ranks ); 393 394 }else { 395exchange < 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 403for (int i = 0 ;i < ElementsPerWorkItem ;++ i ) { 404unsigned_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. 429template < typename Item > 430__dpct_inline__ void sort (const Item & item ,T (& input )[ElementsPerWorkItem ], 431int begin_bit = 0 ,int end_bit = 8 * sizeof (T )) { 432helper_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. 455template < typename Item > 456__dpct_inline__ void 457sort_descending (const Item & item ,T (& input )[ElementsPerWorkItem ], 458int begin_bit = 0 ,int end_bit = 8 * sizeof (T )) { 459helper_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. 483template < typename Item > 484__dpct_inline__ void 485sort_blocked_to_striped (const Item & item ,T (& input )[ElementsPerWorkItem ], 486int begin_bit = 0 ,int end_bit = 8 * sizeof (T )) { 487helper_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. 512template < typename Item > 513__dpct_inline__ void sort_descending_blocked_to_striped ( 514const Item & item ,T (& input )[ElementsPerWorkItem ],int begin_bit = 0 , 515int end_bit = 8 * sizeof (T )) { 516helper_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 { 531using SorterT = group_radix_sort < T ,ElementsPerWorkItem > ; 532SorterT _sorter ; 533 534public : 535static size_t get_local_memory_size (size_t group_threads ) { 536return SorterT ::get_local_memory_size (group_threads ); 537 } 538 539radix_sort (uint8_t * local_memory ) :_sorter (local_memory ) {} 540 541template < typename Item > 542__dpct_inline__ void helper_sort ( 543const Item & item ,T (& keys )[ElementsPerWorkItem ],int begin_bit = 0 , 544int end_bit = 8 * sizeof (T ),bool is_striped = false) { 545if constexpr (DESCENDING ) { 546if (is_striped ) 547_sorter .sort_descending_blocked_to_striped (item ,keys ,begin_bit , 548end_bit ); 549else 550_sorter .sort_descending (item ,keys ,begin_bit ,end_bit ); 551 }else { 552if (is_striped ) 553_sorter .sort_blocked_to_striped (item ,keys ,begin_bit ,end_bit ); 554else 555_sorter .sort (item ,keys ,begin_bit ,end_bit ); 556 } 557 } 558 559template < typename Item > 560__dpct_inline__ void sort_blocked ( 561const Item & item ,T (& keys )[ElementsPerWorkItem ],int begin_bit = 0 , 562int end_bit = 8 * sizeof (T )) { 563helper_sort (item ,keys ,begin_bit ,end_bit , false); 564 } 565 566template < typename Item > 567__dpct_inline__ void sort_blocked_to_striped ( 568const Item & item ,T (& keys )[ElementsPerWorkItem ],int begin_bit = 0 , 569int end_bit = 8 * sizeof (T )) { 570helper_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 { 577BLOCK_LOAD_DIRECT , 578BLOCK_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 , 583typename Item > 584[[deprecated ("Please use load_direct_blocked instead" )]] 585__dpct_inline__ void load_blocked (const Item & item ,InputIteratorT block_itr , 586InputT (& 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 591size_t linear_tid = item .get_local_linear_id (); 592uint32_t workgroup_offset = linear_tid * ITEMS_PER_WORK_ITEM ; 593#pragma unroll 594for (size_t idx = 0 ;idx < ITEMS_PER_WORK_ITEM ;idx ++ ) { 595items [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 , 601typename Item > 602[[deprecated ("Please use load_direct_striped instead" )]] 603__dpct_inline__ void load_striped (const Item & item ,InputIteratorT block_itr , 604InputT (& 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 609size_t linear_tid = item .get_local_linear_id (); 610size_t group_work_items = item .get_local_range ().size (); 611#pragma unroll 612for (size_t idx = 0 ;idx < ITEMS_PER_WORK_ITEM ;idx ++ ) { 613items [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 , 629typename ItemT > 630__dpct_inline__ void load_direct_blocked (const ItemT & item , 631InputIteratorT input_iter , 632T (& data )[ElementsPerWorkItem ]) { 633size_t work_item_id = item .get_local_linear_id (); 634#pragma unroll 635for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) 636data [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 , 651typename ItemT > 652__dpct_inline__ void load_direct_striped (const ItemT & item , 653InputIteratorT input_iter , 654T (& data )[ElementsPerWorkItem ]) { 655size_t work_group_size = item .get_group ().get_local_linear_range (); 656size_t work_item_id = item .get_local_linear_id (); 657#pragma unroll 658for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) 659data [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 , 674typename ItemT > 675__dpct_inline__ void 676load_direct_sub_group_striped (const ItemT & item ,InputIteratorT input_iter , 677T (& data )[ElementsPerWorkItem ]) { 678auto sg = item .get_sub_group (); 679size_t wg_size = item .get_group ().get_local_linear_range (); 680size_t sub_group_sliced_items = 681 std::min < size_t > (sg .get_local_linear_range (),wg_size ); 682 683#pragma unroll 684for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) { 685size_t offset = (sg .get_group_linear_id ()* sub_group_sliced_items * 686ElementsPerWorkItem )+ 687 (i * sub_group_sliced_items )+ sg .get_local_linear_id (); 688data [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 , 705typename ItemT > 706__dpct_inline__ void 707load_direct_blocked (const ItemT & item ,InputIteratorT input_iter , 708T (& data )[ElementsPerWorkItem ],int valid_items ) { 709size_t work_item_id = item .get_local_linear_id (); 710#pragma unroll 711for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) 712if ((work_item_id * ElementsPerWorkItem )+ i < valid_items ) 713data [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 , 729typename ItemT > 730__dpct_inline__ void 731load_direct_striped (const ItemT & item ,InputIteratorT input_iter , 732T (& data )[ElementsPerWorkItem ],int valid_items ) { 733size_t work_group_size = item .get_group ().get_local_linear_range (); 734size_t work_item_id = item .get_local_linear_id (); 735#pragma unroll 736for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) 737if (work_item_id + (i * work_group_size )< valid_items ) 738data [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 , 754typename ItemT > 755__dpct_inline__ void 756load_direct_sub_group_striped (const ItemT & item ,InputIteratorT input_iter , 757T (& data )[ElementsPerWorkItem ],int valid_items ) { 758auto sg = item .get_sub_group (); 759size_t wg_size = item .get_group ().get_local_linear_range (); 760size_t sub_group_sliced_items = 761 std::min < size_t > (sg .get_local_linear_range (),wg_size ); 762 763#pragma unroll 764for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) { 765size_t offset = (sg .get_group_linear_id ()* sub_group_sliced_items * 766ElementsPerWorkItem )+ 767 (i * sub_group_sliced_items )+ sg .get_local_linear_id (); 768if (offset < valid_items ) { 769data [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 , 789typename InputIteratorT ,typename ItemT > 790__dpct_inline__ void 791load_direct_blocked (const ItemT & item ,InputIteratorT input_iter , 792T (& data )[ElementsPerWorkItem ],int valid_items , 793DefaultT default_value ) { 794#pragma unroll 795for (int i = 0 ;i < ElementsPerWorkItem ;i ++ ) { 796data [i ]= default_value ; 797 } 798load_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 , 816typename InputIteratorT ,typename ItemT > 817__dpct_inline__ void 818load_direct_striped (const ItemT & item ,InputIteratorT input_iter , 819T (& data )[ElementsPerWorkItem ],int valid_items , 820DefaultT default_value ) { 821#pragma unroll 822for (int i = 0 ;i < ElementsPerWorkItem ;i ++ ) { 823data [i ]= default_value ; 824 } 825load_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 , 843typename InputIteratorT ,typename ItemT > 844__dpct_inline__ void 845load_direct_sub_group_striped (const ItemT & item ,InputIteratorT input_iter , 846T (& data )[ElementsPerWorkItem ],int valid_items , 847DefaultT default_value ) { 848#pragma unroll 849for (int i = 0 ;i < ElementsPerWorkItem ;i ++ ) { 850data [i ]= default_value ; 851 } 852load_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 , 868typename ItemT > 869__dpct_inline__ void store_direct_blocked (const ItemT & item , 870OutputIteratorT output_iter , 871T (& data )[ElementsPerWorkItem ]) { 872size_t work_item_id = item .get_local_linear_id (); 873OutputIteratorT work_item_iter = 874output_iter + (work_item_id * ElementsPerWorkItem ); 875#pragma unroll 876for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) 877work_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 , 893typename ItemT > 894__dpct_inline__ void store_direct_striped (const ItemT & item , 895OutputIteratorT output_iter , 896T (& data )[ElementsPerWorkItem ]) { 897size_t work_group_size = item .get_group ().get_local_linear_range (); 898size_t work_item_id = item .get_local_linear_id (); 899OutputIteratorT work_item_iter = output_iter + work_item_id ; 900#pragma unroll 901for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) 902work_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 , 918typename ItemT > 919__dpct_inline__ void 920store_direct_sub_group_striped (const ItemT & item ,OutputIteratorT output_iter , 921T (& data )[ElementsPerWorkItem ]) { 922auto sg = item .get_sub_group (); 923size_t wg_size = item .get_group ().get_local_linear_range (); 924size_t sub_group_sliced_items = 925 std::min < size_t > (sg .get_local_linear_range (),wg_size ); 926 927OutputIteratorT work_item_iter = 928output_iter + 929sg .get_group_linear_id ()* sub_group_sliced_items * ElementsPerWorkItem + 930sg .get_local_linear_id (); 931 932#pragma unroll 933for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) { 934work_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 , 952typename ItemT > 953__dpct_inline__ void 954store_direct_blocked (const ItemT & item ,OutputIteratorT output_iter , 955T (& data )[ElementsPerWorkItem ],size_t valid_items ) { 956size_t work_item_id = item .get_local_linear_id (); 957OutputIteratorT work_item_iter = 958output_iter + (work_item_id * ElementsPerWorkItem ); 959#pragma unroll 960for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) 961if (i + (work_item_id * ElementsPerWorkItem )< valid_items ) 962work_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 , 979typename ItemT > 980__dpct_inline__ void 981store_direct_striped (const ItemT & item ,OutputIteratorT output_iter , 982T (& data )[ElementsPerWorkItem ],size_t valid_items ) { 983size_t work_group_size = item .get_group ().get_local_linear_range (); 984size_t work_item_id = item .get_local_linear_id (); 985OutputIteratorT work_item_iter = output_iter + work_item_id ; 986#pragma unroll 987for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) 988if ((i * work_group_size )+ work_item_id < valid_items ) 989work_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 , 1006typename ItemT > 1007__dpct_inline__ void 1008store_direct_sub_group_striped (const ItemT & item ,OutputIteratorT output_iter , 1009T (& data )[ElementsPerWorkItem ], 1010size_t valid_items ) { 1011auto sg = item .get_sub_group (); 1012size_t wg_size = item .get_group ().get_local_linear_range (); 1013size_t sub_group_sliced_items = 1014 std::min < size_t > (sg .get_local_linear_range (),wg_size ); 1015size_t sub_group_offset = 1016sg .get_group_linear_id ()* sub_group_sliced_items * ElementsPerWorkItem ; 1017OutputIteratorT work_item_iter = 1018output_iter + sub_group_offset + sg .get_local_linear_id (); 1019 1020#pragma unroll 1021for (size_t i = 0 ;i < ElementsPerWorkItem ;i ++ ) { 1022if (sub_group_offset + sg .get_local_linear_id ()+ 1023i * sub_group_sliced_items < 1024valid_items ) { 1025work_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 , 1035typename Item > 1036__dpct_inline__ void 1037uninitialized_load_subgroup_striped (const Item & item ,InputIteratorT block_itr , 1038InputT (& 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. 1045uint32_t subgroup_offset = item .get_sub_group ().get_local_linear_id (); 1046uint32_t subgroup_size = item .get_sub_group ().get_local_linear_range (); 1047uint32_t subgroup_idx = item .get_sub_group ().get_group_linear_id (); 1048uint32_t initial_offset = 1049 (subgroup_idx * ITEMS_PER_WORK_ITEM * subgroup_size )+ subgroup_offset ; 1050#pragma unroll 1051for (size_t idx = 0 ;idx < ITEMS_PER_WORK_ITEM ;idx ++ ) { 1052new (& 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 , 1063typename InputIteratorT ,typename Item > 1064class [[deprecated ( 1065"Please use dpct::group::group_load instead" )]]workgroup_load { 1066public : 1067static size_t get_local_memory_size (size_t group_work_items ) {return 0 ; } 1068workgroup_load (uint8_t * local_memory ) :_local_memory (local_memory ) {} 1069 1070__dpct_inline__ void load (const Item & item ,InputIteratorT block_itr , 1071InputT (& items )[ITEMS_PER_WORK_ITEM ]) { 1072 1073if constexpr (ALGORITHM == BLOCK_LOAD_DIRECT ) { 1074load_blocked < ITEMS_PER_WORK_ITEM > (item ,block_itr ,items ); 1075 }else if constexpr (ALGORITHM == BLOCK_LOAD_STRIPED ) { 1076load_striped < ITEMS_PER_WORK_ITEM > (item ,block_itr ,items ); 1077 } 1078 } 1079 1080private : 1081uint8_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. 1089blocked , 1090 1091/// A striped arrangement of data is read directly from memory. 1092striped , 1093 1094/// A striped arrangement of data is read directly from memory and transposed 1095/// into a blocked arrangement. 1096transpose , 1097 1098/// A sub_group striped arrangement of data is read directly from memory and 1099/// transposed into a blocked arrangement. 1100sub_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 , 1111group_load_algorithm LoadAlgorithm = group_load_algorithm::blocked > 1112class group_load { 1113public : 1114static size_t get_local_memory_size (size_t work_group_size ) { 1115if constexpr ((LoadAlgorithm == group_load_algorithm::transpose )|| 1116 (LoadAlgorithm == 1117 group_load_algorithm::sub_group_transpose )) { 1118return dpct::group::exchange < 1119T ,ElementsPerWorkItem > ::get_local_memory_size (work_group_size ); 1120 } 1121return 0 ; 1122 } 1123group_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. 1147template < typename ItemT ,typename InputIteratorT > 1148__dpct_inline__ void load (const ItemT & item ,InputIteratorT input_iter , 1149T (& data )[ElementsPerWorkItem ]) { 1150if constexpr (LoadAlgorithm == group_load_algorithm::blocked ) { 1151load_direct_blocked < T ,ElementsPerWorkItem ,InputIteratorT ,ItemT > ( 1152item ,input_iter ,data ); 1153 }else if constexpr (LoadAlgorithm == group_load_algorithm::striped ) { 1154load_direct_striped < T ,ElementsPerWorkItem ,InputIteratorT ,ItemT > ( 1155item ,input_iter ,data ); 1156 }else if constexpr (LoadAlgorithm == group_load_algorithm::transpose ) { 1157load_direct_striped < T ,ElementsPerWorkItem ,InputIteratorT ,ItemT > ( 1158item ,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 ) { 1163load_direct_sub_group_striped < T ,ElementsPerWorkItem ,InputIteratorT , 1164ItemT > (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 1193template < typename ItemT ,typename InputIteratorT > 1194__dpct_inline__ void load (const ItemT & item ,InputIteratorT input_iter , 1195T (& data )[ElementsPerWorkItem ],int valid_items ) { 1196if constexpr (LoadAlgorithm == group_load_algorithm::blocked ) { 1197load_direct_blocked < T ,ElementsPerWorkItem ,InputIteratorT ,ItemT > ( 1198item ,input_iter ,data ,valid_items ); 1199 }else if constexpr (LoadAlgorithm == group_load_algorithm::striped ) { 1200load_direct_striped < T ,ElementsPerWorkItem ,InputIteratorT ,ItemT > ( 1201item ,input_iter ,data ,valid_items ); 1202 }else if constexpr (LoadAlgorithm == group_load_algorithm::transpose ) { 1203load_direct_striped < T ,ElementsPerWorkItem ,InputIteratorT ,ItemT > ( 1204item ,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 ) { 1209load_direct_sub_group_striped < T ,ElementsPerWorkItem ,InputIteratorT , 1210ItemT > (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. 1241template < typename ItemT ,typename InputIteratorT ,typename DefaultT > 1242__dpct_inline__ void load (const ItemT & item ,InputIteratorT input_iter , 1243T (& data )[ElementsPerWorkItem ],int valid_items , 1244DefaultT default_value ) { 1245if constexpr (LoadAlgorithm == group_load_algorithm::blocked ) { 1246load_direct_blocked < T ,DefaultT ,ElementsPerWorkItem ,InputIteratorT , 1247ItemT > (item ,input_iter ,data ,valid_items , 1248default_value ); 1249 }else if constexpr (LoadAlgorithm == group_load_algorithm::striped ) { 1250load_direct_striped < T ,DefaultT ,ElementsPerWorkItem ,InputIteratorT , 1251ItemT > (item ,input_iter ,data ,valid_items , 1252default_value ); 1253 }else if constexpr (LoadAlgorithm == group_load_algorithm::transpose ) { 1254load_direct_striped < T ,DefaultT ,ElementsPerWorkItem ,InputIteratorT , 1255ItemT > (item ,input_iter ,data ,valid_items , 1256default_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 ) { 1261load_direct_sub_group_striped < T ,DefaultT ,ElementsPerWorkItem , 1262InputIteratorT ,ItemT > ( 1263item ,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 : 1270uint8_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. 1278blocked , 1279 1280/// A striped arrangement of data is written directly to memory. 1281striped , 1282 1283/// A blocked arrangement of data transposed into a striped arrangement and 1284/// then written directly to memory. 1285transpose , 1286 1287/// A blocked arrangement of data transposed into a sub_group striped 1288/// arrangement and then written directly to memory. 1289sub_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 , 1300group_store_algorithm StoreAlgorithm = group_store_algorithm::blocked > 1301class group_store { 1302public : 1303static size_t get_local_memory_size (size_t work_group_size ) { 1304if constexpr ((StoreAlgorithm == group_store_algorithm::transpose )|| 1305 (StoreAlgorithm == 1306 group_store_algorithm::sub_group_transpose )) { 1307return dpct::group::exchange < 1308T ,ElementsPerWorkItem > ::get_local_memory_size (work_group_size ); 1309 } 1310return 0 ; 1311 } 1312group_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. 1336template < typename ItemT ,typename OutputIteratorT > 1337__dpct_inline__ void store (const ItemT & item ,OutputIteratorT output_iter , 1338T (& data )[ElementsPerWorkItem ]) { 1339if constexpr (StoreAlgorithm == group_store_algorithm::blocked ) { 1340store_direct_blocked < T ,ElementsPerWorkItem ,OutputIteratorT ,ItemT > ( 1341item ,output_iter ,data ); 1342 }else if constexpr (StoreAlgorithm == group_store_algorithm::striped ) { 1343store_direct_striped < T ,ElementsPerWorkItem ,OutputIteratorT ,ItemT > ( 1344item ,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 ); 1348store_direct_striped < T ,ElementsPerWorkItem ,OutputIteratorT ,ItemT > ( 1349item ,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 ); 1354store_direct_sub_group_striped < T ,ElementsPerWorkItem ,OutputIteratorT , 1355ItemT > (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 1382template < typename ItemT ,typename OutputIteratorT > 1383__dpct_inline__ void store (const ItemT & item ,OutputIteratorT output_iter , 1384T (& data )[ElementsPerWorkItem ], 1385size_t valid_items ) { 1386if constexpr (StoreAlgorithm == group_store_algorithm::blocked ) { 1387store_direct_blocked < T ,ElementsPerWorkItem ,OutputIteratorT ,ItemT > ( 1388item ,output_iter ,data ,valid_items ); 1389 }else if constexpr (StoreAlgorithm == group_store_algorithm::striped ) { 1390store_direct_striped < T ,ElementsPerWorkItem ,OutputIteratorT ,ItemT > ( 1391item ,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 ); 1395store_direct_striped < T ,ElementsPerWorkItem ,OutputIteratorT ,ItemT > ( 1396item ,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 ); 1401store_direct_sub_group_striped < T ,ElementsPerWorkItem ,OutputIteratorT , 1402ItemT > (item ,output_iter ,data , 1403valid_items ); 1404 } 1405 } 1406 1407private : 1408uint8_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 { 1420T * _local_memory = nullptr ; 1421static constexpr size_t group_work_items = 1422group_dim_0 * group_dim_1 * group_dim_2 ; 1423 1424public : 1425static constexpr size_t get_local_memory_size (size_t work_group_size ) { 1426return sizeof (T )* work_group_size ; 1427 } 1428group_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. 1439template < typename ItemT > 1440__dpct_inline__ void select (const ItemT & item ,T input ,T & output , 1441int distance = 1 ) { 1442auto g = item .get_group (); 1443size_t id = g .get_local_linear_id (); 1444_local_memory [id ]= input ; 1445 1446 sycl::group_barrier (g , sycl::memory_scope::work_group ); 1447 1448const int target_id = static_cast < int > (id )+ distance ; 1449if ((target_id >=0 )&& (target_id < group_work_items )) { 1450output = _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. 1463template < typename ItemT > 1464__dpct_inline__ void select2 (const ItemT & item ,T input ,T & output , 1465unsigned int distance = 1 ) { 1466auto g = item .get_group (); 1467size_t id = g .get_local_linear_id (); 1468_local_memory [id ]= input ; 1469 1470 sycl::group_barrier (g , sycl::memory_scope::work_group ); 1471 1472unsigned int offset = id + distance ; 1473if (offset >=group_work_items ) 1474offset -= group_work_items ; 1475 1476output = _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. 1487template < int ElementsPerWorkItem ,typename ItemT > 1488__dpct_inline__ void shuffle_right (const ItemT & item , 1489T (& input )[ElementsPerWorkItem ], 1490T (& output )[ElementsPerWorkItem ]) { 1491auto g = item .get_group (); 1492size_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 1498for (int index = ElementsPerWorkItem - 1 ;index > 0 ;-- index ) 1499output [index ]= input [index - 1 ]; 1500 1501if (id > 0 ) 1502output [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. 1513template < int ElementsPerWorkItem ,typename ItemT > 1514__dpct_inline__ void 1515shuffle_right (const ItemT & item ,T (& input )[ElementsPerWorkItem ], 1516T (& output )[ElementsPerWorkItem ],T & group_suffix ) { 1517shuffle_right (item ,input ,output ); 1518group_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. 1529template < int ElementsPerWorkItem ,typename ItemT > 1530__dpct_inline__ void shuffle_left (const ItemT & item , 1531T (& input )[ElementsPerWorkItem ], 1532T (& output )[ElementsPerWorkItem ]) { 1533auto g = item .get_group (); 1534size_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 1540for (int index = 0 ;index < ElementsPerWorkItem - 1 ;index ++ ) 1541output [index ]= input [index + 1 ]; 1542 1543if (id < group_work_items - 1 ) 1544output [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. 1555template < int ElementsPerWorkItem ,typename ItemT > 1556__dpct_inline__ void 1557shuffle_left (const ItemT & item ,T (& input )[ElementsPerWorkItem ], 1558T (& output )[ElementsPerWorkItem ],T & group_prefix ) { 1559shuffle_left (item ,input ,output ); 1560group_prefix = _local_memory [0 ]; 1561 } 1562}; 1563}// namespace group 1564}// namespace dpct 1565 1566#endif // __DPCT_GROUP_UTILS_HPP__