-
-
Notifications
You must be signed in to change notification settings - Fork 67
Add CUSOLVERRF.jl integration for GPU-accelerated sparse LU factorization #673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ChrisRackauckas
merged 26 commits into
SciML:main
from
ChrisRackauckas-Claude:add-cusolverrf-support
Aug 6, 2025
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
1cdb1be
Add CUSOLVERRF.jl integration for GPU-accelerated sparse LU factoriza…
claude e40ad85
Update Project.toml
ChrisRackauckas 6c1633d
Add CUSOLVERRF tests to GPU test suite
claude 0e2e254
Add CUSOLVERRF documentation
ChrisRackauckas e61de2f
Update GPU sparse solver docs to mention both CUDSS and CUSOLVERRF
ChrisRackauckas eee3ff4
Fix CUDSS documentation to correctly describe LUFactorization usage
ChrisRackauckas cc7911b
Update Project.toml
ChrisRackauckas 235e333
Update factorization.jl
ChrisRackauckas f784d42
Update extension_algs.jl
ChrisRackauckas 0ac5d28
Update solvers.md
ChrisRackauckas d7f1f8c
Update Project.toml
ChrisRackauckas 0a075fe
Update src/extension_algs.jl
ChrisRackauckas 1c1e917
Update src/extension_algs.jl
ChrisRackauckas b92906c
Update Project.toml
ChrisRackauckas e88bad8
Update Project.toml
ChrisRackauckas 82fbc55
Update Project.toml
ChrisRackauckas 7a8dac7
Update ext/LinearSolveCUSOLVERRFExt.jl
ChrisRackauckas 288d382
Update ext/LinearSolveCUSOLVERRFExt.jl
ChrisRackauckas 62bc9ae
Update ext/LinearSolveCUSOLVERRFExt.jl
ChrisRackauckas d559e8b
Update ext/LinearSolveCUSOLVERRFExt.jl
ChrisRackauckas 5175137
Update ext/LinearSolveCUSOLVERRFExt.jl
ChrisRackauckas f1f3bb8
Update test/gpu/cusolverrf.jl
ChrisRackauckas 6db7c55
Update ext/LinearSolveCUSOLVERRFExt.jl
ChrisRackauckas b8ca961
Update ext/LinearSolveCUSOLVERRFExt.jl
ChrisRackauckas 6a96db1
Update test/gpu/cusolverrf.jl
ChrisRackauckas b4bd9ed
Update resolve.jl
ChrisRackauckas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
module LinearSolveCUSOLVERRFExt | ||
|
||
using LinearSolve: LinearSolve, @get_cacheval, pattern_changed, OperatorAssumptions | ||
using CUSOLVERRF: CUSOLVERRF, RFLU, CUDA | ||
using SparseArrays: SparseArrays, SparseMatrixCSC, nnz | ||
using CUSOLVERRF.CUDA.CUSPARSE: CuSparseMatrixCSR | ||
using LinearAlgebra: LinearAlgebra, Adjoint, ldiv!, lu! | ||
using SciMLBase: SciMLBase, LinearProblem, ReturnCode | ||
|
||
function LinearSolve.init_cacheval(alg::LinearSolve.CUSOLVERRFFactorization, | ||
A, b, u, Pl, Pr, | ||
maxiters::Int, abstol, reltol, | ||
verbose::Bool, assumptions::OperatorAssumptions) | ||
nothing | ||
end | ||
|
||
function LinearSolve.init_cacheval(alg::LinearSolve.CUSOLVERRFFactorization, | ||
A::Union{CuSparseMatrixCSR{Float64, Int32}, SparseMatrixCSC{Float64, <:Integer}}, | ||
b, u, Pl, Pr, | ||
maxiters::Int, abstol, reltol, | ||
verbose::Bool, assumptions::OperatorAssumptions) | ||
# Create initial factorization with appropriate options | ||
nrhs = b isa AbstractMatrix ? size(b, 2) : 1 | ||
symbolic = alg.symbolic | ||
# Convert to CuSparseMatrixCSR if needed | ||
A_gpu = A isa CuSparseMatrixCSR ? A : CuSparseMatrixCSR(A) | ||
RFLU(A_gpu; nrhs=nrhs, symbolic=symbolic) | ||
end | ||
|
||
function SciMLBase.solve!(cache::LinearSolve.LinearCache, alg::LinearSolve.CUSOLVERRFFactorization; kwargs...) | ||
A = cache.A | ||
|
||
# Convert to appropriate GPU format if needed | ||
if A isa SparseMatrixCSC | ||
A_gpu = CuSparseMatrixCSR(A) | ||
elseif A isa CuSparseMatrixCSR | ||
A_gpu = A | ||
else | ||
error("CUSOLVERRFFactorization only supports SparseMatrixCSC or CuSparseMatrixCSR matrices") | ||
end | ||
|
||
if cache.isfresh | ||
cacheval = @get_cacheval(cache, :CUSOLVERRFFactorization) | ||
if cacheval === nothing | ||
# Create new factorization | ||
nrhs = cache.b isa AbstractMatrix ? size(cache.b, 2) : 1 | ||
fact = RFLU(A_gpu; nrhs=nrhs, symbolic=alg.symbolic) | ||
else | ||
# Reuse symbolic factorization if pattern hasn't changed | ||
if alg.reuse_symbolic && !pattern_changed(cacheval, A_gpu) | ||
fact = cacheval | ||
lu!(fact, A_gpu) | ||
else | ||
# Create new factorization if pattern changed | ||
nrhs = cache.b isa AbstractMatrix ? size(cache.b, 2) : 1 | ||
fact = RFLU(A_gpu; nrhs=nrhs, symbolic=alg.symbolic) | ||
end | ||
end | ||
cache.cacheval = fact | ||
cache.isfresh = false | ||
end | ||
|
||
F = @get_cacheval(cache, :CUSOLVERRFFactorization) | ||
|
||
# Ensure b and u are on GPU | ||
b_gpu = cache.b isa CUDA.CuArray ? cache.b : CUDA.CuArray(cache.b) | ||
u_gpu = cache.u isa CUDA.CuArray ? cache.u : CUDA.CuArray(cache.u) | ||
|
||
# Solve | ||
copyto!(u_gpu, b_gpu) | ||
ldiv!(F, u_gpu) | ||
|
||
# Copy back to CPU if needed | ||
if !(cache.u isa CUDA.CuArray) | ||
copyto!(cache.u, u_gpu) | ||
end | ||
|
||
SciMLBase.build_linear_solution(alg, cache.u, nothing, cache; retcode = ReturnCode.Success) | ||
end | ||
|
||
# Helper function for pattern checking | ||
function LinearSolve.pattern_changed(rf::RFLU, A::CuSparseMatrixCSR) | ||
# For CUSOLVERRF, we need to check if the sparsity pattern has changed | ||
# This is a simplified check - you might need a more sophisticated approach | ||
size(rf) != size(A) || nnz(rf.M) != nnz(A) | ||
end | ||
|
||
|
||
end |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using LinearSolve | ||
using CUSOLVERRF | ||
using CUDA | ||
using SparseArrays | ||
using LinearAlgebra | ||
using Test | ||
|
||
@testset "CUSOLVERRFFactorization" begin | ||
# Skip tests if CUDA is not available | ||
if !CUDA.functional() | ||
@info "CUDA not available, skipping CUSOLVERRF tests" | ||
return | ||
end | ||
|
||
# Test with a small sparse matrix | ||
n = 100 | ||
A = sprand(n, n, 0.1) + I | ||
b = rand(n) | ||
|
||
# Test with CPU sparse matrix (should auto-convert to GPU) | ||
@testset "CPU Sparse Matrix" begin | ||
prob = LinearProblem(A, b) | ||
|
||
# Test with default symbolic (:RF) | ||
sol = solve(prob, CUSOLVERRFFactorization()) | ||
@test norm(A * sol.u - b) / norm(b) < 1e-10 | ||
|
||
# Test with KLU symbolic | ||
sol_klu = solve(prob, CUSOLVERRFFactorization(symbolic = :KLU)) | ||
@test norm(A * sol_klu.u - b) / norm(b) < 1e-10 | ||
end | ||
|
||
# Test with GPU sparse matrix | ||
@testset "GPU Sparse Matrix" begin | ||
A_gpu = CUDA.CUSPARSE.CuSparseMatrixCSR(A) | ||
b_gpu = CuArray(b) | ||
|
||
prob_gpu = LinearProblem(A_gpu, b_gpu) | ||
sol_gpu = solve(prob_gpu, CUSOLVERRFFactorization()) | ||
|
||
# Check residual on GPU | ||
res_gpu = A_gpu * sol_gpu.u - b_gpu | ||
@test norm(res_gpu) / norm(b_gpu) < 1e-10 | ||
end | ||
|
||
# Test matrix update with same sparsity pattern | ||
@testset "Matrix Update" begin | ||
# Create a new matrix with same pattern but different values | ||
A2 = A + 0.1 * sprand(n, n, 0.01) | ||
b2 = rand(n) | ||
|
||
prob2 = LinearProblem(A2, b2) | ||
sol2 = solve(prob2, CUSOLVERRFFactorization(reuse_symbolic = true)) | ||
@test norm(A2 * sol2.u - b2) / norm(b2) < 1e-10 | ||
end | ||
|
||
# Test error handling for unsupported types | ||
@testset "Error Handling" begin | ||
# Test with Float32 (not supported) | ||
A_f32 = Float32.(A) | ||
b_f32 = Float32.(b) | ||
prob_f32 = LinearProblem(A_f32, b_f32) | ||
|
||
# This should error since CUSOLVERRF only supports Float64 | ||
@test_throws Exception solve(prob_f32, CUSOLVERRFFactorization()) | ||
end | ||
end |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.