← Back to Pull requests

Java: scope the OpenMP thread count to each build; fix builds without index parameters #2844

Closed
unknown closed this pull request without merging on Oct 28, 2026 16:38 UTC
View on GitHub
unknown unknown commented on Oct 12, 2026 18:37 UTC
Author

Part of #2842.

Problem

CagraIndexImpl.build, buildBbq and BruteForceIndexImpl.build call omp_set_num_threads(n) before the native build and omp_set_num_threads(1) afterwards. They force 1 when no index parameters are given.

nthreads-var is 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 different CuVSResources/threads do not race on this setting. The problems are these:

  • The finally resets the calling thread to 1 rather than to its previous value, so later OpenMP work on that thread runs single-threaded.

  • CagraIndex.merge() ignores numWriterThreads entirely and runs with whatever count an earlier build on the same thread left behind.

  • A brute-force build without parameters throws a NullPointerException from getNumWriterThreads().
  • (Before #2774, a CAGRA build without parameters also segfaulted the JVM by reading IVF-PQ n_lists through a NULL ivf_pq_build_params. Since #2774 the builders fill in the CagraIndexParams.Builder defaults, 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

  • New OmpThreadScope. It records the calling thread's omp_get_max_threads() (newly added to headers.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 on CagraIndexParams.Builder.withNumWriterThreads. On main, 0 reached omp_set_num_threads(0), which libgomp clamps to 1. The scope is opened inside resources.access() so it is restored on the thread that set it. Used by the CAGRA build, the BBQ build and CagraIndex.merge().

  • Read IVF-PQ n_lists only when the IVF-PQ build params exist (a defensive guard now that #2774 always supplies params).

  • Brute force: drop the OpenMP calls. Nothing on the brute-force build path uses OpenMP, and reading the thread count was what made params mandatory there.
  • The numWriterThreads defaults are unchanged (builder: 2). The Elasticsearch side is elastic/elasticsearch#161751.

    Tests

    New OmpThreadsIT. Three of its four tests fail on main:

  • with the thread count pinned to 5, it is 1 after a CAGRA build;

  • the same holds for a CAGRA build without params (since #2774 it builds with the default params instead of crashing, then resets the count to 1);

  • a brute-force build without params throws an NPE.
  • The fourth, testCagraMergeRestoresOmpThreads, passes on main too: 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, BruteForceAndSearchIT and BruteForceRandomizedIT still 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:

  • Flush: CuVSMatrix.deviceBuilder fed from heap float[].

  • Merge: a zero-copy host view of the mmapped vectors file.

  • int8 merge: rows of dims + 4 bytes (a 4-byte correction trailer per row).

  • Graph read-back: getGraph(), then toHost() below 128 MB or getRow() 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

    Diff: 564 lines (+423 / -141) across 7 files Reviewer: imotov Review duration: 22921m
    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
    0.50
    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.379
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #2844"
    general weight: 0.35
    Reviewers
    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: 49.7% (MEDIUM) · Higher = larger blast radius
    49.7%
    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: 19.6% · Safe to merge
    19.6%
    Repository