Giles method to differentiate solvers and minor edits to docs - #11
Open
Shreyas911 wants to merge 3 commits into
Open
Giles method to differentiate solvers and minor edits to docs#11Shreyas911 wants to merge 3 commits into
Shreyas911 wants to merge 3 commits into
Conversation
Reference LAPACK's CMake build produces libblas.a/liblapack.a rather than librefblas.a, which the Make and Meson builds expect by default. Document the symlink/override workarounds. Tested (Make path only; Meson not installed locally): - Make + symlink (ln -s libblas.a librefblas.a): build succeeds - Make + BLAS_LIB override (BLAS_LIB="-L$LAPACKDIR -lblas"): build succeeds - Meson + -Dlibblas=blas -Dlibblas_path=$LAPACKDIR: build succeeds
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.
What
Two things in this PR:
libblas.a(andliblapack.a), notlibrefblas.a, but both the Make and Meson build paths in this repo expectlibrefblas.aby default (-lrefblas/-Dlibblas=refblas). Following the README as written with a CMake-built LAPACK fails at link time with no real explanation of why.dtrsm_d.f,dtrsm_b.f,dtrsm_dv.f,dtrsm_bv.f). Instead of differentiating the triangular substitution algorithm line by line like Tapenade does, these differentiate the defining relationop(A)*X = alpha*Bdirectly, so the derivative is expressed purely as calls to the original DTRSM and DTRMM. This is the "black box" adjoint approach from Christianson (1994) and Giles (2008), extended here from the vector-solve case in those papers to the BLAS3 matrix case.Fix
README (docs only): Documents two ways to handle the naming mismatch on the Make path (symlink
libblas.atolibrefblas.a, or overrideBLAS_LIBwith the full linker flags) plus the equivalent Meson option (-Dlibblas=blas). Also updates themakeline tomake # use make -j 8 or make -j 16 for a faster build using multi-threading, so readers know they can parallelize the build.DTRSM (new source):
dtrsm_d.f: forward mode. Comes from perturbingop(A)*X = alpha*B. Works out to one DTRMM call followed by one DTRSM call.dtrsm_b.f: reverse mode. The adjoint comes from solving the transposed system with the seed as the right-hand side, then a rank-restricted outer product forAb(only touching the triangle DTRSM actually reads, and zeroing the diagonal whenDIAG='U').dtrsm_dv.f/dtrsm_bv.f: vector versions of the above. Each direction is computed independently (forward mode is linear in the seed direction, and each reverse-mode direction is its own adjoint solve), though the shared primal solutionXis only computed once, outside the per-direction loop. Neither one calls the scalar_d/_broutines. Tapenade's layout stores the direction as the fastest-varying array index (nbdirsmax, LDA/LDB, *), which isn't something you can hand straight to a BLAS call, so each routine gathers a direction's data into a plain contiguous array first, calls DTRSM/DTRMM, then scatters the result back.Also fixes a bug that was already in the auto-generated
test_dtrsm.f90andtest_dtrsm_reverse.f90(generated byrun_tapenade_blas.py, not by Tapenade itself): both declared a random-seed array twice, with the second declaration coming after executable statements, which Fortran doesn't allow. That's a compile failure unrelated to anything in this PR. (test_dtrsm_vector_forward.f90andtest_dtrsm_vector_reverse.f90didn't have this problem.)Testing
README / library naming, checked against a CMake-built Reference LAPACK:
ln -s libblas.a librefblas.a+makemake BLAS_LIB="-L$LAPACKDIR -lblas"meson setup builddir -Dlibblas=blas -Dlibblas_path=$LAPACKDIR && meson compile -C builddirDTRSM Giles-method routines, checked with finite-difference/VJP tests in all four generated test programs:
test_dtrsm(forward mode): PASStest_dtrsm_reverse(reverse mode): PASStest_dtrsm_vector_forward(vector forward, nbdirs=4): PASStest_dtrsm_vector_reverse(vector reverse, nbdirs=4): PASSNotes for reviewers
run_tapenade_blas.pywould otherwise generate for this routine. The substitution-loop version works fine; it just carries a checkpointing cost (a PUSHREAL8/POPREAL8 stack) that the black-box version avoids completely. Happy to discuss whether the generator should skip DTRSM if it gets re-run later.run_tapenade_blas.pyrewrites top-level build files (Makefile,meson.build,run_tests.sh, the Python interface test files) every time it runs, regardless of--file/--mode. Can open that as its own issue if that's useful.