# Using sparse matrix in CUDA kernel

**URL:** https://discourse.julialang.org/t/using-sparse-matrix-in-cuda-kernel/106704
**Category:** GPU
**Tags:** question, gpu
**Created:** [November 25, 2023, 9:41am UTC](https://discourse.julialang.org/t/using-sparse-matrix-in-cuda-kernel/106704 "2023-11-25T09:41:27Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![joel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joel/32/204349_2.png) [@joel](https://discourse.julialang.org/u/joel)
#### Post date: [November 25, 2023, 9:41am UTC](https://discourse.julialang.org/t/using-sparse-matrix-in-cuda-kernel/106704/1 "2023-11-25T09:41:27Z")

</div>

Hi,  
I’m trying to call `nonzeros` on a sparse matrix in my CUDA kernel but I’m getting a dynamic function invocation error: `unsupported dynamic function invocation (call to nonzeros)`.

Is this simply not supported or am I missing something?

Here’s an example that should reproduce the error:

```julia
using CUDA
using SparseArrays

function kernel(sm)
    vals = nonzeros(sm)
    for val in vals
        @cuprintln("$val")
    end
    nothing
end

sm = cu(sprand(5, 5, 0.5))

@cuda kernel(sm)

```

---

<div class="post-metadata">

### Author: ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)
#### Post date: [November 27, 2023, 5:05pm UTC](https://discourse.julialang.org/t/using-sparse-matrix-in-cuda-kernel/106704/2 "2023-11-27T17:05:24Z")

</div>

Device-side functionality for sparse arrays is practically nonexisting. Many sparse array-related functions (like simply indexing) would require iteration, which is not something you want to do on each thread.

If you’re instead looking into actually implementing sparse array kernels, have a look at the implementation of broadcast for sparse arrays in the CUDA.jl source code, [https://github.com/JuliaGPU/CUDA.jl/blob/master/lib/cusparse/broadcast.jl](https://github.com/JuliaGPU/CUDA.jl/blob/master/lib/cusparse/broadcast.jl), but beware that this isn’t simple code. For simple element-wise operations like broadcast you can basically work on a thread per compressed row (or column) and use a for loop to iterate elements, which is what the linked code does through iteration helper structures to deduplicate code, but for more complex operations (like matmul) that isn’t viable.
