Skip to content

Instantly share code, notes, and snippets.

@PolarNick239
Last active January 27, 2018 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PolarNick239/9dffaf365b332b4442e2ac63b867034f to your computer and use it in GitHub Desktop.
Save PolarNick239/9dffaf365b332b4442e2ac63b867034f to your computer and use it in GitHub Desktop.
OpenCL overload of atomic_cmpxchg and atomic_add for float
// See https://streamhpc.com/blog/2016-02-09/atomic-operations-for-floats-in-opencl-improved/
static float atomic_cmpxchg_f32(volatile __global float *p, float cmp, float val) {
union {
unsigned int u32;
float f32;
} cmp_union, val_union, old_union;
cmp_union.f32 = cmp;
val_union.f32 = val;
old_union.u32 = atomic_cmpxchg((volatile __global unsigned int *) p, cmp_union.u32, val_union.u32);
return old_union.f32;
}
static float atomic_add_f32(volatile __global float *p, float val) {
float found = *p;
float expected;
do {
expected = found;
found = atomic_cmpxchg_f32(p, expected, expected + val);
} while (found != expected);
return found;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment