Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion lib/OrdinaryDiffEqBDF/test/dae_nlsolve_path_tests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using OrdinaryDiffEqBDF, OrdinaryDiffEqCore, Test
using SciMLBase: DAEProblem
using SciMLBase: DAEFunction, DAEProblem, NoInit
using ADTypes: AutoFiniteDiff
using LinearAlgebra: I
using SparseArrays

function dae_lin!(res, du, u, p, t)
@. res = du - u
Expand Down Expand Up @@ -28,3 +31,38 @@ const dae_bdf_algs = (DImplicitEuler(), DABDF2(), DFBDF(), DNordsieckBDF())
end
end
end

# A sparse matrix type without `setindex!`, as `CuSparseMatrixCSR`: `fill!` on it throws,
# so the separated DAE Jacobian `J_du` has to be zeroed through its stored values.
struct NoSetindexSparse{T} <: SparseArrays.AbstractSparseMatrix{T, Int}
A::SparseMatrixCSC{T, Int}
end
Base.size(M::NoSetindexSparse) = size(M.A)
Base.getindex(M::NoSetindexSparse, i::Int, j::Int) = M.A[i, j]
Base.similar(M::NoSetindexSparse, ::Type{T}) where {T} = NoSetindexSparse(similar(M.A, T))
SparseArrays.nonzeros(M::NoSetindexSparse) = nonzeros(M.A)
SparseArrays.rowvals(M::NoSetindexSparse) = rowvals(M.A)
SparseArrays.nzrange(M::NoSetindexSparse, j::Integer) = nzrange(M.A, j)
SparseArrays.nnz(M::NoSetindexSparse) = nnz(M.A)

@testset "DAE J_du for a sparse jac_prototype without setindex!" begin
sparse_dae!(res, du, u, p, t) = (res .= du .+ u; nothing)
sparse_dae_jac!(J, du, u, p, gamma, t) = (nonzeros(J) .= 1 + gamma; nothing)
pattern = sparse(1.0I, 4, 4)
prototype = NoSetindexSparse(pattern)
for (label, f) in (
("user jac", DAEFunction{true}(sparse_dae!; jac = sparse_dae_jac!, jac_prototype = prototype)),
("AD jac", DAEFunction{true}(sparse_dae!; jac_prototype = prototype)),
)
@testset "$label" begin
prob = DAEProblem(f, fill(-1.0, 4), ones(4), (0.0, 1.0))
integrator = init(prob, DFBDF(autodiff = AutoFiniteDiff()); initializealg = NoInit())
J_du = integrator.cache.nlsolver.cache.dae_jacobians.J_du
@test J_du isa NoSetindexSparse{Float64}
@test rowvals(J_du) == rowvals(pattern)
@test [nzrange(J_du, j) for j in axes(J_du, 2)] ==
[nzrange(pattern, j) for j in axes(pattern, 2)]
@test all(iszero, nonzeros(J_du))
end
end
end
4 changes: 4 additions & 0 deletions lib/OrdinaryDiffEqBDF/test/gpu/Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
CUDSS = "45b445bb-4962-46a0-9369-b4df9d0f772e"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
OrdinaryDiffEqBDF = "6ad6398a-0878-4a85-9266-38940aa047c8"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[sources.OrdinaryDiffEqBDF]
path = "../.."

[compat]
CUDA = "6"
CUDSS = "0.8"
OrdinaryDiffEqBDF = "2"
18 changes: 17 additions & 1 deletion lib/OrdinaryDiffEqBDF/test/gpu/bdf_solvers.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#fixing gpu tolerances
using OrdinaryDiffEqBDF, CUDA, Test
using OrdinaryDiffEqBDF, CUDA, CUDSS, LinearAlgebra, SparseArrays, Test
using CUDA.CUSPARSE: CuSparseMatrixCSR
CUDA.allowscalar(false)

function f_gpu!(du, u, p, t)
Expand Down Expand Up @@ -30,3 +31,18 @@ end
@test sol_gpu.u[end] isa CuArray
@test isapprox(Array(sol_gpu.u[end]), sol_cpu.u[end], rtol = 1.0e-3)
end

# The separated DAE Jacobian J_du is allocated from the CuSparseMatrixCSR prototype,
# which has no `fill!`
@testset "DFBDF GPU sparse jac_prototype" begin
dae!(res, du, u, p, t) = (res .= du .+ u; nothing)
N = 8
prob_gpu = DAEProblem(
DAEFunction{true}(dae!; jac_prototype = CuSparseMatrixCSR(sparse(1.0I, N, N))),
CUDA.fill(-1.0, N), CUDA.ones(Float64, N), (0.0, 1.0)
)
sol_gpu = solve(prob_gpu, DFBDF(), abstol = 1.0e-6, reltol = 1.0e-6)
@test SciMLBase.successful_retcode(sol_gpu)
@test sol_gpu.u[end] isa CuArray
@test isapprox(Array(sol_gpu.u[end]), fill(exp(-1), N), rtol = 1.0e-3)
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module OrdinaryDiffEqNonlinearSolve

using ADTypes: ADTypes, AutoForwardDiff, AutoFiniteDiff
using SparseArrays: AbstractSparseMatrix, nonzeros
using CommonSolve: init, solve, solve!, step!

import SciMLBase
Expand Down
9 changes: 7 additions & 2 deletions lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -952,10 +952,16 @@ function build_nlsolver(

# Build separated DAE Jacobian cache if applicable
if isdae
# cuSPARSE matrices have no `fill!`, so sparse J_du is zeroed through its stored values
J_du = similar(J)
if J isa AbstractSparseMatrix
nonzeros(J_du) .= false
else
fill!(J_du, false)
end
if islinear(f) || SciMLBase.has_jac(f) ||
(SciMLBase.has_jac_u(f) && SciMLBase.has_jac_du(f))
# User provides Jacobian(s) — no wrappers, but still need J_du storage
J_du = fill!(similar(J), false)
dae_jacobians = DAEJacobiansCache(
J_du, nothing, nothing, nothing, nothing
)
Expand All @@ -968,7 +974,6 @@ function build_nlsolver(
jac_config_du = build_jac_config(
alg, nf, uf_du, du1, uprev, u, ztmp, dz
)
J_du = fill!(similar(J), false)
dae_jacobians = DAEJacobiansCache(
J_du, uf_u, uf_du, jac_config_u, jac_config_du
)
Expand Down
Loading