Repository navigation
Allocate enough IWORK and RWORK in LAPACKE_?gesvdmr3 - #1484
Merged
langou merged 1 commit intoNov 20, 2026
Merged
Conversation
LAPACKE_?gesvdmr3 was modelled on LAPACKE_?gesvdx and kept its workspace sizes, which are too small for xGESVDMR3: - It allocates IWORK with 12*MIN(M,N) entries, the size xGESVDX needs. xGESVDMR3 documents 24*MIN(M,N) and passes LIWORK = 24*MIN(M,N) on to xBDSVDMR3, so no argument check catches the shortfall. With singular vectors, xBDSVDMR3_WORK hands DLARRV_TGK the IWORK from position 8*N+1 on, and DLARRV_TGK always writes IWORK(12*N+1:12*N+2), the start of its cluster lists; the lists and the workspace of DLAR1V_TGK lie further out. So every call with singular vectors and MIN(M,N) >= 2 writes past the end of the buffer. - CGESVDMR3 and ZGESVDMR3 also need an RWORK of 7*MIN(M,N)**2 + 41*MIN(M,N) entries, but LAPACKE_cgesvdmr3 and LAPACKE_zgesvdmr3 allocate 17*MIN(M,N)**2, the size xGESVDX needs. That is too small for MIN(M,N) <= 4: with singular vectors, ZGESVDMR3 writes 104 entries of RWORK for MIN(M,N) = 2 (68 allocated) and 174 for MIN(M,N) = 3 (153 allocated). Both buffers now have the documented sizes. superb keeps the 12*MIN(M,N)-1 entries of LAPACKE_?gesvdx, so callers are unaffected. The workspace descriptions of xBDSVDMR3 and xBDSVDMR3_WORK asked for LIWORK >= 20*N, but both routines check LIWORK >= 24*N, the size that xGESVDMR3 documents and passes, so a caller that followed the documentation stopped in XERBLA. The LWORK of xBDSVDMR3 for JOBZ = 'N' was documented as 2*N*N + 37*N, two less than the check requires. The descriptions now give the sizes that the code checks. The test allocator of the LAPACKE high-level tests now follows every allocation with a guard region of 1024 known bytes and fails the test when the region has changed by the time the allocation is freed. A new gesvdmr3 test, in all precisions, calls LAPACKE_?gesvdmr3 with singular vectors for a 4-by-3 and a 3-by-4 matrix in both layouts, and checks its allocation failure paths, its allocation counts and an invalid matrix_layout like the other tests. With the parent's LAPACKE_?gesvdmr3, every call that computes singular vectors fails the guard check: for IWORK in all precisions, and also for RWORK in complex. Under valgrind, with LAPACKE's default allocator, a standalone reproducer shows the writes past IWORK as invalid on the parent, after which the RWORK overrun corrupts the heap and valgrind aborts; with this change valgrind reports no errors. The full test suite passes, with and without the _64 API. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 tasks done
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1484 +/- ##
==========================================
+ Coverage 70.10% 70.16% +0.06%
==========================================
Files 6213 6213
Lines 498005 497965 -40
Branches 23446 23446
==========================================
+ Hits 349106 349386 +280
+ Misses 148462 148083 -379
- Partials 437 496 +59
... and 10 files with indirect coverage changes Continue to review full report in Codecov by Harness.
|
This was referenced Nov 9, 2026
langou
approved these changes
Nov 20, 2026
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
LAPACKE_?gesvdmr3(#1361) was modelled onLAPACKE_?gesvdxand kept its workspace sizes, which are too small forxGESVDMR3. Every call with singular vectors writes past the end of theIWORKthat LAPACKE allocates, and the complex calls also overrunRWORKwhenMIN(M,N)is 2 or 3. This PR allocates the sizes thatxGESVDMR3documents, corrects two workspace descriptions ofxBDSVDMR3that disagree with its argument checks, and makes the LAPACKE high-level tests catch a workspace overrun.LAPACKE_?gesvdmr3xGESVDMR3(documented)IWORK12*MIN(M,N)24*MIN(M,N)RWORK(c,z)17*MIN(M,N)**27*MIN(M,N)**2 + 41*MIN(M,N)Description
IWORK:xGESVDMR3passesLIWORK = 24*MIN(M,N)on toxBDSVDMR3, so no argument check catches a smaller array. With singular vectors,xBDSVDMR3_WORKhandsDLARRV_TGKtheIWORKfrom position8*N+1on, andDLARRV_TGKalways writesIWORK(12*N+1:12*N+2), the start of its cluster lists; the lists and the workspace ofDLAR1V_TGKlie further out. So every call with singular vectors andMIN(M,N) >= 2writes past the buffer.RWORK(c,z):CGESVDMR3andZGESVDMR3keep the bidiagonal and the TGK eigenvectors inRWORK(2*MIN(M,N)**2 + 3*MIN(M,N)entries) and pass the5*MIN(M,N)**2 + 37*MIN(M,N)entries after them toxBDSVDMR3. The17*MIN(M,N)**2ofxGESVDXis too small forMIN(M,N) <= 4. With singular vectors,ZGESVDMR3writes 104 entries ofRWORKforMIN(M,N) = 2(68 allocated) and 174 forMIN(M,N) = 3(153 allocated).Fix
lapacke_{s,d,c,z}gesvdmr3.c: allocateIWORKwith24*MIN(m,n)entries.superbkeeps the12*MIN(m,n)-1entries ofLAPACKE_?gesvdx, so callers are unaffected.lapacke_{c,z}gesvdmr3.c: allocateRWORKwith7*MIN(m,n)**2 + 41*MIN(m,n)entries.{s,d}bdsvdmr3.f,{s,d}bdsvdmr3_work.f: the descriptions asked forLIWORK >= 20*N, but both routines checkLIWORK >= 24*N, so a caller that followed the documentation stopped inXERBLA. TheLWORKofxBDSVDMR3forJOBZ = 'N'was documented as2*N*N + 37*N, two less than the check requires. The descriptions now give the sizes that the code checks.Regression tests
LAPACKE/testing/high_level/lapacke_test_alloc.c: the test allocator, which the LAPACKE high-level tests install for LAPACKE's internal buffers, now follows each allocation with a guard region of 1024 known bytes. When the allocation is freed, a changed byte fails the test. The existing tests pass with it.LAPACKE/testing/high_level/{s,d,c,z}gesvdmr3.c: a newgesvdmr3test callsLAPACKE_?gesvdmr3with singular vectors for a 4-by-3 and a 3-by-4 matrix in both layouts and checksNS, and, like the other tests, checks the allocation failure paths, the allocation counts and an invalidmatrix_layout.With the
LAPACKE_?gesvdmr3of master, every call that computes singular vectors fails the guard check, forIWORKin all precisions and also forRWORKin complex (6 failures ins/dand 12 inc/z):As an independent check, a standalone program calling
LAPACKE_?gesvdmr3with LAPACKE's default allocator runs clean under valgrind with this PR. With master, valgrind reports writes 0 and 4 bytes past the 144-byteIWORKfromslarrv_tgk_, and the complexRWORKoverrun then corrupts the heap until valgrind itself aborts.The NaN check of
LAPACKE_?gesvdmr3is not swept here: #1483 changes theINFOit returns from-6to-7(ais its seventh argument), so the NaN sweep can join this test once both PRs are in. The two PRs touch the same four LAPACKE files on different lines and merge cleanly in either order.Test suite
The full test suite passes on this branch, configured as in CI (
CBLAS=ON,LAPACKE=ON,LAPACKE_WITH_TMG=ON) with the_64API: all 313 CTest entries, including the eightLAPACKE-high-levelrunners, with 5739011 LAPACK tests, 11973112 LAPACKE tests, 319677 BLAS tests and 645492 CBLAS tests for each API, 0 numerical errors and 0 other errors (13th Gen Intel(R) Core(TM) i7-13700HX, Linux under WSL2, GNU Fortran and GCC 15.2.0,CMAKE_BUILD_TYPE=Release).Not changed here
LAPACKE_?gesvdmr3fillssuperbfromIWORK(2:12*MIN(M,N)), asLAPACKE_?gesvdxdoes, where those entries hold the indices of eigenvectors that failed to converge.xGESVDMR3leaves other data there (IWORK(2)is the path thatxBDSVDMR3took), and theIWORKdescription ofxGESVDMR3, also inherited fromxGESVDX, does not match the routine. That interface question is left alone.Checklist
xBDSVDMR3andxBDSVDMR3_WORK.)