Skip to content

Allocate the documented workspace in 32 LAPACKE wrappers - #1489

Merged
langou merged 2 commits into
Reference-LAPACK:masterfrom
rmlarsen:lapacke-workspace-trim
Nov 15, 2026
Merged

langou merged 2 commits into
Reference-LAPACK:masterfrom
rmlarsen:lapacke-workspace-trim

Conversation

@rmlarsen

@rmlarsen rmlarsen commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

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.

wrappers allocated documented and used since
complex ?{ge,gb,po,sy,he}rfsx, ?{ge,gb,po,sy,he}svxx (20) RWORK(3*N) RWORK(2*N) LAPACK 3.2.1 (2009)
{c,z}gbcon RWORK(2*N) RWORK(N) always
{s,d}sytri WORK(2*N) WORK(N) always
{c,z}sytrs2 WORK(2*N) WORK(N) always
?syequb, {c,z}heequb (6) WORK(3*N) WORK(2*N) LAPACK 3.7.0 (2016)

The other members of these families already allocate the documented size: {c,z}hetri, {s,d}sytrs2 and {c,z}hetrs2 use N.

Compatibility

LAPACKE can be built against an external LAPACK (USE_OPTIMIZED_LAPACK). The complex refinement routines need 2*N since 3.2.1, which predates LAPACKE, so their reduction is safe with any LAPACK. xSYEQUB and xHEEQUB needed 3*N before 3.7.0. Since LAPACKE_?gesdd deliberately keeps a size that LAPACK 3.6 needs, the ?syequb/?heequb change 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:

  • ?gbcon after ?gbtrf;
  • ?sytri and ?sytrs2 with both values of uplo after ?sytrf;
  • ?syequb and ?heequb with both values of uplo;
  • ZGESVXX, through the XBLAS-only sources compiled with stub XBLAS kernels.

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 _64 API. 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 with CMAKE_BUILD_TYPE=Release, and the tests ran on x86-64 Linux build workers.

Not changed here

  • xGESVDX: its RWORK (complex) is documented and allocated as 17*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.
  • Allocations that exceed a long-standing Fortran contract by O(N) (?ggev/?gges RWORK(8*N), ?trsna WORK(LDWORK,N+6)) are left alone.

Checklist

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

rmlarsen and others added 2 commits November 8, 2026 23:23
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

codecov Bot commented Nov 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.10%. Comparing base (c9967aa) to head (1efaffd).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
LAPACKE/src/lapacke_cgbcon.c 0.00% 1 Missing ⚠️
LAPACKE/src/lapacke_cheequb.c 0.00% 1 Missing ⚠️
LAPACKE/src/lapacke_csyequb.c 0.00% 1 Missing ⚠️
LAPACKE/src/lapacke_csytrs2.c 0.00% 1 Missing ⚠️
LAPACKE/src/lapacke_dsyequb.c 0.00% 1 Missing ⚠️
LAPACKE/src/lapacke_dsytri.c 0.00% 1 Missing ⚠️
LAPACKE/src/lapacke_ssyequb.c 0.00% 1 Missing ⚠️
LAPACKE/src/lapacke_ssytri.c 0.00% 1 Missing ⚠️
LAPACKE/src/lapacke_zgbcon.c 0.00% 1 Missing ⚠️
LAPACKE/src/lapacke_zheequb.c 0.00% 1 Missing ⚠️
... and 2 more
Additional details and impacted files

Impacted file tree graph

@@           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     
Components Coverage Δ
BLAS 99.73% <ø> (+0.02%) ⬆️
CBLAS 97.10% <ø> (ø)
LAPACK 83.50% <ø> (ø)
LAPACKE 2.17% <0.00%> (ø)
TMGLIB 55.69% <ø> (ø)
BLAS testing 88.77% <ø> (ø)
CBLAS testing 89.65% <ø> (ø)
LAPACK testing 82.33% <ø> (ø)
LAPACKE testing ∅ <ø> (∅)
Files with missing lines Coverage Δ
LAPACKE/src/lapacke_cgbcon.c 0.00% <0.00%> (ø)
LAPACKE/src/lapacke_cheequb.c 0.00% <0.00%> (ø)
LAPACKE/src/lapacke_csyequb.c 0.00% <0.00%> (ø)
LAPACKE/src/lapacke_csytrs2.c 0.00% <0.00%> (ø)
LAPACKE/src/lapacke_dsyequb.c 0.00% <0.00%> (ø)
LAPACKE/src/lapacke_dsytri.c 0.00% <0.00%> (ø)
LAPACKE/src/lapacke_ssyequb.c 0.00% <0.00%> (ø)
LAPACKE/src/lapacke_ssytri.c 0.00% <0.00%> (ø)
LAPACKE/src/lapacke_zgbcon.c 0.00% <0.00%> (ø)
LAPACKE/src/lapacke_zheequb.c 0.00% <0.00%> (ø)
... and 2 more

... and 1 file with indirect coverage changes


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 c9967aa...1efaffd. Read the comment docs.

@mohawk2

mohawk2 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

@rmlarsen Couple of thoughts:

  • you say "disclaimer: prepared with Claude". I'd get if you were disclosing (letting people know); do you actually mean "disclaiming" (saying you don't take responsibility)?
  • The markdown in your PR description has been a bit careless, and where it features multiplication, that turns into italics
  • I don't know why LAPACKE doesn't just use a workspace query rather than trying to reproduce the calculation (leading to drift, as in fact seen here) - what do you think?
  • edit (missed one) It's a bit concerning that the workspace calculation stuff isn't hit by the test suite at all

@rmlarsen

rmlarsen commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

@mohawk2 I'm happy to change disclaimer to /disclosing/FYI/heads up/ or whatever. I just know that some library maintainers frown upon agent-generated PRs, so I wanted to be upfront about it.

@rmlarsen

rmlarsen commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

@mohawk2 yes, the lack of CI coverage probably explains why these bugs have festered for so long.

@rmlarsen

rmlarsen commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

@mohawk2 updated to "AI disclosure". Thanks for noticing.

@mohawk2

mohawk2 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

@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?

@mohawk2

mohawk2 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

@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)

@rmlarsen

Copy link
Copy Markdown
Contributor Author

@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.

@rmlarsen

Copy link
Copy Markdown
Contributor Author

@mohawk2 following up on the two points I had not answered:

@langou

langou commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

@rmlarsen has already answered but confirming.

@mohawk2 wrote

Any thoughts on LAPACKE doing workplace queries to avoid duplication and drift?

and

I don't know why LAPACKE doesn't just use a workspace query rather than trying to reproduce the calculation (leading to drift, as in fact seen here) - what do you think?

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.

@langou
langou merged commit 1a966dd into Reference-LAPACK:master Nov 15, 2026
48 of 49 checks passed
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.

3 participants