Skip to content

feat(cuda-module): #[cooperative_launch] for grid-wide sync on the typed path - #169

Merged
nihalpasham merged 6 commits into
mainfrom
feat/cuda-module-cooperative-launch
Jun 11, 2026
Merged

nihalpasham merged 6 commits into
mainfrom
feat/cuda-module-cooperative-launch

Conversation

@nihalpasham

Copy link
Copy Markdown
Collaborator

What

Kernels using grid::sync() can now launch through the typed #[cuda_module] path; previously cooperative launch existed only in the legacy cuda_launch\! macro.

#[kernel]
#[cooperative_launch]                    // mirrors #[cluster_launch(...)]
pub fn my_grid_sync_kernel(...) { ... }

module.my_grid_sync_kernel(stream, cfg, ...)   // launches cooperatively

Mirrors the #[cluster_launch] precedent at every layer: cuda-core gains launch_kernel_ex_cooperative[_on_stream] (one cuLaunchKernelEx carrying both the cluster and cooperative attributes when combined), AsyncKernelLaunch gains set_cooperative next to set_cluster_dim with a 4-way dispatch, cuda-host gains the doc-hidden shim, and the macro stores the flag and routes all three generated launch shapes (sync, async, owned-async). Combining with #[cluster_launch] is allowed because cuLaunchKernelEx expresses both attributes in one array; a never-launched kernel pins that the combination compiles.

Evidence

  • coop_groups_demo's two grid-sync tests now launch through the typed path and PASS on sm_120 (they deadlock or mis-sum if the attribute is not genuinely applied), with kernel bodies byte-identical to before.
  • Review independently expanded the generated code (-Zunpretty=expanded) to verify the sync path reaches launch_kernel_cooperative_on_stream and both async builders set the flag; non-cooperative kernels are unchanged (several untouched examples rebuilt and verified).
  • 5 new macro token tests + a cooperative kernel in the cuda-host API test (sync/async/owned-async); book and README sections added.
  • Tests/clippy/fmt/doc gates clean; compile-only smoketest green; reviewer's merge probe vs main: zero conflicts.

cuLaunchKernelEx takes an array of launch attributes, so one call can
carry both CU_LAUNCH_ATTRIBUTE_CLUSTER_DIMENSION and
CU_LAUNCH_ATTRIBUTE_COOPERATIVE. Add launch_kernel_ex_cooperative and
its context-binding launch_kernel_ex_cooperative_on_stream wrapper,
mirroring the existing launch_kernel_ex / launch_kernel_cooperative
pair. This is the driver path used when a #[cuda_module] kernel
combines #[cluster_launch(...)] with #[cooperative_launch].
Mirror the cluster_dim knob: a cooperative flag stored on the builder
(set_cooperative, next to set_cluster_dim) and applied at launch time.
The launch dispatch now picks the driver entry point from the
(cluster_dim, cooperative) pair: plain cuLaunchKernel when neither is
set, and cuLaunchKernelEx with the matching attribute(s) otherwise.

Cooperative launches are the precondition for grid-wide barriers
(cuda_device::grid::sync()); until now only the legacy cuda_launch\!
macro could request one.
Doc-hidden helper next to set_async_kernel_cluster_dim, so code
generated by #[cuda_module] can flip the cooperative knob on an
AsyncKernelLaunch without naming cuda-async types directly. Exported
from the crate root under the async feature, exactly like the
cluster-dim shim.
…rnels

A bare #[cooperative_launch] marker on a #[kernel] inside a
#[cuda_module] module routes every generated launch shape through the
cooperative driver path, mirroring how #[cluster_launch] is wired:

- the sync method calls launch_kernel_cooperative_on_stream instead of
  launch_kernel_on_stream;
- the borrowed-async and owned-async builders call
  set_async_kernel_cooperative right where set_async_kernel_cluster_dim
  is set for cluster kernels;
- combining #[cooperative_launch] with #[cluster_launch] is allowed
  (cuLaunchKernelEx accepts both attributes in one call) and routes
  through launch_kernel_ex_cooperative_on_stream.

The attribute itself is a validating pass-through: unlike
#[cluster_launch] it changes nothing in the PTX, because cooperative-ness
is purely a launch-time property. It is re-exported from cuda-device
next to the other kernel attributes.

Tests:
- cuda-macros unit tests pin the generated tokens for the cooperative,
  plain, combined, and async shapes, and the no-arguments rule;
- cuda-host's cuda_module_api compile test grows a #[cooperative_launch]
  kernel so the generated sync/async/owned-async methods keep
  typechecking against the real helpers.
…yped path

Move test_grid_sync and test_typed_grid_sync into a #[cuda_module]
module and mark them #[cooperative_launch]; their two
'cooperative: true' cuda_launch\! call sites become typed
module.<kernel>(...) calls. Kernel bodies and every other cuda_launch\!
call in the example are unchanged, so the device-verified grid-sync
checks remain the live regression for the new attribute on GPU runs.

Also add a compile-only kernel combining #[cluster_launch(2, 1, 1)]
with #[cooperative_launch]. It is never launched; it pins that the
combination compiles end to end (macro expansion, generated host
methods against the combined cuda-core helper, and device PTX).
Add a 'Cooperative launch' section to the launching-kernels chapter,
list the attribute in the API quick reference, and point the
supported-features entry at the typed #[cuda_module] path alongside the
legacy cuda_launch\! flag.
@nihalpasham nihalpasham added enhancement New feature or request host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) labels Jun 11, 2026
@nihalpasham
nihalpasham merged commit 4afde42 into main Jun 11, 2026
24 checks passed
@nihalpasham
nihalpasham deleted the feat/cuda-module-cooperative-launch branch June 12, 2026 10:19
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant