Cap OpenMP loop threads at the loop size #12792
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
RAMitchell
OpenMPExecutor::Executestarted a team ofn_threadsthreads 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 capsn_threadsat the loop size, which is whatGlobalThreadPoolExecutor::Executealready 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_probaon 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_probaon one numpy row 121 → 32 µs, on one pandas row 779 → 673 µs, andXGBClassifier.fiton 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
predictoutput 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.ThreadsCappedBySizefails on master (OpenMP teams of 4 for loops of 1–3 iterations) and passes with the fix.testxgboostpasses 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/callsomp_get_num_threads(),omp_get_thread_num()or a barrier outside the executor, and the thread pool executor already behaves this way.Execute2dis unchanged. Revert: drop the commit; it touches onlyOpenMPExecutor::Execute.