Java: scope the OpenMP thread count to each build; fix builds without index parameters #2844
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 564 lines diff across 7 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.379
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #2844"
general
weight: 0.35
imotov
Part of #2842.
Problem
CagraIndexImpl.build,buildBbqandBruteForceIndexImpl.buildcallomp_set_num_threads(n)before the native build andomp_set_num_threads(1)afterwards. They force 1 when no index parameters are given.nthreads-varis per thread in OpenMP. libgomp keeps it in the calling thread's implicit task, and a thread that never set it starts from the process default. A standalone check: thread A sets 16 and keeps a team of 16 after thread B sets 1, and an untouched thread C gets the default of 24. So concurrent builds on differentCuVSResources/threads do not race on this setting. The problems are these:finallyresets the calling thread to 1 rather than to its previous value, so later OpenMP work on that thread runs single-threaded.CagraIndex.merge()ignoresnumWriterThreadsentirely and runs with whatever count an earlier build on the same thread left behind.NullPointerExceptionfromgetNumWriterThreads().(Before #2774, a CAGRA build without parameters also segfaulted the JVM by reading IVF-PQ
n_liststhrough a NULLivf_pq_build_params. Since #2774 the builders fill in theCagraIndexParams.Builderdefaults, so that path is no longer reachable from the public API.)How much the count matters: Elasticsearch passes 1. Same build, baseline library, 1 vs 24 threads:
| CAGRA via NN-Descent | threads = 1 | threads = 24 | speedup [95% CI] |
|---|---:|---:|---:|
| merge path, 4M × 1024 | 37.8 s | 16.7 s | 2.27× [2.26, 2.29] |
| flush path, 4M × 1024 | 34.8 s | 13.5 s | 2.58× [2.57, 2.59] |
| merge path, wiki_all 1M × 768 | 9.00 s | 4.05 s | 2.22× [2.20, 2.24] |
| flush path, wiki_all 1M × 768 | 8.26 s | 3.33 s | 2.48× [2.45, 2.52] |
Change
OmpThreadScope. It records the calling thread'somp_get_max_threads()(newly added toheaders.h), applies the requested count, and restores the previous value on close. A count of zero or less (or no params) leaves OpenMP alone, so native code uses the OpenMP default; this is now documented onCagraIndexParams.Builder.withNumWriterThreads. Onmain, 0 reachedomp_set_num_threads(0), which libgomp clamps to 1. The scope is opened insideresources.access()so it is restored on the thread that set it. Used by the CAGRA build, the BBQ build andCagraIndex.merge().n_listsonly when the IVF-PQ build params exist (a defensive guard now that #2774 always supplies params).The
numWriterThreadsdefaults are unchanged (builder: 2). The Elasticsearch side is elastic/elasticsearch#161751.Tests
New
OmpThreadsIT. Three of its four tests fail onmain:The fourth,
testCagraMergeRestoresOmpThreads, passes onmaintoo:main's merge never changes the count, and the count used during a merge can't be observed from Java. It guards the restore. Each test pins the count to 5 first, which differs from both the old reset value (1) and the requested count (3).CagraBuildAndSearchIT,CagraMultiThreadStabilityIT,BruteForceAndSearchITandBruteForceRandomizedITstill pass.How this was measured
RTX PRO 6000 Blackwell Server Edition (sm_120a, 96 GB), 24 vCPU Xeon, CUDA 13.0, driver 580, JDK 25,
main@ 24b0b170. The harness drives the Java API the way Elasticsearch's GPU codec (ES92GpuHnswVectorsWriter) does:CuVSMatrix.deviceBuilderfed from heapfloat[].dims + 4bytes (a 4-byte correction trailer per row).getGraph(), thentoHost()below 128 MB orgetRow()above.CAGRA parameters are Elastic's (graph degree 12, intermediate degree 22, 11 NN-Descent iterations, cosine). Each number is the mean of 5 builds after a warm-up build, in a fresh JVM on an otherwise idle machine. Speedups carry a 95% bootstrap CI. The data is wiki_all 1M × 768 and a clustered synthetic set at 1M and 4M × 1024, the same generator as #2837.
🤖 Generated with Claude Code