Skip to content

Conversation

Zinoex
Copy link
Contributor

@Zinoex Zinoex commented Sep 29, 2025

I have a usecase where I effectively need to iterate columns of a sparse matrix and access its nonzero indices and values, which fails without the functions added in this PR. And preferably, I would want to avoid to manually keep track of the internals during the iteration.

Below I attach a MWE to that fails to compile without the added functions. The concrete usecase is more complicated than the MWE with other additional logic and computation, and multiple threads per column, but it is not important to show the error.

using CUDA, SparseArrays

function mwe()
    A = cu(sprand(1000, 1000, 0.01))
    @cuda threads=256 kernel(A)
end

function kernel(A)
    i = threadIdx().x
    while i <= size(A, 2)
        col = @view A[:, i]
        inds = rowvals(col)
        vals = nonzeros(col)
        num_nonzeros = nnz(col)

        v = col[inds[1]]

        i += blockDim().x
    end
end

Copy link
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/device.jl b/lib/cusparse/device.jl
index 90a7b5e6f..aa6778d5d 100644
--- a/lib/cusparse/device.jl
+++ b/lib/cusparse/device.jl
@@ -23,7 +23,7 @@ Base.length(g::CuSparseDeviceVector) = g.len
 Base.size(g::CuSparseDeviceVector) = (g.len,)
 SparseArrays.nnz(g::CuSparseDeviceVector) = g.nnz
 
-struct CuSparseDeviceMatrixCSC{Tv,Ti,A} <: SparseArrays.AbstractSparseMatrixCSC{Tv,Ti}
+struct CuSparseDeviceMatrixCSC{Tv, Ti, A} <: SparseArrays.AbstractSparseMatrixCSC{Tv, Ti}
     colPtr::CuDeviceVector{Ti, A}
     rowVal::CuDeviceVector{Ti, A}
     nzVal::CuDeviceVector{Tv, A}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant