liveBook · Manning

Tl.atomic_cas

As explained in GPU Programming with Triton

tl.atomic_cas is a Triton compare-and-swap operation that writes a new value only when the stored value equals an expected comparison value.

When multiple GPU threads update the same location, compare-and-swap provides a way to retry an update if another thread intervenes. In Triton, tl.atomic_cas atomically writes a new value only when the stored value equals the expected comparison value. It returns the old value. section 4.1.3

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

The call takes a pointer, comparison value, and replacement value, with optional sem and scope arguments. section 4.1.3

Fallback atomic addition

On hardware without native bfloat16 atomicAdd, a compare-and-swap loop reads the current value, computes a result, and attempts to write it back. If another thread changed the value, the loop retries. Under contention, these retries can cost more than a native atomic operation. section 4.1.3

How GPU Programming with Triton covers it

How deep it goes
Defines the operation and signature, and explains the retry logic and contention cost of a fallback loop.
Examples
Uses bfloat16 atomic addition on older hardware to illustrate a compare-and-swap fallback loop.
What you can do after
Evaluate when a compare-and-swap fallback may cost more than native atomic addition under contention.
How it connects
Connects compare-and-swap to atomic addition, contention, and read-modify-write operations.

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.