# Initializing Sparse Matrices with CuArrays.jl

**URL:** <https://discourse.julialang.org/t/initializing-sparse-matrices-with-cuarrays-jl/21062>\
**Category:** GPU\
**Tags:** first-steps\
**Created:** [February 22, 2019, 12:14am UTC](https://discourse.julialang.org/t/initializing-sparse-matrices-with-cuarrays-jl/21062 "2019-02-22T00:14:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlomontec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlomontec/32/2011_2.png) [@carlomontec](https://discourse.julialang.org/u/carlomontec)\
**Post date:** [February 22, 2019, 12:14am UTC](https://discourse.julialang.org/t/initializing-sparse-matrices-with-cuarrays-jl/21062/1 "2019-02-22T00:14:52Z")

</div>

Hello,

total newbie with GPU computing here.

I have seen that is possible to use sparse matrices with CuArrays.jl, but I have not found a way to initialize them. I tried to with the example described at [https://github.com/JuliaAttic/CUSPARSE.jl](https://github.com/JuliaAttic/CUSPARSE.jl) but did not worked.

The used command and the error are shown below. Does anyone has a MWE with sparse matrices?

Thank you!

```julia
julia> CuArrays.allowscalar(true); 
julia> a = CuArrays.CUSPARSE.CuSparseMatrixCSR(sprand(10,10,0.1));
julia> a 

10×10 CuArrays.CUSPARSE.CuSparseMatrixCSR{Float64}:
Error showing value of type CuArrays.CUSPARSE.CuSparseMatrixCSR{Float64}:
ERROR: getindex not defined for CuArrays.CUSPARSE.CuSparseMatrixCSR{Float64}
Stacktrace:
 [1] error(::String, ::Type) at ./error.jl:42
 [2] error_if_canonical_getindex(::IndexCartesian, ::CuArrays.CUSPARSE.CuSparseMatrixCSR{Float64}, ::Int64, ::Int64) at ./abstractarray.jl:937
 [3] getindex at ./abstractarray.jl:926 [inlined]
 [4] isassigned(::CuArrays.CUSPARSE.CuSparseMatrixCSR{Float64}, ::Int64, ::Int64) at ./abstractarray.jl:351
 [5] alignment(::IOContext{REPL.Terminals.TTYTerminal}, ::CuArrays.CUSPARSE.CuSparseMatrixCSR{Float64}, ::UnitRange{Int64}, ::UnitRange{Int64}, ::Int64, ::Int64, ::Int64) at ./arrayshow.jl:67
 [6] print_matrix(::IOContext{REPL.Terminals.TTYTerminal}, ::CuArrays.CUSPARSE.CuSparseMatrixCSR{Float64}, ::String, ::String, ::String, ::String, ::String, ::String, ::Int64, ::Int64) at ./arrayshow.jl:186
 [7] print_matrix at ./arrayshow.jl:159 [inlined]
 [8] print_array at ./arrayshow.jl:308 [inlined]
 [9] show(::IOContext{REPL.Terminals.TTYTerminal}, ::MIME{Symbol("text/plain")}, ::CuArrays.CUSPARSE.CuSparseMatrixCSR{Float64}) at ./arrayshow.jl:345
 [10] display(::REPL.REPLDisplay, ::MIME{Symbol("text/plain")}, ::Any) at /home/zurdo/Software/Julia_Git/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:131
 [11] display(::REPL.REPLDisplay, ::Any) at /home/zurdo/Software/Julia_Git/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:135
 [12] display(::Any) at ./multimedia.jl:287
 [13] #invokelatest#1 at ./essentials.jl:742 [inlined]
 [14] invokelatest at ./essentials.jl:741 [inlined]
 [15] print_response(::IO, ::Any, ::Any, ::Bool, ::Bool, ::Any) at /home/zurdo/Software/Julia_Git/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:155
 [16] print_response(::REPL.AbstractREPL, ::Any, ::Any, ::Bool, ::Bool) at /home/zurdo/Software/Julia_Git/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:140
 [17] (::getfield(REPL, Symbol("#do_respond#38")){Bool,getfield(REPL, Symbol("##48#57")){REPL.LineEditREPL,REPL.REPLHistoryProvider},REPL.LineEditREPL,REPL.LineEdit.Prompt})(::Any, ::Any, ::Any) at /home/zurdo/Software/Julia_Git/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:714
 [18] #invokelatest#1 at ./essentials.jl:742 [inlined]
 [19] invokelatest at ./essentials.jl:741 [inlined]
 [20] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at /home/zurdo/Software/Julia_Git/julia/usr/share/julia/stdlib/v1.1/REPL/src/LineEdit.jl:2273
 [21] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at /home/zurdo/Software/Julia_Git/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:1035
 [22] run_repl(::REPL.AbstractREPL, ::Any) at /home/zurdo/Software/Julia_Git/julia/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:192
 [23] (::getfield(Base, Symbol("##734#736")){Bool,Bool,Bool,Bool})(::Module) at ./client.jl:362
 [24] #invokelatest#1 at ./essentials.jl:742 [inlined]
 [25] invokelatest at ./essentials.jl:741 [inlined]
 [26] run_main_repl(::Bool, ::Bool, ::Bool, ::Bool, ::Bool) at ./client.jl:346
 [27] exec_options(::Base.JLOptions) at ./client.jl:284
 [28] _start() at ./client.jl:436

```

EDIT: update of command used

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 22, 2019, 12:23am UTC](https://discourse.julialang.org/t/initializing-sparse-matrices-with-cuarrays-jl/21062/2 "2019-02-22T00:23:33Z")

</div>

This is just a problem showing the matrix in the REPL. Use a semi-colon after the line, and things should be fine. That said, this should probably be fixed, so you can open an issue in the repo.

---

<div class="post-metadata">

**Author:** ![carlomontec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlomontec/32/2011_2.png) [@carlomontec](https://discourse.julialang.org/u/carlomontec)\
**Post date:** [February 22, 2019, 12:29am UTC](https://discourse.julialang.org/t/initializing-sparse-matrices-with-cuarrays-jl/21062/3 "2019-02-22T00:29:57Z")

</div>

Thank you for your fast answer. I added the semicolon, but the error persists as I try to access the variable. Perhaps its a bug?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 22, 2019, 12:36am UTC](https://discourse.julialang.org/t/initializing-sparse-matrices-with-cuarrays-jl/21062/4 "2019-02-22T00:36:05Z")

</div>

When you type `a` in the REPL, it attempts to `show` it. `show` tries to query the elements of the array by scalar indexing ~~and you switched off scalar indexing (which is the right thing to do for detecting computational inefficiencies)~~. Edit: It seems that scalar getindex is not defined for this array type. This is the cause of the error. I guess all I am saying is don’t show the matrix as a GPU matrix. If you want to display it, move it to the CPU first ~~, or turn on scalar indexing~~. Otherwise, you can get on with your program normally, and this won’t affect the semantics of your code. Also note that this is only a problem in REPL. In normal Julia code, the output of each line is not displayed.

---

<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 22, 2019, 7:05am UTC](https://discourse.julialang.org/t/initializing-sparse-matrices-with-cuarrays-jl/21062/5 "2019-02-22T07:05:41Z")

</div>

I’ve created an issue: [Showing sparse arrays goes wrong · Issue #146 · JuliaGPU/CUDA.jl · GitHub](https://github.com/JuliaGPU/CuArrays.jl/issues/291)
