Skip to content

Fix MPI/count_eltype regressions and route large 2D histograms to global atomics - #35

Merged
jbphyswx merged 8 commits into
mainfrom
gpu-sp2d-routing-and-count-eltype-fixes
Aug 20, 2026
Merged

jbphyswx merged 8 commits into
mainfrom
gpu-sp2d-routing-and-count-eltype-fixes

Conversation

@jbphyswx

@jbphyswx jbphyswx commented Aug 20, 2026 •

Copy link
Copy Markdown
Owner

Follow-on to the b4da818 checkpoint: three bug fixes, two measured speedups, and the test coverage
that would have caught two of the bugs.

Bugs

count_eltype positional/keyword mismatches (4b05764) — regression from making count_eltype
positional in the 1D chain. Four sites were missed:

  • The entire MPI backend was dead. SFC._dispatch_execution_backend declared count_eltype as a
    keyword while the core passes it as the 7th positional argument, so no method matched and every
    call fell through to the (::AbstractMPIBackend, args...) stub — which reports "MPI backend is
    unavailable. Load MPI"
    . A signature mismatch disguised as a missing package. test_mpi reported
    Broken (its mpiexec launch failed) rather than failing, so this was invisible.
    Now: MPI_PARITY_OK np=2 nthreads=2 cases=20.
  • The 2D batch path ignored count_eltype and hardcoded zeros(UInt32, …), returning
    wrong-typed counts instead of erroring; it also forwarded the keyword into auxiliary_joint2d!,
    which takes already-allocated buffers, so Distributed batched calls raised MethodError.
  • batch.jl used a hardcoded UInt32 per-slice buffer, which overflows above N≈92k when wider
    counts were requested.

Single-pass 2D threw for any n_dist > 128 (5b963df) — above the tiled cap the only route is
the naive global-atomic kernel, which digitizes the value axis by binary search and demanded
workspace.value_edges_sp2d_dev. With no workspace (the default) every large-bin call raised
ArgumentError. Now reconstructs the edge vector from the linear plan, matching the
allocate-on-demand pattern the general-bins routes already use.

Performance

Four accumulators instead of six (2473352) — T2 = S2 − L2 and L1T2 = S3 − L3 hold per pair,
and a histogram bin is a sum, so two of the six moments are recovered exactly at flush instead of
costing a per-pair atomic. Applied to the CPU and KA sp1d kernels (the CUDA path landed in the
checkpoint). The derive runs at the end of each kernel and uses =, never +=, so it is
idempotent and correct under threaded/partial reduction — which avoids threading it through 20+
accumulator assembly points, several of which are sp2d where the identity does not hold.

sf1d also drops a per-i local histogram (fill! two buffers, branchless clamp into guard
cells, merge nb bins, every i) for a direct scatter — clamp(bin, 0, nb+1)+1 with cells 1 and
nb+2 dropped is exactly if 1 <= bin <= nb.

kernel before after
CPU sp1d (6 moments) 6.00 ns/pair 4.57 1.31×
CPU sf1d (1 moment) 5.03 ns/pair 4.35 1.16×

Large 2D histograms go to global atomics (5b963df) — :direct abandons the on-chip histogram
for block-private global partitions plus a merge, so its cost grows with cells × tile-blocks, while
plain global atomics get cheaper as more cells spread the contention. Above a measured 340 KB of
histogram bytes the naive kernel wins:

FT bins before after
F32 100×100 1.9 bapps 4.4 2.3×
F32 128×128 1.1 4.8 4.4×
F64 100×100 1.9 4.3 2.3×
F64 128×128 1.1 3.5 3.2×

The threshold (rather than an unconditional skip) keeps :direct reachable below the crossover,
where Float32 measured it still ~11% ahead — an unconditional skip would also have orphaned the
63-kernel template in kernels_2d_direct.jl and overfit one GPU.

Tests

Nothing in the suite exercised 2D bin counts past 60×60, and no test asserted which kernel ran
— which is why both the routing cliff and the outright failure went unnoticed. Adds 100×100 and
200×200 cases plus direct assertions on the byte crossover.

It caught a real hole in this PR's own routing fix on the first run: the change handled
GPUValueLinearShared but not the per-moment GPUValueLinearCols plan, which has no global-atomic
method above 128 bins. Root cause was the rule living in two places (the value-plan builder and the
launcher) that could disagree; both now consult one _sp2d_prefers_global_atomics predicate.

Float64 is used for the large-bin assertions deliberately: Float32 carries ~7 digits and a histogram
this sparse disagrees with a Float64 reference by percent even on the CPU, so a Float32 assertion
there would be testing arithmetic, not the kernel.

Verification

  • CUDA suite 29/29 on A100
  • KA/CPU 918 assertions, zero failures; new testsets 4/4 and 11/11
  • MPI 20 cases, 2 ranks × 2 threads
  • Large-bin correctness vs CPU serial: Σcounts exact, Float64 sums to 1.7e-15
  • JET 48/48, Aqua 10/10

gpu/SPEED_OF_LIGHT.md records the analysis with every claim tagged measured / derived /
inherited-unverified / hypothesis, including the levers that were measured dead (registers and
occupancy ≤3%, constant memory, textures 8.6% slower, a CUDA typeplane mode that was implemented and
then reverted for being ~2× slower than this routing change, larger tiles, rsqrt) so they are not
re-derived.

CI

benchmark/Project.toml carried a machine-specific absolute [sources] path — the only one of six
such files that was not relative (docs/, docs/generate_assets/, examples/, gpu/ all use
".."). It was introduced in the b4da818 checkpoint as a side effect of running
julia --project=benchmark, and it broke Pkg.update() on any other machine:

ERROR: expected package `StructureFunctions [e93fee26]` to exist at path `<an absolute local path>`

Also stops tracking .dev/Manifest.toml and removes the UpDeps workflow. .gitignore has ignored
Manifest.toml for a long time, but that has no effect on an already-tracked file, so this was the
one committed manifest in the repo; there are now none. The workflow existed to assert that tracked
manifests were current (update_deps then git diff --exit-code), which is vacuous with none
tracked — and UPDATE_PLAN.md:443 already recorded the decision to remove it. .dev/up_deps.jl is
kept: it refreshes the local, untracked manifests in one step.

@jbphyswx
jbphyswx force-pushed the gpu-sp2d-routing-and-count-eltype-fixes branch from 41cc22b to b019a1e Compare August 20, 2026 06:40
@jbphyswx
jbphyswx merged commit fb8bdb1 into main Aug 20, 2026
5 checks passed
@jbphyswx
jbphyswx deleted the gpu-sp2d-routing-and-count-eltype-fixes branch August 20, 2026 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant