Skip to content

Optimize MP2 with blocked AO-to-MO contractions - #74

Merged
mveril merged 6 commits into
mainfrom
perf/blocked-ao-to-mo-mp2
Sep 17, 2026
Merged

mveril merged 6 commits into
mainfrom
perf/blocked-ao-to-mo-mp2

Conversation

@mveril

@mveril mveril commented Sep 13, 2026

Copy link
Copy Markdown
Owner

The previous MP2 implementation expanded a dense AO-pair matrix and retained complete occupied/virtual MO tensors. Replace it for RHF and UHF with four successive contractions blocked over active occupied orbitals, accumulating each block's energy immediately.

Add [mp2] memory_limit = "auto" (default) or explicit sizes such as "512 MiB". Automatic selection uses half of available system memory, with correct KiB conversion and a 512 MiB fallback. Preserve automatic/fixed selection in the Rust API and round-trip explicit sizes without losing integer bytes. Text reports include the chosen block, workspace budget, dense-method upper bound, and resident inputs; JSON schema v1 is unchanged.

The budget covers owned matrix payloads, including coefficient copies and panels. It excludes existing HF data, allocator overhead and matrix-kernel scratch storage, so it is not an RSS limit. Tiny budgets fail with a located, typed error before transformation allocation. Adding the configuration field requires Rust struct literals to specify it or use ..Default::default().

Validation:

  • cargo test --workspace --locked --offline, followed by the focused MP2 suite after final adjustments.
  • cargo test --workspace --no-default-features --locked --offline.
  • cargo clippy --workspace --all-targets --all-features --locked --offline -- -D warnings.
  • cargo fmt --all -- --check, Ruff checks and formatting checks for the reference tooling.
  • pytest tools/reference against the built binary: 17 passed, including 8 MP2 comparisons. New frozen-core H₂O/6-31G and OH/STO-3G cases cover small and large budgets.
  • Independent four-index integral checks and dense energy comparisons cover frozen orbitals, unequal spin coefficients, rectangular MO matrices, block boundaries, 1/4 threads, budget boundaries and numerical errors.

Synthetic RHF release benchmark, four Rayon threads, median of three runs after warm-up; input/ERI construction and SCF are excluded:

AO count Occupied Dense time Blocked time (auto) Speedup Dense payload upper bound Blocked payload
12 4 0.650 ms 0.250 ms 2.60× 133.5 KiB 74.2 KiB
24 8 2.010 ms 1.838 ms 1.09× 2.0 MiB 1.1 MiB
48 16 56.017 ms 33.847 ms 1.66× 30.9 MiB 18.0 MiB

With a 4 MiB budget, the 48-AO case selects three occupied orbitals per block and uses 3.4 MiB of matrix payload, but takes about 182 ms. Small blocks trade speed for memory by repeating AO reads. These synthetic results are machine-dependent, not a speed guarantee for molecular calculations.

Run cargo bench --bench mp2_timings --features bench-support; RUSTIQ_MP2_MEMORY, RUSTIQ_MP2_SIZES, and RAYON_NUM_THREADS select the budget, dimensions and thread count.

@mveril
mveril force-pushed the perf/blocked-ao-to-mo-mp2 branch from 5f29cf2 to e6459ad Compare September 16, 2026 19:13
@mveril
mveril force-pushed the perf/blocked-ao-to-mo-mp2 branch 2 times, most recently from 5f9c1e0 to 40bca14 Compare September 17, 2026 07:29
@mveril
mveril merged commit af7f5af into main Sep 17, 2026
8 checks passed
@mveril
mveril deleted the perf/blocked-ao-to-mo-mp2 branch September 17, 2026 18:45
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