Skip to content

[STF] Add exec places from externally-owned CUDA contexts#9779

Merged
caugonnet merged 10 commits into
NVIDIA:mainfrom
caugonnet:stf-exec-place-cuda-context
Jul 15, 2026
Merged

[STF] Add exec places from externally-owned CUDA contexts#9779
caugonnet merged 10 commits into
NVIDIA:mainfrom
caugonnet:stf-exec-place-cuda-context

Conversation

@caugonnet

Copy link
Copy Markdown
Contributor

Allow an exec_place to wrap a CUcontext created outside CUDASTF, so SM partitioning can be owned by one library (e.g. cuda.core in Python) and shared across frameworks: the same green context can back a CUDASTF exec place and, for instance, a Warp device mapped from the same handle.

  • cudax: new exec_place_cuda_ctx_impl generalizing the green-context place; exec_place::cuda_context(ctx, devid = -1) factory (devid derived via cuCtxGetDevice when omitted). exec_place::green_ctx now builds the same impl, converting the CUgreenCtx once at construction instead of on every activate(). Identity (hash/cmp) is keyed on the converted CUcontext, which is stable per green context, so places built from a green_ctx_view and from the converted context compare equal.
  • C API: stf_exec_place_cuda_context(CUcontext, int dev_id).
  • Header unittests for equality, device-ordinal derivation, the activate/deactivate round trip, and cross-route equality with the green-context place; C API test running a task on a place built from the primary context.

Places are non-owning: the caller must keep the context alive while the place is in use.

Description

closes

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

Allow an exec_place to wrap a CUcontext created outside CUDASTF, so SM
partitioning can be owned by one library (e.g. cuda.core in Python) and
shared across frameworks: the same green context can back a CUDASTF exec
place and, for instance, a Warp device mapped from the same handle.

- cudax: new exec_place_cuda_ctx_impl generalizing the green-context
  place; exec_place::cuda_context(ctx, devid = -1) factory (devid derived
  via cuCtxGetDevice when omitted). exec_place::green_ctx now builds the
  same impl, converting the CUgreenCtx once at construction instead of on
  every activate(). Identity (hash/cmp) is keyed on the converted
  CUcontext, which is stable per green context, so places built from a
  green_ctx_view and from the converted context compare equal.
- C API: stf_exec_place_cuda_context(CUcontext, int dev_id).
- Header unittests for equality, device-ordinal derivation, the
  activate/deactivate round trip, and cross-route equality with the
  green-context place; C API test running a task on a place built from
  the primary context.

Places are non-owning: the caller must keep the context alive while the
place is in use.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@copy-pr-bot

copy-pr-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

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.

@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Progress in CCCL Jul 10, 2026
Comment thread c/experimental/stf/src/stf.cu Outdated
@caugonnet

Copy link
Copy Markdown
Contributor Author

/ok to test 4e54fc8

A null context with an explicit dev_id would construct a place that only
misbehaves at activation time (cuCtxSetCurrent(nullptr) succeeds and
unbinds the current context). Reject it eagerly instead:

- exec_place::cuda_context throws std::invalid_argument (works in
  release builds, matching the logic_error throws elsewhere in places).
- The C API asserts in debug builds; in release the exception is mapped
  to a null handle with a stderr trace by stf_try_allocate, like every
  other failure on this surface.
- Header unittest for the rejection; the C API test already covers the
  null-handle return.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@caugonnet

Copy link
Copy Markdown
Contributor Author

/ok to test 94c2461

@copy-pr-bot

copy-pr-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

/ok to test 94c2461

@caugonnet, there was an error processing your request: E2

See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/2/

@caugonnet

Copy link
Copy Markdown
Contributor Author

/ok to test 94d2461

@github-actions

This comment has been minimized.

@caugonnet caugonnet self-assigned this Jul 10, 2026
@caugonnet caugonnet added the stf Sequential Task Flow programming model label Jul 10, 2026
@caugonnet
caugonnet marked this pull request as ready for review July 15, 2026 10:45
@caugonnet
caugonnet requested review from a team as code owners July 15, 2026 10:45
@cccl-authenticator-app cccl-authenticator-app Bot moved this from In Progress to In Review in CCCL Jul 15, 2026
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 069835e6-5d03-47b0-b071-f79fbb0604a8

📥 Commits

Reviewing files that changed from the base of the PR and between 8c3845b and 592057b.

📒 Files selected for processing (1)
  • cudax/include/cuda/experimental/__places/exec/cuda_context.cuh
🚧 Files skipped from review as they are similar to previous changes (1)
  • cudax/include/cuda/experimental/__places/exec/cuda_context.cuh

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • Added a CUDASTF C API entry point to create a non-owning execution-place handle from an externally owned CUDA CUcontext.
    • Added a public exec_place::cuda_context factory (optional device id, configurable stream-pool size).
  • Refactor
    • Updated green-context execution places to use the shared externally-owned CUDA context mechanism, including consistent equality behavior.
  • Tests
    • Added/extended coverage for null handling, device selection/derivation, identity/equality, CUDA stream usage, and activation/deactivation round-trips.
  • Documentation
    • Documented exec_place::cuda_context, including non-owning lifetime requirements for the provided CUcontext.

Walkthrough

Changes

Adds non-owning CUDA CUcontext execution places with activation, device derivation, identity, and stream-pool support. Green contexts delegate to this implementation, and the C STF API exposes the factory with creation and task-execution tests.

CUDA context place core

Layer / File(s) Summary
CUDA context place contract and implementation
cudax/include/cuda/experimental/..., cudax/test/places/CMakeLists.txt, docs/cudax/places.rst
Adds the public factory and context-backed implementation, including context switching, device resolution, identity, stream pools, unit tests, header wiring, test registration, and documentation.
Green-context delegation
cudax/include/cuda/experimental/__places/exec/green_context.cuh
Constructs green-context places through the CUDA context implementation and verifies equality for matching context partitions.
C STF API integration
c/experimental/stf/include/.../stf.h, c/experimental/stf/src/stf.cu, c/experimental/stf/test/test_places.cpp
Adds the C factory, maps allocation failures to null handles, and tests context-derived/device-associated task execution.

Suggested reviewers: robertmaynard, andralex, griwes, wmaxey


Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 4

🧹 Nitpick comments (1)
c/experimental/stf/test/test_places.cpp (1)

46-85: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

suggestion: Rename N to a snake_case descriptive name and declare it constexpr with uniform initialization, for example constexpr size_t element_count{1024}. Also make handles and pointers that are never reassigned const, including place_derived, place, ctx, lX, t, stream, and the dX pointer itself.

As per coding guidelines, “All variables that are not modified must be declared const” and compile-time values must be constexpr.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 02ec1c61-707d-4bda-9cf3-c13c30836dd0

📥 Commits

Reviewing files that changed from the base of the PR and between 401f8d3 and 3a46f4e.

📒 Files selected for processing (9)
  • c/experimental/stf/include/cccl/c/experimental/stf/stf.h
  • c/experimental/stf/src/stf.cu
  • c/experimental/stf/test/test_places.cpp
  • cudax/include/cuda/experimental/__places/exec/cuda_context.cuh
  • cudax/include/cuda/experimental/__places/exec/green_context.cuh
  • cudax/include/cuda/experimental/__places/places.cuh
  • cudax/include/cuda/experimental/places.cuh
  • cudax/include/cuda/experimental/stf.cuh
  • cudax/test/places/CMakeLists.txt

Comment thread c/experimental/stf/src/stf.cu
Comment thread cudax/include/cuda/experimental/__places/exec/cuda_context.cuh Outdated
Comment thread cudax/include/cuda/experimental/__places/exec/cuda_context.cuh
Comment thread cudax/include/cuda/experimental/__places/exec/cuda_context.cuh Outdated
Use the existing context guard so device lookup restores the caller's context when querying the device fails.
Keep context identity and device affinity consistent, preserve C API error translation, and use portable context ordering.
@caugonnet

Copy link
Copy Markdown
Contributor Author

Also addressed the review-summary style suggestion in 55a49cc: the element count is now constexpr with a descriptive name, and unchanged handles/pointers are const-qualified.

@caugonnet

Copy link
Copy Markdown
Contributor Author

/ok to test ea1e817

Comment thread cudax/include/cuda/experimental/__places/places.cuh
Comment thread cudax/include/cuda/experimental/__places/exec/cuda_context.cuh Outdated
List the context factory with the other execution-place factories and clarify its non-owning lifetime contract.
@caugonnet
caugonnet requested a review from a team as a code owner July 15, 2026 15:17
@caugonnet
caugonnet requested a review from griwes July 15, 2026 15:17

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 120dcbc3-9d6d-4640-8844-577caaa69e82

📥 Commits

Reviewing files that changed from the base of the PR and between 55a49cc and 8c3845b.

📒 Files selected for processing (1)
  • docs/cudax/places.rst

Comment thread docs/cudax/places.rst
Comment on lines +37 to +49
- ``exec_place::cuda_context(ctx, devid)`` -- an externally-owned CUDA driver
context; the device ordinal is derived from the context when ``devid`` is
omitted

When an execution place is activated, it sets the appropriate CUDA context
(e.g. calls ``cudaSetDevice``). Each execution place also has an *affine*
data place: the memory location naturally associated with it. For a device
execution place the affine data place is the device's global memory; for
the host it is pinned host memory (RAM).

A CUDA-context execution place is non-owning. The caller must keep the
``CUcontext`` alive while the place and any streams obtained from it are in use.

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.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

important: Document CUDA-context activation and stream ownership consistently.

The new factory is listed here, but the later “Behavior by execution place type” and stream-management sections omit CUDA-context places. Add that they activate via the driver context and use their own embedded stream pool, so readers do not infer device-place registry semantics.

As per path instructions, documentation changes must focus on technical accuracy, buildable examples, API/version consistency, and migration impact.

Source: Path instructions

caugonnet and others added 2 commits July 15, 2026 17:52
Keep only the configuration header before the system-header pragma block, matching cudax header conventions.
@caugonnet

Copy link
Copy Markdown
Contributor Author

/ok to test a669f4d

@caugonnet
caugonnet enabled auto-merge (squash) July 15, 2026 15:59
@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 1h 05m: Pass: 100%/61 | Total: 1d 02h | Max: 1h 05m | Hits: 15%/178068

See results here.

@caugonnet
caugonnet merged commit 01457b3 into NVIDIA:main Jul 15, 2026
80 of 82 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stf Sequential Task Flow programming model

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants