Skip to content

Commit 1f221e3

Browse files
authored
Fix for sparse-to-sparse matrix broadcast (#686)
* Fix for sparse-to-sparse matrix broadcast * Add test * Fix test * Fix again * stupid fix * Let int types work too
1 parent 69b2df8 commit 1f221e3

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/host/sparse.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,6 @@ end
670670
if leading_dim leading_dim_size
671671
iter = @inbounds iter_type(T, Ti)(leading_dim, args...)
672672

673-
674673
output_ptrs = output isa GPUSparseDeviceMatrixCSR ? output.rowPtr : output.colPtr
675674
output_ivals = output isa GPUSparseDeviceMatrixCSR ? output.colVal : output.rowVal
676675
# fetch the row offset, and write it to the output
@@ -691,7 +690,6 @@ end
691690
ptr = @inbounds ptrs[i]
692691
_getindex(arg, I, ptr)
693692
end
694-
695693
@inbounds output_ivals[output_ptr] = sub_leading_dim
696694
@inbounds output.nzVal[output_ptr] = f(vals...)
697695
output_ptr += one(Ti)
@@ -903,7 +901,13 @@ function Broadcast.copy(bc::Broadcasted{<:Union{GPUSparseVecStyle,GPUSparseMatSt
903901
if output isa AbstractGPUSparseArray
904902
args = (bc.f, output, offsets, bc.args...)
905903
kernel = sparse_to_sparse_broadcast_kernel(get_backend(bc.args[first(sparse_args)]))
906-
ndrange = output.nnz
904+
ndrange = if sparse_typ <: AbstractGPUSparseVector
905+
output.nnz
906+
elseif sparse_typ <: AbstractGPUSparseMatrixCSC
907+
size(output, 2)
908+
else
909+
size(output, 1)
910+
end
907911
else
908912
args = sparse_typ <: AbstractGPUSparseVector ? (sparse_typ, bc.f, output, offsets, bc.args...) :
909913
(sparse_typ, bc.f, output, bc.args...)

test/testsuite/sparse.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,19 @@ function broadcasting_matrix(AT, eltypes)
283283
dz = dx .* dy .* dw
284284
@test dz isa AT{ET}
285285
@test z == SparseMatrixCSC(dz)
286+
287+
# create a matrix with nnz < leading_dim
288+
x = spdiagm(m, m, 2=>rand(ET, m - 2))
289+
dx = AT(x)
290+
y = ET(3) * x
291+
dy = ET(3) * dx
292+
@test y == SparseMatrixCSC(dy)
293+
294+
x = spdiagm(m, m, -2=>rand(ET, m - 2))
295+
dx = AT(x)
296+
y = ET(3) * x
297+
dy = ET(3) * dx
298+
@test y == SparseMatrixCSC(dy)
286299
end
287300
end
288301
end

0 commit comments

Comments
 (0)