Last active
January 27, 2018 19:41
-
-
Save PolarNick239/9dffaf365b332b4442e2ac63b867034f to your computer and use it in GitHub Desktop.
OpenCL overload of atomic_cmpxchg and atomic_add for float
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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