Repository navigation
[Thrust] Support fill and device_vector for types larger than the kernel parameter limit - #11756
SamMausberg wants to merge 1 commit into
Conversation
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.
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. 📝 SummarySummary by CodeRabbit
WalkthroughCUDA 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. ChangesLarge-value fill support
Assessment against linked issues
Suggested reviewers: Priority: ➖ Normal Change: Bug fix · Severity of issue fixed: Medium Merge Risk: 🟡 Moderate · up to 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 |
There was a problem hiding this comment.
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
📒 Files selected for processing (5)
thrust/testing/cuda/fill.cuthrust/testing/cuda/uninitialized_fill.cuthrust/testing/vector.cuthrust/thrust/system/cuda/detail/fill.hthrust/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.
|
@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. |
Description
closes #2777
thrust::device_vector<T> v(n)does not compile whenTdoes not fit into the kernel parameter space, e.g. with a 40000-byteT:The CUDA backend of
thrust::fillandthrust::uninitialized_fillpasses the fill value to the kernel inside the function object, and thedevice_vectorconstructors,resize,assign,insertandpush_backgo 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_forassigns 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-systemthrust::copyalso uses. For these types, which never compiled before, this means that underpar_nosyncthe 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_nandvector_inserttests 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,removewith such a value,reduce/exclusive_scanwith such aninit, andcub::DeviceTransform::Fillitself. 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:TestVectorContainingLargeTypealso runs with a 40000-byte type and additionally comparesresize,insertandassignagainsthost_vector.cuda/fill.cu: fill with a 40000-byte value throughpar_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.cucompile-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):
thrust-cpp17) and clang++ 15 (thrust-cpp20): built and passedthrust.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.thrust::fillwiththrust::deviceover 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