Skip to content

[thrust] Use CCCL Runtime in Thrust set operation tests#9551

Open
pciolkosz wants to merge 2 commits into
NVIDIA:mainfrom
pciolkosz:adopt-cccl-runtime-2800-set1
Open

[thrust] Use CCCL Runtime in Thrust set operation tests#9551
pciolkosz wants to merge 2 commits into
NVIDIA:mainfrom
pciolkosz:adopt-cccl-runtime-2800-set1

Conversation

@pciolkosz

Copy link
Copy Markdown
Contributor

Part of #2800

This PR is first on a series to use cccl runtime APIs in other CCCL tests as a form of dog fooding. The first PR is an arbitrary set of thrust tests to mostly gather feedback before I do more changes.

The test framework uses current device setting, which I believe is also needed for thrust. I don't think we can rework the tests to use an explicit device, even if that would make more sense with cccl runtime APIs.

@pciolkosz pciolkosz requested a review from a team as a code owner June 23, 2026 04:29
@pciolkosz pciolkosz requested a review from elstehle June 23, 2026 04:29
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jun 23, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Jun 23, 2026
@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note: CodeRabbit is enabled on this repository as a convenience for maintainers
and contributors. Use your best judgment when considering its review comments and
suggestions — a suggested change may be inadequate, unnecessary, or safe to ignore.
Contributors are not expected to address every comment. Human reviews are what
ultimately matter for merging.

Overview

This PR refactors Thrust set operation tests to use CCCL Runtime APIs instead of raw CUDA stream management and thrust::device_vector-based handling. This serves as dogfooding to validate the runtime APIs with real-world test use cases as part of a broader initiative to incorporate CCCL runtime APIs across the test suite.

Summary of Changes

New Testing Helper Header

A new helper header thrust/testing/cuda/cccl_runtime_test_helper.cuh introduces reusable runtime assertion utilities:

  • test_runtime::current_test_device() — queries the active CUDA device
  • test_runtime::single_thread_config() — produces a 1×1 launch configuration
  • test_runtime::assert_device(...) — device-side assertion that prints a failure message and executes __trap() on failure
  • test_runtime::assert_equal(...) — host-side utility that copies a device buffer to host memory, synchronizes the stream, and compares against expected values
  • TEST_ASSERT_DEVICE macro — wraps device-side assertions with source location information

Set Operation Test Refactoring

All six set operation test files follow a consistent refactoring pattern:

Device-side tests (THRUST_TEST_DEVICE_SIDE):

  • Replaced __global__ kernels with device-callable functors
  • Moved iterator-end validation from a separate device buffer (end_vec) read on the host to device-side assertions within the functor
  • Updated allocations to use cuda::make_device_buffer instead of thrust::device_vector
  • Replaced cudaDeviceSynchronize() with stream.sync()

CUDA streams tests:

  • Replaced manual cudaStreamCreate/cudaStreamDestroy with cuda::stream RAII wrapper
  • Updated buffer allocations to cuda::make_device_buffer
  • Replaced explicit stream synchronization and host-side reference vector comparisons with stream-aware test_runtime::assert_equal checks
  • Bound execution policies to the stream via thrust::cuda::par.on(stream.get())

Files updated:

  • set_difference.cu (30 lines added, 41 removed)
  • set_difference_by_key.cu (75 added, 73 removed) — kernel declaration changed from __global__ to struct with __device__ operator()
  • set_symmetric_difference.cu (30 added, 41 removed)
  • set_symmetric_difference_by_key.cu (76 added, 72 removed) — kernel declaration changed similarly
  • set_union.cu (30 added, 39 removed)
  • set_union_by_key.cu (75 added, 71 removed)

Implementation Notes

The refactoring maintains the existing requirement that the test framework relies on current device settings. The author notes that reworking tests to use explicit device specifications—more aligned with CCCL runtime API best practices—is not currently feasible within existing test framework constraints. This PR establishes a pattern for future test updates as part of a planned series of PRs addressing this initiative.

Walkthrough

Adds thrust/testing/cuda/cccl_runtime_test_helper.cuh with test_runtime utilities (current_test_device, single_thread_config, assert_device, assert_equal, TEST_ASSERT_DEVICE) and applies them across six CUDA set-operation test files, replacing raw cudaStream_t lifecycle, __global__ kernels with end_vec patterns, and thrust::device_vector reference comparisons with cuda::stream RAII, cuda::make_device_buffer, cuda::launch device functors, and stream-synchronized assertions.

Changes

CCCL Set-Op Test Refactor

Layer / File(s) Summary
CCCL runtime test helper header
thrust/testing/cuda/cccl_runtime_test_helper.cuh
New header defining test_runtime::current_test_device, single_thread_config, device-side assert_device (with __trap), host-side assert_equal (copies device buffer to host, synchronizes, compares against initializer_list<int>), and the TEST_ASSERT_DEVICE macro.
Device-side functor + host harness refactor (all 6 set ops)
thrust/testing/cuda/set_difference.cu, thrust/testing/cuda/set_difference_by_key.cu, thrust/testing/cuda/set_symmetric_difference.cu, thrust/testing/cuda/set_symmetric_difference_by_key.cu, thrust/testing/cuda/set_union.cu, thrust/testing/cuda/set_union_by_key.cu
__global__ kernels writing to end_vec device buffers and cudaDeviceSynchronize removed. Replaced with device functor structs whose operator() calls thrust::set_op directly and validates the returned end iterator via TEST_ASSERT_DEVICE. Host harnesses use cuda::make_device_buffer, cuda::launch with single_thread_config, stream.sync(), and test_runtime::assert_equal.
CUDA streams test refactor (all 6 set ops)
thrust/testing/cuda/set_difference.cu, thrust/testing/cuda/set_difference_by_key.cu, thrust/testing/cuda/set_symmetric_difference.cu, thrust/testing/cuda/set_symmetric_difference_by_key.cu, thrust/testing/cuda/set_union.cu, thrust/testing/cuda/set_union_by_key.cu
Manual cudaStreamCreate/cudaStreamSynchronize/cudaStreamDestroy and thrust::device_vector reference vectors replaced with cuda::stream RAII, cuda::make_device_buffer, thrust::cuda::par.on(stream.get()), and test_runtime::assert_equal for final value verification.

Suggested reviewers

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

🧹 Nitpick comments (6)
thrust/testing/cuda/set_difference.cu (1)

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

suggestion: Use angle brackets for the helper include to match the project include rule.

-#include "cccl_runtime_test_helper.h"
+#include <cuda/cccl_runtime_test_helper.h>

As per coding guidelines, "All header inclusions must use the syntax <header>."

Source: Coding guidelines

thrust/testing/cuda/set_difference_by_key.cu (1)

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

suggestion: Use angle brackets for the helper include to match the project include rule.

-#include "cccl_runtime_test_helper.h"
+#include <cuda/cccl_runtime_test_helper.h>

As per coding guidelines, "All header inclusions must use the syntax <header>."

Source: Coding guidelines

thrust/testing/cuda/set_symmetric_difference.cu (1)

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

suggestion: Use angle brackets for the helper include to match the project include rule.

-#include "cccl_runtime_test_helper.h"
+#include <cuda/cccl_runtime_test_helper.h>

As per coding guidelines, "All header inclusions must use the syntax <header>."

Source: Coding guidelines

thrust/testing/cuda/set_symmetric_difference_by_key.cu (1)

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

suggestion: Use angle brackets for the helper include to match the project include rule.

-#include "cccl_runtime_test_helper.h"
+#include <cuda/cccl_runtime_test_helper.h>

As per coding guidelines, "All header inclusions must use the syntax <header>."

Source: Coding guidelines

thrust/testing/cuda/set_union.cu (1)

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

suggestion: Use angle brackets for the helper include to match the project include rule.

-#include "cccl_runtime_test_helper.h"
+#include <cuda/cccl_runtime_test_helper.h>

As per coding guidelines, "All header inclusions must use the syntax <header>."

Source: Coding guidelines

thrust/testing/cuda/set_union_by_key.cu (1)

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

suggestion: Use angle brackets for the helper include to match the project include rule.

-#include "cccl_runtime_test_helper.h"
+#include <cuda/cccl_runtime_test_helper.h>

As per coding guidelines, "All header inclusions must use the syntax <header>."

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 8d474be3-8b07-4e01-9de2-e9a08aaeaf38

📥 Commits

Reviewing files that changed from the base of the PR and between f150d51 and 8e7661e.

📒 Files selected for processing (7)
  • thrust/testing/cuda/cccl_runtime_test_helper.h
  • thrust/testing/cuda/set_difference.cu
  • thrust/testing/cuda/set_difference_by_key.cu
  • thrust/testing/cuda/set_symmetric_difference.cu
  • thrust/testing/cuda/set_symmetric_difference_by_key.cu
  • thrust/testing/cuda/set_union.cu
  • thrust/testing/cuda/set_union_by_key.cu

Comment thread thrust/testing/cuda/cccl_runtime_test_helper.cuh
Comment on lines +20 to +54
inline cuda::device_ref current_test_device()
{
int device = 0;
ASSERT_EQUAL(cudaSuccess, cudaGetDevice(&device));
return cuda::device_ref{device};
}

inline auto single_thread_config()
{
return cuda::make_config(cuda::make_hierarchy(cuda::grid_dims(1), cuda::block_dims<1>()));
}

__device__ inline void assert_device(bool condition, const char* expression, const char* file, int line)
{
if (!condition)
{
printf("Device assertion failed: %s (%s:%d)\n", expression, file, line);
__trap();
}
}

template <typename Buffer>
void assert_equal(cuda::stream_ref stream, Buffer& buffer, cuda::std::initializer_list<int> expected)
{
std::vector<int> actual(buffer.size());
cuda::copy_bytes(stream, buffer, actual);
stream.sync();

ASSERT_EQUAL(actual.size(), expected.size());

for (cuda::std::size_t i = 0; i < expected.size(); ++i)
{
ASSERT_EQUAL(expected.begin()[i], actual[i]);
}
}

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 | 🟠 Major | ⚡ Quick win

important: these functions are missing required CCCL API/noexcept annotations for header code. mark each function with the appropriate _CCCL_*_API macro (_CCCL_HOST_API for host helpers, _CCCL_DEVICE_API for assert_device) and add noexcept where applicable.

As per coding guidelines, **/*.{h,hpp,cu,cpp,cuh}: “Functions must be marked with one of: _CCCL_HOST_API, _CCCL_DEVICE_API, _CCCL_HOST_DEVICE_API, _CCCL_TILE_API, or _CCCL_API” and “Functions that do not throw exceptions must use noexcept.”

Source: Coding guidelines

@github-actions

This comment has been minimized.

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

I love this

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.

Question: Why isnt this a .cuh file?

#include <cstdio>
#include <vector>

namespace test

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.

Question: Could we name this like test_runtime? test is just so overloaded

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
thrust/testing/cuda/cccl_runtime_test_helper.cuh (1)

24-43: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

important: add inline to the header-defined non-template functions at Line 24, Line 31, and Line 36. In this header form, missing inline can produce ODR/multiple-definition failures across test translation units.

As per coding guidelines, **/*.{h,hpp,cu,cpp,cuh}: “Non-template, non-constexpr functions must use inline.”

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: a147b50e-f88d-41c9-a4b5-ba8a3de15e07

📥 Commits

Reviewing files that changed from the base of the PR and between 8e7661e and 2648d61.

📒 Files selected for processing (7)
  • thrust/testing/cuda/cccl_runtime_test_helper.cuh
  • thrust/testing/cuda/set_difference.cu
  • thrust/testing/cuda/set_difference_by_key.cu
  • thrust/testing/cuda/set_symmetric_difference.cu
  • thrust/testing/cuda/set_symmetric_difference_by_key.cu
  • thrust/testing/cuda/set_union.cu
  • thrust/testing/cuda/set_union_by_key.cu
🚧 Files skipped from review as they are similar to previous changes (6)
  • thrust/testing/cuda/set_difference.cu
  • thrust/testing/cuda/set_union.cu
  • thrust/testing/cuda/set_union_by_key.cu
  • thrust/testing/cuda/set_symmetric_difference.cu
  • thrust/testing/cuda/set_symmetric_difference_by_key.cu
  • thrust/testing/cuda/set_difference_by_key.cu

@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 54m 53s: Pass: 100%/70 | Total: 22h 42m | Max: 54m 36s | Hits: 93%/147545

See results here.

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

Labels

None yet

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

2 participants