Skip to content
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

This PR adds support to __half and nb_bfloat16 to myAtomicReduce #1585

Merged
merged 3 commits into from
Jun 10, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions cpp/include/raft/util/cuda_utils.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,41 @@ DI void myAtomicReduce(float* address, float val, ReduceLambda op)
} while (assumed != old);
}

template <typename ReduceLambda>
DI void myAtomicReduce(__half* address, __half val, ReduceLambda op)
{
#if (__CUDA_ARCH__ >= 530)
unsigned short int* address_as_uint = (unsigned short int*)address;
unsigned short int old = *address_as_uint, assumed;
do {
assumed = old;
old = atomicCAS(address_as_uint, assumed, __half_as_ushort(op(val, __ushort_as_half(assumed))));
} while (assumed != old);
#else
// Fail during template instantiation if the compute capability doesn't support this operation
static_assert(sizeof(__half) != sizeof(__half),
"__half is only supported on __CUDA_ARCH__ >= 530");
Kh4ster marked this conversation as resolved.
Show resolved Hide resolved
#endif
}

template <typename ReduceLambda>
DI void myAtomicReduce(nv_bfloat16* address, nv_bfloat16 val, ReduceLambda op)
{
#if (__CUDA_ARCH__ >= 800)
unsigned short int* address_as_uint = (unsigned short int*)address;
unsigned short int old = *address_as_uint, assumed;
do {
assumed = old;
old = atomicCAS(
address_as_uint, assumed, __bfloat16_as_ushort(op(val, __ushort_as_bfloat16(assumed))));
} while (assumed != old);
#else
// Fail during template instantiation if the compute capability doesn't support this operation
static_assert(sizeof(nv_bfloat16) != sizeof(nv_bfloat16),
"nv_bfloat16 is only supported on __CUDA_ARCH__ >= 800");
#endif
}

template <typename ReduceLambda>
DI void myAtomicReduce(int* address, int val, ReduceLambda op)
{
Expand Down