Skip to content

Commit ded449e

Browse files
committed
add test for CuSparseMatrixCSR + Cholesky
1 parent 7769ca3 commit ded449e

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

test/gpu/cuda.jl

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,66 @@
11
using LinearSolve, CUDA, LinearAlgebra, SparseArrays, StableRNGs
2-
using CUDA.CUSPARSE, CUDSS
2+
using CUDA.CUSPARSE
33
using Test
44

5+
@testset "Test default solver choice for CuSparse" begin
6+
b = Float64[1, 2, 3, 4]
7+
b_gpu = CUDA.adapt(CuArray, b)
8+
9+
A = Float64[1 1 0 0
10+
0 1 1 0
11+
0 0 3 1
12+
0 0 0 4]
13+
A_gpu_csr = CUDA.CUSPARSE.CuSparseMatrixCSR(sparse(A))
14+
A_gpu_csc = CUDA.CUSPARSE.CuSparseMatrixCSC(sparse(A))
15+
prob_csr = LinearProblem(A_gpu_csr, b_gpu)
16+
prob_csc = LinearProblem(A_gpu_csc, b_gpu)
17+
18+
A_sym = Float64[1 1 0 0
19+
1 0 0 2
20+
0 0 3 0
21+
0 2 0 0]
22+
A_gpu_sym_csr = CUDA.CUSPARSE.CuSparseMatrixCSR(sparse(A_sym))
23+
A_gpu_sym_csc = CUDA.CUSPARSE.CuSparseMatrixCSC(sparse(A_sym))
24+
prob_sym_csr = LinearProblem(A_gpu_sym_csr, b_gpu)
25+
prob_sym_csc = LinearProblem(A_gpu_sym_csc, b_gpu)
26+
27+
@testset "Test without CUDSS loaded" begin
28+
# assert CuDSS is not loaded yet
29+
@test !LinearSolve.cudss_loaded(A_gpu_csr)
30+
# csr fallback to krylov
31+
alg = solve(prob_csr).alg
32+
@test alg.alg == LinearSolve.DefaultAlgorithmChoice.KrylovJL_GMRES
33+
# csc fallback to krylov
34+
alg = solve(prob_csc).alg
35+
@test alg.alg == LinearSolve.DefaultAlgorithmChoice.KrylovJL_GMRES
36+
# csr symmetric fallback to krylov
37+
alg = solve(prob_sym_csr).alg
38+
@test alg.alg == LinearSolve.DefaultAlgorithmChoice.KrylovJL_GMRES
39+
# csc symmetric fallback to krylov
40+
alg = solve(prob_sym_csc).alg
41+
@test alg.alg == LinearSolve.DefaultAlgorithmChoice.KrylovJL_GMRES
42+
end
43+
44+
# after loading CUDSS, it should fall back tu LU factorization
45+
using CUDSS
46+
47+
@testset "Test with CUDSS loaded" begin
48+
@test LinearSolve.cudss_loaded(A_gpu_csr)
49+
# csr uses LU
50+
alg = solve(prob_csr).alg
51+
@test alg.alg == LinearSolve.DefaultAlgorithmChoice.LUFactorization
52+
# csc fallback to krylov
53+
alg = solve(prob_csc).alg
54+
@test alg.alg == LinearSolve.DefaultAlgorithmChoice.KrylovJL_GMRES
55+
# csr symmetric uses LU/cholesky
56+
alg = solve(prob_sym_csr).alg
57+
@test alg.alg == LinearSolve.DefaultAlgorithmChoice.LUFactorization
58+
# csc symetric fallback to krylov
59+
alg = solve(prob_sym_csc).alg
60+
@test alg.alg == LinearSolve.DefaultAlgorithmChoice.KrylovJL_GMRES
61+
end
62+
end
63+
564
CUDA.allowscalar(false)
665

766
n = 8
@@ -96,9 +155,9 @@ end
96155
@testset "CUDSS" begin
97156
T = Float32
98157
n = 100
99-
A_cpu = sprand(T, n, n, 0.05) + I
158+
A_cpu = sprand(rng, T, n, n, 0.05) + I
100159
x_cpu = zeros(T, n)
101-
b_cpu = rand(T, n)
160+
b_cpu = rand(rng, T, n)
102161

103162
A_gpu_csr = CuSparseMatrixCSR(A_cpu)
104163
b_gpu = CuVector(b_cpu)

0 commit comments

Comments
 (0)