← Back to Pull requests

perf(dbscan): speed up the CPU neighborhood search and cluster expansion #4004

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 07, 2026 13:59 UTC
Author

Description

Three CPU DBSCAN speedups, one commit each. Each table below compares that commit alone with main.

1. Allocate each neighborhood once per in-block

On CPU, the DBSCAN neighborhood search (NeighborhoodEngine::queryFull, dbscan_utils.h) was bound by memory allocation, not by the distance computation. It appended the hits of every 128 x 128 distance tile straight into the neighborhoods, so a neighborhood was regrown once per out-block, and Neighborhood::grow jumps from 64 to 4096 entries. With many threads the search spent most of its time in the scalable allocator's large-object path (perf: about 60% of the samples, the GEMM under 3%), and on a two-socket machine it got slower from about 56 threads up.

Change (all in cpp/daal/src/algorithms/dbscan/dbscan_utils.h):

  • Each thread stages the (row, index) hits of its in-block in one buffer that it reuses for every in-block, and allocates every neighborhood once at the end of the in-block: at its exact size up to 1024 entries, rounded up to a power of two above, because odd large sizes miss the allocator's large-object cache (exact large sizes measured up to 1.7x slower on dense neighborhoods).

  • The buffer is the existing Vector, given reserve/resize. Vector now allocates from the scalable allocator without zero-filling: with calloc the buffer, rebuilt for every call, faulted its pages in again on every call.

  • Neighborhood::grow and Vector::grow raised the capacity before checking the allocation, and Neighborhood::grow returned success when it failed; both now report the error and leave the object unchanged. allocateNewEntries and fastAdd have no callers left and are removed.
  • The neighbors keep their order, so labels, core indices and core observations are unchanged (checked on every benchmark case below).

    Tests. New dbscan with neighborhoods of more than 1024 rows matches the definition (3000 rows, so several tiles on both sides, neighborhoods above 1024 rows, both memory modes), checked against the DBSCAN definition on reference distances. bazel test --config=host //cpp/oneapi/dal/algo/dbscan:tests passes.

    Performance. scikit-learn-intelex DBSCAN.fit (with uxlfoundation/scikit-learn-intelex#3483 for the non-euclidean metrics) measured with scikit-learn_bench on a two-socket Intel CPU, n_jobs = physical cores, eps = 1% quantile of the pairwise distances in each metric, min_samples = 5, float32, pandas input. Builds of main and of this branch were interleaved case by case, 2 rounds of 3 runs each; speedup = median on main / median on this branch:

    | data | euclidean | cosine | manhattan | chebyshev |
    |---|---|---|---|---|
    | blobs 50000 x 8 | 5.92x | 4.68x | 1.53x | 1.24x |
    | blobs 100000 x 32 | 2.28x | 2.04x | 1.19x | 0.99x |
    | blobs 200000 x 8 | 2.22x | 1.84x | 1.10x | 1.04x |
    | blobs 500000 x 2 | 1.15x | 1.16x | 0.99x | 0.78x |
    | covtype 100000 x 54 | 1.42x | 1.67x | 1.03x | 1.01x |
    | cifar 60000 x 3072 | 1.00x | 0.99x | 1.01x | not run |

    cifar with Chebyshev is left out of the interleaved runs (about 3 minutes per fit, distance-bound).

    Gains are largest where neighborhoods are a few hundred to a few thousand rows and the distance is cheap (GEMM-based euclidean and cosine). Manhattan and Chebyshev spend most of their time in the distance kernels, which this PR does not touch. In blobs 500000 x 2 every point has about 5000 neighbors (one cluster, roughly 20 GB of neighbor lists); there staging the hits adds memory traffic and Chebyshev is 0.78x.

    2. Size the parallel expansion of a neighborhood by its size

    The CPU DBSCAN cluster expansion (processNeighborhoodParallel, dbscan_dense_default_batch_impl.i) starts a parallel region for every core point it reaches, with one task per thread whatever the size of the neighborhood. With many small neighborhoods almost all of those tasks are empty: on 500000 x 2 blobs with about 18000 clusters the expansion took 62 ms on one thread and about 1.5 s on 224.

    The number of tasks is now min(threads, neighborhood size / 32 + 1) (__DBSCAN_EXPANSION_BLOCK_SIZE). Large neighborhoods are still split over all threads, small ones run in one task on the calling thread. Assignments and labels are unchanged.

    Performance. scikit-learn-intelex DBSCAN.fit, scikit-learn_bench, two-socket Intel CPU, make_blobs(n_samples=500000, n_features=2, centers=100, cluster_std=1), eps 0.02 (euclidean) / 0.025 (manhattan), min_samples 5, float32. main and this branch interleaved, 2 rounds of 7 runs; speedup = median on main / median on this branch, clusters identical:

    | metric | n_jobs = physical cores | n_jobs = default (all threads) |
    |---|---|---|
    | euclidean | 1.40x | 1.52x |
    | manhattan | 1.05x | 1.11x |

    Manhattan spends most of its time in the distance kernel. On the sklbench DBSCAN cases with few large clusters the change is within noise.

    Tests. No behavior change; bazel test --config=host //cpp/oneapi/dal/algo/dbscan:tests passes.

    3. Copy column-major input to row-major once

    The CPU DBSCAN neighborhood search reads the data in 128-row tiles. A column-major input table was wrapped in the SOA adapter, so every tile read transposed a block into a temporary buffer. The table is now copied to row-major once, through the existing interop::convert_to_daal_table(table, need_copy) path that k-means initialization already uses.

    The cost is one row-major copy of the input. The gain is on wide data, where each tile transposes many columns.

    Performance. scikit-learn-intelex DBSCAN.fit on Fortran-ordered numpy input, scikit-learn_bench, two-socket Intel CPU, n_jobs = physical cores, eps = 1% distance quantile, min_samples 5, float32; main and this branch interleaved, 2 rounds of 3 runs:

    | data | euclidean | cosine | manhattan |
    |---|---|---|---|
    | cifar 60000 x 3072 | 1.23x | 1.23x | 1.07x |

    On blobs with 2-32 columns and covtype (54 columns) the change is within noise (0.86-1.16x).

    Tests. The metrics test now runs every case on both layouts, CPU and GPU.

    No public API or ABI changes.

    🤖 Generated with Claude Code

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: Alexandr-Solovev Review duration: 21976m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). 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.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #4004"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 27.7% · Safe to merge
    27.7%