Skip to content

Fixes and tests for COO indexing, exclude more kernels from coverage - #2668

Merged
maleadt merged 1 commit into
masterfrom
ksh/more_sparse
Feb 20, 2025
Merged

maleadt merged 1 commit into
masterfrom
ksh/more_sparse

Conversation

@kshyatt

@kshyatt kshyatt commented Feb 19, 2025

Copy link
Copy Markdown
Member

Added support for :-based indexing of COO arrays and fixed an issue - if the r2 variable in the diff there isn't capped at the length of the underlying array, you can get an out of bounds error! Also added tests for the new stuff and excluded some more device side code from coverage

@kshyatt kshyatt added cuda array Stuff about CuArray. bugfix This gets something working again. tests Adds or changes tests. labels Feb 19, 2025
@github-actions

Copy link
Copy Markdown
Contributor

Your PR requires formatting changes to meet the project's style guidelines.
Please consider running Runic (git runic master) to apply these changes.

Click here to view the suggested changes.
diff --git a/lib/cusparse/array.jl b/lib/cusparse/array.jl
index 9afa9583e..711f76c24 100644
--- a/lib/cusparse/array.jl
+++ b/lib/cusparse/array.jl
@@ -387,13 +387,13 @@ end
 
 function Base.getindex(x::CuSparseMatrixCOO{T}, i::Integer, ::Colon) where {T}
     checkbounds(x, i, :)
-    if issorted(x.rowInd)
+    return if issorted(x.rowInd)
         row_start = searchsortedfirst(x.rowInd, i)
-        row_end   = min(searchsortedlast(x.rowInd, i), length(x.rowInd))
+        row_end = min(searchsortedlast(x.rowInd, i), length(x.rowInd))
         row_start == length(x.rowInd) + 1 && return CuSparseVector(similar(x.rowInd, 0), CUDA.zeros(T, 0), size(x, 2))
         CuSparseVector(x.colInd[row_start:row_end], x.nzVal[row_start:row_end], size(x, 2))
     else
-        row_inds = findall(ix->ix == i, x.rowInd)
+        row_inds = findall(ix -> ix == i, x.rowInd)
         isnothing(row_inds) && return CuSparseVector(similar(x.rowInd, 0), CUDA.zeros(T, 0), size(x, 2))
         CuSparseVector(x.colInd[row_inds], x.nzVal[row_inds], size(x, 2))
     end
@@ -401,13 +401,13 @@ end
 
 function Base.getindex(x::CuSparseMatrixCOO{T}, ::Colon, j::Integer) where {T}
     checkbounds(x, :, j)
-    if issorted(x.colInd)
+    return if issorted(x.colInd)
         col_start = searchsortedfirst(x.colInd, j)
-        col_end   = min(searchsortedlast(x.colInd, j), length(x.colInd))
+        col_end = min(searchsortedlast(x.colInd, j), length(x.colInd))
         col_start == length(x.colInd) + 1 && return CuSparseVector(similar(x.colInd, 0), CUDA.zeros(T, 0), size(x, 2))
         CuSparseVector(x.rowInd[col_start:col_end], x.nzVal[col_start:col_end], size(x, 1))
     else
-        col_inds = findall(ix->ix == j, x.colInd)
+        col_inds = findall(ix -> ix == j, x.colInd)
         isnothing(col_inds) && return CuSparseVector(similar(x.colInd, 0), CUDA.zeros(T, 0), size(x, 1))
         CuSparseVector(x.rowInd[col_inds], x.nzVal[col_inds], size(x, 1))
     end
@@ -448,7 +448,7 @@ function Base.getindex(A::CuSparseMatrixCOO{T}, i0::Integer, i1::Integer) where
     @boundscheck checkbounds(A, i0, i1)
     r1 = searchsortedfirst(A.rowInd, i0, Base.Order.Forward)
     (r1 > length(A.rowInd) || A.rowInd[r1] > i0) && return zero(T)
-    r2 = min(searchsortedfirst(A.rowInd, i0+1, Base.Order.Forward), length(A.rowInd))
+    r2 = min(searchsortedfirst(A.rowInd, i0 + 1, Base.Order.Forward), length(A.rowInd))
     c1 = searchsortedfirst(A.colInd, i1, r1, r2, Base.Order.Forward)
     (c1 > r2 || c1 == length(A.colInd) + 1 || A.colInd[c1] > i1) && return zero(T)
     nonzeros(A)[c1]
diff --git a/test/libraries/cusparse.jl b/test/libraries/cusparse.jl
index 2d75623c7..ee6b525b8 100644
--- a/test/libraries/cusparse.jl
+++ b/test/libraries/cusparse.jl
@@ -75,27 +75,27 @@ blockdim = 5
     y = sprand(k,n,0.2)
     d_y = CuSparseMatrixCSC(y)
     @test_throws ArgumentError copyto!(d_y,d_x)
-    x = sprand(m,n,0.2)
+    x = sprand(m, n, 0.2)
     d_x = CuSparseMatrixCOO(x)
     @test CuSparseMatrixCOO(d_x) === d_x
-    @test length(d_x) == m*n
-    @test size(d_x)   == (m,n)
-    @test size(d_x,1) == m
-    @test size(d_x,2) == n
-    @test size(d_x,3) == 1
-    @test ndims(d_x)  == 2
+    @test length(d_x) == m * n
+    @test size(d_x) == (m, n)
+    @test size(d_x, 1) == m
+    @test size(d_x, 2) == n
+    @test size(d_x, 3) == 1
+    @test ndims(d_x) == 2
     d_x2 = copy(d_x)
     @test d_x2 isa CuSparseMatrixCOO
     @test size(d_x2) == size(d_x)
     @test length(d_x) == length(x)
     CUDA.@allowscalar begin
-        @test Array(d_x[:])        == x[:]
+        @test Array(d_x[:]) == x[:]
         @test d_x[firstindex(d_x)] == x[firstindex(x)]
-        @test d_x[div(end, 2)]     == x[div(end, 2)]
-        @test d_x[end]             == x[end]
+        @test d_x[div(end, 2)] == x[div(end, 2)]
+        @test d_x[end] == x[end]
         @test d_x[firstindex(d_x), firstindex(d_x)] == x[firstindex(x), firstindex(x)]
-        @test d_x[div(end, 2), div(end, 2)]         == x[div(end, 2), div(end, 2)]
-        @test d_x[end, end]        == x[end, end]
+        @test d_x[div(end, 2), div(end, 2)] == x[div(end, 2), div(end, 2)]
+        @test d_x[end, end] == x[end, end]
         @test Array(d_x[firstindex(d_x):end]) == x[:]
         for i in 1:size(x, 2)
             @test Array(d_x[:, i]) == x[:, i]
@@ -104,9 +104,9 @@ blockdim = 5
             @test Array(d_x[i, :]) == x[i, :]
         end
     end
-    y = sprand(k,n,0.2)
+    y = sprand(k, n, 0.2)
     d_y = CuSparseMatrixCOO(y)
-    @test_throws ArgumentError copyto!(d_y,d_x)
+    @test_throws ArgumentError copyto!(d_y, d_x)
     d_y = CuSparseMatrixCSR(d_y)
     d_x = CuSparseMatrixCSR(d_x)
     @test CuSparseMatrixCSR(d_x) === d_x

@maleadt
maleadt merged commit 1786fd1 into master Feb 20, 2025
@maleadt
maleadt deleted the ksh/more_sparse branch February 20, 2025 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix This gets something working again. cuda array Stuff about CuArray. tests Adds or changes tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants