Skip to content

Perf: optimize the time-consuming host packing in GPU workflow - #17

Open
maki49 wants to merge 17 commits into
abacusmodeling:masterfrom
maki49:lr-gpu-batch-plan
Open

maki49 wants to merge 17 commits into
abacusmodeling:masterfrom
maki49:lr-gpu-batch-plan

Conversation

@maki49

@maki49 maki49 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Repeated EXX GPU contractions spend substantial time preparing batches on the host and uploading unchanged tensors. This PR reduces that overhead for the four labels used by EXX and LR-TDDFT:

  1. Deferred batch registration: collect batch records independently in each OpenMP thread, register the shared batch layout serially, then copy unique input blocks in parallel. This removes per-batch lock contention.
  2. Layout caching across calls: reuse batch layouts, block offsets and GEMM dimensions while refreshing tensor values. Changes to block keys, shapes or parallel settings invalidate the cache; custom atom filters bypass it. This is especially useful for LR-TDDFT where cal_Hs is called thousands of times.
  3. Optional C/V GPU residency: retain geometry-fixed C/V tensors on device and reuse their device addresses while uploading updated D values. Residency is enabled through set_gpu_resident_data_names(), invalidated when tensors are replaced or freed, and falls back to ordinary uploads when the memory budget is exceeded.

Benchmark: 63-atom NV⁻ in diamond, PBE0, one NVIDIA V100, six OpenMP threads. Values are median cal_loop3_GPU wall times during early Davidson iterations.

Optimization Comparison Before (s/call) After (s/call) Speedup
Deferred registration Original implementation → deferred registration 6.5126 4.2129 1.55×
Layout caching Deferred registration → cached layouts 4.2129 3.3647 1.25×
C/V residency Residency off → on, with both preceding optimizations enabled 3.4261 2.0114 1.70×

The first comparison uses 31 calls per run; the cached-layout result uses 33 cache hits. The residency comparison uses the same binary and 54 calls per run after excluding initial cache construction. Ground-state GPU contractions also improved from 4.3460 to 2.9063 s/call with residency enabled (1.50×).

GPU validation in ABACUS covered changing D values, changing D block keys, and replacing C/V tensors with the same shapes. The residency on/off runs produced identical ground-state energies at printed precision.

Follow-up: A800 performance and host-memory measurements

This follow-up includes all eight commits from a3a8f88 through a7589be, preserving their individual history and authors. The five most recent optimization commits are compared below, together with 0e8afc3 as the original baseline. The 0e8afc3 → 1e37530 comparison also includes the preceding worker-private maps, monotonic arenas and Tmp compaction commits; its entire benefit cannot be attributed to 1e37530 alone.

System and environment

  • NV⁻ defect in a 3×3×3 diamond supercell: 215 atoms (214 C + 1 N), 2795 numerical atomic orbitals, Gamma point, spin-polarized PBE0 with exact-exchange fraction 0.18. Full band window: 2795 bands; 2363/2365 virtual orbitals in the two spin channels; plane-wave cutoff 100 Ry; SCF threshold 1e-8.
  • Tianjin supercomputer: one NVIDIA A800-SXM4-80GB, one MPI rank × 16 OpenMP threads; OMP_PROC_BIND=false and OMP_PLACES unset. GCC 11.5, CUDA 12.3, Intel MPI 2019.8, MKL, LibDDLA 0.0.6 (7f2675d, with an MPI name-lookup build fix), and LibXC 7.0.0 with third derivatives enabled.
  • Replay the actual C/V/D tensors captured at the first EXX contraction for this structure via the CPU KS path. This is a fixed-input contraction benchmark, not an end-to-end SCF/LR timing. C: 31,484 blocks / 324,568,556 doubles; V: 46,225 / 172,003,225; D: 46,225 / 7,812,025.

Measurement protocol

Each version/configuration runs in its own process. Measure five calls in forward version order and five in reverse order, discarding the first call of each process: eight retained samples per row, reported as mean ± sample standard deviation. Uninstrumented cal_loop3 wall time includes transpose, packing, uploads, contraction, output recovery and object destruction; input loading and correctness comparisons are outside the timed region. Peak process RSS includes warm-up; post-call RSS is the mean VmRSS after retained calls. Separate processes prevent one configuration’s high-water mark contaminating another. Minor page faults are measured during the contraction call.

Every H block, shape and element is checked against an independent CPU reference; all 128 retained samples passed, with maximum scaled error 7.682e-16 (tolerance 1e-10). Separate diagnostic runs verify actual cache loads and resident hits and are excluded from these timing statistics.

The same original baseline is repeated as the first row of all three tables for reference. It has neither layout caching nor residency; the repeated rows are the same measurement, not three supported baseline configurations.

Layout cache disabled / no residency

Commit (change) Seconds/call (n=8) Peak RSS (GiB) Post-call RSS (GiB) Minor faults/call (×10⁴)
0e8afc3 (baseline: no layout cache or residency) 107.998 ± 0.986 17.613 7.426 2306.3
1e37530 (remove registration lock contention; cache layouts and residency; compact Tmp metadata) 54.730 ± 0.364 17.115 6.700 1596.9
3a3009a (compact Mul and Output batches) 49.642 ± 0.433 15.601 6.663 1300.3
0869200 (compact Input batches) 43.480 ± 0.611 14.218 6.703 956.3
2cd488d (release host work earlier) 43.112 ± 0.343 11.691 6.705 1013.3
a7589be (remove redundant initialization) 42.896 ± 0.396 11.681 6.681 1007.2

Layout-cache hits / no residency

Commit (change) Seconds/call (n=8) Peak RSS (GiB) Post-call RSS (GiB) Minor faults/call (×10⁴)
0e8afc3 (baseline: no layout cache or residency) 107.998 ± 0.986 17.613 7.426 2306.3
1e37530 (remove registration lock contention; cache layouts and residency; compact Tmp metadata) 36.041 ± 0.697 25.441 19.221 907.9
3a3009a (compact Mul and Output batches) 35.056 ± 0.487 21.199 15.018 855.6
0869200 (compact Input batches) 34.085 ± 0.269 15.591 9.384 789.2
2cd488d (release host work earlier) 34.020 ± 0.385 13.326 9.310 823.0
a7589be (remove redundant initialization) 33.913 ± 0.338 13.353 9.322 814.4

Layout-cache hits / partial C/V residency

Commit (change) Seconds/call (n=8) Peak RSS (GiB) Post-call RSS (GiB) Minor faults/call (×10⁴)
0e8afc3 (baseline: no layout cache or residency) 107.998 ± 0.986 17.613 7.426 2306.3
1e37530 (remove registration lock contention; cache layouts and residency; compact Tmp metadata) 32.264 ± 0.554 24.172 19.249 490.1
3a3009a (compact Mul and Output batches) 31.612 ± 0.625 19.916 14.976 445.5
0869200 (compact Input batches) 30.402 ± 0.336 14.286 9.385 379.1
2cd488d (release host work earlier) 30.479 ± 0.345 11.998 9.324 397.9
a7589be (remove redundant initialization) 30.417 ± 0.327 12.026 9.329 398.8

Interpretation and residency limits

The latest commit reduces cache-disabled time from 107.998 to 42.896 s/call (60.3% less time, 2.52× throughput) and peak RSS from 17.613 to 11.681 GiB (33.7% lower). 3a3009a and 0869200 improve both time and memory; 2cd488d primarily reduces peak memory. The final initialization change does not show a timing benefit clearly exceeding the observed single-sample variability. These are fixed-input results and must not be interpreted as whole-application speedups.

Residency uses the unchanged 4 GiB budget: original C and V occupy 3,972,574,248 bytes (~3.700 GiB) and are reused on device. Transposed C needs another 2,596,548,448 bytes and falls back to uploads. Thus the third table measures partial residency, not full residency of all C/V variants. All five candidates showed cache reuse and resident hits; cold runs showed no layout-cache loads.

An additional latest-commit-only ABACUS single-point LR calculation with default caching/residency and analytic forces completed with process exit code 0: LR Davidson converged 3/3 roots; both Z-vector solves reached true residual 5.63e-7. Internal total time was 28,412 s. Aggregated stages: ground-state runner 3407.02 s; LR including setup but excluding ground-state runner/force 6583.37 s; Z-vector 6615.61 s; LR force excluding Z-vector 11000.86 s. Ground-state forces add 745.64 s; other overhead is ~59.50 s. These stages are derived from nested existing timers, not additive kernel timings. Actual peak host RSS was 252.307 GiB; the 96 GiB submission request is not the measured peak. This single run is not an end-to-end cross-version speedup benchmark. The Slurm wrapper reported failure only because its trailing grep expected a closed-shell output string absent from this open-shell run; the calculation itself exited normally.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 09:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@Stardust0831 Stardust0831 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These suggestions address two reproduced GPU regressions: accessing an input that the contraction does not need, and sharing cached tensor values between copied objects after their inputs diverge. The a0b0 lookup fix can be accepted independently; please add the four cache suggestions to one batch and commit them together.

Validated on NVIDIA V100 GPUs with GCC 13.3 and DDLA 0.0.4:

  • Minimal-input contractions: the original head fails 14/18 with unordered_map::at; the suggested code passes all 18, with residency disabled and enabled.
  • Copy construction, copy assignment, C/V replacement, freeing/reloading tensors, D updates, and subsequent updates to either object pass in real/complex arithmetic, with 1/2 MPI ranks and 1/4 OpenMP threads. A two-rank case where only rank 0 retains an extra copy also completes correctly.
  • Symmetry and physics regression checks pass. ABACUS completes 20 HSE runs and all 16 energy/force/stress comparisons, including eight GPU EXX runs, real/complex tensors and symmetry on/off.

The ABACUS integration check uses commit 1bd79c473134b366fa45f422e0efd0c54491c43c and default residency. All 95 translation units depending on LibRI were rebuilt. Optional residency is covered by the direct API checks; performance was not remeasured.

Comment thread include/RI/ri/LRI-cal_loop3_GPU.hpp Resolved
Comment thread include/RI/ri/LRI-set.hpp Resolved
Comment thread include/RI/ri/LRI-set.hpp Resolved
Comment thread include/RI/ri/gpu/GPU_Resident_Blocks.h Resolved
Comment thread include/RI/ri/gpu/GPU_Resident_Blocks.h Resolved
@maki49

maki49 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for catching these issues! Applied the a0b0 guard separately and the cache/context fixes together, with a shared invalidation helper for the setter and free_tensors_map2. Added regression tests in a third commit.

Validated on V100 GPUs (GCC 13.3, CUDA 12.6): all 9 CTest cases passed, with no skips. Coverage includes all 18 minimal-input contractions with residency on/off, copy construction/assignment, C/V replacement and free/reload, D updates, and rank-dependent cache ownership, in real/complex arithmetic with 1/2 MPI ranks and 1/4 OpenMP threads. The existing symmetry tests also pass.

@Stardust0831

Copy link
Copy Markdown
Contributor

Thanks for addressing these issues and adding the regression coverage. I re-tested the latest head (a5760c7) on V100 GPUs: all 52 independent regression tests and all 9 tests in the updated suite passed, with no failures or skips.

I also rebuilt ABACUS against the updated headers and verified four GPU HSE integration cases covering real/complex arithmetic and EXX symmetry on/off. Energies, forces, and stresses agree with the CPU and previously validated GPU references within the test tolerances.

The issues I reported are resolved in my tests. I have no remaining blockers from my review and consider this PR ready to merge.

userchba and others added 9 commits November 10, 2026 01:54
Port the worker-private packing design from userchba/LibRI 365b29d. Retain per-owner input buffers, independent cached layouts, resident C/V bindings, and ordinary allocation.
Port the per-worker arena design from userchba/LibRI 365b29d with the default new/delete upstream. Keep host tensor buffers and batch storage unchanged.
Port temporary metadata compression and bounded segmentation from userchba/LibRI 365b29d. Preserve worker ordering and update layout cache capture/restore together.
Move immutable pair maps, Pack arrays and dimensions into the plan and borrow them during each contraction. Retain device dimension arrays with their allocating context across calls, and invalidate on communicator, layout or shape changes. Rebind numerical input tasks to current tensors and retain address-dependent pointer uploads. Cover aliasing, cross-queue dimension lifetime and cache invalidation in regression checks.
Resolve the GPU regression-directory move by keeping the packing, resident-cache and budget tests alongside the new callback tests. Retain the master DDLA public API adapter and GPU symmetry callbacks.
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.

4 participants