okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- atomic.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_ATOMIC_HPP__ 10#define __DPCT_ATOMIC_HPP__ 11 12#include <sycl/sycl.hpp> 13 14namespace dpct { 15 16/// Atomically add the value operand to the value at the addr and assign the 17/// result to the value at addr. 18/// \param [in, out] addr The pointer to the data. 19/// \param operand The value to add to the value at \p addr. 20/// \param memoryOrder The memory ordering used. 21/// \returns The value at the \p addr before the call. 22template < typename T , 23 sycl::access::address_space addressSpace = 24 sycl::access::address_space::global_space , 25 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 26 sycl::memory_scope memoryScope = sycl::memory_scope::device > 27inline T atomic_fetch_add (T * addr ,T operand ) { 28auto atm = 29 sycl::atomic_ref < T ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 30return atm .fetch_add (operand ); 31} 32 33template < sycl::access::address_space addressSpace = 34 sycl::access::address_space::global_space , 35 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 36 sycl::memory_scope memoryScope = sycl::memory_scope::device , 37typename T1 ,typename T2 > 38inline T1 atomic_fetch_add (T1 * addr ,T2 operand ) { 39auto atm = 40 sycl::atomic_ref < T1 ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 41return atm .fetch_add (operand ); 42} 43 44template < sycl::access::address_space addressSpace = 45 sycl::access::address_space::global_space , 46 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 47 sycl::memory_scope memoryScope = sycl::memory_scope::device > 48inline sycl::half2 atomic_fetch_add (sycl::half2 * addr , sycl::half2 operand ) { 49auto atm = sycl::atomic_ref < unsigned ,memoryOrder ,memoryScope ,addressSpace > ( 50* reinterpret_cast < unsigned *> (addr )); 51 52union { 53unsigned i ; 54 sycl::half2 h ; 55 }old {0 },output {0 }; 56 57while (true) { 58old .i = atm .load (); 59output .h = old .h + operand ; 60if (atm .compare_exchange_strong (old .i ,output .i )) 61break ; 62 } 63 64return output .h ; 65} 66 67/// Atomically add the value operand to the value at the addr and assign the 68/// result to the value at addr. 69/// \param [in, out] addr The pointer to the data. 70/// \param operand The value to add to the value at \p addr. 71/// \param memoryOrder The memory ordering used. 72/// \returns The value at the \p addr before the call. 73template < typename T , sycl::access::address_space addressSpace = 74 sycl::access::address_space::global_space > 75inline T atomic_fetch_add (T * addr ,T operand , 76 sycl::memory_order memoryOrder ) { 77switch (memoryOrder ) { 78case sycl::memory_order::relaxed : 79return atomic_fetch_add < T ,addressSpace , sycl::memory_order::relaxed , 80 sycl::memory_scope::device > (addr ,operand ); 81case sycl::memory_order::acq_rel : 82return atomic_fetch_add < T ,addressSpace , sycl::memory_order::acq_rel , 83 sycl::memory_scope::device > (addr ,operand ); 84case sycl::memory_order::seq_cst : 85return atomic_fetch_add < T ,addressSpace , sycl::memory_order::seq_cst , 86 sycl::memory_scope::device > (addr ,operand ); 87default : 88assert (false&& "Invalid memory_order for atomics. Valid memory_order for " 89"atomics are: sycl::memory_order::relaxed, " 90"sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!" ); 91 } 92} 93 94template < sycl::access::address_space addressSpace = 95 sycl::access::address_space::global_space , 96typename T1 ,typename T2 > 97inline T1 atomic_fetch_add (T1 * addr ,T2 operand , 98 sycl::memory_order memoryOrder ) { 99atomic_fetch_add < T1 ,addressSpace > (addr ,operand ,memoryOrder ); 100} 101 102/// Atomically subtract the value operand from the value at the addr and assign 103/// the result to the value at addr. 104/// \param [in, out] addr The pointer to the data. 105/// \param operand The value to subtract from the value at \p addr 106/// \param memoryOrder The memory ordering used. 107/// \returns The value at the \p addr before the call. 108template < typename T , 109 sycl::access::address_space addressSpace = 110 sycl::access::address_space::global_space , 111 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 112 sycl::memory_scope memoryScope = sycl::memory_scope::device > 113inline T atomic_fetch_sub (T * addr ,T operand ) { 114auto atm = 115 sycl::atomic_ref < T ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 116return atm .fetch_sub (operand ); 117} 118 119template < sycl::access::address_space addressSpace = 120 sycl::access::address_space::global_space , 121 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 122 sycl::memory_scope memoryScope = sycl::memory_scope::device , 123typename T1 ,typename T2 > 124inline T1 atomic_fetch_sub (T1 * addr ,T2 operand ) { 125auto atm = 126 sycl::atomic_ref < T1 ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 127return atm .fetch_sub (operand ); 128} 129 130/// Atomically subtract the value operand from the value at the addr and assign 131/// the result to the value at addr. 132/// \param [in, out] addr The pointer to the data. 133/// \param operand The value to subtract from the value at \p addr 134/// \param memoryOrder The memory ordering used. 135/// \returns The value at the \p addr before the call. 136template < typename T , sycl::access::address_space addressSpace = 137 sycl::access::address_space::global_space > 138inline T atomic_fetch_sub (T * addr ,T operand , 139 sycl::memory_order memoryOrder ) { 140switch (memoryOrder ) { 141case sycl::memory_order::relaxed : 142return atomic_fetch_sub < T ,addressSpace , sycl::memory_order::relaxed , 143 sycl::memory_scope::device > (addr ,operand ); 144case sycl::memory_order::acq_rel : 145return atomic_fetch_sub < T ,addressSpace , sycl::memory_order::acq_rel , 146 sycl::memory_scope::device > (addr ,operand ); 147case sycl::memory_order::seq_cst : 148return atomic_fetch_sub < T ,addressSpace , sycl::memory_order::seq_cst , 149 sycl::memory_scope::device > (addr ,operand ); 150default : 151assert (false&& "Invalid memory_order for atomics. Valid memory_order for " 152"atomics are: sycl::memory_order::relaxed, " 153"sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!" ); 154 } 155} 156 157template < sycl::access::address_space addressSpace = 158 sycl::access::address_space::global_space , 159typename T1 ,typename T2 > 160inline T1 atomic_fetch_sub (T1 * addr ,T2 operand , 161 sycl::memory_order memoryOrder ) { 162atomic_fetch_sub < T1 ,addressSpace > (addr ,operand ,memoryOrder ); 163} 164 165/// Atomically perform a bitwise AND between the value operand and the value at the addr 166/// and assign the result to the value at addr. 167/// \param [in, out] addr The pointer to the data. 168/// \param operand The value to use in bitwise AND operation with the value at the \p addr. 169/// \param memoryOrder The memory ordering used. 170/// \returns The value at the \p addr before the call. 171template < typename T , 172 sycl::access::address_space addressSpace = 173 sycl::access::address_space::global_space , 174 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 175 sycl::memory_scope memoryScope = sycl::memory_scope::device > 176inline T atomic_fetch_and (T * addr ,T operand ) { 177auto atm = 178 sycl::atomic_ref < T ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 179return atm .fetch_and (operand ); 180} 181 182template < sycl::access::address_space addressSpace = 183 sycl::access::address_space::global_space , 184 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 185 sycl::memory_scope memoryScope = sycl::memory_scope::device , 186typename T1 ,typename T2 > 187inline T1 atomic_fetch_and (T1 * addr ,T2 operand ) { 188auto atm = 189 sycl::atomic_ref < T1 ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 190return atm .fetch_and (operand ); 191} 192 193/// Atomically perform a bitwise AND between the value operand and the value at the addr 194/// and assign the result to the value at addr. 195/// \param [in, out] addr The pointer to the data. 196/// \param operand The value to use in bitwise AND operation with the value at the \p addr. 197/// \param memoryOrder The memory ordering used. 198/// \returns The value at the \p addr before the call. 199template < typename T , sycl::access::address_space addressSpace = 200 sycl::access::address_space::global_space > 201inline T atomic_fetch_and (T * addr ,T operand , 202 sycl::memory_order memoryOrder ) { 203switch (memoryOrder ) { 204case sycl::memory_order::relaxed : 205return atomic_fetch_and < T ,addressSpace , sycl::memory_order::relaxed , 206 sycl::memory_scope::device > (addr ,operand ); 207case sycl::memory_order::acq_rel : 208return atomic_fetch_and < T ,addressSpace , sycl::memory_order::acq_rel , 209 sycl::memory_scope::device > (addr ,operand ); 210case sycl::memory_order::seq_cst : 211return atomic_fetch_and < T ,addressSpace , sycl::memory_order::seq_cst , 212 sycl::memory_scope::device > (addr ,operand ); 213default : 214assert (false&& "Invalid memory_order for atomics. Valid memory_order for " 215"atomics are: sycl::memory_order::relaxed, " 216"sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!" ); 217 } 218} 219 220template < sycl::access::address_space addressSpace = 221 sycl::access::address_space::global_space , 222typename T1 ,typename T2 > 223inline T1 atomic_fetch_and (T1 * addr ,T2 operand , 224 sycl::memory_order memoryOrder ) { 225atomic_fetch_and < T1 ,addressSpace > (addr ,operand ,memoryOrder ); 226} 227 228/// Atomically or the value at the addr with the value operand, and assign 229/// the result to the value at addr. 230/// \param [in, out] addr The pointer to the data. 231/// \param operand The value to use in bitwise OR operation with the value at the \p addr. 232/// \param memoryOrder The memory ordering used. 233/// \returns The value at the \p addr before the call. 234template < typename T , 235 sycl::access::address_space addressSpace = 236 sycl::access::address_space::global_space , 237 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 238 sycl::memory_scope memoryScope = sycl::memory_scope::device > 239inline T atomic_fetch_or (T * addr ,T operand ) { 240auto atm = 241 sycl::atomic_ref < T ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 242return atm .fetch_or (operand ); 243} 244 245template < sycl::access::address_space addressSpace = 246 sycl::access::address_space::global_space , 247 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 248 sycl::memory_scope memoryScope = sycl::memory_scope::device , 249typename T1 ,typename T2 > 250inline T1 atomic_fetch_or (T1 * addr ,T2 operand ) { 251auto atm = 252 sycl::atomic_ref < T1 ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 253return atm .fetch_or (operand ); 254} 255 256/// Atomically or the value at the addr with the value operand, and assign 257/// the result to the value at addr. 258/// \param [in, out] addr The pointer to the data. 259/// \param operand The value to use in bitwise OR operation with the value at the \p addr. 260/// \param memoryOrder The memory ordering used. 261/// \returns The value at the \p addr before the call. 262template < typename T , sycl::access::address_space addressSpace = 263 sycl::access::address_space::global_space > 264inline T atomic_fetch_or (T * addr ,T operand , 265 sycl::memory_order memoryOrder ) { 266switch (memoryOrder ) { 267case sycl::memory_order::relaxed : 268return atomic_fetch_or < T ,addressSpace , sycl::memory_order::relaxed , 269 sycl::memory_scope::device > (addr ,operand ); 270case sycl::memory_order::acq_rel : 271return atomic_fetch_or < T ,addressSpace , sycl::memory_order::acq_rel , 272 sycl::memory_scope::device > (addr ,operand ); 273case sycl::memory_order::seq_cst : 274return atomic_fetch_or < T ,addressSpace , sycl::memory_order::seq_cst , 275 sycl::memory_scope::device > (addr ,operand ); 276default : 277assert (false&& "Invalid memory_order for atomics. Valid memory_order for " 278"atomics are: sycl::memory_order::relaxed, " 279"sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!" ); 280 } 281} 282 283template < sycl::access::address_space addressSpace = 284 sycl::access::address_space::global_space , 285typename T1 ,typename T2 > 286inline T1 atomic_fetch_or (T1 * addr ,T2 operand , 287 sycl::memory_order memoryOrder ) { 288atomic_fetch_or < T1 ,addressSpace > (addr ,operand ,memoryOrder ); 289} 290 291/// Atomically xor the value at the addr with the value operand, and assign 292/// the result to the value at addr. 293/// \param [in, out] addr The pointer to the data. 294/// \param operand The value to use in bitwise XOR operation with the value at the \p addr. 295/// \param memoryOrder The memory ordering used. 296/// \returns The value at the \p addr before the call. 297template < typename T , 298 sycl::access::address_space addressSpace = 299 sycl::access::address_space::global_space , 300 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 301 sycl::memory_scope memoryScope = sycl::memory_scope::device > 302inline T atomic_fetch_xor (T * addr ,T operand ) { 303auto atm = 304 sycl::atomic_ref < T ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 305return atm .fetch_xor (operand ); 306} 307 308template < sycl::access::address_space addressSpace = 309 sycl::access::address_space::global_space , 310 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 311 sycl::memory_scope memoryScope = sycl::memory_scope::device , 312typename T1 ,typename T2 > 313inline T1 atomic_fetch_xor (T1 * addr ,T2 operand ) { 314auto atm = 315 sycl::atomic_ref < T1 ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 316return atm .fetch_xor (operand ); 317} 318 319/// Atomically xor the value at the addr with the value operand, and assign 320/// the result to the value at addr. 321/// \param [in, out] addr The pointer to the data. 322/// \param operand The value to use in bitwise XOR operation with the value at the \p addr. 323/// \param memoryOrder The memory ordering used. 324/// \returns The value at the \p addr before the call. 325template < typename T , sycl::access::address_space addressSpace = 326 sycl::access::address_space::global_space > 327inline T atomic_fetch_xor (T * addr ,T operand , 328 sycl::memory_order memoryOrder ) { 329switch (memoryOrder ) { 330case sycl::memory_order::relaxed : 331return atomic_fetch_xor < T ,addressSpace , sycl::memory_order::relaxed , 332 sycl::memory_scope::device > (addr ,operand ); 333case sycl::memory_order::acq_rel : 334return atomic_fetch_xor < T ,addressSpace , sycl::memory_order::acq_rel , 335 sycl::memory_scope::device > (addr ,operand ); 336case sycl::memory_order::seq_cst : 337return atomic_fetch_xor < T ,addressSpace , sycl::memory_order::seq_cst , 338 sycl::memory_scope::device > (addr ,operand ); 339default : 340assert (false&& "Invalid memory_order for atomics. Valid memory_order for " 341"atomics are: sycl::memory_order::relaxed, " 342"sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!" ); 343 } 344} 345 346template < sycl::access::address_space addressSpace = 347 sycl::access::address_space::global_space , 348typename T1 ,typename T2 > 349inline T1 atomic_fetch_xor (T1 * addr ,T2 operand , 350 sycl::memory_order memoryOrder ) { 351atomic_fetch_xor < T1 ,addressSpace > (addr ,operand ,memoryOrder ); 352} 353 354/// Atomically calculate the minimum of the value at addr and the value operand 355/// and assign the result to the value at addr. 356/// \param [in, out] addr The pointer to the data. 357/// \param operand. 358/// \param memoryOrder The memory ordering used. 359/// \returns The value at the \p addr before the call. 360template < typename T , 361 sycl::access::address_space addressSpace = 362 sycl::access::address_space::global_space , 363 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 364 sycl::memory_scope memoryScope = sycl::memory_scope::device > 365inline T atomic_fetch_min (T * addr ,T operand ) { 366auto atm = 367 sycl::atomic_ref < T ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 368return atm .fetch_min (operand ); 369} 370 371template < sycl::access::address_space addressSpace = 372 sycl::access::address_space::global_space , 373 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 374 sycl::memory_scope memoryScope = sycl::memory_scope::device , 375typename T1 ,typename T2 > 376inline T1 atomic_fetch_min (T1 * addr ,T2 operand ) { 377auto atm = 378 sycl::atomic_ref < T1 ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 379return atm .fetch_min (operand ); 380} 381 382/// Atomically calculate the minimum of the value at addr and the value operand 383/// and assign the result to the value at addr. 384/// \param [in, out] addr The pointer to the data. 385/// \param operand. 386/// \param memoryOrder The memory ordering used. 387/// \returns The value at the \p addr before the call. 388template < typename T , sycl::access::address_space addressSpace = 389 sycl::access::address_space::global_space > 390inline T atomic_fetch_min (T * addr ,T operand , 391 sycl::memory_order memoryOrder ) { 392switch (memoryOrder ) { 393case sycl::memory_order::relaxed : 394return atomic_fetch_min < T ,addressSpace , sycl::memory_order::relaxed , 395 sycl::memory_scope::device > (addr ,operand ); 396case sycl::memory_order::acq_rel : 397return atomic_fetch_min < T ,addressSpace , sycl::memory_order::acq_rel , 398 sycl::memory_scope::device > (addr ,operand ); 399case sycl::memory_order::seq_cst : 400return atomic_fetch_min < T ,addressSpace , sycl::memory_order::seq_cst , 401 sycl::memory_scope::device > (addr ,operand ); 402default : 403assert (false&& "Invalid memory_order for atomics. Valid memory_order for " 404"atomics are: sycl::memory_order::relaxed, " 405"sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!" ); 406 } 407} 408 409template < sycl::access::address_space addressSpace = 410 sycl::access::address_space::global_space , 411typename T1 ,typename T2 > 412inline T1 atomic_fetch_min (T1 * addr ,T2 operand , 413 sycl::memory_order memoryOrder ) { 414atomic_fetch_min < T1 ,addressSpace > (addr ,operand ,memoryOrder ); 415} 416 417/// Atomically calculate the maximum of the value at addr and the value operand 418/// and assign the result to the value at addr. 419/// \param [in, out] addr The pointer to the data. 420/// \param operand. 421/// \param memoryOrder The memory ordering used. 422/// \returns The value at the \p addr before the call. 423template < typename T , 424 sycl::access::address_space addressSpace = 425 sycl::access::address_space::global_space , 426 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 427 sycl::memory_scope memoryScope = sycl::memory_scope::device > 428inline T atomic_fetch_max (T * addr ,T operand ) { 429auto atm = 430 sycl::atomic_ref < T ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 431return atm .fetch_max (operand ); 432} 433 434template < sycl::access::address_space addressSpace = 435 sycl::access::address_space::global_space , 436 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 437 sycl::memory_scope memoryScope = sycl::memory_scope::device , 438typename T1 ,typename T2 > 439inline T1 atomic_fetch_max (T1 * addr ,T2 operand ) { 440auto atm = 441 sycl::atomic_ref < T1 ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 442return atm .fetch_max (operand ); 443} 444 445/// Atomically calculate the maximum of the value at addr and the value operand 446/// and assign the result to the value at addr. 447/// \param [in, out] addr The pointer to the data. 448/// \param operand. 449/// \param memoryOrder The memory ordering used. 450/// \returns The value at the \p addr before the call. 451template < typename T , sycl::access::address_space addressSpace = 452 sycl::access::address_space::global_space > 453inline T atomic_fetch_max (T * addr ,T operand , 454 sycl::memory_order memoryOrder ) { 455switch (memoryOrder ) { 456case sycl::memory_order::relaxed : 457return atomic_fetch_max < T ,addressSpace , sycl::memory_order::relaxed , 458 sycl::memory_scope::device > (addr ,operand ); 459case sycl::memory_order::acq_rel : 460return atomic_fetch_max < T ,addressSpace , sycl::memory_order::acq_rel , 461 sycl::memory_scope::device > (addr ,operand ); 462case sycl::memory_order::seq_cst : 463return atomic_fetch_max < T ,addressSpace , sycl::memory_order::seq_cst , 464 sycl::memory_scope::device > (addr ,operand ); 465default : 466assert (false&& "Invalid memory_order for atomics. Valid memory_order for " 467"atomics are: sycl::memory_order::relaxed, " 468"sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!" ); 469 } 470} 471 472template < sycl::access::address_space addressSpace = 473 sycl::access::address_space::global_space , 474typename T1 ,typename T2 > 475inline T1 atomic_fetch_max (T1 * addr ,T2 operand , 476 sycl::memory_order memoryOrder ) { 477atomic_fetch_max < T1 ,addressSpace > (addr ,operand ,memoryOrder ); 478} 479 480/// Atomically set \p operand to the value stored in \p addr, if old value stored in 481/// \p addr is equal to zero or greater than \p operand, else decrease the value stored 482/// in \p addr. 483/// \param [in, out] addr The pointer to the data. 484/// \param operand The threshold value. 485/// \param memoryOrder The memory ordering used. 486/// \returns The old value stored in \p addr. 487template < sycl::access::address_space addressSpace = sycl::access::address_space::global_space , 488 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 489 sycl::memory_scope memoryScope = sycl::memory_scope::device > 490inline unsigned int atomic_fetch_compare_dec (unsigned int * addr , 491unsigned int operand ) { 492auto atm = sycl::atomic_ref < unsigned int ,memoryOrder ,memoryScope , 493addressSpace > (addr [0 ]); 494unsigned int old ; 495 496while (true) { 497old = atm .load (); 498if (old == 0 || old > operand ) { 499if (atm .compare_exchange_strong (old ,operand )) 500break ; 501 }else if (atm .compare_exchange_strong (old ,old - 1 )) 502break ; 503 } 504 505return old ; 506} 507 508/// Atomically increment the value stored in \p addr if old value stored in \p 509/// addr is less than \p operand, else set 0 to the value stored in \p addr. 510/// \param [in, out] addr The pointer to the data. 511/// \param operand The threshold value. 512/// \param memoryOrder The memory ordering used. 513/// \returns The old value stored in \p addr. 514template < sycl::access::address_space addressSpace = 515 sycl::access::address_space::global_space , 516 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 517 sycl::memory_scope memoryScope = sycl::memory_scope::device > 518inline unsigned int atomic_fetch_compare_inc (unsigned int * addr , 519unsigned int operand ) { 520auto atm = sycl::atomic_ref < unsigned int ,memoryOrder ,memoryScope , 521addressSpace > (addr [0 ]); 522unsigned int old ; 523while (true) { 524old = atm .load (); 525if (old >=operand ) { 526if (atm .compare_exchange_strong (old ,0 )) 527break ; 528 }else if (atm .compare_exchange_strong (old ,old + 1 )) 529break ; 530 } 531return old ; 532} 533 534/// Atomically increment the value stored in \p addr if old value stored in \p 535/// addr is less than \p operand, else set 0 to the value stored in \p addr. 536/// \param [in, out] addr The pointer to the data. 537/// \param operand The threshold value. 538/// \param memoryOrder The memory ordering used. 539/// \returns The old value stored in \p addr. 540template < sycl::access::address_space addressSpace = 541 sycl::access::address_space::global_space > 542inline unsigned int 543atomic_fetch_compare_inc (unsigned int * addr ,unsigned int operand , 544 sycl::memory_order memoryOrder ) { 545switch (memoryOrder ) { 546case sycl::memory_order::relaxed : 547return atomic_fetch_compare_inc < addressSpace , sycl::memory_order::relaxed , 548 sycl::memory_scope::device > (addr , 549operand ); 550case sycl::memory_order::acq_rel : 551return atomic_fetch_compare_inc < addressSpace , sycl::memory_order::acq_rel , 552 sycl::memory_scope::device > (addr , 553operand ); 554case sycl::memory_order::seq_cst : 555return atomic_fetch_compare_inc < addressSpace , sycl::memory_order::seq_cst , 556 sycl::memory_scope::device > (addr , 557operand ); 558default : 559assert (false&& "Invalid memory_order for atomics. Valid memory_order for " 560"atomics are: sycl::memory_order::relaxed, " 561"sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!" ); 562 } 563} 564 565/// Atomically exchange the value at the address addr with the value operand. 566/// \param [in, out] addr The pointer to the data. 567/// \param operand The value to be exchanged with the value pointed by \p addr. 568/// \param memoryOrder The memory ordering used. 569/// \returns The value at the \p addr before the call. 570template < typename T , 571 sycl::access::address_space addressSpace = 572 sycl::access::address_space::global_space , 573 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 574 sycl::memory_scope memoryScope = sycl::memory_scope::device > 575inline T atomic_exchange (T * addr ,T operand ) { 576auto atm = 577 sycl::atomic_ref < T ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 578return atm .exchange (operand ); 579} 580 581template < sycl::access::address_space addressSpace = 582 sycl::access::address_space::global_space , 583 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 584 sycl::memory_scope memoryScope = sycl::memory_scope::device , 585typename T1 ,typename T2 > 586inline T1 atomic_exchange (T1 * addr ,T2 operand ) { 587auto atm = 588 sycl::atomic_ref < T1 ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 589return atm .exchange (operand ); 590} 591 592/// Atomically exchange the value at the address addr with the value operand. 593/// \param [in, out] addr The pointer to the data. 594/// \param operand The value to be exchanged with the value pointed by \p addr. 595/// \param memoryOrder The memory ordering used. 596/// \returns The value at the \p addr before the call. 597template < typename T , sycl::access::address_space addressSpace = 598 sycl::access::address_space::global_space > 599inline T atomic_exchange (T * addr ,T operand , 600 sycl::memory_order memoryOrder ) { 601switch (memoryOrder ) { 602case sycl::memory_order::relaxed : 603return atomic_exchange < T ,addressSpace , sycl::memory_order::relaxed , 604 sycl::memory_scope::device > (addr ,operand ); 605case sycl::memory_order::acq_rel : 606return atomic_exchange < T ,addressSpace , sycl::memory_order::acq_rel , 607 sycl::memory_scope::device > (addr ,operand ); 608case sycl::memory_order::seq_cst : 609return atomic_exchange < T ,addressSpace , sycl::memory_order::seq_cst , 610 sycl::memory_scope::device > (addr ,operand ); 611default : 612assert (false&& "Invalid memory_order for atomics. Valid memory_order for " 613"atomics are: sycl::memory_order::relaxed, " 614"sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!" ); 615 } 616} 617 618template < sycl::access::address_space addressSpace = 619 sycl::access::address_space::global_space , 620typename T1 ,typename T2 > 621inline T1 atomic_exchange (T1 * addr ,T2 operand , 622 sycl::memory_order memoryOrder ) { 623atomic_exchange < T1 ,addressSpace > (addr ,operand ,memoryOrder ); 624} 625 626/// Atomically compare the value at \p addr to the value expected and exchange 627/// with the value desired if the value at \p addr is equal to the value expected. 628/// Returns the value at the \p addr before the call. 629/// \param [in, out] addr Multi_ptr. 630/// \param expected The value to compare against the value at \p addr. 631/// \param desired The value to assign to \p addr if the value at \p addr is expected. 632/// \param success The memory ordering used when comparison succeeds. 633/// \param fail The memory ordering used when comparison fails. 634/// \returns The value at the \p addr before the call. 635template < typename T , 636 sycl::access::address_space addressSpace = 637 sycl::access::address_space::global_space , 638 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 639 sycl::memory_scope memoryScope = sycl::memory_scope::device > 640T atomic_compare_exchange_strong ( 641 sycl::multi_ptr < T ,addressSpace > addr ,T expected ,T desired , 642 sycl::memory_order success = sycl::memory_order::relaxed , 643 sycl::memory_order fail = sycl::memory_order::relaxed ) { 644auto atm = sycl::atomic_ref < T ,memoryOrder ,memoryScope ,addressSpace > (* addr ); 645 646atm .compare_exchange_strong (expected ,desired ,success ,fail ); 647return expected ; 648} 649 650template < sycl::access::address_space addressSpace = 651 sycl::access::address_space::global_space , 652 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 653 sycl::memory_scope memoryScope = sycl::memory_scope::device , 654typename T1 ,typename T2 ,typename T3 > 655T1 atomic_compare_exchange_strong ( 656 sycl::multi_ptr < T1 ,addressSpace > addr ,T2 expected ,T3 desired , 657 sycl::memory_order success = sycl::memory_order::relaxed , 658 sycl::memory_order fail = sycl::memory_order::relaxed ) { 659auto atm = 660 sycl::atomic_ref < T1 ,memoryOrder ,memoryScope ,addressSpace > (* addr ); 661T1 expected_value = expected ; 662atm .compare_exchange_strong (expected_value ,desired ,success ,fail ); 663return expected_value ; 664} 665 666/// Atomically compare the value at \p addr to the value expected and exchange 667/// with the value desired if the value at \p addr is equal to the value expected. 668/// Returns the value at the \p addr before the call. 669/// \param [in] addr The pointer to the data. 670/// \param expected The value to compare against the value at \p addr. 671/// \param desired The value to assign to \p addr if the value at \p addr is expected. 672/// \param success The memory ordering used when comparison succeeds. 673/// \param fail The memory ordering used when comparison fails. 674/// \returns The value at the \p addr before the call. 675template < typename T , 676 sycl::access::address_space addressSpace = 677 sycl::access::address_space::global_space , 678 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 679 sycl::memory_scope memoryScope = sycl::memory_scope::device > 680T atomic_compare_exchange_strong ( 681T * addr ,T expected ,T desired , 682 sycl::memory_order success = sycl::memory_order::relaxed , 683 sycl::memory_order fail = sycl::memory_order::relaxed ) { 684auto atm = 685 sycl::atomic_ref < T ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 686atm .compare_exchange_strong (expected ,desired ,success ,fail ); 687return expected ; 688} 689 690template < sycl::access::address_space addressSpace = 691 sycl::access::address_space::global_space , 692 sycl::memory_order memoryOrder = sycl::memory_order::relaxed , 693 sycl::memory_scope memoryScope = sycl::memory_scope::device , 694typename T1 ,typename T2 ,typename T3 > 695T1 atomic_compare_exchange_strong ( 696T1 * addr ,T2 expected ,T3 desired , 697 sycl::memory_order success = sycl::memory_order::relaxed , 698 sycl::memory_order fail = sycl::memory_order::relaxed ) { 699T1 expected_value = expected ; 700auto atm = 701 sycl::atomic_ref < T1 ,memoryOrder ,memoryScope ,addressSpace > (addr [0 ]); 702atm .compare_exchange_strong (expected_value ,desired ,success ,fail ); 703return expected_value ; 704} 705 706/// Atomic extension to implement standard APIs in std::atomic 707namespace detail{ 708template < typename T > struct IsValidAtomicType { 709static constexpr bool value = 710 (std::is_same < T ,int > ::value || std::is_same < T ,unsigned int > ::value || 711 std::is_same < T ,long > ::value || std::is_same < T ,unsigned long > ::value || 712 std::is_same < T ,long long > ::value || 713 std::is_same < T ,unsigned long long > ::value || 714 std::is_same < T ,float > ::value || std::is_same < T ,double > ::value || 715 std::is_pointer < T > ::value ); 716}; 717}// namespace detail 718 719template < typename T , 720 sycl::memory_scope DefaultScope = sycl::memory_scope::system , 721#ifdef __AMDGPU__ 722 sycl::memory_orderDefaultOrder = sycl::memory_order::acq_rel , 723#else 724 sycl::memory_order DefaultOrder = sycl::memory_order::seq_cst , 725#endif 726 sycl::access::address_space Space = 727 sycl::access::address_space::generic_space > 728class atomic { 729 static_assert( 730 detail::IsValidAtomicType < T > ::value , 731"Invalid atomic type. Valid types are int, unsigned int, long, " 732"unsigned long, long long, unsigned long long, float, double " 733"and pointer types" ); 734T __d ; 735 736public : 737/// default memory synchronization order 738static constexpr sycl::memory_order default_read_order = 739 sycl::atomic_ref < T ,DefaultOrder ,DefaultScope ,Space > ::default_read_order ; 740static constexpr sycl::memory_order default_write_order = 741 sycl::atomic_ref < T ,DefaultOrder ,DefaultScope ,Space > ::default_write_order ; 742static constexpr sycl::memory_scope default_scope = DefaultScope ; 743static constexpr sycl::memory_order default_read_modify_write_order = 744DefaultOrder ; 745 746/// Default constructor. 747constexpr atomic ()noexcept = default ; 748/// Constructor with initialize value. 749constexpr atomic (T d )noexcept :__d (d ){}; 750 751/// atomically replaces the value of the referenced object with a non-atomic argument 752/// \param operand The value to replace the pointed value. 753/// \param memoryOrder The memory ordering used. 754/// \param memoryScope The memory scope used. 755void store (T operand , sycl::memory_order memoryOrder = default_write_order , 756 sycl::memory_scope memoryScope = default_scope )noexcept { 757 sycl::atomic_ref < T ,DefaultOrder ,DefaultScope ,Space > atm (__d ); 758atm .store (operand ,memoryOrder ,memoryScope ); 759 } 760 761/// atomically obtains the value of the referenced object 762/// \param memoryOrder The memory ordering used. 763/// \param memoryScope The memory scope used. 764/// \returns The value of the referenced object 765T load (sycl::memory_order memoryOrder = default_read_order , 766 sycl::memory_scope memoryScope = default_scope )const noexcept { 767 sycl::atomic_ref < T ,DefaultOrder ,DefaultScope ,Space > atm ( 768const_cast < T &> (__d )); 769return atm .load (memoryOrder ,memoryScope ); 770 } 771 772/// atomically replaces the value of the referenced object and obtains the value held previously 773/// \param operand The value to replace the pointed value. 774/// \param memoryOrder The memory ordering used. 775/// \param memoryScope The memory scope used. 776/// \returns The value of the referenced object before the call. 777T exchange (T operand , 778 sycl::memory_order memoryOrder = default_read_modify_write_order , 779 sycl::memory_scope memoryScope = default_scope )noexcept { 780 781 sycl::atomic_ref < T ,DefaultOrder ,DefaultScope ,Space > atm (__d ); 782return atm .exchange (operand ,memoryOrder ,memoryScope ); 783 } 784 785/// atomically compares the value of the referenced object with non-atomic argument 786/// and performs atomic exchange if equal or atomic load if not 787/// \param expected The value expected to be found in the object referenced by the atomic_ref object 788/// \param desired The value to store in the referenced object if it is as expected 789/// \param success The memory models for the read-modify-write 790/// \param failure The memory models for load operations 791/// \param memoryScope The memory scope used. 792/// \returns true if the referenced object was successfully changed, false otherwise. 793bool compare_exchange_weak ( 794T & expected ,T desired , 795 sycl::memory_order success , sycl::memory_order failure , 796 sycl::memory_scope memoryScope = default_scope )noexcept { 797 sycl::atomic_ref < T ,DefaultOrder ,DefaultScope ,Space > atm (__d ); 798return atm .compare_exchange_weak (expected ,desired ,success ,failure ,memoryScope ); 799 } 800/// \param expected The value expected to be found in the object referenced by the atomic_ref object 801/// \param desired The value to store in the referenced object if it is as expected 802/// \param memoryOrder The memory synchronization ordering for operations 803/// \param memoryScope The memory scope used. 804/// \returns true if the referenced object was successfully changed, false otherwise. 805bool compare_exchange_weak (T & expected ,T desired , 806 sycl::memory_order memoryOrder = default_read_modify_write_order , 807 sycl::memory_scope memoryScope = default_scope )noexcept { 808 sycl::atomic_ref < T ,DefaultOrder ,DefaultScope ,Space > atm (__d ); 809return atm .compare_exchange_weak (expected ,desired ,memoryOrder ,memoryScope ); 810 } 811 812/// atomically compares the value of the referenced object with non-atomic argument 813/// and performs atomic exchange if equal or atomic load if not 814/// \param expected The value expected to be found in the object referenced by the atomic_ref object 815/// \param desired The value to store in the referenced object if it is as expected 816/// \param success The memory models for the read-modify-write 817/// \param failure The memory models for load operations 818/// \param memoryScope The memory scope used. 819/// \returns true if the referenced object was successfully changed, false otherwise. 820bool compare_exchange_strong ( 821T & expected ,T desired , 822 sycl::memory_order success , sycl::memory_order failure , 823 sycl::memory_scope memoryScope = default_scope )noexcept { 824 825 sycl::atomic_ref < T ,DefaultOrder ,DefaultScope ,Space > atm (__d ); 826return atm .compare_exchange_strong (expected ,desired ,success ,failure ,memoryScope ); 827 } 828/// \param expected The value expected to be found in the object referenced by the atomic_ref object 829/// \param desired The value to store in the referenced object if it is as expected 830/// \param memoryOrder The memory synchronization ordering for operations 831/// \param memoryScope The memory scope used. 832/// \returns true if the referenced object was successfully changed, false otherwise. 833bool compare_exchange_strong (T & expected ,T desired , 834 sycl::memory_order memoryOrder = default_read_modify_write_order , 835 sycl::memory_scope memoryScope = default_scope )noexcept { 836 sycl::atomic_ref < T ,DefaultOrder ,DefaultScope ,Space > atm (__d ); 837return atm .compare_exchange_strong (expected ,desired ,memoryOrder ,memoryScope ); 838 } 839 840/// atomically adds the argument to the value stored in the atomic object and obtains the value held previously 841/// \param operand The other argument of arithmetic addition 842/// \param memoryOrder The memory ordering used. 843/// \param memoryScope The memory scope used. 844/// \returns The value of the referenced object before the call. 845T fetch_add (T operand , 846 sycl::memory_order memoryOrder = default_read_modify_write_order , 847 sycl::memory_scope memoryScope = default_scope )noexcept { 848 849 sycl::atomic_ref < T ,DefaultOrder ,DefaultScope ,Space > atm (__d ); 850return atm .fetch_add (operand ,memoryOrder ,memoryScope ); 851 } 852 853/// atomically subtracts the argument from the value stored in the atomic object and obtains the value held previously 854/// \param operand The other argument of arithmetic subtraction 855/// \param memoryOrder The memory ordering used. 856/// \param memoryScope The memory scope used. 857/// \returns The value of the referenced object before the call. 858T fetch_sub (T operand , 859 sycl::memory_order memoryOrder = default_read_modify_write_order , 860 sycl::memory_scope memoryScope = default_scope )noexcept { 861 862 sycl::atomic_ref < T ,DefaultOrder ,DefaultScope ,Space > atm (__d ); 863return atm .fetch_sub (operand ,memoryOrder ,memoryScope ); 864 } 865}; 866 867}// namespace dpct 868#endif // __DPCT_ATOMIC_HPP__