Skip to content

Avoid overflow of the two-factor scaling in xLAQGE, xLAQGB and the xLAQSY family - #1507

Open
rmlarsen wants to merge 2 commits into
Reference-LAPACK:masterfrom
rmlarsen:laqge-scale-overflow
Open

rmlarsen wants to merge 2 commits into
Reference-LAPACK:masterfrom
rmlarsen:laqge-scale-overflow

Conversation

@rmlarsen

Copy link
Copy Markdown
Contributor

AI disclosure: This PR was prepared with Claude Code.

Summary

xLAQGE and xLAQGB (EQUED = 'B'), and xLAQSY, xLAQSP, xLAQSB, xLAQHE, xLAQHP and xLAQHB, scale each entry by two factors written as CJ*R( I )*A( I, J ) or CJ*S( I )*A( I, J ). Fortran multiplies the two factors first, and that product can overflow to Inf although the scaled entry is O(1). With d = 2^-1074, DGEEQU on A = [ 2d d ; 1 1e-20 ] returns R(1) = 2^1022 and C(2) = 2^52, and DLAQGE then sets A(1,2) to Inf instead of 1. For the symmetric positive definite matrix with all entries equal to d, DPOEQU, DPPEQU and DPBEQU return S = 2^537, and DLAQSY, DLAQSP and DLAQSB return Inf for every entry instead of 1. The input is finite and the exact scaled result is representable.

Description

The scale factors computed by xGEEQU, xGBEQU (and the xGEEQUB variants) are clamped only to [SAFMIN, 1/SAFMIN], so R and C can both be about 2^1022, and the product CJR(I) can reach 2^2044. xPOEQU, xPPEQU and xPBEQU return S = 1/sqrt(A(I,I)), which is 2^537 for a subnormal diagonal entry, so CJS(I) can reach 2^1074. In both cases the entry A(I,J) being scaled is correspondingly tiny and the final result is at most about 1. The Hermitian routines also use CJ*CJ for the diagonal. The routines are reached through xGESVX and xGBSVX with FACT = 'E', and through xPOSVX, xPPSVX and xPBSVX, with finite input.

Fix

The factor T = CJR(I) (or CJS(I), or CJ*CJ) is formed as before and used as before unless it overflows, so every input whose factor product does not overflow gives bit-for-bit the same result. If T overflows, the entry is computed as MIN( CJ, R(I) )*( MAX( CJ, R(I) )*A(I,J) ): the larger factor is applied to A(I,J) first, which lifts the tiny entry into the normal range so that only the second multiply rounds, then the smaller. For the diagonal of the Hermitian routines both factors are equal and the entry is multiplied by CJ twice. With A(I,J) = 0 the result is 0. xLAQGE/xLAQGB only change the 'B' branch, because the row-only and column-only branches use a single factor.

xLASCL2, xLARSCL2 and the xx drivers use a single factor and are not affected. xLAQZ* do not use these formulas.

Reproducer

program repro
   implicit none
   double precision :: a(2,2), ap(3), s(2), r(2), c(2), rowcnd, colcnd, amax
   double precision :: den
   integer :: info
   character :: eq
   den = 4.9406564584124654d-324   ! 2**(-1074), the smallest subnormal
   ! General: row 1 is subnormal, so R(1) = 2**1022; column 2 has C(2) = 2**52.
   a = reshape([2d0*den, 1d0, den, 1d-20], [2, 2])
   call dgeequ(2, 2, a, 2, r, c, rowcnd, colcnd, amax, info)
   call dlaqge(2, 2, a, 2, r, c, rowcnd, colcnd, amax, eq)
   print *, 'dlaqge equed =', eq, ' A(1,2) =', a(1, 2), ' (expected 1)'
   ! Symmetric positive definite 2x2 matrix [d d; d d] in packed storage.
   ap = den
   call dppequ('U', 2, ap, s, rowcnd, amax, info)
   call dlaqsp('U', 2, ap, s, rowcnd, amax, eq)
   print *, 'dlaqsp equed =', eq, ' AP =', ap, ' (expected 1 1 1)'
end program repro

On master:

 dlaqge equed =B A(1,2) =                  Infinity  (expected 1)
 dlaqsp equed =Y AP =                  Infinity                  Infinity                  Infinity  (expected 1 1 1)

With the fix:

 dlaqge equed =B A(1,2) =   1.0000000000000000       (expected 1)
 dlaqsp equed =Y AP =   1.0000000000000000        1.0000000000000000        1.0000000000000000       (expected 1 1 1)

Regression test

xCHKEQ (the EQ path) now also calls xLAQGE, xLAQGB, xLAQSY, xLAQSP and xLAQSB (and xLAQHE, xLAQHP, xLAQHB for complex) after the matching equilibration routine, on 2-by-2 matrices with subnormal entries, for UPLO = 'U' and 'L'. The complex symmetric and Hermitian cases use an off-diagonal entry with a nonzero imaginary part. It requires every scaled entry to agree with the exact value to within the test threshold and EQUED to be 'B' or 'Y' as expected. The test is skipped on a machine without subnormal numbers. On master the new check fails in all four precisions (value Infinity); with this change All tests for SEQ, DEQ, CEQ and ZEQ routines passed the threshold.

Separately, 20000 random 6-by-6 matrices with entries spread over about 60 decades were equilibrated and scaled with DLAQGE, DLAQSY, ZLAQHE and SLAQGE (in the range of single precision). A checksum of every result bit was identical before and after the change, as was a 2-by-2 matrix with entries of 1e306 to 1.7e308, for which the factor is subnormal.

Test suite

The full test suite passes, configured as in CI (CBLAS=ON, LAPACKE=ON, LAPACKE_WITH_TMG=ON) with the _64 API. All 333 CTest entries pass, with 5755188 LAPACK tests (5755190 for the _64 API), 11973112 LAPACKE tests, 319677 BLAS tests and 645492 CBLAS tests for each API, and 0 numerical errors and 0 other errors. These are the counts of master; the equilibration tests are not counted in them. A second build with CI's gfortran flags (-O2 -Wall -Wno-unused-dummy-argument -Wno-unused-variable -Wno-unused-label -Werror=conversion -fimplicit-none -frecursive -fcheck=all) passes the suite as well, and the changed files compile with the same warnings as on master. The builds used GNU Fortran and GCC 15.2.0 with CMAKE_BUILD_TYPE=Release on x86-64 Linux.

Not changed here

  • For entries above about 2^1022, xGEEQU gives a subnormal factor CJ*R(I), and the scaled entry can lose a few ulps. This is not changed.
  • Another pending change adds a test to the same xCHKEQ routines (for xGEEQUB and xGBEQUB), so whichever is merged second needs a trivial rebase of the RESLTS slots.

Checklist

  • The documentation has been updated. (Not needed: the routines now do what they document.)
  • If the PR solves a specific issue, it is set to be closed on merge. (No tracking issue.)

…AQSY family

xLAQGE and xLAQGB (EQUED = 'B') and xLAQSY, xLAQSP, xLAQSB, xLAQHE,
xLAQHP and xLAQHB scale each entry with two factors, written
CJ*R( I )*A( I, J ) or CJ*S( I )*A( I, J ). Fortran evaluates the
product of the two factors first. The factors that xGEEQU, xGBEQU,
xPOEQU, xPPEQU and xPBEQU compute can each be large, and when both are,
the product overflows to Inf although the scaled entry CJ*R( I )*A( I, J )
is O(1) because A( I, J ) is tiny.

For example, A = [ 2*d d ; 1 1e-20 ] with d = 2**(-1074) gives R( 1 ) =
2**1022 and C( 2 ) = 2**52 in DGEEQU, so DLAQGE computes the factor 2**1074
for A( 1, 2 ), which is Inf, and returns Inf where the exact result is
1. The same happens through DGBEQU and DLAQGB. For a symmetric positive
definite matrix with all entries equal to d, DPOEQU, DPPEQU and DPBEQU
return S = 2**537, and DLAQSY, DLAQSP and DLAQSB return Inf for every
entry (the exact result is 1); the Hermitian routines also overflow in
the factor CJ*CJ used for the diagonal. This reaches xGESVX and xGBSVX
with FACT = 'E' (and the xPOSVX, xPPSVX and xPBSVX drivers) with finite
input.

Form the factor T = CJ*R( I ) (CJ*S( I ), CJ*CJ) as before, and use
T*A( I, J ) unless T overflows, so the result for every input whose
factor product does not overflow is unchanged bit for bit. If T
overflows, apply the larger of the two factors to A( I, J ) first and
the smaller second, as MIN( CJ, R( I ) )*( MAX( CJ, R( I ) )*A( I, J ) ).
The first multiply lifts the tiny entry into the normal range, so only
the second rounds. For the diagonal of the Hermitian routines the two
factors are equal and the entry is scaled by CJ twice. With A( I, J ) = 0
the result is 0 as before.

Test: xCHKEQ (the EQ path) now also calls xLAQGE, xLAQGB, xLAQSY, xLAQSP,
xLAQSB (and, for complex, xLAQHE, xLAQHP, xLAQHB) after the matching
equilibration routine on 2-by-2 matrices with subnormal entries, for
both UPLO values (the complex symmetric and Hermitian cases use an
off-diagonal entry with a nonzero imaginary part), and requires every scaled entry to agree with the
exact value to within the test threshold. On the parent commit the test
fails in all four precisions (the entries are Inf); with this change it
passes. The test is skipped if the machine does not have subnormal
numbers.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@rmlarsen
rmlarsen marked this pull request as draft November 22, 2026 03:15
@rmlarsen
rmlarsen marked this pull request as ready for review November 22, 2026 03:25
@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.46193% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.70%. Comparing base (ef55c9b) to head (9a0a0e5).
⚠️ Report is 3 commits behind head on master.

⚠️ Current head 9a0a0e5 differs from pull request most recent head d1dc835

Please upload reports for the commit d1dc835 to get more accurate results.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
SRC/claqsb.f 77.77% 2 Missing ⚠️
SRC/claqsp.f 77.77% 2 Missing ⚠️
SRC/claqsy.f 77.77% 2 Missing ⚠️
SRC/zlaqsb.f 77.77% 2 Missing ⚠️
SRC/zlaqsp.f 77.77% 2 Missing ⚠️
SRC/zlaqsy.f 77.77% 2 Missing ⚠️
TESTING/LIN/cchkeq.f 98.86% 2 Missing ⚠️
TESTING/LIN/dchkeq.f 97.84% 2 Missing ⚠️
TESTING/LIN/schkeq.f 97.84% 2 Missing ⚠️
TESTING/LIN/zchkeq.f 98.86% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1507      +/-   ##
==========================================
+ Coverage   70.63%   70.70%   +0.06%     
==========================================
  Files        6243     6243              
  Lines      501290   502014     +724     
  Branches    23516    23516              
==========================================
+ Hits       354100   354932     +832     
+ Misses     146327   146219     -108     
  Partials      863      863              
Components Coverage Δ
BLAS 99.73% <ø> (ø)
CBLAS 96.95% <ø> (ø)
LAPACK 83.80% <95.20%> (+0.07%) ⬆️
LAPACKE 4.18% <ø> (ø)
TMGLIB 55.69% <ø> (ø)
BLAS testing 88.79% <ø> (ø)
CBLAS testing 89.65% <ø> (ø)
LAPACK testing 82.46% <98.51%> (+0.05%) ⬆️
LAPACKE testing ∅ <ø> (∅)
Files with missing lines Coverage Δ
SRC/claqgb.f 100.00% <100.00%> (ø)
SRC/claqge.f 100.00% <100.00%> (ø)
SRC/claqhb.f 100.00% <100.00%> (ø)
SRC/claqhe.f 100.00% <100.00%> (ø)
SRC/claqhp.f 100.00% <100.00%> (ø)
SRC/dlaqgb.f 100.00% <100.00%> (ø)
SRC/dlaqge.f 100.00% <100.00%> (ø)
SRC/dlaqsb.f 100.00% <100.00%> (ø)
SRC/dlaqsp.f 100.00% <100.00%> (ø)
SRC/dlaqsy.f 100.00% <100.00%> (ø)
... and 20 more

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ef55c9b...d1dc835. Read the comment docs.

Comment thread TESTING/LIN/zchkeq.f Outdated
*
* ZLAQGE
*
A( 1, 1 ) = DCMPLX( TWO*DEN, ZERO )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

DCMPLX is a non-standard intrinsic. 773fd2f ("TESTING: replace non-standard double complex intrinsics") removed every use from TESTING because NAG Fortran only accepts it under -dcfuns, and this reintroduces it (31 uses in this file, lines 483-698). The NAG jobs are skipped on fork PRs (they finish in a few seconds here) but do build on master, so this would only show up after merge. Use CMPLX( ..., KIND=... ) with a kind parameter, as that commit did.

Found in an AI-assisted self-review (Claude Code).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d1dc835: all 31 uses are now CMPLX( ..., KIND=DP ).

The test added on this branch builds its double complex values with
DCMPLX, a VAX extension that NAG Fortran only accepts under -dcfuns.
773fd2f removed every such use from TESTING, and the NAG jobs do not
run on pull requests from forks, so the build would only break after
the merge.

Use CMPLX( ..., KIND=DP ) with a DP kind parameter, as that commit
did. The values are the same; the complex16 LAPACK tests of this
branch run 1151229 tests with no failures.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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