Initializing Sparse Matrices with CuArrays.jl


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 but did not worked.

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

Thank you!

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}
 [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

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.

1 Like

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?

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.

1 Like

I’ve created an issue: