Skip to content

Use thrust::is_contiguous_iterator_v over cuda::std::contiguous_iterator#9894

Open
bernhardmgruber wants to merge 1 commit into
NVIDIA:mainfrom
bernhardmgruber:fix_contig_iter
Open

Use thrust::is_contiguous_iterator_v over cuda::std::contiguous_iterator#9894
bernhardmgruber wants to merge 1 commit into
NVIDIA:mainfrom
bernhardmgruber:fix_contig_iter

Conversation

@bernhardmgruber

Copy link
Copy Markdown
Contributor

The former trait includes more types that we can turn into pointers

…erator`

The former trait includes more types that we can turn into pointers
@bernhardmgruber
bernhardmgruber requested a review from a team as a code owner July 16, 2026 08:34
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jul 16, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Jul 16, 2026
@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved detection of contiguous iterators across loading, storing, caching, and search operations.
    • Increased compatibility with supported iterator types and execution environments.
    • Preserved existing vectorized and optimized memory-access behavior while improving compile-time selection of fast paths.
  • Compatibility
    • Standardized iterator handling across low-level memory operations without changing public APIs or expected application behavior.

Walkthrough

Changes

CUB’s load, store, and cache-modified iterator paths now use thrust::is_contiguous_iterator_v instead of CUDA standard contiguous-iterator concepts. Relevant Thrust trait headers were added, and existing vectorized and cache-modified paths were preserved.

Contiguous load paths

Layer / File(s) Summary
Load-path contiguity checks
cub/cub/agent/agent_find.cuh, cub/cub/block/block_load.cuh, cub/cub/thread/thread_load.cuh
Vectorization and load-path selection now use Thrust’s contiguous-iterator trait; block loading no longer checks __can_to_address explicitly.

Contiguous store paths

Layer / File(s) Summary
Store-path contiguity checks
cub/cub/block/block_store.cuh, cub/cub/thread/thread_store.cuh
Block and thread stores use Thrust’s trait while retaining the existing store-path conditions and behavior.

Cache-modified iterator path

Layer / File(s) Summary
Cache-modified iterator adaptation
cub/cub/iterator/cache_modified_input_iterator.cuh
Cache-modified iterator creation uses Thrust’s trait and adds a FIXME for replacing the current contiguous-iterator unwrapping approach.

Possibly related issues

Possibly related PRs

  • NVIDIA/cccl#9861 — Uses try_make_cache_modified_iterator, whose contiguous-iterator detection is updated here.

Suggested reviewers: srinivasyadav18, caugonnet


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

🧹 Nitpick comments (2)
cub/cub/block/block_load.cuh (1)

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

suggestion: Align both vectorization docs with the new selection rule. The implementation now uses Thrust’s contiguous-iterator trait plus __can_to_address, so the existing “simple pointer” restrictions are stale.

  • cub/cub/block/block_load.cuh#L1014-L1017: update the BlockLoad vectorization requirements.
  • cub/cub/block/block_store.cuh#L843-L846: update the BlockStore vectorization requirements. (raw.githubusercontent.com)
cub/cub/iterator/cache_modified_input_iterator.cuh (1)

230-234: 🎯 Functional Correctness | 🔵 Trivial | 🏗️ Heavy lift

suggestion: Validate the broadened iterator-unwrapping contract. This branch now admits additional Thrust pointer-like iterators, but still derives the base pointer through raw_pointer_cast(&*it) and leaves a FIXME for a dedicated unwrap facility. Add compile coverage for the newly accepted iterator types and either adopt the supported unwrap operation or document why this expression is valid for every trait-positive type. (raw.githubusercontent.com)


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 9ae78556-04bf-4cfb-8ab5-2b87557b17f4

📥 Commits

Reviewing files that changed from the base of the PR and between 0d919ed and cf41e69.

📒 Files selected for processing (6)
  • cub/cub/agent/agent_find.cuh
  • cub/cub/block/block_load.cuh
  • cub/cub/block/block_store.cuh
  • cub/cub/iterator/cache_modified_input_iterator.cuh
  • cub/cub/thread/thread_load.cuh
  • cub/cub/thread/thread_store.cuh

Comment on lines +45 to 46
(VecSize > 1) && (ItemsPerThread % VecSize == 0) && (THRUST_NS_QUALIFIER::is_contiguous_iterator_v<InputIteratorT>)
&& THRUST_NS_QUALIFIER::is_trivially_relocatable_v<InputT>;

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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n== agent_find ==\n'
sed -n '1,120p' cub/cub/agent/agent_find.cuh

printf '\n== thread_load ==\n'
sed -n '320,390p' cub/cub/thread/thread_load.cuh

printf '\n== thread_store ==\n'
sed -n '300,360p' cub/cub/thread/thread_store.cuh

printf '\n== contiguity trait defs/usages ==\n'
rg -n "is_contiguous_iterator_v|__can_to_address|to_address\\(" thrust cub -g '!**/build/**' -g '!**/dist/**'

printf '\n== trait definition file(s) ==\n'
fd -a "is_contiguous_iterator.h" thrust cub

Repository: NVIDIA/cccl

Length of output: 26490


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n== locate trait definition ==\n'
fd -a "is_contiguous_iterator.h" .

printf '\n== search minimal trait-related symbols ==\n'
rg -n "is_contiguous_iterator|__can_to_address|contiguous_iterator" . -g '!**/build/**' -g '!**/dist/**' -g '!**/.git/**'

Repository: NVIDIA/cccl

Length of output: 50367


🌐 Web query:

thrust type_traits is_contiguous_iterator.h definition contiguous_iterator to_address

💡 Result:

Thrust provides several tools within its type traits and iterator utilities to handle contiguous iterators and address extraction [1][2]. is_contiguous_iterator thrust::is_contiguous_iterator is a type trait used to determine if an iterator points to elements that are contiguous in memory [3]. It is defined in the header thrust/type_traits/is_contiguous_iterator.h [1]. The trait returns a true_type if the given Iterator satisfies the ContiguousIterator concept, and false_type otherwise [3]. A corresponding convenience variable template, thrust::is_contiguous_iterator_v, is also available for ease of use [2][4]. to_address and Unwrapping Thrust does not define a custom to_address function; instead, it relies on the C++ standard library's std::to_address, typically accessed via the CUDA Core Compute Libraries (CCCL) integration (often via cuda::std::to_address) [5]. To bridge the gap between Thrust iterators and raw pointers, Thrust provides specific utilities: - unwrap_contiguous_iterator: This function takes a contiguous iterator and converts it to its underlying raw pointer [6]. It includes a static_assert to ensure the iterator is indeed contiguous [6]. - try_unwrap_contiguous_iterator: This utility conditionally unwraps an iterator to a raw pointer if it is contiguous; otherwise, it returns the iterator unmodified [6]. These utilities are located in thrust/type_traits/unwrap_contiguous_iterator.h and are designed to facilitate high-performance operations by allowing direct memory access when safe [6].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '1,120p' thrust/thrust/type_traits/unwrap_contiguous_iterator.h
printf '\n== block_load branch ==\n'
sed -n '1000,1035p' cub/cub/block/block_load.cuh
printf '\n== block_store branch ==\n'
sed -n '830,860p' cub/cub/block/block_store.cuh
printf '\n== contig trait definition ==\n'
sed -n '1,180p' thrust/thrust/type_traits/is_contiguous_iterator.h

Repository: NVIDIA/cccl

Length of output: 9635


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '== proclaim_contiguous_iterator uses ==\n'
rg -n "THRUST_PROCLAIM_CONTIGUOUS_ITERATOR|proclaim_contiguous_iterator<" . -g '!**/build/**' -g '!**/dist/**' -g '!**/.git/**'

printf '\n== thread_load/thread_store callers ==\n'
rg -n "ThreadLoad<|ThreadLoad\\(|ThreadStore<|ThreadStore\\(" cub thrust -g '!**/build/**' -g '!**/dist/**' -g '!**/.git/**' | sed -n '1,120p'

Repository: NVIDIA/cccl

Length of output: 12658


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '== normal_iterator ==\n'
sed -n '1,140p' thrust/thrust/iterator/detail/normal_iterator.h

printf '\n== tagged_iterator ==\n'
sed -n '1,120p' thrust/thrust/iterator/detail/tagged_iterator.h

printf '\n== to_address support search ==\n'
rg -n "to_address\\(|__can_to_address|pointer_traits.*to_address|struct pointer_traits" thrust/thrust/iterator thrust/thrust/detail libcudacxx/include cuda -g '!**/build/**' -g '!**/dist/**' -g '!**/.git/**'

Repository: NVIDIA/cccl

Length of output: 16545


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '== iterator_adaptor operator-> / base access ==\n'
rg -n "operator->|to_address|base\\(" thrust/thrust/iterator/iterator_adaptor.h thrust/thrust/iterator/detail/normal_iterator.h thrust/thrust/iterator/detail/tagged_iterator.h

printf '\n== iterator_adaptor excerpt ==\n'
sed -n '1,260p' thrust/thrust/iterator/iterator_adaptor.h

Repository: NVIDIA/cccl

Length of output: 8566


🏁 Script executed:

#!/bin/bash
set -euo pipefail

nl -ba cub/cub/agent/agent_find.cuh | sed -n '1,260p'

Repository: NVIDIA/cccl

Length of output: 189


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '== agent_find slice ==\n'
awk 'NR>=1 && NR<=260 { printf "%4d  %s\n", NR, $0 }' cub/cub/agent/agent_find.cuh | sed -n '1,260p'

Repository: NVIDIA/cccl

Length of output: 8416


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '== agent_find vectorization control flow ==\n'
rg -n "attempt_vectorization|ConsumeTile\\(|is_aligned_and_full_tile\\(" cub/cub/agent/agent_find.cuh
printf '\n== surrounding lines ==\n'
awk 'NR>=120 && NR<=260 { printf "%4d  %s\n", NR, $0 }' cub/cub/agent/agent_find.cuh

Repository: NVIDIA/cccl

Length of output: 4198


🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n "operator->|to_address" thrust/thrust/iterator/iterator_facade.h thrust/thrust/iterator/detail/iterator_facade* thrust/thrust/iterator/iterator_adaptor.h thrust/thrust/iterator/detail/normal_iterator.h thrust/thrust/iterator/detail/tagged_iterator.h

Repository: NVIDIA/cccl

Length of output: 685


🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n "operator->|arrow|to_address" thrust/thrust/iterator/iterator_facade.h thrust/thrust/iterator/detail/iterator_facade*.h thrust/thrust/iterator/detail/iterator_facade*.cuh thrust/thrust/iterator/iterator_adaptor.h

Repository: NVIDIA/cccl

Length of output: 524


🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n "operator->" thrust/thrust/iterator/iterator_facade.h thrust/thrust/iterator/detail -g '!**/build/**' -g '!**/dist/**'

Repository: NVIDIA/cccl

Length of output: 433


🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n "attempt_vectorization|ConsumeTile\\(|is_aligned_and_full_tile\\(" cub/cub/agent/agent_find.cuh
printf '\n== agent_find around vectorization ==\n'
awk 'NR>=120 && NR<=260 { printf "%4d  %s\n", NR, $0 }' cub/cub/agent/agent_find.cuh

Repository: NVIDIA/cccl

Length of output: 4168


important: is_contiguous_iterator_v is broader than the raw-pointer path these overloads need.

  • cub/cub/agent/agent_find.cuh: keep vectorization pointer-only; the vectorized tile path still does raw reinterpret_cast loads, and is_aligned_and_full_tile() already requires std::is_pointer_v.
  • cub/cub/thread/thread_load.cuh / cub/cub/thread/thread_store.cuh: keep the ::cuda::std::__can_to_address guard so contiguous iterators that can't be unwrapped still take the dereference fallback.
📍 Affects 3 files
  • cub/cub/agent/agent_find.cuh#L45-L46 (this comment)
  • cub/cub/thread/thread_load.cuh#L353-L359
  • cub/cub/thread/thread_store.cuh#L321-L327

Source: Learnings

@github-actions

Copy link
Copy Markdown
Contributor

😬 CI Workflow Results

🟥 Finished in 1h 48m: Pass: 14%/287 | Total: 3d 05h | Max: 1h 25m | Hits: 19%/155820

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.

1 participant