# How to perform a sparse matrix dense matrix product with addition (cuda library style)

**URL:** <https://discourse.julialang.org/t/how-to-perform-a-sparse-matrix-dense-matrix-product-with-addition-cuda-library-style/126093>\
**Category:** GPU\
**Created:** [February 20, 2025, 12:29am UTC](https://discourse.julialang.org/t/how-to-perform-a-sparse-matrix-dense-matrix-product-with-addition-cuda-library-style/126093 "2025-02-20T00:29:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![victor\_vhrn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victor_vhrn/32/214016_2.png) [@victor\_vhrn](https://discourse.julialang.org/u/victor_vhrn)\
**Post date:** [February 20, 2025, 12:29am UTC](https://discourse.julialang.org/t/how-to-perform-a-sparse-matrix-dense-matrix-product-with-addition-cuda-library-style/126093/1 "2025-02-20T00:29:35Z")

</div>

I want to compute the following

```julia
result = -1 * (e * (e' * U)) + M * U

```

Where e = ones(n,1), M is a n x n CuSparseMatrixCSR and U is a n x r dense CuMatrix.

Since it is my understanding that the above operation invokes 4 kernels, I am trying to reduce the number to three by using a function that performs both the product and the sum, like the SPMM function in the [CUDA Libraries](https://docs.nvidia.com/cuda/cusparse/index.html?highlight=SPMM#cusparsespmm), something like the function below:

```julia
function grad_function(e::CuArray, U::CuArray, M::CuSparseMatrixCSR)
  out = -e .* sum(U, dims=1)
  alpha = 1.0
  beta = 1.0
  CUDA.mul!(out, M, U, alpha, beta) # out = alpha*(M*U) + beta*out
  return out
end

```

Is it possible to do it with the CUDA.jl?

---

<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:** [February 20, 2025, 7:33am UTC](https://discourse.julialang.org/t/how-to-perform-a-sparse-matrix-dense-matrix-product-with-addition-cuda-library-style/126093/2 "2025-02-20T07:33:09Z")

</div>

`spmm!` is both available directly, by calling `CUSPARSE.cusparseSpMM`; as a slightly higher-level `CUSPARSE.mm!`, [CUDA.jl/lib/cusparse/generic.jl at 5b470c4614f7388c7b5b0938a93f781294673e80 · JuliaGPU/CUDA.jl · GitHub](https://github.com/JuliaGPU/CUDA.jl/blob/5b470c4614f7388c7b5b0938a93f781294673e80/lib/cusparse/generic.jl#L212-L278); and via `LinearAlgebra.mul!` as called on sparse arrays, [CUDA.jl/lib/cusparse/interfaces.jl at 5b470c4614f7388c7b5b0938a93f781294673e80 · JuliaGPU/CUDA.jl · GitHub](https://github.com/JuliaGPU/CUDA.jl/blob/5b470c4614f7388c7b5b0938a93f781294673e80/lib/cusparse/interfaces.jl#L32-L86).
