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

Fixed compilation for Pascal #412

Merged
merged 1 commit into from
May 2, 2023
Merged
Changes from all 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
84 changes: 76 additions & 8 deletions include/matx/transforms/reduce.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ union HalfBits {
__nv_bfloat16 b;
};

union PascalHalfBits {
constexpr PascalHalfBits(unsigned short x) : i(x) {}
PascalHalfBits() = default;
unsigned int i;
__half h[2];
__nv_bfloat16 b[2];
};

#ifdef __CUDACC__
/**
* Warp shuffle down with a complex float
Expand Down Expand Up @@ -159,20 +167,50 @@ __MATX_DEVICE__ __MATX_INLINE__ void atomicMax(float *addr, float val)
*/
__MATX_DEVICE__ __MATX_INLINE__ __nv_bfloat16 atomicMax(__nv_bfloat16 *addr, __nv_bfloat16 val)
{
#if __CUDA_ARCH__ > 600
HalfBits tmpval;
HalfBits old;
unsigned short *address_as_ushort = (unsigned short *)addr;
unsigned short *address_as_other = (unsigned short *)addr;

unsigned short assumed;
old.i = *address_as_ushort;
old.i = *address_as_other;
tmpval.b = val;

// nan should be ok here but should verify
while (val > old.b) {
assumed = old.i;
old.b = static_cast<float>(atomicCAS(address_as_ushort, assumed, tmpval.i));
old.b = static_cast<float>(atomicCAS(address_as_other, assumed, tmpval.i));
}

return old.b;
#else // Pascal doesn't support short atomicCAS
PascalHalfBits tmpval;
PascalHalfBits old;
unsigned int *address_as_other;
int offset;

// We need to move our pointer back
if ((uintptr_t)addr & 0x10) {
address_as_other = (unsigned int *)(reinterpret_cast<uint8_t*>(addr) - 2);
offset = 1;
}
else {
address_as_other = (unsigned int *)(addr);
offset = 0;
}

unsigned short assumed;
old.i = *address_as_other;
tmpval.b[offset] = val;

// nan should be ok here but should verify
while (val > old.b[offset]) {
assumed = old.i;
old.b[offset] = static_cast<float>(atomicCAS(address_as_other, assumed, tmpval.i));
}

return old.b;
return old.b[offset];
#endif
};


Expand All @@ -188,20 +226,50 @@ __MATX_DEVICE__ __MATX_INLINE__ __nv_bfloat16 atomicMax(__nv_bfloat16 *addr, __n
*/
__MATX_DEVICE__ __MATX_INLINE__ __half atomicMax(__half *addr, __half val)
{
#if __CUDA_ARCH__ > 600
HalfBits tmpval;
HalfBits old;
unsigned short *address_as_ushort = (unsigned short *)addr;
unsigned short *address_as_other = (unsigned short *)addr;

unsigned short assumed;
old.i = *address_as_ushort;
old.i = *address_as_other;
tmpval.h = val;

// nan should be ok here but should verify
while (val > old.h) {
assumed = old.i;
old.h = atomicCAS(address_as_ushort, assumed, tmpval.i);
old.h = atomicCAS(address_as_other, assumed, tmpval.i);
}

return old.h;
#else // Pascal doesn't support short atomicCAS
PascalHalfBits tmpval;
PascalHalfBits old;
unsigned int *address_as_other;
int offset;

// We need to move our pointer back to align to a 2b boundary
if ((uintptr_t)addr & 0x10) {
address_as_other = (unsigned int *)(reinterpret_cast<uint8_t*>(addr) - 2);
offset = 1;
}
else {
address_as_other = (unsigned int *)(addr);
offset = 0;
}

unsigned short assumed;
old.i = *address_as_other;
tmpval.h[offset] = val;

// nan should be ok here but should verify
while (val > old.h[offset]) {
assumed = old.i;
old.h[offset] = atomicCAS(address_as_other, assumed, tmpval.i);
}

return old.h;
return old.h[offset];
#endif
};

/**
Expand Down