Repository navigation
Fix workspace queries and LWORK checks that allow too little workspace - #1485
Merged
Merged
Conversation
xSBEVX_2STAGE keeps D, E and the Householder vectors of the band reduction (LHTRD entries) at the start of WORK, and gives the rest, from INDWRK = 2*N+LHTRD+1 on, first to xSYTRD_SB2ST (LWTRD entries) and then to the tridiagonal eigensolver: xSTEBZ uses 4*N entries there, and the xSTERF path copies E to WORK(INDWRK+2*N). The minimum LWORK that the routine checks, and returns from a workspace query, was 2*N+LHTRD+LWTRD, which does not cover the 4*N of xSTEBZ when LWTRD = (2*KD+1)*N+KD*NTHREADS is smaller, that is for KD = 0 and KD = 1. A caller that allocates what the query returns, as LAPACKE_?sbevx_2stage does, gets WORK written past its end: by up to 3*N entries for KD = 0 and up to N-NTHREADS for KD = 1. With LAPACKE_dsbevx_2stage and N = 10, KD = 1, RANGE = 'I', the 728-byte buffer is written 56 bytes past its end. The documented size, MAX(1, 7*N, (2*KD+1)*N+KD*NTHREADS+2*N), is too small as well: it leaves out the Householder vectors. The minimum is now 2*N+LHTRD+MAX(4*N,LWTRD), and the documentation gives it for JOBZ = 'N' (the only job available, with LHTRD = 4*N): 6*N+MAX(4*N, (2*KD+1)*N+KD*NTHREADS). Nothing changes for KD >= 2. The complex xHBEVX_2STAGE keeps its real workspace in RWORK(7*N) and is not affected. Test: xDRVST2STG now gives each of its three xSBEVX_2STAGE calls exactly the LWORK that a workspace query returns, followed by a guard of 4*N entries that the call must leave unchanged. On the parent commit, 58 to 64 of the 13464 DST and SST driver tests of each run fail with "DSBEVX_2STAGE(N,I,L) wrote past the workspace size that its workspace query returned." (the driver uses ABSTOL > 0, so every call with KD <= 1 reaches xSTEBZ); with this change they pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For SENSE = 'V' or 'B', xGEESX returns (N*N)/4 in IWORK(1) from a workspace query, which is 0 for N = 1, but it rejects LIWORK < 1 with INFO = -18. A caller that sizes IWORK from the query, as LAPACKE_?geesx does, therefore fails for every 1-by-1 matrix with these jobs; with the reference XERBLA, LAPACKE_dgeesx(..., sense = 'V', n = 1, ...) stops the program with "On entry to DGEESX parameter number 18 had an illegal value". The query now returns MAX(1, (N*N)/4). SGEESX also returns MAX(1, SDIM*(N-SDIM)) in IWORK(1) on exit, as DGEESX already does. The complex routines have no IWORK. Test: xGET24 took LIWORK = N*N for all its xGEESX calls. It now takes the LIWORK that a workspace query of xGEESX returns for SENSE = 'B', which also checks that the queried size is enough for every test matrix ((N*N)/4 >= SDIM*(N-SDIM) for every SDIM). On the parent commit the DSX and SSX tests then fail 44 of 2888 tests each, the test XERBLA reporting INFO = 18 from xGEESX for N = 1; with this change all 3508 pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 tasks done
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1485 +/- ##
==========================================
+ Coverage 70.10% 70.11% +0.01%
==========================================
Files 6213 6213
Lines 498005 498285 +280
Branches 23446 23446
==========================================
+ Hits 349106 349355 +249
- Misses 148462 148494 +32
+ Partials 437 436 -1
... and 1 file with indirect coverage changes Continue to review full report in Codecov by Harness.
|
This was referenced Nov 9, 2026
xGGSVP3 and xGGSVD3 documented LWORK >= 1 and checked only that, but
they need much more:
- xGGSVP3 calls xGEQP3 on B and on A, which needs 3*N+1 entries (N+1 in
the complex routines), the unblocked QR, RQ and
orthogonal-transformation routines, which need M or N, and, to form
V, xORG2R (xUNG2R), which needs P.
- xGGSVD3 keeps the TAU of xGGSVP3 in WORK(1:N) and gives it
WORK(N+1:LWORK) as its workspace; xTGSJA uses WORK(1:2*N).
A caller that follows the documentation therefore gets an argument
error from an internal routine (DGEQP3 parameter 8, or DGGSVP3
parameter 24 from DGGSVD3), and the routines go on after it: with an
XERBLA that returns, DGGSVD3 with LWORK = 1 and N = 3 writes WORK(2:3).
Worse, xGGSVD3 with LWORK = N-1 passes LWORK-N = -1 to xGGSVP3, which
takes it as a workspace query and returns without computing anything;
xGGSVD3 then goes on with whatever K and L held on entry, writes past
the end of WORK, and returns INFO = 0.
The routines now check the minimum they need and document it:
- xGGSVP3: if N > 0 and MAX(M,P) > 0, LWORK >= MAX(M,3*N+1)
(MAX(M,N+1) in complex), and also LWORK >= P if JOBV = 'V';
otherwise LWORK >= 1, as no routine uses WORK.
- xGGSVD3: N more than xGGSVP3 if N > 0 and MAX(M,P) > 0, and
LWORK >= MAX(1,2*N) otherwise.
P counts only when V is formed, as it already did in the optimal size
that the workspace query returns.
The workspace query returns at least this minimum. CGGSVP3 and ZGGSVP3
reported a too small LWORK as INFO = -24, but LWORK is their argument
25 (RWORK precedes TAU); they now return -25.
A standalone program calls DGGSVP3, DGGSVD3, ZGGSVP3 and ZGGSVD3 with
exactly this LWORK, followed by a guard, for M, P, N in {0,1,2,3,5,8}
and JOBU, JOBV, JOBQ = 'N','N','N', 'U','V','Q', 'U','N','Q' and
'N','V','N'. All 3456 cases run without a write past the end, an
argument error or INFO != 0. With one less, all 2840 cases with
LWORK >= 1 stop with the argument error.
Tests: xERRGG checks that LWORK below the minimum is an error in
argument 22 of xGGSVD3 and in argument 24 (real) or 25 (complex) of
xGGSVP3.
xGSVTS3 now calls xGGSVD3 twice, with JOBU = JOBV = JOBQ = 'N' and
then with 'U', 'V', 'Q' for its residual tests, each time with exactly
the minimum LWORK documented for those jobs followed by a guard. A
rejected LWORK or an overwritten guard makes test ratio 1 equal to
1/ULP. The GSV path runs with block size 1, so xGEQP3 takes the same
unblocked path as with the larger LWORK it got before. These tests
check that the minimum is enough; xERRGG checks that less is an error.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xGEMQR applies the Q of xGEQR with xGEMQRT or xLAMTSQR. For SIDE = 'R'
both of these use M*NB entries of WORK (xGEMQRT and xTPMQRT with
LDWORK = M), but xGEMQR checked and returned MB*NB, and so did SLAMTSQR
and DLAMTSQR (CLAMTSQR and ZLAMTSQR already compute M*NB, but document
MB*NB). A caller that allocates what the query returns gets WORK
written past its end whenever M > MB: in xTSQR01, DGEMQR('R', ..., M =
50, N = 3) gets 3 entries from its query and writes 50.
xGELQ computes the LQ factorization with xGELQT or xLASWLQ, which both
use MB*M entries of WORK. For the xGELQT branch (N <= M, or NB outside
(M,N)), xGELQ checked and returned MB*N and a minimum of N. DGELQ with
M = 50, N = 2 gets 2 entries from its query and writes 49.
Both are found by running the test suite with every workspace sized
exactly by the routine's own workspace query, followed by a guard.
The routines now check and return M*NB (xGEMQR, xLAMTSQR) and MB*M
(xGELQ). For N = 0, xGELQ therefore needs LWORK >= M instead of 1, as
xGEQR has always needed LWORK >= N for M = 0. CGEMQR also checked
LWORK against MAX(1,LW) instead of LWMIN, so for MIN(M,N,K) = 0 it
rejected the LWORK = 1 that its own query returns; it now checks
LWMIN, as SGEMQR, DGEMQR and ZGEMQR do. The LWORK descriptions of
xGEMQR and xGEMLQ, which said only LWORK >= 1, and of xGELQ give the
minimum that the routines check.
Test: xTSQR01 sized WORK by the largest of all its queries, so each
call got at least the workspace of the largest one. Its xGELQ call and
its two right-side xGEMQR calls now get exactly the LWORK that their
own workspace query returns, followed by a guard that must stay
unchanged; an overwritten guard makes the test ratio 1/EPS. On the
parent commit the TS tests then fail 1060 of 10800 tests in each
precision; with this change they all pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For N <= 1, SSBGVD and DSBGVD required, and returned from a workspace query, LWORK = 1. For N = 1 the routines do not return early: they keep E in WORK(1), the eigenvectors of the tridiagonal matrix (from xSTEDC) in WORK(2), and the product with Z (from xGEMM) in WORK(3). A caller that allocates what the query returns gets WORK written two entries past its end for JOBZ = 'V', and a wrong eigenvector with INFO = 0: xSTEDC gets LWORK-2 = -1, takes it as a workspace query and returns without setting WORK(2) = 1, and xGEMM multiplies Z by whatever WORK(2) holds. For JOBZ = 'V' the minimum for N <= 1 is now 1+2*N. For JOBZ = 'N' it stays 1: WORK(3) is used only for eigenvectors, and with N = 1 nothing is written past WORK(1) (checked for KA, KB <= 2). CHBGVD and ZHBGVD already require 1+N for N <= 1 in WORK and RWORK (since 2011), but still documented 1; their documentation now says 1+N. The documentation of SSBGVD also gave 3*N instead of 2*N for JOBZ = 'N' and N > 1. Test: xDRVSG2STG gives its xSBGVD call exactly the LWORK that a workspace query returns, followed by a guard that the call must leave unchanged. (xDRVSG is no longer run by xCHKEE.) On the parent commit the DSG and SSG tests report 4 to 8 failures per run, all for N = 1; with this change they pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rmlarsen
force-pushed
the
workspace-query-fixes
branch
from
November 9, 2026 08:29
442cdbf to
0d7d846
Compare
langou
approved these changes
Nov 15, 2026
2 tasks done
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.
AI disclosure: This PR was prepared with Claude Code.
Summary
Six routine families return a workspace query or accept an LWORK that is smaller than what they then use. A caller that allocates what the query returns, as LAPACKE does, or that follows the documented minimum gets its workspace written past the end. One family stops in an argument check on the size its own query returned. Each commit fixes one family and adds a test that fails on the parent commit.
xSBEVX_2STAGE, KD = 0 or 12*N+LHTRD+LWTRD2*N+LHTRD+MAX(4*N,LWTRD)(xSTEBZ)xGEESX, SENSE = 'V' or 'B', N = 1(N*N)/4= 0xGGSVP3,xGGSVD3MAX(M,3*N+1), and P with JOBV = 'V'; N more inxGGSVD3xGEMQR,xLAMTSQR, SIDE = 'R'MB*NBM*NB(xGEMQRT,xTPMQRT)xGELQ,xGELQTbranchMB*NMB*M(xGELQT)xSBGVD, N = 1, JOBZ = 'V'xSTEDC,xGEMM)Description
xSBEVX_2STAGEkeeps D, E and the Householder vectors (LHTRD) at the start of WORK and gives the rest toxSYTRD_SB2STand then toxSTEBZ, which uses4*Nentries. For KD <= 1, LWTRD <4*N. The documentedMAX(1, 7*N, (2*KD+1)*N+KD*NTHREADS+2*N)was too small as well, because it leaves out the Householder vectors.xGEESXreturns(N*N)/4from its LIWORK query, which is 0 for N = 1.LAPACKE_dgeesx(..., sense = 'V', n = 1, ...)therefore stops with "On entry to DGEESX parameter number 18 had an illegal value".xGGSVP3andxGGSVD3documented and checked only LWORK >= 1. Below the real minimum, an internal routine reports the error and the routines go on regardless. With LWORK = N-1,xGGSVD3passes LWORK-N = -1 toxGGSVP3.xGGSVP3takes that as a workspace query and computes nothing, andxGGSVD3then writes past WORK and returns INFO = 0.CGGSVP3andZGGSVP3also reported a too small LWORK as argument 24, but LWORK is their argument 25.xGEMQR(all precisions) andSLAMTSQR/DLAMTSQRsize WORK asMB*NBfor SIDE = 'R', butxGEMQRTandxTPMQRTuseM*NB.CLAMTSQRandZLAMTSQRalready computeM*NB, but documentMB*NB. With M = 50, the query returns 3 and DGEMQR writes 50 entries.CGEMQRalso rejected the LWORK = 1 that its query returns when MIN(M,N,K) = 0.xGELQsizes WORK asMB*Nfor itsxGELQTbranch, butxGELQTusesMB*M. With M = 50 and N = 2, the query returns 2 and DGELQ writes 49 entries.SSBGVDandDSBGVDrequire LWORK = 1 for N <= 1 but use 3 entries for N = 1 with JOBZ = 'V'. With the LWORK = 1 that their query returns, they also return a wrong eigenvector with INFO = 0:xSTEDCgets LWORK = -1, takes it as a workspace query and leaves its eigenvector unset.CHBGVDandZHBGVDhave required 1+N since 2011, but still documented 1.Fix
Each routine now checks, and returns from its workspace query, the size it uses, and documents it. See the commit messages for the formulas. Nothing changes where the old sizes were already enough: KD >= 2 for
xSBEVX_2STAGE, N >= 2 forxGEESX, N >= 2 or JOBZ = 'N' forxSBGVD, and SIDE = 'L' forxGEMQR. The one exception isxGELQwith N = 0, which now needs LWORK >= M instead of 1, asxGEQRhas always needed LWORK >= N for M = 0.xGGSVP3andxGGSVD3count P only when they form V.Regression tests
Five of the six tests make the existing test drivers pass a routine exactly the LWORK that its workspace query returns, followed by a guard region that the call must leave unchanged. An overwritten guard fails the test. On the parent commit they fail as follows:
xDRVST2STG(xSBEVX_2STAGE): 58 to 64 of the 13464 DST/SST driver tests per run.xGET24(xGEESX): takes LIWORK from the query instead ofN*N. 44 of 2888 DSX/SSX tests fail, from INFO = -18 at N = 1.xTSQR01(xGEMQR,xGELQ): 1060 of 10800 TS tests in each precision.xDRVSG2STG(xSBGVD): 4 to 8 DSG/SSG tests per run.The sixth,
xERRGG, checks that LWORK below the minimum is an argument error inxGGSVP3andxGGSVD3.xGSVTS3now checks that the minimum is enough: it callsxGGSVD3with exactly the documented minimum for JOBU = JOBV = JOBQ = 'N' and then for 'U','V','Q', each followed by a guard.For
xGGSVP3andxGGSVD3, a standalone program callsDGGSVP3,DGGSVD3,ZGGSVP3andZGGSVD3with exactly the new minimum and a guard. It covers M, P, N in {0,1,2,3,5,8} with JOBU, JOBV, JOBQ = 'N','N','N', 'U','V','Q', 'U','N','Q' and 'N','V','N', 3456 cases, and none writes past the end or reports an error. With one less, all 2840 cases with LWORK >= 1 stop with the argument error.How these were found
Each LAPACK subroutine with a workspace-length argument and a workspace query (546 of them) was wrapped by a generated shim. The shim asks the routine's own query for the sizes, then calls it with buffers of exactly MIN(length given, size queried) entries, each followed by a guard. LAPACK, tmglib, LAPACKE and the test programs all call through the shims. Running the whole test suite this way reported
xSBEVX_2STAGE,xGEMQR,xGELQandxSBGVD, plus one more, listed under "Not changed here". With this PR, that one is the only report left.The other two were found by reading the code, while auditing LAPACKE's workspace sizes after #1484. The test suite never passes
xGEESXthe LIWORK from its query, and never passesxGGSVP3orxGGSVD3an LWORK below their real minimum.Test suite
The full test suite passes, configured as in CI (
CBLAS=ON,LAPACKE=ON,LAPACKE_WITH_TMG=ON) with the_64API. All 313 CTest entries pass, with 5739011 LAPACK tests, 11973112 LAPACKE tests, 13539 BLAS tests and 645492 CBLAS tests for each API, and 0 numerical errors and 0 other errors. The builds used GNU Fortran and GCC 15.2.0 withCMAKE_BUILD_TYPE=Release, and the tests ran on x86-64 Linux build workers.Not changed here
DBDSVDXwith RANGE = 'V' and VL = 0 writes2*Ncolumns of Z for a bidiagonal with zero singular values, although it documents NS+1 columns.RANGE = 'A'stays within NS+1. For example,DGESVDXon a 30-by-30 zero matrix with JOBU = JOBVT = 'V' and RANGE = 'V' uses 3720 entries of WORK, while its query returns 2910 and its minimum is 2370. The complexxGESVDXkeep Z in RWORK. #1488 fixes it.Checklist