Skip to content

[CUSPARSE] Missing appropriate similar methods #3061

@alonsoC1s

Description

@alonsoC1s

Describe the bug

Creating CUDA arrays with similar with a sparse matrix type, such as SparseMatrixCSC, works as expected when the number of dimensions is <=2. When there are 3 dimensions or more, the fallback for Base.Array is called. I understand that the CSC format only makes sense for 2d arrays. The proposed solution is to fall back to a CuArray constructor so the resulting array created with similar(A <: CUSPARSE.AbstractCuSparseArray) is still a GPU array. This prevents inadvertent scalar indexing down the line

Expected behavior
Array created with similar should be a GPU array

julia> using CUDA, SparseArrays
julia> A = sprand(20, 20, 0.5) |> cu
julia> A_sim_3d = similar(A, 20, 20, 5)
julia> typeof(A_sim_3d)
Array{Float32, 3} # Should be of type `CuArray` or similar
CUDA toolchain: 
- runtime 13.1, artifact installation
- driver 535.261.3 for 13.2
- compiler 13.2

CUDA libraries: 
- CUBLAS: 13.2.1
- CURAND: 10.4.1
- CUFFT: 12.1.0
- CUSOLVER: 12.0.9
- CUSPARSE: 12.7.3
- CUPTI: 2025.4.1 (API 13.1.1)
- NVML: 12.0.0+535.261.3

Julia packages: 
- CUDA: 5.10.1
- GPUArrays: 11.4.1
- GPUCompiler: 1.8.2
- KernelAbstractions: 0.9.41
- CUDA_Driver_jll: 13.2.0+0
- CUDA_Compiler_jll: 0.4.2+0
- CUDA_Runtime_jll: 0.20.1+0

Toolchain:
- Julia: 1.12.1
- LLVM: 18.1.7

1 device:
  0: NVIDIA GeForce RTX 2060 (sm_75, 4.841 GiB / 6.000 GiB available)

If there is no PR for this, I would be happy to attempt one

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions