Skip to content

[CUB] Refactor DevicePartition::If to always take an environment - #9464

Merged
bernhardmgruber merged 2 commits into
mainfrom
device_partition_flagged_env
Jul 16, 2026
Merged

bernhardmgruber merged 2 commits into
mainfrom
device_partition_flagged_env

Conversation

@miscco

@miscco miscco commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Description

closes

Checklist

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

@copy-pr-bot

copy-pr-bot Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

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 Jun 15, 2026
@miscco
miscco force-pushed the device_partition_flagged_env branch from 89ae2b2 to b6d1633 Compare June 15, 2026 09:51
@miscco
miscco force-pushed the device_partition_flagged_env branch 2 times, most recently from 48a510a to bd7b7d9 Compare June 16, 2026 10:49
@miscco
miscco force-pushed the device_partition_flagged_env branch from bd7b7d9 to 1fd1bb1 Compare June 22, 2026 10:17
@copy-pr-bot

copy-pr-bot Bot commented Jun 22, 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.

@miscco
miscco marked this pull request as ready for review June 22, 2026 10:19
@miscco
miscco requested review from a team as code owners June 22, 2026 10:19
@cccl-authenticator-app cccl-authenticator-app Bot moved this from In Progress to In Review in CCCL Jun 22, 2026
@miscco
miscco force-pushed the device_partition_flagged_env branch from 1fd1bb1 to 459699b Compare June 22, 2026 11:15
@miscco
miscco force-pushed the device_partition_flagged_env branch from 459699b to 4e582bc Compare June 22, 2026 11:20
@github-actions

This comment has been minimized.

@miscco
miscco force-pushed the device_partition_flagged_env branch from 4e582bc to 1abbfcf Compare June 23, 2026 07:21
@miscco
miscco force-pushed the device_partition_flagged_env branch from 1abbfcf to d1954a8 Compare June 23, 2026 08:46

@Jacobfaib Jacobfaib 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.

libcu++ LGTM


// Copy the result back from storage
_CCCL_TRY_CUDA_API(
::cudaMemcpyAsync,

@Jacobfaib Jacobfaib Jun 23, 2026 •

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.

Unrelated to the current PR, but we should try and use the driver wherever possible so that we are explicit about the CUDA contexts. Especially for multi-GPU, the fact that runtime API leaves its the context on the stack is very problematic because some APIs (like cuMemcpy() actually) will behave differently if there is a context on the stack already.

@github-actions

This comment has been minimized.

@miscco
miscco force-pushed the device_partition_flagged_env branch from d200e8a to c381575 Compare July 2, 2026 09:17
@github-actions

This comment has been minimized.

@miscco
miscco force-pushed the device_partition_flagged_env branch from c381575 to b858f79 Compare July 2, 2026 18:49
@github-actions

This comment has been minimized.

@miscco
miscco force-pushed the device_partition_flagged_env branch from b858f79 to 6c749a9 Compare July 3, 2026 07:22
@github-actions

This comment has been minimized.

@bernhardmgruber
bernhardmgruber requested a review from a team as a code owner July 15, 2026 17:10
@bernhardmgruber
bernhardmgruber requested a review from elstehle July 15, 2026 17:10
Base automatically changed from device_partition_flagged to main July 15, 2026 20:39
We want to be able to pass tunings to the APIs that take user provided memory

Make sure we can pass any environment or stream type to them
@bernhardmgruber
bernhardmgruber force-pushed the device_partition_flagged_env branch from 6c749a9 to 66eeb73 Compare July 15, 2026 20:41
@coderabbitai

coderabbitai Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

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: 64e16e1a-62e1-479f-b7f4-258461c01516

📥 Commits

Reviewing files that changed from the base of the PR and between 66eeb73 and 2bdc1a8.

📒 Files selected for processing (2)
  • cub/cub/device/device_partition.cuh
  • cub/test/catch2_test_device_partition_if.cu
🚧 Files skipped from review as they are similar to previous changes (2)
  • cub/test/catch2_test_device_partition_if.cu
  • cub/cub/device/device_partition.cuh

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • Extended device partition operations to accept an execution environment parameter (env) for both temp-storage sizing and kernel execution.
  • Bug Fixes
    • Updated partition, partition-copy, and stable-partition device backends to pass through the execution policy/environment consistently during CUB invocations.
  • Tests
    • Added a test covering user-provided temporary storage plus correctness across multiple execution environment types (including stream and GPU policy variants).

Walkthrough

Changes

DevicePartition::If now accepts execution environments for temporary-storage and execution overloads, derives policies through environment-aware tuning, and receives propagated policies from CUDA PSTL partition algorithms. Tests cover user-provided storage and multiple environment configurations.

DevicePartition environment support

Layer / File(s) Summary
Environment-aware partition API
cub/cub/device/device_partition.cuh
Single-select and three-way DevicePartition::If overloads accept const environment references and use environment-based tuning dispatch.
PSTL policy propagation
libcudacxx/include/cuda/std/__pstl/cuda/partition.h, libcudacxx/include/cuda/std/__pstl/cuda/partition_copy.h, libcudacxx/include/cuda/std/__pstl/cuda/stable_partition.h
CUDA PSTL partition operations pass __policy to partition and transform calls for storage sizing and kernel execution.
Environment configuration coverage
cub/test/catch2_test_device_partition_if.cu
Tests validate temporary-storage sizing, execution status, output correctness, and multiple stream and CUDA execution environment forms.

Possibly related PRs

  • NVIDIA/cccl#9463: Applies the same environment-based API refactor to DevicePartition::Flagged.

Suggested reviewers: elstehle, pciolkosz, bernhardmgruber


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: 2

🧹 Nitpick comments (1)
cub/test/catch2_test_device_partition_if.cu (1)

186-215: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

important: declare immutable local bindings const. le, d_first_num_selected_out, temp_storage, and test_partition_if are never reassigned. As per coding guidelines, “All variables that are not modified must be declared const.”

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: e415d8ba-c558-4809-aed6-c7b4ef95d496

📥 Commits

Reviewing files that changed from the base of the PR and between 8ce2dd4 and 66eeb73.

📒 Files selected for processing (5)
  • cub/cub/device/device_partition.cuh
  • cub/test/catch2_test_device_partition_if.cu
  • libcudacxx/include/cuda/std/__pstl/cuda/partition.h
  • libcudacxx/include/cuda/std/__pstl/cuda/partition_copy.h
  • libcudacxx/include/cuda/std/__pstl/cuda/stable_partition.h

Comment thread cub/cub/device/device_partition.cuh Resolved
Comment on lines +175 to +275
#if TEST_LAUNCH == 0
C2H_TEST("DevicePartition::If works with user provided memory and environment", "[device][partition_if]", types)
{
using type = typename c2h::get<0, TestType>;

const int num_items = GENERATE_COPY(take(2, random(1, 1000000)));
c2h::device_vector<type> in(num_items, thrust::default_init);
c2h::device_vector<type> out(num_items, thrust::default_init);
c2h::gen(C2H_SEED(2), in);

// just pick one of the input elements as boundary
less_than_t<type> le{in[num_items / 2]};

// Needs to be device accessible
c2h::device_vector<int> num_selected_out(1, 0);
int* d_first_num_selected_out = thrust::raw_pointer_cast(num_selected_out.data());

// Ensure that we create the same output as std
c2h::host_vector<type> reference = in;
// The main difference between stable_partition and DevicePartition::If is that the false partition is in reverse
// order
const auto boundary = std::stable_partition(reference.begin(), reference.end(), le);
std::reverse(boundary, reference.end());

size_t expected_allocation_size = 0;
auto error = cub::DevicePartition::If(
static_cast<void*>(nullptr),
expected_allocation_size,
in.begin(),
out.begin(),
d_first_num_selected_out,
num_items,
le);
REQUIRE(error == cudaSuccess);
REQUIRE(cudaSuccess == cudaPeekAtLastError());
REQUIRE(cudaSuccess == cudaDeviceSynchronize());

auto d_temp = c2h::device_vector<uint8_t>(expected_allocation_size, thrust::no_init);
void* temp_storage = thrust::raw_pointer_cast(d_temp.data());

auto test_partition_if = [&](const auto& env) {
size_t num_bytes = 0;
error = cub::DevicePartition::If(
static_cast<void*>(nullptr), num_bytes, in.begin(), out.begin(), d_first_num_selected_out, num_items, le, env);
REQUIRE(error == cudaSuccess);
REQUIRE(cudaSuccess == cudaPeekAtLastError());
REQUIRE(cudaSuccess == cudaDeviceSynchronize());
REQUIRE(expected_allocation_size == num_bytes);

error = cub::DevicePartition::If(
temp_storage, num_bytes, in.begin(), out.begin(), d_first_num_selected_out, num_items, le, env);
REQUIRE(error == cudaSuccess);
REQUIRE(cudaSuccess == cudaPeekAtLastError());
REQUIRE(cudaSuccess == cudaDeviceSynchronize());

REQUIRE(num_selected_out[0] == cuda::std::distance(reference.begin(), boundary));
REQUIRE(reference == out);
};

int current_device;
error = cudaGetDevice(&current_device);
REQUIRE(error == cudaSuccess);

SECTION("DevicePartition::If works with cudaStream_t")
{
cuda::stream stream{cuda::devices[current_device]};
test_partition_if(stream.get());
}

SECTION("DevicePartition::If works with cuda::stream")
{
cuda::stream stream{cuda::devices[current_device]};
test_partition_if(stream);
}

SECTION("DevicePartition::If works with cuda::stream_ref")
{
cuda::stream stream{cuda::devices[current_device]};
cuda::stream_ref stream_ref{stream};
test_partition_if(stream_ref);
}

SECTION("DevicePartition::If works with cuda::std::execution::env")
{
cuda::std::execution::env env{};
test_partition_if(env);
}

SECTION("DevicePartition::If works with cuda::execution::gpu")
{
const auto policy = cuda::execution::gpu;
test_partition_if(policy);
}

SECTION("DevicePartition::If works with cuda::execution::gpu with stream")
{
cuda::stream stream{cuda::devices[current_device]};
const auto policy = cuda::execution::gpu.with(cuda::get_stream, stream);
test_partition_if(policy);
}
}

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.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

important: add a user-provided-storage case with a custom PartitionPolicy selector or tuning environment. These sections only vary stream/environment representations, so they do not exercise the tuning contract introduced by this PR or catch a selector-type regression in the temporary-storage overload. As per path instructions, CUB reviews must focus on algorithm correctness and test coverage.

Source: Path instructions

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 10h 09m: Pass: 100%/345 | Total: 8d 09h | Max: 1h 33m | Hits: 66%/786879

See results here.

@bernhardmgruber
bernhardmgruber merged commit cc0282d into main Jul 16, 2026
724 of 727 checks passed
@bernhardmgruber
bernhardmgruber deleted the device_partition_flagged_env branch July 16, 2026 12:45
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Jul 16, 2026
davebayer pushed a commit to davebayer/cccl that referenced this pull request Aug 4, 2026
…VIDIA#9464)

We want to be able to pass tunings to the APIs that take user provided memory

Make sure we can pass any environment or stream type to them

Co-authored-by: Bernhard Manfred Gruber <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants