From a36f83b2d8442da95e33d73ee409bb8858dd4fef Mon Sep 17 00:00:00 2001 From: Sri Hari Krishna Narayanan Date: Thu, 3 Dec 2026 12:06:57 -0600 Subject: [PATCH 1/8] Update README: fill in calling convention, vector mode, coverage table Replace the two TODO sections with documentation of the derivative routine calling convention (companion arrays, ndir dimension) and vector mode usage. Fix incorrect test executable names. Add a coverage table listing all 93 base routines across 10 LAPACK families. Co-Authored-By: Claude Opus 4.6 --- README.md | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2c306c5..59bd481 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # `difflapack` -`difflapack`is a library that provides derivative of LAPACK routines from their reference implementation in [lapack](https://github.com/Reference-LAPACK/lapack) using the automatic differentiation tool [Tapenade](https://gitlab.inria.fr/tapenade/tapenade) in four modes: forward (`_d`), vector forward (`_dv`), reverse (`_b`), and vector reverse (`_bv`). +`difflapack` is a library that provides derivatives of LAPACK routines from their reference implementation in [lapack](https://github.com/Reference-LAPACK/lapack) using the automatic differentiation tool [Tapenade](https://gitlab.inria.fr/tapenade/tapenade) in four modes: forward (`_d`), vector forward (`_dv`), reverse (`_b`), and vector reverse (`_bv`). The compiled `libdifflapack` can be linked into applications that need derivatives of LAPACK operations for optimization, sensitivity analysis etc. This work was inspired in part by a need to differentiate a Fortran code [HFBTHO](https://www.sciencedirect.com/science/article/abs/pii/S0010465525004564), that uses LAPACK and BLAS routines, and to use the differentiated application for gradient-based optimization. @@ -13,11 +13,25 @@ Use the library the same way the tests do: 3. Link your application with `libdifflapack` and the LAPACK (`liblapack`) and BLAS library (`librefblas`) libraries. -**Calling convention (same as in the tests):** -TODO +**Calling convention:** -***Handing directions in vector mode:*** -TODO +Each differentiated routine mirrors the primal LAPACK signature with companion (derivative) arrays interleaved after their corresponding primal arguments. For example, `DSYEVR` becomes: + +| Mode | Routine | Key additions | +|------|---------|---------------| +| Forward | `DSYEVR_D` | `ad` after `a`, `wd` after `w`, `zd` after `z`, `workd` after `work` | +| Vector forward | `DSYEVR_DV` | Same as `_D`, plus trailing `ndir` argument | +| Reverse | `DSYEVR_B` | Same companion names; `ad`/`wd`/`zd` carry adjoint (bar) values | +| Vector reverse | `DSYEVR_BV` | Same as `_B`, plus trailing `ndir` argument | + +In scalar modes (`_D`, `_B`), companion arrays have the same shape as their primal. +In vector modes (`_DV`, `_BV`), companion arrays have an extra leading dimension `ndir` for the number of directions, e.g. if `a(lda, n)` then `ad(ndir, lda, n)`. + +**Vector mode — handling multiple directions:** + +Vector forward (`_DV`) computes `ndir` tangent directions simultaneously. Set `ad(idir, :, :)` to the tangent seed for direction `idir` before the call; on return `wd(idir, :)` and `zd(idir, :, :)` hold the corresponding tangent outputs. + +Vector reverse (`_BV`) computes `ndir` adjoint (cotangent) projections simultaneously. Set `wd(idir, :)` and `zd(idir, :, :)` to the cotangent seeds; on return `ad(idir, :, :)` holds the adjoint of `a` for each direction. ## Building the library (and tests) @@ -71,11 +85,34 @@ This builds per-mode test executables in `build/`. Run tests: ```bash ./runscript.sh -# or run individual test executables, e.g. build/test_dsyevr, build/test_dsyevr_vector build/test_dsyevr_reverse build/test_dsyevr_reverse_vector +# or run individual test executables, e.g.: +# build/test_dsyevr_d (forward) +# build/test_dsyevr_dv (vector forward) +# build/test_dsyevr_b (reverse) +# build/test_dsyevr_bv (vector reverse) ``` +## Coverage + +93 base LAPACK routines are differentiated in all four modes (262 source files, 1048 tests validated against central finite differences). The covered families include: + +| Family | Routines | +|--------|----------| +| Symmetric eigenvalue (standard) | `*SYEV*`, `*SPEV*`, `*SBEV*`, `*STEV*`, `*STEDC`, `*STEQR`, `*STERF`, `*STEBZ`, `*STEIN`, `*STEMR`, `*STEGR`, `*PTEQR` | +| Hermitian eigenvalue (standard) | `*HEEV*`, `*HPEV*`, `*HBEV*` | +| Symmetric eigenvalue (generalized) | `*SYGV*`, `*SPGV*`, `*SBGV*` | +| Hermitian eigenvalue (generalized) | `*HEGV*`, `*HPGV*`, `*HBGV*` | +| Non-symmetric eigenvalue / Schur | `*GEES*`, `*GEEV*` | +| SVD | `*GESVD`, `*GESDD` | +| Linear equations | `*GESV*`, `*GBSV*`, `*GTSV*`, `*POSV*`, `*PPSV*`, `*PBSV*`, `*PTSV*`, `*SYSV*`, `*SPSV*`, `*HESV*`, `*HPSV*` | +| Linear least squares | `*GELS`, `*GELSD`, `*GELSS`, `*GELST`, `*GELSY` | +| Matrix factorizations | `*GETRF`, `*POTRF`, `*SYTRF`, `*HETRF`, `*GETRI`, `*POTRI`, `*SYTRI`, `*HETRI` | +| Triangular solves | `*GETRS`, `*POTRS`, `*TRTRS` | + +Each routine is available in all applicable precisions: single (S), double (D), complex (C), and double complex (Z). + ## Future work -There are several routines within LAPACK that are not differentiated with verification, which we plan to add. +There are additional LAPACK routines (e.g. generalized SVD, bidiagonal SVD, additional expert drivers) that we plan to differentiate and add. ## Acknowledgement This work was supported in part by the Applied Mathematics activity within the U.S. Department of Energy, Office of Science, Office From dd6e72b84a6fb6e3a7c494c0ef8b3320f2bc83eb Mon Sep 17 00:00:00 2001 From: Sri Hari Krishna Narayanan Date: Thu, 3 Dec 2026 12:10:00 -0600 Subject: [PATCH 2/8] Remove Tapenade references and add DSYEVR_D usage example Derivatives are implemented using analytic formulas (Giles, 2008), not generated by Tapenade. Remove all Tapenade mentions from the intro, build instructions, and dependency list. Add a complete Fortran example showing how to call DSYEVR_D for forward-mode eigenvalue derivatives. Co-Authored-By: Claude Opus 4.6 --- README.md | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 59bd481..73e968d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # `difflapack` -`difflapack` is a library that provides derivatives of LAPACK routines from their reference implementation in [lapack](https://github.com/Reference-LAPACK/lapack) using the automatic differentiation tool [Tapenade](https://gitlab.inria.fr/tapenade/tapenade) in four modes: forward (`_d`), vector forward (`_dv`), reverse (`_b`), and vector reverse (`_bv`). -The compiled `libdifflapack` can be linked into applications that need derivatives of LAPACK operations for optimization, sensitivity analysis etc. +`difflapack` is a library that provides derivatives of LAPACK routines from their reference implementation in [lapack](https://github.com/Reference-LAPACK/lapack) in four modes: forward (`_d`), vector forward (`_dv`), reverse (`_b`), and vector reverse (`_bv`). +The derivative routines are implemented using analytic formulas (e.g. Giles, 2008 for eigenvalue/SVD derivatives) and can be linked into applications that need derivatives of LAPACK operations for optimization, sensitivity analysis, etc. This work was inspired in part by a need to differentiate a Fortran code [HFBTHO](https://www.sciencedirect.com/science/article/abs/pii/S0010465525004564), that uses LAPACK and BLAS routines, and to use the differentiated application for gradient-based optimization. @@ -33,9 +33,54 @@ Vector forward (`_DV`) computes `ndir` tangent directions simultaneously. Set `a Vector reverse (`_BV`) computes `ndir` adjoint (cotangent) projections simultaneously. Set `wd(idir, :)` and `zd(idir, :, :)` to the cotangent seeds; on return `ad(idir, :, :)` holds the adjoint of `a` for each direction. +### Example: forward-mode eigenvalue derivatives with `DSYEVR_D` + +Given a symmetric matrix `A` and a tangent perturbation `Ad`, compute the derivatives of eigenvalues (`wd`) and eigenvectors (`zd`): + +```fortran +program example_dsyevr_d + implicit none + integer, parameter :: n = 3, lda = n, ldz = n + double precision :: a(lda,n), ad(lda,n), w(n), wd(n), z(ldz,n), zd(ldz,n) + double precision, allocatable :: work(:), workd(:) + integer, allocatable :: iwork(:) + integer :: isuppz(2*n), info, lwork, liwork, m + + ! Set up a symmetric matrix (lower triangle) + a = 0.0d0 + a(1,1) = 2.0d0; a(2,1) = 1.0d0; a(3,1) = 0.0d0 + a(2,2) = 3.0d0; a(3,2) = 1.0d0 + a(3,3) = 4.0d0 + + ! Tangent direction: derivative of A w.r.t. some parameter + ad = 0.0d0 + ad(1,1) = 1.0d0 ! dA/dp = perturbation in the (1,1) entry + + ! Query workspace + lwork = -1; liwork = -1 + allocate(work(1), iwork(1)) + call dsyevr('V','A','L', n, a, lda, 0d0, 0d0, 0, 0, & + 0d0, m, w, z, ldz, isuppz, work, lwork, iwork, liwork, info) + lwork = int(work(1)); liwork = iwork(1) + deallocate(work, iwork) + allocate(work(lwork), workd(lwork), iwork(liwork)) + + ! Compute eigenvalues, eigenvectors, AND their derivatives in one call + call dsyevr_d('V','A','L', n, a, ad, lda, 0d0, 0d0, 0, 0, & + 0d0, m, w, wd, z, zd, ldz, isuppz, work, workd, lwork, & + iwork, liwork, info) + + ! wd(i) = d(lambda_i)/dp, zd(:,i) = d(z_i)/dp + print *, 'Eigenvalues:', w(1:m) + print *, 'dEigenvalues/dp:', wd(1:m) +end program +``` + +The reverse mode (`DSYEVR_B`) works similarly — set `wd` and `zd` to the adjoint seeds before the call; on return `ad` holds the adjoint of `A`. + ## Building the library (and tests) -You need **pre-generated** sources (from step 1). The build compiles them and links with a BLAS library and the Tapenade adStack runtime. +The build compiles the derivative source files in `LAPACK/src/` and links them with a LAPACK and BLAS library. ### Build with Meson (library and tests) @@ -43,8 +88,6 @@ You need **pre-generated** sources (from step 1). The build compiles them and li - **Fortran compiler** (e.g. gfortran, ifort, ifx) and **C compiler** (e.g. gcc). - **LAPACK installation** — a built Reference LAPACK (or compatible) providing BLAS (e.g. `librefblas.a` or `libblas.a`). Set **`LAPACKDIR`** (or equivalent) so Meson can find it (see below). -- **Tapenade adStack** — the repo already contains `TAPENADE/adStack.c` and `TAPENADE/include/`; Meson compiles and links these automatically. No separate Tapenade install is required for the build. - **Configure and build from the project root:** ```bash From 47647ff6850d897edece393db06a71ea6dfb31b9 Mon Sep 17 00:00:00 2001 From: Sri Hari Krishna Narayanan Date: Thu, 3 Dec 2026 12:36:39 -0600 Subject: [PATCH 3/8] Fix example matrix layout to visually show lower triangle Group assignments by row so the staircase shape matches the lower-triangle storage that UPLO='L' reads. Co-Authored-By: Claude Opus 4.6 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 73e968d..d51dfdf 100644 --- a/README.md +++ b/README.md @@ -48,9 +48,9 @@ program example_dsyevr_d ! Set up a symmetric matrix (lower triangle) a = 0.0d0 - a(1,1) = 2.0d0; a(2,1) = 1.0d0; a(3,1) = 0.0d0 - a(2,2) = 3.0d0; a(3,2) = 1.0d0 - a(3,3) = 4.0d0 + a(1,1) = 2.0d0 + a(2,1) = 1.0d0; a(2,2) = 3.0d0 + a(3,1) = 0.0d0; a(3,2) = 1.0d0; a(3,3) = 4.0d0 ! Tangent direction: derivative of A w.r.t. some parameter ad = 0.0d0 From cce76b36ea6334dc51457595e76df56c26007406 Mon Sep 17 00:00:00 2001 From: Sri Hari Krishna Narayanan Date: Thu, 3 Dec 2026 12:38:06 -0600 Subject: [PATCH 4/8] Add DSYEVR description and netlib link to example section Co-Authored-By: Claude Opus 4.6 --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d51dfdf..e5cad21 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,8 @@ Vector reverse (`_BV`) computes `ndir` adjoint (cotangent) projections simultane ### Example: forward-mode eigenvalue derivatives with `DSYEVR_D` -Given a symmetric matrix `A` and a tangent perturbation `Ad`, compute the derivatives of eigenvalues (`wd`) and eigenvectors (`zd`): +[`DSYEVR`](https://www.netlib.org/lapack/explore-html/d2/d8a/group__heevr_ga320e4e6e38df69bde1cbb86da4c3c46d.html) computes eigenvalues and (optionally) eigenvectors of a real symmetric matrix using the Relatively Robust Representations (RRR) algorithm. +`DSYEVR_D` is its forward-mode derivative: given a symmetric matrix `A` and a tangent perturbation `Ad`, it computes the eigenvalues, eigenvectors, and their derivatives (`wd`, `zd`) in a single call: ```fortran program example_dsyevr_d From c523c14a5fdb1bc8f8b62314db0d90f9b968b86f Mon Sep 17 00:00:00 2001 From: Sri Hari Krishna Narayanan Date: Thu, 3 Dec 2026 12:39:58 -0600 Subject: [PATCH 5/8] Add examples for DSYEVR_B, DSYEVR_DV, and DSYEVR_BV Show reverse mode (adjoint seeds for eigenvalues), vector forward (two tangent directions simultaneously), and vector reverse (two adjoint projections simultaneously) with the ndir leading dimension. Co-Authored-By: Claude Opus 4.6 --- README.md | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e5cad21..ff0ebae 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,83 @@ program example_dsyevr_d end program ``` -The reverse mode (`DSYEVR_B`) works similarly — set `wd` and `zd` to the adjoint seeds before the call; on return `ad` holds the adjoint of `A`. +### Example: reverse-mode eigenvalue derivatives with `DSYEVR_B` + +`DSYEVR_B` computes the adjoint (gradient) of `A` given adjoint seeds for the eigenvalues (`wd`) and eigenvectors (`zd`). +First call `DSYEVR` to get the primal eigenvalues and eigenvectors, then call `DSYEVR_B`: + +```fortran + ! ... (same matrix setup and workspace query as above) ... + + ! First compute the primal eigendecomposition + a = a_orig ! restore A (DSYEVR overwrites it) + call dsyevr('V','A','L', n, a, lda, 0d0, 0d0, 0, 0, & + 0d0, m, w, z, ldz, isuppz, work, lwork, iwork, liwork, info) + + ! Set adjoint seeds: e.g. gradient of loss w.r.t. eigenvalues + wd = 0.0d0 + wd(1) = 1.0d0 ! d(loss)/d(lambda_1) = 1 + zd = 0.0d0 ! no gradient contribution from eigenvectors + + ! Compute adjoint of A + a = a_orig + call dsyevr_b('V','A','L', n, a, ad, lda, 0d0, 0d0, 0, 0, & + 0d0, m, w, wd, z, zd, ldz, isuppz, work, workd, lwork, & + iwork, liwork, info) + + ! ad(i,j) = d(loss)/d(A(i,j)) + print *, 'Adjoint of A(1,:):', ad(1,1:n) +``` + +### Example: vector forward mode with `DSYEVR_DV` + +`DSYEVR_DV` computes `ndir` tangent directions simultaneously. The derivative arrays have an extra leading dimension for the direction index: + +```fortran + integer, parameter :: ndir = 2 + double precision :: ad(ndir, lda, n), wd(ndir, n), zd(ndir, ldz, n) + double precision :: workd(ndir, lwork) + + ! Direction 1: perturb A(1,1) + ad = 0.0d0 + ad(1, 1,1) = 1.0d0 + + ! Direction 2: perturb A(2,1) and A(1,2) (symmetric) + ad(2, 2,1) = 1.0d0 + + call dsyevr_dv('V','A','L', n, a, ad, lda, 0d0, 0d0, 0, 0, & + 0d0, m, w, wd, z, zd, ldz, isuppz, work, workd, lwork, & + iwork, liwork, info, ndir) + + ! wd(idir, i) = d(lambda_i) in direction idir + print *, 'dEigenvalues (dir 1):', wd(1, 1:m) + print *, 'dEigenvalues (dir 2):', wd(2, 1:m) +``` + +### Example: vector reverse mode with `DSYEVR_BV` + +`DSYEVR_BV` computes `ndir` adjoint projections simultaneously: + +```fortran + integer, parameter :: ndir = 2 + double precision :: ad(ndir, lda, n), wd(ndir, n), zd(ndir, ldz, n) + double precision :: workd(ndir, lwork) + + ! Adjoint seed 1: gradient w.r.t. lambda_1 + wd = 0.0d0; zd = 0.0d0 + wd(1, 1) = 1.0d0 + + ! Adjoint seed 2: gradient w.r.t. lambda_2 + wd(2, 2) = 1.0d0 + + call dsyevr_bv('V','A','L', n, a, ad, lda, 0d0, 0d0, 0, 0, & + 0d0, m, w, wd, z, zd, ldz, isuppz, work, workd, lwork, & + iwork, liwork, info, ndir) + + ! ad(idir, i, j) = d(loss)/d(A(i,j)) for adjoint seed idir + print *, 'Adjoint of A(1,:) (seed 1):', ad(1, 1, 1:n) + print *, 'Adjoint of A(1,:) (seed 2):', ad(2, 1, 1:n) +``` ## Building the library (and tests) From 60c911fbcc62c9ee6bd6664329272ccd85aa5e03 Mon Sep 17 00:00:00 2001 From: Sri Hari Krishna Narayanan Date: Thu, 3 Dec 2026 12:50:22 -0600 Subject: [PATCH 6/8] Note that calling convention follows Tapenade, with link to FAQ Co-Authored-By: Claude Opus 4.6 --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ff0ebae..c5fbfe8 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,9 @@ Use the library the same way the tests do: **Calling convention:** -Each differentiated routine mirrors the primal LAPACK signature with companion (derivative) arrays interleaved after their corresponding primal arguments. For example, `DSYEVR` becomes: +difflapack follows [Tapenade's calling convention](https://tapenade.gitlabpages.inria.fr/userdoc/build/html/tapenade/faq.html): each derivative array is placed directly after its primal argument, routines are suffixed `_d` (forward) / `_b` (reverse), and vector mode adds an extra leading dimension for directions with a trailing `ndir` parameter. difflapack uses `_dv` and `_bv` suffixes for the vector variants. + +For example, `DSYEVR` becomes: | Mode | Routine | Key additions | |------|---------|---------------| From 758243c06d01136859a39bb3fa83ae7e0ecde51c Mon Sep 17 00:00:00 2001 From: Sri Hari Krishna Narayanan Date: Thu, 3 Dec 2026 13:45:39 -0600 Subject: [PATCH 7/8] Fix usage section: show concrete link command and external declaration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace vague "include the right header/module" with the actual pattern — an external declaration (no Fortran module needed) and a concrete gfortran link command with -ldifflapack. Co-Authored-By: Claude Opus 4.6 --- README.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c5fbfe8..a8d45ae 100644 --- a/README.md +++ b/README.md @@ -5,17 +5,24 @@ The derivative routines are implemented using analytic formulas (e.g. Giles, 200 This work was inspired in part by a need to differentiate a Fortran code [HFBTHO](https://www.sciencedirect.com/science/article/abs/pii/S0010465525004564), that uses LAPACK and BLAS routines, and to use the differentiated application for gradient-based optimization. -## Using the pre-compiled library from your application +## Using the library from your application -Use the library the same way the tests do: -1. Call the differentiated routines from your application. -2. Include the right header/module. -3. Link your application with `libdifflapack` and the LAPACK (`liblapack`) and BLAS library (`librefblas`) libraries. +After building (see below), link your application with `libdifflapack`, LAPACK, and BLAS: + +```bash +gfortran myapp.f90 -L/path/to/builddir -ldifflapack -L$LAPACKDIR -llapack -lrefblas +``` + +In your Fortran source, declare the differentiated routines with `external` — no module is needed: + +```fortran +external :: dsyevr, dsyevr_d +``` **Calling convention:** -difflapack follows [Tapenade's calling convention](https://tapenade.gitlabpages.inria.fr/userdoc/build/html/tapenade/faq.html): each derivative array is placed directly after its primal argument, routines are suffixed `_d` (forward) / `_b` (reverse), and vector mode adds an extra leading dimension for directions with a trailing `ndir` parameter. difflapack uses `_dv` and `_bv` suffixes for the vector variants. +difflapack follows [Tapenade's calling convention](https://tapenade.gitlabpages.inria.fr/userdoc/build/html/tapenade/faq.html#diffvarsinit): each derivative array is placed directly after its primal argument, routines are suffixed `_d` (forward) / `_b` (reverse), and vector mode adds an extra leading dimension for directions with a trailing `ndir` parameter. difflapack uses `_dv` and `_bv` suffixes for the vector variants. For example, `DSYEVR` becomes: From 93ab967b7c4cd278b3a12f29d5a83b8645f48b9e Mon Sep 17 00:00:00 2001 From: Sri Hari Krishna Narayanan Date: Thu, 3 Dec 2026 14:30:20 -0600 Subject: [PATCH 8/8] Fix README errors found during red-team review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix meson command: duplicate -Dlibblas → -Dliblapack for LAPACK lib - Fix linker flag: -lliblapack → -llapack (-l prepends lib automatically) - Remove spurious C compiler dependency from both build sections - Fix Meson heading: "library and tests" → "library only" (tests use Make) - Remove stale "2b" cross-references to nonexistent section numbering - Expand coverage line to mention S/D/C/Z precisions and real/complex - Remove redundant precision sentence that repeated coverage info - Remove extra blank line after external declaration block Co-Authored-By: Claude Opus 4.6 --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index a8d45ae..b2e8395 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # `difflapack` -`difflapack` is a library that provides derivatives of LAPACK routines from their reference implementation in [lapack](https://github.com/Reference-LAPACK/lapack) in four modes: forward (`_d`), vector forward (`_dv`), reverse (`_b`), and vector reverse (`_bv`). -The derivative routines are implemented using analytic formulas (e.g. Giles, 2008 for eigenvalue/SVD derivatives) and can be linked into applications that need derivatives of LAPACK operations for optimization, sensitivity analysis, etc. +`difflapack` is a library that provides derivatives of LAPACK routines from their reference implementation in [LAPACK](https://github.com/Reference-LAPACK/lapack) in four modes: forward (`_d`), vector forward (`_dv`), reverse (`_b`), and vector reverse (`_bv`). +The derivative routines are implemented using known derivative formulae and can be linked into applications that need derivatives of LAPACK operations for optimization, sensitivity analysis, etc. This work was inspired in part by a need to differentiate a Fortran code [HFBTHO](https://www.sciencedirect.com/science/article/abs/pii/S0010465525004564), that uses LAPACK and BLAS routines, and to use the differentiated application for gradient-based optimization. @@ -19,7 +19,6 @@ In your Fortran source, declare the differentiated routines with `external` — external :: dsyevr, dsyevr_d ``` - **Calling convention:** difflapack follows [Tapenade's calling convention](https://tapenade.gitlabpages.inria.fr/userdoc/build/html/tapenade/faq.html#diffvarsinit): each derivative array is placed directly after its primal argument, routines are suffixed `_d` (forward) / `_b` (reverse), and vector mode adds an extra leading dimension for directions with a trailing `ndir` parameter. difflapack uses `_dv` and `_bv` suffixes for the vector variants. @@ -168,24 +167,25 @@ First call `DSYEVR` to get the primal eigenvalues and eigenvectors, then call `D The build compiles the derivative source files in `LAPACK/src/` and links them with a LAPACK and BLAS library. -### Build with Meson (library and tests) +### Build with Meson (library only) **Dependencies:** -- **Fortran compiler** (e.g. gfortran, ifort, ifx) and **C compiler** (e.g. gcc). +- **Fortran compiler** (e.g. gfortran, ifort, ifx). - **LAPACK installation** — a built Reference LAPACK (or compatible) providing BLAS (e.g. `librefblas.a` or `libblas.a`). Set **`LAPACKDIR`** (or equivalent) so Meson can find it (see below). + **Configure and build from the project root:** ```bash -meson setup builddir -Dlibblas=refblas -Dlibblas=reflapack -Dlibblas_path=/path/to/blas/build -Dliblapack_path=/path/to/lapack/build +meson setup builddir -Dlibblas=refblas -Dliblapack=reflapack -Dlibblas_path=/path/to/blas/build -Dliblapack_path=/path/to/lapack/build meson compile -C builddir ``` This produces `builddir/libdifflapack.a`. The Meson build uses `LAPACK/meson.build`, compiles everything in `LAPACK/src/`; -it does **not** build the test executables (tests are built by the LAPACK Makefile in 2b if you use that). +it does **not** build the test executables (tests are built by the Make workflow below). -To also run the tests, use the LAPACK Makefile (2b) or run the test programs built there. To install the library: +To also run the tests, use the Make workflow below. To install the library: ```bash meson install -C builddir --prefix /your/install @@ -195,9 +195,9 @@ meson install -C builddir --prefix /your/install **Dependencies:** -- **Fortran compiler** (e.g. gfortran) and **C compiler** (e.g. gcc). +- **Fortran compiler** (e.g. gfortran). - **LAPACK installed** — build and install [Reference LAPACK](https://github.com/Reference-LAPACK/lapack) so that you have `liblapack`. -- **`LAPACKDIR`** — set to the directory where LAPACK was built (or where `liblapack.a` lives), so the linker can find `-lliblapack`: +- **`LAPACKDIR`** — set to the directory where LAPACK was built (or where `liblapack.a` lives), so the linker can find `-llapack`: ```bash export LAPACKDIR=/path/to/lapack/build ``` @@ -223,7 +223,7 @@ This builds per-mode test executables in `build/`. Run tests: ## Coverage -93 base LAPACK routines are differentiated in all four modes (262 source files, 1048 tests validated against central finite differences). The covered families include: +93 base LAPACK routines are differentiated across single (S) and double (D) precision, in both real and complex arithmetic (C, Z), yielding 262 precision-specific routines. Each is provided in all four derivative modes, for a total of 1048 test cases validated against central finite differences. The covered families include: | Family | Routines | |--------|----------| @@ -238,11 +238,11 @@ This builds per-mode test executables in `build/`. Run tests: | Matrix factorizations | `*GETRF`, `*POTRF`, `*SYTRF`, `*HETRF`, `*GETRI`, `*POTRI`, `*SYTRI`, `*HETRI` | | Triangular solves | `*GETRS`, `*POTRS`, `*TRTRS` | -Each routine is available in all applicable precisions: single (S), double (D), complex (C), and double complex (Z). - ## Future work There are additional LAPACK routines (e.g. generalized SVD, bidiagonal SVD, additional expert drivers) that we plan to differentiate and add. ## Acknowledgement This work was supported in part by the Applied Mathematics activity within the U.S. Department of Energy, Office of Science, Office of Advanced Scientific Computing Research Applied Mathematics, and Office of Nuclear Physics SciDAC program under Contract No. DE-AC02-06CH11357. This work was supported in part by NSF CSSI grant 2104068. + +This package was developed in part with the help of LLMs from Cursor and Claude Code from Anthropic. \ No newline at end of file