Skip to content

Commit

Permalink
fix Dense case and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ranocha committed Jul 27, 2024
1 parent 1aae311 commit 7efa44e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/solvers/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1947,8 +1947,20 @@ for TI in IndexTypes
# `cholmod_dense_struct`s in CHOLMOD. Instead, we want to reuse
# the existing memory. We can do this by creating new
# `cholmod_dense_struct`s and filling them manually.
dense_x = wrap_dense(x)
dense_b = wrap_dense(b)
# We need to use a special handling for the case of `Dense`
# input arrays since the `pointer` refers to the pointer to the
# `cholmod_dense`, not to the array values themselves as for
# standard arrays.
if x isa Dense
dense_x = unsafe_load(pointer(x))
else
dense_x = wrap_dense(x)
end
if b isa Dense
dense_b = unsafe_load(pointer(b))
else
dense_b = wrap_dense(b)
end

X_Handle = Ptr{cholmod_dense_struct}(pointer_from_objref(dense_x))
Y_Handle = Ptr{cholmod_dense_struct}(C_NULL)
Expand Down
23 changes: 22 additions & 1 deletion test/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ using SparseArrays.LibSuiteSparse: cholmod_l_allocate_sparse, cholmod_allocate_s

# CHOLMOD tests
itypes = sizeof(Int) == 4 ? (Int32,) : (Int32, Int64)
for Ti itypes, Tv (Float32, Float64)
# for Ti ∈ itypes, Tv ∈ (Float32, Float64)
for Ti (Int64,), Tv (Float64,)
Random.seed!(123)

@testset "based on deps/SuiteSparse-4.0.2/CHOLMOD/Demo/ index type $Ti" begin
Expand Down Expand Up @@ -284,6 +285,26 @@ end
end
end

@testset "ldiv! $Tv $Ti" begin
local A, x, x2, b, X, X2, B
A = sprand(10, 10, 0.1)
A = I + A * A'
A = convert(SparseMatrixCSC{Tv,Ti}, A)
factor = cholesky(A)

x = fill(Tv(1), 10)
b = A * x
x2 = zero(x)
@inferred ldiv!(x2, factor, b)
@test x2 x

X = fill(Tv(1), 10, 5)
B = A * X
X2 = zero(X)
@inferred ldiv!(X2, factor, B)
@test X2 X
end

end #end for Ti ∈ itypes

for Tv (Float32, Float64)
Expand Down

0 comments on commit 7efa44e

Please sign in to comment.