Skip to content

Commit 1786fd1

Browse files
authored
Fixes and tests for COO indexing, exclude more kernels from coverage (#2668)
1 parent 5b470c4 commit 1786fd1

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

lib/cusparse/array.jl

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,34 @@ function Base.getindex(x::CuSparseMatrixCSR, i::Integer, ::Colon)
385385
CuSparseVector(x.colVal[c1:c2], nonzeros(x)[c1:c2], size(x, 2))
386386
end
387387

388+
function Base.getindex(x::CuSparseMatrixCOO{T}, i::Integer, ::Colon) where {T}
389+
checkbounds(x, i, :)
390+
if issorted(x.rowInd)
391+
row_start = searchsortedfirst(x.rowInd, i)
392+
row_end = min(searchsortedlast(x.rowInd, i), length(x.rowInd))
393+
row_start == length(x.rowInd) + 1 && return CuSparseVector(similar(x.rowInd, 0), CUDA.zeros(T, 0), size(x, 2))
394+
CuSparseVector(x.colInd[row_start:row_end], x.nzVal[row_start:row_end], size(x, 2))
395+
else
396+
row_inds = findall(ix->ix == i, x.rowInd)
397+
isnothing(row_inds) && return CuSparseVector(similar(x.rowInd, 0), CUDA.zeros(T, 0), size(x, 2))
398+
CuSparseVector(x.colInd[row_inds], x.nzVal[row_inds], size(x, 2))
399+
end
400+
end
401+
402+
function Base.getindex(x::CuSparseMatrixCOO{T}, ::Colon, j::Integer) where {T}
403+
checkbounds(x, :, j)
404+
if issorted(x.colInd)
405+
col_start = searchsortedfirst(x.colInd, j)
406+
col_end = min(searchsortedlast(x.colInd, j), length(x.colInd))
407+
col_start == length(x.colInd) + 1 && return CuSparseVector(similar(x.colInd, 0), CUDA.zeros(T, 0), size(x, 2))
408+
CuSparseVector(x.rowInd[col_start:col_end], x.nzVal[col_start:col_end], size(x, 1))
409+
else
410+
col_inds = findall(ix->ix == j, x.colInd)
411+
isnothing(col_inds) && return CuSparseVector(similar(x.colInd, 0), CUDA.zeros(T, 0), size(x, 1))
412+
CuSparseVector(x.rowInd[col_inds], x.nzVal[col_inds], size(x, 1))
413+
end
414+
end
415+
388416
# row slices
389417
Base.getindex(A::CuSparseMatrixCSC, i::Integer, ::Colon) = CuSparseVector(sparse(A[i, 1:end])) # TODO: optimize
390418
Base.getindex(A::CuSparseMatrixCSR, ::Colon, j::Integer) = CuSparseVector(sparse(A[1:end, j])) # TODO: optimize
@@ -420,9 +448,9 @@ function Base.getindex(A::CuSparseMatrixCOO{T}, i0::Integer, i1::Integer) where
420448
@boundscheck checkbounds(A, i0, i1)
421449
r1 = searchsortedfirst(A.rowInd, i0, Base.Order.Forward)
422450
(r1 > length(A.rowInd) || A.rowInd[r1] > i0) && return zero(T)
423-
r2 = searchsortedfirst(A.rowInd, i0+1, Base.Order.Forward)
451+
r2 = min(searchsortedfirst(A.rowInd, i0+1, Base.Order.Forward), length(A.rowInd))
424452
c1 = searchsortedfirst(A.colInd, i1, r1, r2, Base.Order.Forward)
425-
(c1 > r2 || A.colInd[c1] > i1) && return zero(T)
453+
(c1 > r2 || c1 == length(A.colInd) + 1 || A.colInd[c1] > i1) && return zero(T)
426454
nonzeros(A)[c1]
427455
end
428456

lib/cusparse/broadcast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ end
101101
@inline _zeros_eltypes(A, Bs...) = (zero(eltype(A)), _zeros_eltypes(Bs...)...)
102102

103103

104+
## COV_EXCL_START
104105
## iteration helpers
105106

106107
"""
@@ -300,7 +301,6 @@ _getindex(arg, I, ptr) = Broadcast._broadcast_getindex(arg, I)
300301
## sparse broadcast implementation
301302

302303
# TODO: unify CSC/CSR kernels
303-
## COV_EXCL_START
304304
# kernel to count the number of non-zeros in a row, to determine the row offsets
305305
function compute_offsets_kernel(::Type{<:CuSparseMatrixCSR}, offsets::AbstractVector{Ti},
306306
args...) where Ti

lib/cusparse/reduce.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function Base.mapreduce(f, op, A::Union{CuSparseMatrixCSR,CuSparseMatrixCSC};
5050
end
5151
end
5252

53+
## COV_EXCL_START
5354
function csr_reduce_kernel(f::F, op::OP, neutral, output::CuDeviceArray, args...) where {F, OP}
5455
# every thread processes an entire row
5556
row = threadIdx().x + (blockIdx().x - 1i32) * blockDim().x
@@ -95,3 +96,4 @@ function csc_reduce_kernel(f::F, op::OP, neutral, output::CuDeviceArray, args...
9596
@inbounds output[col] = val
9697
return
9798
end
99+
## COV_EXCL_STOP

test/libraries/cusparse.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,38 @@ blockdim = 5
7575
y = sprand(k,n,0.2)
7676
d_y = CuSparseMatrixCSC(y)
7777
@test_throws ArgumentError copyto!(d_y,d_x)
78+
x = sprand(m,n,0.2)
79+
d_x = CuSparseMatrixCOO(x)
80+
@test CuSparseMatrixCOO(d_x) === d_x
81+
@test length(d_x) == m*n
82+
@test size(d_x) == (m,n)
83+
@test size(d_x,1) == m
84+
@test size(d_x,2) == n
85+
@test size(d_x,3) == 1
86+
@test ndims(d_x) == 2
87+
d_x2 = copy(d_x)
88+
@test d_x2 isa CuSparseMatrixCOO
89+
@test size(d_x2) == size(d_x)
90+
@test length(d_x) == length(x)
91+
CUDA.@allowscalar begin
92+
@test Array(d_x[:]) == x[:]
93+
@test d_x[firstindex(d_x)] == x[firstindex(x)]
94+
@test d_x[div(end, 2)] == x[div(end, 2)]
95+
@test d_x[end] == x[end]
96+
@test d_x[firstindex(d_x), firstindex(d_x)] == x[firstindex(x), firstindex(x)]
97+
@test d_x[div(end, 2), div(end, 2)] == x[div(end, 2), div(end, 2)]
98+
@test d_x[end, end] == x[end, end]
99+
@test Array(d_x[firstindex(d_x):end]) == x[:]
100+
for i in 1:size(x, 2)
101+
@test Array(d_x[:, i]) == x[:, i]
102+
end
103+
for i in 1:size(x, 1)
104+
@test Array(d_x[i, :]) == x[i, :]
105+
end
106+
end
107+
y = sprand(k,n,0.2)
108+
d_y = CuSparseMatrixCOO(y)
109+
@test_throws ArgumentError copyto!(d_y,d_x)
78110
d_y = CuSparseMatrixCSR(d_y)
79111
d_x = CuSparseMatrixCSR(d_x)
80112
@test CuSparseMatrixCSR(d_x) === d_x
@@ -151,6 +183,7 @@ end
151183
@test size(similar(d_x, (3, 4))) == (3, 4)
152184
@test similar(d_x, Float32) isa CuSparseMatrixCSC{Float32}
153185
@test similar(d_x, Float32, n, m) isa CuSparseMatrixCSC{Float32}
186+
@test similar(d_x, Float32, (n, m)) isa CuSparseMatrixCSC{Float32}
154187
end
155188

156189
@testset "CSR" begin
@@ -162,6 +195,7 @@ end
162195
@test size(similar(d_x, (3, 4))) == (3, 4)
163196
@test similar(d_x, Float32) isa CuSparseMatrixCSR{Float32}
164197
@test similar(d_x, Float32, n, m) isa CuSparseMatrixCSR{Float32}
198+
@test similar(d_x, Float32, (n, m)) isa CuSparseMatrixCSR{Float32}
165199
end
166200

167201
@testset "COO" begin
@@ -171,7 +205,9 @@ end
171205
@test similar(d_x) isa CuSparseMatrixCOO{elty}
172206
@test similar(d_x, (3, 4)) isa CuSparseMatrixCOO{elty}
173207
@test size(similar(d_x, (3, 4))) == (3, 4)
208+
@test size(similar(d_x, Float64, (3, 4))) == (3, 4)
174209
@test similar(d_x, Float32) isa CuSparseMatrixCOO{Float32}
210+
@test CuSparseMatrixCOO(d_x) === d_x
175211
end
176212

177213
@testset "BSR" begin

0 commit comments

Comments
 (0)