liveBook · Manning

CAS loop

As explained in GPU Programming with Triton

A CAS loop repeatedly reads a value, computes an update, attempts compare-and-swap, and retries when another thread changes the value.

A compare-and-swap (CAS) loop updates a value by reading its current contents, computing a replacement, and attempting to write it only if the value is unchanged. If another thread has modified it, the attempt fails and the loop retries. section 4.1.3

This fallback can be slower than a native atomic operation: under contention, only one thread succeeds per attempt while others retry. Older GPUs may use CAS loops for data types without native atomic support. section 4.1.3

tl.atomic_cas(pointer, cmp, val, sem=None, scope=None)

The compare-and-swap primitive used to attempt a conditional update. section 4.1.3

How GPU Programming with Triton covers it

How deep it goes
Explains the read-compute-attempt-retry sequence and contention cost without detailing a complete CAS-loop implementation.
Examples
Uses unsupported bfloat16 atomics on older hardware as an example of a compare-and-swap fallback.
What you can do after
Understand why unsupported atomic types and contention can make a CAS fallback slower than native operations.
How it connects
Connects compare-and-swap with atomic operations, atomic addition, global memory, and thread contention.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of GPU Programming with Triton. The section links point to the parts of the book it draws on.