# CuSparseMatrixCSC constructor

**URL:** <https://discourse.julialang.org/t/cusparsematrixcsc-constructor/62480>\
**Category:** GPU\
**Created:** [June 6, 2021, 10:20pm UTC](https://discourse.julialang.org/t/cusparsematrixcsc-constructor/62480 "2021-06-06T22:20:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhiwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhiwei/32/12385_2.png) [@zhiwei](https://discourse.julialang.org/u/zhiwei)\
**Post date:** [June 6, 2021, 10:20pm UTC](https://discourse.julialang.org/t/cusparsematrixcsc-constructor/62480/1 "2021-06-06T22:20:46Z")

</div>

Hello,

I am trying to use the CuSparseMatrixCSC constructor as defined [here](https://github.com/JuliaGPU/CUDA.jl/blob/5d6127dbbef495c94d3dd8de98162188062e11b1/lib/cusparse/array.jl#L141), but it throws an error. I’m wondering what’s gone wrong.

```julia
julia> using CUDA

julia> I = CuArray([1,1]);

julia> J = CuArray([1,2]);

julia> V = CuArray([0.2, 0.3]);

julia> CUSPARSE.CuSparseMatrixCSC(I, J, V, (3,3))
3×3 CUDA.CUSPARSE.CuSparseMatrixCSC{Float64} with 2 stored entries:Error showing value of type CUDA.CUSPARSE.CuSparseMatrixCSC{Float64}:
ERROR: ArgumentError: 2 == length(colptr) < 4

```

---

<div class="post-metadata">

**Author:** ![eliassno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliassno/32/18917_2.png) [@eliassno](https://discourse.julialang.org/u/eliassno)\
**Post date:** [June 6, 2021, 11:40pm UTC](https://discourse.julialang.org/t/cusparsematrixcsc-constructor/62480/2 "2021-06-06T23:40:58Z")

</div>

This only works for [`sparse` from `SparseArrays`](https://github.com/JuliaLang/julia/blob/master/stdlib/SparseArrays/src/sparsematrix.jl#L732-L799):

```julia
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
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] = 0.2
  [1, 2] = 0.3
```

---

<div class="post-metadata">

**Author:** ![zhiwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhiwei/32/12385_2.png) [@zhiwei](https://discourse.julialang.org/u/zhiwei)\
**Post date:** [June 7, 2021, 2:30am UTC](https://discourse.julialang.org/t/cusparsematrixcsc-constructor/62480/3 "2021-06-07T02:30:24Z")

</div>

Thank you for your help, this is great!
