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