This function does not exist in CUSPARSE, and falls back to Base where scalar iteration happens:
julia> @which CUSPARSE.sparse(D_I, D_J, D_V)
sparse(I, J, V::AbstractArray{T,1} where T) in SparseArrays at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/SparseArrays/src/sparsematrix.jl:845
julia> CuArrays.allowscalar(false)
julia> CUSPARSE.sparse(D_I, D_J, D_V)
ERROR: scalar getindex is disallowed
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] assertscalar(::String) at /home/tim/Julia/pkg/GPUArrays/src/indexing.jl:14
[3] getindex at /home/tim/Julia/pkg/GPUArrays/src/indexing.jl:54 [inlined]
[4] sparse!(::CuArray{Int64,1,Nothing}, ::CuArray{Int64,1,Nothing}, ::CuArray{Float32,1,Nothing}, ::Int64, ::Int64, ::typeof(+), ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Float32,1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Float32,1}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/SparseArrays/src/sparsematrix.jl:730
[5] sparse(::CuArray{Int64,1,Nothing}, ::CuArray{Int64,1,Nothing}, ::CuArray{Float32,1,Nothing}, ::Int64, ::Int64, ::Function) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/SparseArrays/src/sparsematrix.jl:660
[6] sparse(::CuArray{Int64,1,Nothing}, ::CuArray{Int64,1,Nothing}, ::CuArray{Float32,1,Nothing}, ::Int64, ::Int64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/SparseArrays/src/sparsematrix.jl:849
[7] sparse(::CuArray{Int64,1,Nothing}, ::CuArray{Int64,1,Nothing}, ::CuArray{Float32,1,Nothing}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/SparseArrays/src/sparsematrix.jl:845
[8] top-level scope at REPL[28]:1
You can however just call the CuSparseMatrixCSC
constructor on your CPU SparseMatrixCSC
, but that would reveal another nasty error:
julia> using CuArrays.CUSPARSE
julia> CuSparseMatrixCSC(A)
ERROR: MethodError: no method matching CuSparseMatrixCSC(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Tuple{Int64,Int64})
The problem here is that CUSPARSE does not support sparse matrices with integer types. Using Float32 (or any other supported type) fixes that:
julia> A = sparse(Float32.(I), Float32.(J), Float32.(V))
3×3 SparseMatrixCSC{Float32,Int64} with 4 stored entries:
[1, 1] = 1.0
[2, 2] = 2.0
[2, 3] = 4.0
[3, 3] = 3.0
julia> CuSparseMatrixCSC(A)
3×3 CuSparseMatrixCSC{Float32}:
Error showing value of type CuSparseMatrixCSC{Float32}
Everything works now, except displaying the sparse matrix. This is a known issue: Showing sparse arrays goes wrong · Issue #146 · JuliaGPU/CUDA.jl · GitHub (the sparse functionality has not been getting as much love as the rest of the package).