← Back to Pull requests

[libcu++][CUB][Thrust] Only include `<iosfwd>` to declare stream operators #12809

Closed
unknown closed this pull request without merging on Nov 26, 2026 07:04 UTC
View on GitHub
unknown unknown commented on Nov 25, 2026 14:48 UTC
Author

Description

Many CCCL headers include <ostream>, <istream> or <sstream> only to declare stream operators. With libstdc++, <ostream> alone adds about 16k lines to an empty TU, while <iosfwd> adds about 600.

This PR makes those headers include <iosfwd> instead, through the new <cuda/std/__host_stdlib/iosfwd> wrapper:

  • The stream operators of the libcu++ random number engines and distributions, and those of the Thrust random number engines and distributions, error_code, pointer and reference, are templates over the stream type already, so only the include changes. normal_distribution and poisson_distribution named the format flags through std::ios_base, which needs its definition. They now name them through the stream type, like the other distributions.

  • The CUB stream operators of the algorithm enums, the tuning policies, KeyValuePair and the iterators become templates over the stream type, so that declaring them only needs <iosfwd>. Wide character streams can now print them too. The policy example in the CUB developer guide shows the new form.

  • cub/detail/logging.cuh only includes <sstream> with CUB_DEBUG_LOG or when logging is compiled in. cub/device/dispatch/dispatch_merge.cuh drops an unused <sstream>, and thrust/system/tbb/memory.h an unused <ostream>.
  • Code that streams CCCL types and relied on CCCL for <ostream>, <istream> or <sstream> now needs to include them directly. c2h's half_t and bfloat16_t did, so they now include <ostream>.

    Because the CUB stream operators are now templates, a user-provided function template such as template <class K, class V> std::ostream& operator<<(std::ostream&, const cub::KeyValuePair<K, V>&) is ambiguous with CUB's. The CUB test utilities had such an operator. It was never selected, so this PR removes it.

    Not in this PR:

  • <complex>, which libstdc++ implements on top of <sstream>. It will get its own PR.

  • CUDASTF, which uses stream objects such as std::cerr.

  • log_dispatch() in cub/detail/logging.cuh, which needs a complete std::stringstream whenever logging is compiled in.
  • Measurements

    Non-blank lines of the preprocessed output of a TU that only includes the header, without line markers (nvcc 13.4 host pass, GCC 15.2, main at 92873cab3f):

    | Header | C++17 main | C++17 PR | C++20 main | C++20 PR |
    |---|---|---|---|---|
    | cuda/std/random | 71,653 | 67,459 | 76,181 | 71,897 |
    | thrust/random.h | 63,920 | 59,725 | 68,951 | 64,666 |
    | thrust/random/linear_congruential_engine.h | 38,466 | 21,912 | 44,122 | 25,520 |
    | thrust/system/error_code.h | 38,292 | 31,978 | 43,949 | 37,565 |
    | thrust/iterator/counting_iterator.h | 57,004 | 54,094 | 63,842 | 60,836 |
    | cub/block/block_load.cuh | 54,116 | 51,210 | 59,030 | 56,028 |
    | cub/block/block_scan.cuh | 70,508 | 67,604 | 75,371 | 72,371 |
    | cub/block/block_reduce.cuh | 85,234 | 82,330 | 98,088 | 97,734 |
    | cub/device/device_reduce.cuh | 139,820 | 139,862 | 147,674 | 147,716 |
    | cub/cub.cuh | 289,202 | 289,378 | 297,208 | 297,384 |
    | cub/device/device_reduce.cuh with CCCL_DISABLE_LOGGING | 139,810 | 135,026 | 147,664 | 145,213 |
    | thrust/device_vector.h with CCCL_DISABLE_LOGGING | 182,578 | 177,832 | 190,474 | 188,061 |

    An empty TU has 20,663 lines in C++17 and 24,258 in C++20.

  • The random number headers save the most when nothing else brings in what <ostream> is made of, for example 43% for thrust/random/linear_congruential_engine.h.

  • The CUB block, warp, iterator and tuning headers save about 2.9k lines in C++17 and 3k in C++20. Those that reach cuda/std/__memory/construct_at.h, such as cub/block/block_reduce.cuh, save only 300 to 360 lines in C++20, because libstdc++'s <memory> includes <bits/ostream.h> there.

  • The device-wide CUB algorithms, the Thrust algorithms and <cub/cub.cuh> still get <sstream> through cub/detail/logging.cuh. They grow by up to 176 lines, because each templated operator declaration takes three lines instead of one. With CCCL_DISABLE_LOGGING they save about 4.8k lines in C++17 and 2.4k in C++20.
  • Testing

    Locally with CUDA 13.4 and GCC 15:

  • CUB and Thrust (CUDA, OMP and TBB) header tests in C++17 and C++20, libcu++ internal and public header tests in C++17, cudax header tests, and the C Parallel library with its header tests

  • In C++20 on sm_86 and sm_120: the CUB printing, enum formatting and logging tests, the 21 *_env device tests that print their tuning policies, and the CUB block and warp load, store, reduce, scan and radix rank, thread load and iterator tests. The printing, enum formatting and logging tests also in C++17.

  • The Thrust random, device_reference, dereference, raw_reference_cast and reference_swap tests in C++17 and C++20

  • The libcu++ random lit tests in C++17 and C++20 on sm_86 and sm_120
  • With CUDA 13.4 and GCC 12, every changed header compiles in a TU that includes nothing else, also when the host <iterator> is replaced by libstdc++'s <bits/stl_iterator_base_types.h>, and the operators work on narrow and wide streams once the TU includes <sstream>.

    Checklist


  • New or existing tests cover these changes.

  • The documentation is up to date with these changes.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: bernhardmgruber Review duration: 976m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #12809"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%
    Repository