Skip to content

[Thrust] Support fill and device_vector for types larger than the kernel parameter limit - #11756

Open
SamMausberg wants to merge 1 commit into
NVIDIA:mainfrom
SamMausberg:fix/thrust-fill-large-value
Open

SamMausberg wants to merge 1 commit into
NVIDIA:mainfrom
SamMausberg:fix/thrust-fill-large-value

Conversation

@SamMausberg

@SamMausberg SamMausberg commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

closes #2777

thrust::device_vector<T> v(n) does not compile when T does not fit into the kernel parameter space, e.g. with a 40000-byte T:

cub/cub/device/dispatch/kernels/kernel_transform.cuh(1223): Error: Formal parameter space overflowed (40022 bytes required, max 32764 bytes allowed)

The CUDA backend of thrust::fill and thrust::uninitialized_fill passes the fill value to the kernel inside the function object, and the device_vector constructors, resize, assign, insert and push_back go through them. The parameter space is 32764 bytes since PTX ISA 8.1 (CTK 12.1) and 4096 bytes before, so with CTK 12.0 a 5000-byte type already fails.

With this change, a value larger than that limit minus 256 bytes (room for the iterator and item count) is copied to temporary device memory allocated through the execution policy, and a parallel_for assigns or copy-constructs each element from that copy. The limit is taken from __cccl_ptx_isa, which under clang-cuda also reflects the PTX version clang can emit. The value is copied with the synchronous host-to-device copy that cross-system thrust::copy also uses. For these types, which never compiled before, this means that under par_nosync the fill synchronizes the stream once, and that it cannot be captured into a CUDA graph.

Smaller values keep their code path; the SASS of the existing fill, uninitialized_fill, uninitialized_fill_n and vector_insert tests is identical. Among types that compile today, only 32509 to about 32738 bytes (CTK 12.1+) and 3841 to about 4074 bytes (CTK 12.0) move to the new path; the exact upper end depends on the output iterator.

Not addressed: count, find, replace, remove with such a value, reduce/exclusive_scan with such an init, and cub::DeviceTransform::Fill itself. Related but not fixed here: for power-of-two value sizes, DeviceTransform's vectorized fill kernel reserves local memory proportional to the value size (17 GiB after one fill with 4 KiB values on a GH200); reported in #11758.

Tests

  • vector.cu: TestVectorContainingLargeType also runs with a 40000-byte type and additionally compares resize, insert and assign against host_vector.
  • cuda/fill.cu: fill with a 40000-byte value through par_nosync.on(stream).
  • cuda/uninitialized_fill.cu: a 40000-byte, non-trivially copyable type; checks the data and that every element was copy-constructed.

All three fail to compile on main and pass with the change. Calls from device code take Thrust's sequential fallback, like smaller values; a kernel in cuda/uninitialized_fill.cu compile-checks that path but is never launched, because its 120 KB of stack per thread would reserve about 30 GiB of local memory on a GH200.

On a GH200 (sm_90):

  • CTK 12.8, g++ 11.4 (thrust-cpp17) and clang++ 15 (thrust-cpp20): built and passed thrust.cpp.cuda.test.{fill, uninitialized_fill, uninitialized_fill_n, vector, vector_insert, vector_manipulation, vector_allocators, device_delete, memory, allocator, allocator_aware_policies, device_reference}, thrust.cpp.cuda.test.cuda.{fill, uninitialized_fill, memory}.cdp_{0,1}, thrust.cpp.{omp,tbb}.test.vector, and the header tests of the public headers that include the changed files.
  • CTK 12.0.1, g++ 11.4: the three changed test files built and passed.
  • compute-sanitizer memcheck and initcheck on the new tests: no errors. The kernels on the new path use no stack, and a fill with 40000-byte values leaves free device memory unchanged.
  • thrust::fill with thrust::device over 1 GiB of 40000-byte elements: 7.2 ms per call (149 GB/s; CUDA events, median of 10 calls). Copying words instead of whole elements per thread reached 2.9 TB/s in a quick experiment; that could be a follow-up.

Not tested: MSVC, NVHPC, clang as CUDA compiler, CTK 13, architectures other than sm_90.

Checklist

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

The CUDA backend of thrust::fill and thrust::uninitialized_fill, and the
device_vector constructors and members built on them, pass the fill value
to the kernel inside the function object. When the value does not fit
into the kernel parameter space (32764 bytes since PTX ISA 8.1 / CTK 12.1,
4096 bytes before), this fails to compile, for example with
thrust::device_vector<T>(n) and a 40000-byte T ("Formal parameter space
overflowed").

Such values are now copied to temporary device memory allocated through
the execution policy, and a parallel_for assigns or copy-constructs the
elements from that copy. The value is copied with the synchronous
host-to-device copy that cross-system thrust::copy also uses, so under
par_nosync the fill synchronizes the stream once, and it cannot be
captured into a CUDA graph. Values up to the parameter limit minus a
256-byte margin for the other kernel parameters keep the existing code
paths.
@SamMausberg
SamMausberg requested a review from a team as a code owner September 29, 2026 23:28
@copy-pr-bot

copy-pr-bot Bot commented Sep 29, 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.

@coderabbitai

coderabbitai Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

📝 Summary

Summary by CodeRabbit

  • New Features
    • CUDA fill operations now support large values that exceed kernel parameter limits, including non-trivially-copyable values in uninitialized fills.
  • Bug Fixes
    • Improved handling of large values in CUDA vector operations, including comparisons after resizing, inserting, and assigning repeated values.

Walkthrough

CUDA fill operations use temporary device storage when values exceed kernel parameter limits. Uninitialized fill adds a device-memory construction path for large nontrivial values. Tests cover large-value filling and vector operations.

Changes

Large-value fill support

Layer / File(s) Summary
CUDA fill path for large values
thrust/thrust/system/cuda/detail/fill.h, thrust/testing/cuda/fill.cu
CUDA fill dispatches large values through temporary device storage. A stream test fills and checks three elements using a large array value.
Large-value uninitialized fill
thrust/thrust/system/cuda/detail/uninitialized_fill.h, thrust/testing/cuda/uninitialized_fill.cu
Uninitialized fill adds a pointer-based construction path for large nontrivial values. Tests check copy construction and include a compile-only kernel instantiation.
Large-value vector operations
thrust/testing/vector.cu
The templated test checks two element sizes and compares host and device vectors after resizing, insertion, and assignment.

Assessment against linked issues

Objective Addressed Explanation
[#2777] Allow thrust::device_vector to be used with large element types without fill-kernel parameter-space compile errors. ✅

Suggested reviewers: bernhardmgruber

Priority: ➖ Normal

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: 🟡 Moderate · up to 35dbe

Large-value fills using par_nosync with a caching allocator can produce incorrect results or illegal memory accesses. Preserve the temporary value until the kernel completes before merging.


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


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: NVIDIA/cccl/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 478789d3-5cbe-4f3e-b33a-a170856121c1

📥 Commits

Reviewing files that changed from the base of the PR and between 038f78b and 35dbe4f.

📒 Files selected for processing (5)
  • thrust/testing/cuda/fill.cu
  • thrust/testing/cuda/uninitialized_fill.cu
  • thrust/testing/vector.cu
  • thrust/thrust/system/cuda/detail/fill.h
  • thrust/thrust/system/cuda/detail/uninitialized_fill.h

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.

Comment thread thrust/thrust/system/cuda/detail/fill.h Resolved
@bernhardmgruber

Copy link
Copy Markdown
Contributor

@SamMausberg do you have a realistic use case for supporting such large data types? I hardly believe our other algorithms would work, if we can't pass such a value to a kernel by value, so I question the usefulness of a vector holding such values, if we can't run algorithms on it.

@SamMausberg

SamMausberg commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

@SamMausberg do you have a realistic use case for supporting such large data types? I hardly believe our other algorithms would work, if we can't pass such a value to a kernel by value, so I question the usefulness of a vector holding such values, if we can't run algorithms on it.

Please don’t feel inclined to take any of my PRs if you don’t think they’re useful. I really don’t mind.

I was thinking device_vector could own storage for large records. You could then reference those elements from custom kernels and not need to ever pass T by value. But to your point then large types aren't generally useable across Thrust. Would you like me to tweak it in any way or look at something else instead?

Edit: never mind, I see your suggestion posted on the issue. Working on it now.

This branch has not been deployed

No deployments
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.

[BUG]: thrust::device_vector does not compile with large element type

2 participants