← Back to Pull requests

Cap OpenMP loop threads at the loop size #12792

Closed
unknown closed this pull request without merging on Nov 04, 2026 09:58 UTC
View on GitHub
unknown unknown commented on Nov 04, 2026 02:27 UTC
Author

OpenMPExecutor::Execute started a team of n_threads threads even when the loop had fewer iterations, so the extra threads were woken only to find no work. It now returns early for an empty loop and caps n_threads at the loop size, which is what GlobalThreadPoolExecutor::Execute already does. A single-iteration loop then runs on the calling thread.

This matters most for small-batch prediction. Predicting one row splits it into one block of rows, so the main loop has one iteration, but with the default nthread (all cores) every call paid for waking the whole team. On a 12-core machine, XGBClassifier.predict_proba on one numpy row went from 121 µs to 32 µs.

Booster.inplace_predict, 100 trees of depth 6, 50 features; macOS arm64 (12 cores), CPU build of master with OpenMP. Each side runs in its own process, 5 interleaved base/branch pairs; median (range) of the per-process best of 7 × 200 calls:

| nthread | rows | Before | After |
|---|---:|---:|---:|
| 1 | 1 | 22.2 µs (22.0–22.6) | 22.1 µs (21.6–22.2) |
| 4 | 1 | 48.5 µs (47.5–49.3) | 22.4 µs (22.3–23.2) |
| 4 | 8 | 48.2 µs (46.8–48.3) | 39.9 µs (38.7–40.6) |
| 4 | 1024 | 124.3 µs (121.1–125.0) | 124.9 µs (122.6–128.7) |
| 12 | 1 | 112.6 µs (93.9–113.8) | 23.3 µs (23.1–24.2) |
| 12 | 8 | 112.7 µs (90.6–114.4) | 58.5 µs (50.9–59.4) |
| 12 | 64 | 152.1 µs (128.6–154.2) | 132.4 µs (113.1–134.0) |
| 12 | 1024 | 201.0 µs (178.5–202.2) | 201.5 µs (180.4–202.6) |

With library defaults (all cores), same pairing: predict_proba on one numpy row 121 → 32 µs, on one pandas row 779 → 673 µs, and XGBClassifier.fit on 20,000 × 50 with 200 trees 0.559 → 0.566 s (within noise).

Results are unchanged: for nthread 1, 4 and 12, the predictions for every batch size above, the predict output on the full matrix and the saved model JSON hash the same before and after. A thread that gets no iteration never ran the callback before either, so worker ids and per-thread buffers see the same accesses.

Tests: the new ParallelFor.ThreadsCappedBySize fails on master (OpenMP teams of 4 for loops of 1–3 iterations) and passes with the fix. testxgboost passes except the distributed suites, which could not connect to their local tracker on my machine (unrelated to this change). test_predict.py, test_basic.py, test_basic_models.py, test_updaters.py, test_with_sklearn.py: 318 passed, 2 skipped.

Risk: a callback that relied on idle threads entering the parallel region would now see a smaller team, but no code in src/ calls omp_get_num_threads(), omp_get_thread_num() or a barrier outside the executor, and the thread pool executor already behaves this way. Execute2d is unchanged. Revert: drop the commit; it touches only OpenMPExecutor::Execute.

Diff: 343 lines (+257 / -86) across 4 files Reviewer: RAMitchell Review duration: 450m
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.42) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 343 lines diff across 4 files
0.42
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%
56%
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.349
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #12792"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 56.8% 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: 42.3% (MEDIUM) · Higher = larger blast radius
42.3%
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: 56.8% · Lower = rushed or shallow review
56.8%
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: 18.3% · Safe to merge
18.3%
Repository