Repository navigation
Allocate the documented workspace in 32 LAPACKE wrappers - #1489
Conversation
These LAPACKE wrappers allocated more workspace than the LAPACK
routines document and use:
- LAPACKE_{c,z}{ge,gb,po,sy,he}rfsx and LAPACKE_{c,z}{ge,gb,po,sy,he}svxx
allocated RWORK(3*N). The complex refinement routines need 2*N; their
RWORK was reduced from 3*N to 2*N in 2009 (LAPACK 3.2.1, "bring work
and rwork down to 2*N in the complex refinement routines"), before
LAPACKE was added.
- LAPACKE_{c,z}gbcon allocated RWORK(2*N); CGBCON and ZGBCON document
and use RWORK(N).
- LAPACKE_{s,d}sytri allocated WORK(2*N); SSYTRI and DSYTRI document
and use WORK(N) (LAPACKE_{c,z}hetri already allocate N).
- LAPACKE_{c,z}sytrs2 allocated WORK(2*N); CSYTRS2 and ZSYTRS2
document and use WORK(N) (LAPACKE_{s,d}sytrs2 and LAPACKE_{c,z}hetrs2
already allocate N).
They now allocate the documented sizes. A program that calls these
wrappers under an allocator that guards the end of every allocation
(n = 12; ?gbcon after ?gbtrf, ?sytri and ?sytrs2 with both uplo after
?sytrf, ZGESVXX through the XBLAS-only sources with stub kernels) runs
without a write past the end of any buffer.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
LAPACKE_?syequb and LAPACKE_?heequb allocated WORK(3*N). xSYEQUB and xHEEQUB use and document WORK(2*N) since LAPACK 3.7.0 (2016), when their workspace was reduced from 3*N. LAPACKE now allocates 2*N. LAPACKE built against an external LAPACK older than 3.7.0 would need the old size; LAPACKE_?gesdd keeps such a compatibility size for LAPACK 3.6 and older. If that compatibility matters here too, this commit can be dropped without affecting the rest. The guarded-allocator program of the previous commit also calls the six wrappers with uplo = 'U' and 'L' without a write past the end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #1489 +/- ##
=======================================
Coverage 70.10% 70.10%
=======================================
Files 6213 6213
Lines 498005 498005
Branches 23446 23446
=======================================
+ Hits 349106 349109 +3
+ Misses 148462 148460 -2
+ Partials 437 436 -1
... and 1 file with indirect coverage changes Continue to review full report in Codecov by Harness.
|
|
@rmlarsen Couple of thoughts:
|
|
@mohawk2 I'm happy to change |
|
@mohawk2 yes, the lack of CI coverage probably explains why these bugs have festered for so long. |
|
@mohawk2 updated to "AI disclosure". Thanks for noticing. |
|
@rmlarsen I thought that was what you meant, but I also thought it was valuable to be clear :-) Any thoughts on LAPACKE doing workplace queries to avoid duplication and drift? |
|
@rmlarsen This is somewhat of a wild-eyed idea. I see you have lots of open pull requests. Review capacity here recently has been extremely tight. Do you think nVidia would be willing to sponsor one or several of the current reviewers on here for a couple of days a month of just reviewing stuff? (I also think if you turned loose your AI thingies on other currently-open PRs for some drive-by reviewing, as checked by you, that would probably add value) |
|
@mohawk2 all interesting ideas. We are obviously happy to help support and give back to LAPACK, as the library and its authors have given so much to the NLA community over the years. Let me discuss the options with my management chain. |
|
@mohawk2 following up on the two points I had not answered:
|
|
@rmlarsen has already answered but confirming. @mohawk2 wrote
and
The issue is that not all LAPACK routines have an LWORK. Unfortunately. This is an issue inherited from a past design. The size of the workspace has sometimes been written in the comments. This is an issue for a few reasons: (1) this prevents systematically doing workspace query, (2) this prevents changing the size of a workspace. One issue was with GGHRD for example. There was a new blocked algorithm invented, faster, but more workspace, and so we had to create a new GGHD3 which is kind of OK, but then GGES would not work as well, so we created GGES3, and same for GGEV. This is not great. So it would be much better if for each WORK, there always would be an LWORK and a workspace query, but this is not the case. |
AI disclosure: This PR was prepared with Claude Code.
Summary
32 LAPACKE high-level wrappers allocate more workspace than the LAPACK routines document and use. This PR makes them allocate the documented sizes. It came out of the workspace audit behind #1484, #1485 and #1486, which mostly found allocations that were too small.
?{ge,gb,po,sy,he}rfsx,?{ge,gb,po,sy,he}svxx(20)RWORK(3*N)RWORK(2*N){c,z}gbconRWORK(2*N){s,d}sytriWORK(2*N){c,z}sytrs2WORK(2*N)?syequb,{c,z}heequb(6)WORK(3*N)WORK(2*N)The other members of these families already allocate the documented size:
{c,z}hetri,{s,d}sytrs2and{c,z}hetrs2use N.Compatibility
LAPACKE can be built against an external LAPACK (
USE_OPTIMIZED_LAPACK). The complex refinement routines need2*Nsince 3.2.1, which predates LAPACKE, so their reduction is safe with any LAPACK.xSYEQUBandxHEEQUBneeded3*Nbefore 3.7.0. SinceLAPACKE_?gesdddeliberately keeps a size that LAPACK 3.6 needs, the?syequb/?heequbchange is a separate commit that can be dropped.Validation
The reduced sizes match the documentation and what the routines use. I also checked them with a program that calls the trimmed wrappers under an allocator that guards the end of every allocation, with n = 12:
?gbconafter?gbtrf;?sytriand?sytrs2with both values of uplo after?sytrf;?syequband?heequbwith both values of uplo;No call writes past the end of a buffer.
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
xGESVDX: its RWORK (complex) is documented and allocated as17*MIN(M,N)**2, more than the routine needs. Zero at most N columns of Z in xBDSVDX for RANGE = 'V' #1488 shows how much Z can grow, so reducing it needs care. I left it alone.?ggev/?ggesRWORK(8*N),?trsnaWORK(LDWORK,N+6)) are left alone.Checklist