Repository navigation
Call init_suitesparse when KLU loads - #57
Merged
Merged
Conversation
SparseArrays requires this before any use of libklu. Without it, on Julia 1.12 and later, KLU allocated with libc's malloc until a SparseArrays solver switched the allocator, and those factors were then freed through jl_free, after which GC could become extremely slow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #57 +/- ##
==========================================
- Coverage 97.54% 97.30% -0.25%
==========================================
Files 1 1
Lines 367 371 +4
==========================================
+ Hits 358 361 +3
- Misses 9 10 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Calls
SparseArrays.LibSuiteSparse.init_suitesparse()in__init__. Its docstring says that any package directly wrapping KLU must do this before using the library, and KLU.jl never did.The bug
On Julia 1.12 and later, SparseArrays sets SuiteSparse's allocator lazily, the first time one of its solvers runs. All SuiteSparse libraries, including libklu, allocate through that one shared setting. So this sequence mixes allocators:
klu(A)allocates with libc'smalloc.lu,cholesky,qror\on any sparse matrix switches the allocator tojl_malloc.jl_free, which counts them as freed although the GC never counted them as allocated.After that, GC can become extremely slow. On Linux with Julia 1.13, a single
GC.gc()took about 23s afterqrwith two large factorizations, against 0.015s without the switch.klu, thenlu, then freeing 20 large factorizations (m = 200) stalled the process until it was killed at 45s. Julia 1.12 needed more or larger factorizations to trigger it. The accounting explanation in step 3 fits the source and these results, but I haven't traced it inside the GC.Julia 1.10 and 1.11 set the allocator when SparseArrays loads, so they don't have this bug, and the call is skipped there.
Results with this PR
Both stalling cases above now finish on 1.12 and 1.13, with at most 3 GCs in the allocation loop.
The GC can now see libklu's memory. 150 rounds of factoring and discarding on Julia 1.13, measured once:
mainThings to know
set_libdir!: on SparseArraysmain, it throws once a library has loaded, and__init__now loads them atusing KLU, so it would have to be called before that. Julia 1.12 and 1.13 don't have this function.Testing
A new test runs in a subprocess and checks that a later
ludoesn't change the allocator. It fails onmainwith Julia 1.13.The full suite passes locally on Linux with Julia 1.10, 1.11, 1.12 and 1.13 at
-t 2, and Aqua passes.🤖 Generated with Claude Code