Skip to content

Replace Base.convert(::Type{SparseArrays.SparseMatrixCSC}, J::SUNMat… #316

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
merged 1 commit into from
Jul 14, 2021
Merged
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
21 changes: 8 additions & 13 deletions src/common_interface/function_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,14 @@ function cvodejac(
tmp3::N_Vector,
)
jac_prototype = funjac.jac_prototype
J = convert(SparseArrays.SparseMatrixCSC, _J)


funjac.u = unsafe_wrap(Vector{Float64}, N_VGetArrayPointer_Serial(u), length(funjac.u))
_u = funjac.u

funjac.jac(jac_prototype, _u, funjac.p, t)
J.nzval .= jac_prototype.nzval
# Sundials resets the value pointers each time, so reset it too
@. J.rowval = jac_prototype.rowval - 1
@. J.colptr = jac_prototype.colptr - 1

copyto!(_J, jac_prototype)

return CV_SUCCESS
end

Expand Down Expand Up @@ -128,7 +126,6 @@ function idajac(
)

jac_prototype = funjac.jac_prototype
J = convert(SparseArrays.SparseMatrixCSC, _J)

funjac.u = unsafe_wrap(Vector{Float64}, N_VGetArrayPointer_Serial(u), length(funjac.u))
_u = funjac.u
Expand All @@ -137,10 +134,8 @@ function idajac(
_du = funjac.du

funjac.jac(jac_prototype, _du, _u, funjac.p, cj, t)
J.nzval .= jac_prototype.nzval
# Sundials resets the value pointers each time, so reset it too
@. J.rowval = jac_prototype.rowval - 1
@. J.colptr = jac_prototype.colptr - 1

copyto!(_J, jac_prototype)

return IDA_SUCCESS
end
Expand All @@ -155,10 +150,10 @@ function massmat(
)
if typeof(mmf.mass_matrix) <: Array
M = convert(Matrix, _M)
M .= mmf.mass_matrix
else
M = convert(SparseArrays.SparseMatrixCSC, _M)
copyto!(_M, mmf.mass_matrix)
end
M .= mmf.mass_matrix

return IDA_SUCCESS
end
Expand Down
24 changes: 13 additions & 11 deletions src/types_and_consts_additions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ function Base.convert(::Type{Matrix}, J::SUNMatrix)
unsafe_wrap(Array, mat.data, (mat.M, mat.N), own = false)
end

function Base.convert(::Type{SparseArrays.SparseMatrixCSC}, J::SUNMatrix)
_sunmat = unsafe_load(J)
# sparse SUNMatrix uses zero-offset indices, so provide copyto!, not convert
function Base.copyto!(Asun::SUNMatrix, Acsc::SparseArrays.SparseMatrixCSC)
_sunmat = unsafe_load(Asun)
_mat = convert(SUNMatrixContent_Sparse, _sunmat.content)
mat = unsafe_load(_mat)
# own is false as memory is allocated by sundials
# TODO: Get rid of allocation for 1-based index change
rowval = unsafe_wrap(Array, mat.indexvals, (mat.NNZ), own = false)
colptr = unsafe_wrap(Array, mat.indexptrs, (mat.NP + 1), own = false)
colptr .+= 1
m = mat.M
n = mat.N
nzval = unsafe_wrap(Array, mat.data, (mat.NNZ), own = false)
SparseArrays.SparseMatrixCSC(m, n, colptr, rowval, nzval)
# own is false as memory is allocated by sundials
indexvals = unsafe_wrap(Array, mat.indexvals, (mat.NNZ), own = false)
indexptrs = unsafe_wrap(Array, mat.indexptrs, (mat.NP + 1), own = false)
data = unsafe_wrap(Array, mat.data, (mat.NNZ), own = false)

@. indexvals = Acsc.rowval - 1
@. indexptrs = Acsc.colptr - 1
data .= Acsc.nzval

return nothing
end

abstract type SundialsMatrix end
Expand Down