Skip to content

Sparse GPU take 2 #181

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 2 commits into from
Feb 14, 2022
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
14 changes: 12 additions & 2 deletions src/differentiation/compute_jacobian_ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
sparsity = jac_cache.sparsity
chunksize = jac_cache.chunksize
color_i = 1
adaptedcolorvec = adapt(__parameterless_type(typeof(dx)),colorvec)

maxcolor = maximum(colorvec)

if J isa AbstractSparseMatrix
Expand Down Expand Up @@ -357,9 +359,17 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
+= means requires a zero'd out start
=#
if J isa AbstractSparseMatrix
@. setindex!((J.nzval,),getindex((J.nzval,),rows_index) + (getindex((colorvec,),cols_index) == color_i) * getindex((vecdx,),rows_index),rows_index)
if J isa SparseMatrixCSC
@. void_setindex!(Ref(nonzeros(J)),getindex(Ref(nonzeros(J)),rows_index) + (getindex(Ref(adaptedcolorvec),cols_index) == color_i) * getindex(Ref(vecdx),rows_index),rows_index)
else
nzval = @view nonzeros(J)[rows_index]
cv = @view adaptedcolorvec[cols_index]
vdx = @view dx[rows_index]
tmp = cv .== color_i
nzval .+= tmp .* vdx
end
else
@. setindex!((J,),getindex((J,),rows_index, cols_index) + (getindex((colorvec,),cols_index) == color_i) * getindex((vecdx,),rows_index),rows_index, cols_index)
@. void_setindex!(Ref(J),getindex(Ref(J),rows_index, cols_index) + (getindex(Ref(colorvec),cols_index) == color_i) * getindex(Ref(vecdx),rows_index),rows_index, cols_index)
end
end
color_i += 1
Expand Down
15 changes: 11 additions & 4 deletions test/test_gpu_ad.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
using SparseDiffTools, CUDA, Test, LinearAlgebra
using ArrayInterface: allowed_getindex, allowed_setindex!
using SparseArrays

function f(dx,x)
dx[2:end-1] = x[1:end-2] - 2x[2:end-1] + x[3:end]
allowed_setindex!(dx,-2allowed_getindex(x,1) + allowed_getindex(x,2),1)
allowed_setindex!(dx,-2allowed_getindex(x,30) + allowed_getindex(x,29),30)
nothing
end

x = rand(4)
_J1 = similar(rand(30,30))
_denseJ1 = cu(collect(_J1))
x = cu(rand(30))
CUDA.allowscalar(false)
forwarddiff_color_jacobian!(_denseJ1, f, x)
@test_broken forwarddiff_color_jacobian!(_denseJ1, f, x, sparsity = _J1) isa Nothing
@test_broken forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3,10), sparsity = _J1) isa Nothing
_J2 = sparse(forwarddiff_color_jacobian!(_denseJ1, f, x))
out = copy(_J2)
forwarddiff_color_jacobian!(out, f, x, colorvec = repeat(1:3,10), sparsity = _J2)

@test_broken forwarddiff_color_jacobian!(_denseJ1, f, x, sparsity = cu(_J1)) isa Nothing
@test_broken forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3,10), sparsity = cu(_J1)) isa Nothing
_Jt = similar(Tridiagonal(_J1))
@test_broken forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3,10), sparsity = _Jt) isa Nothing
_Jt2 = similar(Tridiagonal(cu(_J1)))
@test_broken forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3,10), sparsity = _Jt2) isa Nothing