ATOMIC_CMPXCHG(3clc)

πŸ“¦
version 1.0~svn33624 Β· 3 versions
, in ubuntu jammy, ubuntu noble

πŸ’… ATOMIC_CMPXCHG πŸ’…

okay bestie, welcome to ATOMIC_CMPXCHG(3clc) from the OpenCL Manual β€” yeah we're in the 3clc section, come on in, the vibes are atomic πŸ¦‹


✨ NAME ✨

atomic_cmpxchg β€” the atomic_cmpxchg functions. plural. yes there's more than one. keep up πŸ’–

int atomic_cmpxchg(volatile global(3clc) int *p, int cmp, int val);

unsigned int atomic_cmpxchg(volatile global(3clc) unsigned int *p,
                            unsigned int cmp, unsigned int val);

int atomic_cmpxchg(volatile local(3clc) int *p, int cmp, int val);

unsigned int atomic_cmpxchg(volatile local(3clc) unsigned int *p,
                            unsigned int cmp, unsigned int val);

four glow-ups of the same function, depending on whether you're kicking it in global or local memory and whether your int is signed or unsigned. pick your fighter πŸŽ€


🌸 DESCRIPTION 🌸

okay so here's the tea. this function:

  1. Reads the 32-bit value (the manual calls it old, iconic name, no notes) that lives at the location pointed to by p.
  2. Computes (old == cmp) ? val : old β€” like, "does the thing match what I expected? if yes, swap in the new thing, if no, keep the old thing" πŸ’…
  3. Stores that result at the location pointed to by p.

and then β€” the moment we've all been waiting for β€” the function returns old. yes, the original value, not the new one. it's giving "I remember who you were before" 😭

there's also a 64-bit version of this whole situation, atom_cmpxchg(3clc), but it's not just sitting there unlocked. it's enabled by cl_khr_int64_base_atomics(3clc). no extension, no 64-bit. periodt πŸ”₯

πŸ“œ SPECIFICATION πŸ“œ

OpenCL Specification[1]

(the footnote is real, we'll get to it, stay with me 🫢)


πŸ‘€ SEE ALSO πŸ‘€

go read those too, bestie, they're the rest of the friend group 🌈


πŸ‘‘ AUTHORS πŸ‘‘

The Khronos Group β€” the actual icons. respect. πŸ’œ


Copyright Β© 2007-2011 The Khronos Group Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the following condition: the above copyright notice and this permission notice shall be included in all copies or substantial portions of the Materials.

(yeah, the whole legal paragraph, every word, because we do not skip the fine print in this house πŸŽ€)


πŸ“ NOTES πŸ“

  1. OpenCL Specification β€” page 279, section 6.12.11 - Atomic Functions for 32-bit integers

and that's the whole page!! every signature, every default-free option-free detail, every reference, all of it, untouched, just with better energy ✨

this little masterpiece came from The Khronos Group, dated 01/14/2021, and it lives under the name ATOMIC_CMPXCHG(3clc) β€” mwah, iconic, no cap πŸ’–

πŸ“¦ where this page comes from