-
Notifications
You must be signed in to change notification settings - Fork 787
[SYCL][ESIMD][EMU] Atomic update #6661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
364874f
0197331
b2cff06
a26feea
17e3d48
17f287a
61fab41
241bb93
7992db6
4f33d98
fd9a2c3
87e39d5
b6ae426
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,126 +16,205 @@ namespace __ESIMD_DNS { | |
// This function implements atomic update of pre-existing variable in the | ||
// absense of C++ 20's atomic_ref. | ||
|
||
template <typename Ty> Ty atomic_load(Ty *ptr) { | ||
// __atomic_* functions support only integral types. In order to | ||
// support floating types for certain operations like min/max, | ||
// 'cmpxchg' operation is applied for result values using | ||
// 'bridging' variables in integral type. | ||
template <typename Ty> using CmpxchgTy = __ESIMD_DNS::uint_type_t<sizeof(Ty)>; | ||
|
||
template <typename Ty> inline Ty atomic_load(Ty *ptr) { | ||
#ifdef _WIN32 | ||
// TODO: Windows will be supported soon | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
#else | ||
return __atomic_load(ptr, __ATOMIC_SEQ_CST); | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
// TODO : Enable with unit test | ||
/* return sycl::bit_cast<Ty>(__atomic_load_n((CmpxchgTy<Ty> *)ptr, | ||
__ATOMIC_SEQ_CST)); */ | ||
#endif | ||
} | ||
|
||
template <typename Ty> Ty atomic_store(Ty *ptr, Ty val) { | ||
template <typename Ty> inline Ty atomic_store(Ty *ptr, Ty val) { | ||
#ifdef _WIN32 | ||
// TODO: Windows will be supported soon | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
#else | ||
__atomic_store(ptr, val, __ATOMIC_SEQ_CST); | ||
Ty ret = atomic_load<Ty>((CmpxchgTy<Ty> *)ptr); | ||
__atomic_store_n((CmpxchgTy<Ty> *)ptr, val, __ATOMIC_SEQ_CST); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: strictly speaking, reinterpret_cast should be used. Can be fixed later. |
||
return ret; | ||
#endif | ||
} | ||
|
||
template <typename Ty> Ty atomic_add_fetch(Ty *ptr, Ty val) { | ||
template <typename Ty> inline Ty atomic_add(Ty *ptr, Ty val) { | ||
#ifdef _WIN32 | ||
// TODO: Windows will be supported soon | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
#else | ||
return __atomic_add_fetch(ptr, val, __ATOMIC_SEQ_CST); | ||
if constexpr (std::is_integral_v<Ty>) { | ||
return __atomic_fetch_add(ptr, val, __ATOMIC_SEQ_CST); | ||
} else { | ||
// For Floating type | ||
Ty _old, _new; | ||
CmpxchgTy<Ty> _old_bits, _new_bits; | ||
do { | ||
_old = *ptr; | ||
_new = _old + val; | ||
_old_bits = *(CmpxchgTy<Ty> *)&_old; | ||
_new_bits = *(CmpxchgTy<Ty> *)&_new; | ||
dongkyunahn-intel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} while (!__atomic_compare_exchange_n((CmpxchgTy<Ty> *)ptr, &_old_bits, | ||
_new_bits, false, __ATOMIC_SEQ_CST, | ||
__ATOMIC_SEQ_CST)); | ||
return _old; | ||
} | ||
#endif | ||
} | ||
|
||
template <typename Ty> Ty atomic_sub_fetch(Ty *ptr, Ty val) { | ||
template <typename Ty> inline Ty atomic_sub(Ty *ptr, Ty val) { | ||
#ifdef _WIN32 | ||
// TODO: Windows will be supported soon | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
#else | ||
return __atomic_sub_fetch(ptr, val, __ATOMIC_SEQ_CST); | ||
if constexpr (std::is_integral_v<Ty>) { | ||
return __atomic_fetch_sub(ptr, val, __ATOMIC_SEQ_CST); | ||
} else { | ||
// For Floating type | ||
Ty _old, _new; | ||
CmpxchgTy<Ty> _old_bits, _new_bits; | ||
do { | ||
_old = *ptr; | ||
_new = _old - val; | ||
_old_bits = *(CmpxchgTy<Ty> *)&_old; | ||
_new_bits = *(CmpxchgTy<Ty> *)&_new; | ||
} while (!__atomic_compare_exchange_n((CmpxchgTy<Ty> *)ptr, &_old_bits, | ||
_new_bits, false, __ATOMIC_SEQ_CST, | ||
__ATOMIC_SEQ_CST)); | ||
return _old; | ||
} | ||
#endif | ||
} | ||
|
||
template <typename Ty> Ty atomic_and_fetch(Ty *ptr, Ty val) { | ||
template <typename Ty> inline Ty atomic_and(Ty *ptr, Ty val) { | ||
#ifdef _WIN32 | ||
// TODO: Windows will be supported soon | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
#else | ||
return __atomic_and_fetch(ptr, val, __ATOMIC_SEQ_CST); | ||
static_assert(std::is_integral<Ty>::value); | ||
return __atomic_fetch_and(ptr, val, __ATOMIC_SEQ_CST); | ||
#endif | ||
} | ||
|
||
template <typename Ty> Ty atomic_or_fetch(Ty *ptr, Ty val) { | ||
template <typename Ty> inline Ty atomic_or(Ty *ptr, Ty val) { | ||
#ifdef _WIN32 | ||
// TODO: Windows will be supported soon | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
#else | ||
return __atomic_or_fetch(ptr, val, __ATOMIC_SEQ_CST); | ||
static_assert(std::is_integral<Ty>::value); | ||
return __atomic_fetch_or(ptr, val, __ATOMIC_SEQ_CST); | ||
#endif | ||
} | ||
|
||
template <typename Ty> Ty atomic_xor_fetch(Ty *ptr, Ty val) { | ||
template <typename Ty> inline Ty atomic_xor(Ty *ptr, Ty val) { | ||
#ifdef _WIN32 | ||
// TODO: Windows will be supported soon | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
#else | ||
return __atomic_xor_fetch(ptr, val, __ATOMIC_SEQ_CST); | ||
static_assert(std::is_integral<Ty>::value); | ||
return __atomic_fetch_xor(ptr, val, __ATOMIC_SEQ_CST); | ||
#endif | ||
} | ||
|
||
template <typename Ty> Ty atomic_min(Ty *ptr, Ty val) { | ||
template <typename Ty> inline Ty atomic_min(Ty *ptr, Ty val) { | ||
#ifdef _WIN32 | ||
// TODO: Windows will be supported soon | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
#else | ||
// TODO FIXME: fix implementation for FP types. | ||
if constexpr (std::is_integral_v<Ty>) { | ||
Ty _old, _new; | ||
do { | ||
_old = *ptr; | ||
_new = std::min<Ty>(_old, val); | ||
} while (!__atomic_compare_exchange_n(ptr, &_old, _new, false, | ||
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); | ||
return _new; | ||
return _old; | ||
} else { | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
Ty _old, _new; | ||
CmpxchgTy<Ty> _old_bits, _new_bits; | ||
do { | ||
_old = *ptr; | ||
_new = std::min(_old, val); | ||
_old_bits = *(CmpxchgTy<Ty> *)&_old; | ||
_new_bits = *(CmpxchgTy<Ty> *)&_new; | ||
} while (!__atomic_compare_exchange_n((CmpxchgTy<Ty> *)ptr, &_old_bits, | ||
_new_bits, false, __ATOMIC_SEQ_CST, | ||
__ATOMIC_SEQ_CST)); | ||
return _old; | ||
} | ||
#endif | ||
} | ||
|
||
template <typename Ty> Ty atomic_max(Ty *ptr, Ty val) { | ||
template <typename Ty> inline Ty atomic_max(Ty *ptr, Ty val) { | ||
#ifdef _WIN32 | ||
// TODO: Windows will be supported soon | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
#else | ||
// TODO FIXME: fix implementation for FP types. | ||
if constexpr (std::is_integral_v<Ty>) { | ||
Ty _old, _new; | ||
do { | ||
_old = *ptr; | ||
_new = std::max<Ty>(_old, val); | ||
} while (!__atomic_compare_exchange_n(ptr, &_old, _new, false, | ||
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); | ||
return _new; | ||
return _old; | ||
} else { | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
Ty _old, _new; | ||
CmpxchgTy<Ty> _old_bits, _new_bits; | ||
do { | ||
_old = *ptr; | ||
_new = std::max(_old, val); | ||
_old_bits = *(CmpxchgTy<Ty> *)&_old; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and in many more places: |
||
_new_bits = *(CmpxchgTy<Ty> *)&_new; | ||
} while (!__atomic_compare_exchange_n((CmpxchgTy<Ty> *)(CmpxchgTy<Ty> *)ptr, | ||
&_old_bits, _new_bits, false, | ||
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); | ||
return _old; | ||
} | ||
#endif | ||
} | ||
|
||
template <typename Ty> Ty atomic_cmpxchg(Ty *ptr, Ty expected, Ty desired) { | ||
template <typename Ty> | ||
inline Ty atomic_cmpxchg(Ty *ptr, Ty expected, Ty desired) { | ||
#ifdef _WIN32 | ||
// TODO: Windows will be supported soon | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
#else | ||
// TODO FIXME: fix implementation for FP types. | ||
if constexpr (std::is_integral_v<Ty>) { | ||
Ty _old = expected; | ||
__atomic_compare_exchange_n(ptr, &_old, desired, false, __ATOMIC_SEQ_CST, | ||
Ty local = expected; | ||
__atomic_compare_exchange_n(ptr, &local, desired, false, __ATOMIC_SEQ_CST, | ||
__ATOMIC_SEQ_CST); | ||
return *ptr; | ||
// if exchange occured, this means 'local=expected=*ptr'. So local | ||
// is returned as old val | ||
// if exchange did not occur, *ptr value compared against 'local' | ||
// is stored in 'local'. So local is returned as old val | ||
return local; | ||
} else { | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
CmpxchgTy<Ty> desired_bits = *(CmpxchgTy<Ty> *)&desired; | ||
CmpxchgTy<Ty> local_bits = *(CmpxchgTy<Ty> *)&expected; | ||
__atomic_compare_exchange_n((CmpxchgTy<Ty> *)ptr, &local_bits, desired_bits, | ||
false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); | ||
return *((Ty *)&local_bits); | ||
} | ||
#endif | ||
} | ||
|
||
inline void atomic_fence() { | ||
#ifdef _WIN32 | ||
// TODO: Windows will be supported soon | ||
__ESIMD_UNSUPPORTED_ON_HOST; | ||
#else | ||
__atomic_thread_fence(__ATOMIC_SEQ_CST); | ||
#endif | ||
} | ||
|
||
} // namespace __ESIMD_DNS | ||
|
||
/// @endcond ESIMD_DETAIL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks strange that we need atomic_load as a part of atomic_store implementation, but that's how Gen ISA is :)
no action for this comment is needed