Skip to content

docs(examples): move atomics to the typed-slice sound form - #1500

Open
navneet83 wants to merge 1 commit into
NVIDIA:mainfrom
navneet83:atomics-example-sound-form
Open

navneet83 wants to merge 1 commit into
NVIDIA:mainfrom
navneet83:atomics-example-sound-form

Conversation

@navneet83

Copy link
Copy Markdown
Contributor

Summary

This cleans up the atomics example so kernels no longer have to accept counters as raw *mut T pointers and immediately rebuild DeviceAtomicU32 values with unsafe { DeviceAtomicU32::from_ptr(...) }.

Instead, the host retypes the buffers once with cast_elem::<DeviceAtomicU32>(), and the kernels take &[DeviceAtomicU32]. Shared updates now go through interior mutability, which removes the unsafe blocks from the kernel bodies. The launch-site unsafe remains, since launching still depends on the usual shape and resource guarantees.

Readback casts the buffers back to the plain element type. The old atomic_storage helper is gone, since it only existed to turn a buffer into a raw pointer.

Behavior

No behavior change intended. The examples still use the same operations, orderings, launch configs, and expected values across all 20 tests.

The scope coverage is unchanged too: the block-scope tests still use BlockAtomicU32, and test 20 still uses core::sync::atomic::AtomicU32 as the slice element for core-intercept coverage.

Verification

Ran:

scripts/smoketest.sh --compile-only atomics

on current main.

Result: compiled successfully, including ptxas for sm_80.

The atomics example passed shared counters as raw device pointers and
rebuilt atomic views inside every kernel with unsafe
DeviceAtomic*::from_ptr. The sound form takes the counter as
&[DeviceAtomic*] and retypes the host buffer with cast_elem, so the
shared updates go through interior mutability and the kernels need no
unsafe. This is the pattern legacy_atomic_rmw_cas already uses and the
form recommended in Discord #help when this example was flagged as
stale.

Tests 1-20 convert; the AtomicPtr tests and the compile-only intrinsic
probe keep raw pointers because pointer-based core atomics are what
they exercise. The atomic_storage helper had no remaining callers.

Signed-off-by: Navneet Kumar <4686373+navneet83@users.noreply.github.com>
@copy-pr-bot

copy-pr-bot Bot commented Oct 12, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant