Fix BLAS spec violation: zero C when beta=0 in split_k and local_multiply_cpu#168
Open
exopoiesis wants to merge 1 commit intoeth-cscs:masterfrom
Open
Fix BLAS spec violation: zero C when beta=0 in split_k and local_multiply_cpu#168exopoiesis wants to merge 1 commit intoeth-cscs:masterfrom
exopoiesis wants to merge 1 commit intoeth-cscs:masterfrom
Conversation
…al_multiply_cpu When beta=0, the BLAS specification states that matrix C may be uninitialized and its contents must not be read. COSMA violated this in two places: 1. multiply.cpp split_k path: iterations K>0 set beta=1 to accumulate partial results, but the C buffer from the memory pool was never initialized. With NaN/Inf in uninitialized memory, this produces garbage results (0*NaN=NaN per IEEE 754, and 1*NaN=NaN for K>0). 2. local_multiply_cpu: `Cvalue *= beta` reads C even when beta=0. Per IEEE 754, 0.0 * NaN = NaN, so this corrupts the output. Fix: (1) zero-fill C before the split_k loop when beta=0 and divisor>1, (2) use conditional assignment in local_multiply_cpu. Add regression tests (BetaZero/FloatUninitialized, DoubleUninitialized) that fill C with NaN and verify beta=0 produces correct, NaN-free results. Found while debugging sporadic segfaults in CP2K AIMD with GPU-accelerated COSMA (parallel_gemm called with beta=0 from make_basis_lowdin). PR eth-cscs#159 (v2.8.0) partially addressed this in multiply_using_layout but did not cover the internal split_k accumulation or local_multiply_cpu. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
4 tasks
Member
|
cscs-ci run GH200 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
beta=0, the BLAS specification states that matrix C may be uninitialized and must not be read. COSMA violates this in two places, causing sporadic incorrect results and segfaults in downstream codes (specifically CP2K AIMD with GPU-accelerated COSMA).Bug 1:
multiply.cppsplit_k accumulationWhen the strategy splits along k, iterations
K>0setbeta=1to accumulate partial results into C. However, C is allocated from the memory pool and never initialized. With NaN/Inf in uninitialized memory:K=0:beta=0→ OK (result overwrites C)K=1:beta=1→ reads uninitialized C →1.0 * NaN + partial = NaNFix: zero-fill C before the split_k loop when
beta=0anddivisor > 1.Bug 2:
local_multiply_cpuscalingCvalue *= beta; // when beta=0 and Cvalue=NaN: 0.0 * NaN = NaN (IEEE 754)Fix: conditional assignment:
Cvalue = (beta == 0) ? 0 : Cvalue * betaRelationship to PR #159
PR #159 (v2.8.0) added
C.fill(beta)inmultiply_using_layout, which fixes the user-facing matrix C. However, the internal C_cosma buffer (from memory pool) used in recursivemultiply()is not covered. This PR fixes the internal path.Reproduction
Found in CP2K 2025.2+ AIMD:
parallel_gemm('T','N', ..., beta=0.0, overlap_vv)called frommake_basis_lowdininqs_mo_methods.F. With MPI ranks > 1 and COSMA splitting along k, uninitialized pool memory corrupts the Lowdin orthogonalization → segfault or silent wrong results.CP2K correctly passes
beta=0per BLAS spec — the matrices need not be pre-initialized.Testing
BetaZero.FloatUninitializedandBetaZero.DoubleUninitializedregression testslocal_multiply_cpuwithbeta=0, verify no NaN in output and correct resulttest.multiply(16 MPI ranks) fails/hangs on both upstream master and this branch — pre-existing MPI RMA issue unrelated to this change🤖 Generated with Claude Code