Skip to content

Add Cooperative Groups examples - #486

Open
j-stephan wants to merge 8 commits into
amd-stagingfrom
jstephan/cooperative-groups
Open

Add Cooperative Groups examples#486
j-stephan wants to merge 8 commits into
amd-stagingfrom
jstephan/cooperative-groups

Conversation

@j-stephan

@j-stephan j-stephan commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Motivation

In ROCm 7.14, we introduced new Cooperative Groups features. This example demonstrates how to use memcpy_async and the new inclusive / exclusive scan functionality.

Technical Details

Not supported for HIP SDK or CUDA.

Test Plan

Tested locally on gfx1100 lab machine.

Test Result

Works.

Added/Updated documentation?

  • Yes
  • No, does not apply to this PR.

Included Visual Studio files?

  • Yes
  • No, does not apply to this PR.

Submission Checklist

@j-stephan
j-stephan requested a review from zichguan-amd July 17, 2026 12:41
@j-stephan j-stephan self-assigned this Jul 17, 2026
@j-stephan
j-stephan requested review from a team as code owners July 17, 2026 12:41

@randyh62 randyh62 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Comment thread HIP-Basic/cooperative_groups_double_buffered_tile/README.md Outdated
Comment thread HIP-Basic/cooperative_groups_double_buffered_tile/README.md Outdated
Comment thread HIP-Basic/cooperative_groups_double_buffered_tile/README.md Outdated
Comment thread HIP-Basic/cooperative_groups_double_buffered_tile/README.md Outdated
Comment thread HIP-Basic/cooperative_groups_double_buffered_tile/README.md Outdated
Comment thread HIP-Basic/cooperative_groups_double_buffered_tile/README.md
Signed-off-by: Jan Stephan <jan.stephan@amd.com>
Signed-off-by: Jan Stephan <jan.stephan@amd.com>
Signed-off-by: Jan Stephan <jan.stephan@amd.com>
Signed-off-by: Jan Stephan <jan.stephan@amd.com>
@j-stephan
j-stephan force-pushed the jstephan/cooperative-groups branch from 43a654b to d6c2be3 Compare July 23, 2026 09:15
@j-stephan j-stephan changed the title Add Cooperative Groups double buffered tile example Add Cooperative Groups examples Jul 23, 2026
@j-stephan

Copy link
Copy Markdown
Contributor Author

@randyh62 @sourabhuday - I've added a new prefix_sum example to this PR. Please review those files as well.

Comment thread HIP-Basic/cooperative_groups_double_buffered_tile/CMakeLists.txt Outdated
Comment thread HIP-Basic/cooperative_groups_prefix_sum/CMakeLists.txt Outdated
@zichguan-amd

Copy link
Copy Markdown
Collaborator

hip/cooperative_groups/hip_scan.h doesn't seem to be in the 7.14 apt packages. https://rocm.docs.amd.com/projects/HIP/en/docs-7.14.0/how-to/hip_runtime_api/cooperative_groups.html#unsupported-nvidia-cuda-features, looks like develop has it but not released in 7.14.

@zichguan-amd

zichguan-amd commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Just need to use SKIP_FROM_BUILD similar to https://github.com/ROCm/rocm-examples/blob/d6017851e7d0cb860b6a78b8d2016735a31dcc01/Libraries/hipFFT/Makefile to skip the build. I haven't found a better solution that can inject this info without modifying all the Makefiles.

@zichguan-amd zichguan-amd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if CI passes

Comment on lines +11 to +15
A single block streams a 1D array through two LDS (shared memory) buffers, one tile at a time.
The async load of the next tile is issued into the second buffer while the current tile is consumed.
A split barrier separates the moment a thread has finished reading a buffer from the moment the
block guarantees that every thread is done. The kernel applies the element-wise operation
`out[i] = scale * in[i] + bias`, which is trivial to validate against a CPU reference.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reference to the docs that can be added here?

run as independent work between `barrier_arrive` and `barrier_wait`. Correctness (no data races,
validated output) is the top priority.

This example targets the AMD/HIP (ROCm) backend, and it requires a ROCm version recent enough to ship `hip/cooperative_groups/memcpy_async.h`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could mention a specific version, or later than...

Comment on lines +5 to +9
This program showcases a double-buffered tile load pipeline built from two cooperative groups
APIs: the group-collective `cooperative_groups::memcpy_async` and the split barrier of a
`thread_block`. A split barrier decomposes an ordinary block barrier into two phases,
`barrier_arrive` and `barrier_wait`, so that independent work can run between them instead of every
thread blocking immediately.

@jujiang-del jujiang-del Aug 5, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This program showcases a double-buffered tile load pipeline built from two cooperative groups
APIs: the group-collective `cooperative_groups::memcpy_async` and the split barrier of a
`thread_block`. A split barrier decomposes an ordinary block barrier into two phases,
`barrier_arrive` and `barrier_wait`, so that independent work can run between them instead of every
thread blocking immediately.
This program demonstrates double-buffered tile load pipeline built with two cooperative groups
APIs:
* `cooperative_groups::memcpy_async`
* The `thread_block` split barrier (`barrier_arrive()` / `barrier_wait()`)
A split barrier decomposes an ordinary block barrier into two phases:
* `barrier_arrive`
* `barrier_wait`
This separation allows useful independent work to execute between the two operations instead of forcing every thread to block immediately at a single barrier.

Comment on lines +17 to +21
`cooperative_groups::memcpy_async` is an **asynchronous**, group-collective copy (typically
global <-> LDS). HIP does not expose a separate wait handle (there is no `cg::wait()`), so its
completion must be enforced by a following group barrier - either a `block.sync()` (as the official
reference test does) or, as in this example, the `barrier_wait` of a split barrier whose
`barrier_arrive` is issued *after* the copy. Ordering matters: the prefetch of the next tile is

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`cooperative_groups::memcpy_async` is an **asynchronous**, group-collective copy (typically
global <-> LDS). HIP does not expose a separate wait handle (there is no `cg::wait()`), so its
completion must be enforced by a following group barrier - either a `block.sync()` (as the official
reference test does) or, as in this example, the `barrier_wait` of a split barrier whose
`barrier_arrive` is issued *after* the copy. Ordering matters: the prefetch of the next tile is
`cooperative_groups::memcpy_async()` is an **asynchronous**, group-collective copy operation, typically used to stage data between global memory and LDS (shared memory). HIP does not provide a separate completion handle or equivalent cg::wait() primitive, Therefore, completion of an asynchronous copy must be synchronized through a subsequent group barrier. The official reference test uses block.sync(), while this example relies on the `barrier_wait()` phase of a split barrier.
Correct ordering is critical. The prefetch of the next tile is

@jujiang-del jujiang-del left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comments.

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.

5 participants