# Sparse Matrix with CUDA.jl

**URL:** <https://discourse.julialang.org/t/sparse-matrix-with-cuda-jl/62627>\
**Category:** Performance\
**Tags:** question\
**Created:** [June 9, 2021, 2:24pm UTC](https://discourse.julialang.org/t/sparse-matrix-with-cuda-jl/62627 "2021-06-09T14:24:01Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![boutor2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boutor2/32/27869_2.png) [@boutor2](https://discourse.julialang.org/u/boutor2)\
**Post date:** [June 10, 2021, 4:14pm UTC](https://discourse.julialang.org/t/sparse-matrix-with-cuda-jl/62627/4 "2021-06-10T16:14:32Z")

</div>

Thank you very much for your comprehensive answer. However, it does not seem trivial to convert `CartesianIndex` in a CSC format. But may be I am wrong?

Here is a proposal to do so, that uses the `sparse` method from `SparseArrays`

> [@Cartesian indices, linear indices, and sparse matrices](https://discourse.julialang.org/t/cartesian-indices-linear-indices-and-sparse-matrices/13781/2):
>
> Thanks for writing this up as a comprehensive question! It’d probably be nice to add a sparse method that takes arrays of CartesianIndexes. Here’s a simple implementation that could serve you in the mean-time: function SparseArrays.sparse(IJ::Vector{\<:CartesianIndex}, v, m, n) IJ′ = reinterpret(Int, reshape(IJ, 1, :)) return sparse(view(IJ′, 1, :), view(IJ′, 2, :), v, m, n) end In the above I used reinterpret and lazy views to prevent needlessly allocating new vectors that contai…

but as you mentioned in an other post we miss the `sparse` method for `CuSparseMatricCSC`

> [@CuSparseMatrixCSC constructor](https://discourse.julialang.org/t/cusparsematrixcsc-constructor/62480/2):
>
> This only works for [sparse from SparseArrays](https://github.com/JuliaLang/julia/blob/master/stdlib/SparseArrays/src/sparsematrix.jl#L732-L799): julia\> using SparseArrays julia\> sparse(Array(I), Array(J), Array(V), 3, 3) 3×3 SparseMatrixCSC{Float64,Int64} with 2 stored entries: [1, 1] = 0.2 [1, 2] = 0.3 You need to convert your coordinate lists (I and J) to CSC format (colPtr and rowVal). julia\> colPtr = CuArray([1, 2, 3, 3]); julia\> rowVal = CuArray([1, 1]); julia\> CUSPARSE.CuSparseMatrixCSC(colPtr, rowVal, V, (3,3)) 3×3 CuSparseMatrixCSC{Float64} with 2 stored entries: [1, 1…

---

_[View the full topic](https://discourse.julialang.org/t/sparse-matrix-with-cuda-jl/62627)._
