GPUArrays.jl
GPUArrays.jl copied to clipboard
Introduce AbstractGPUSparseArray
Hello,
dense CuArrays are recognized as AbstractGPUArray type. Indeed, if I do
using CUDA
using GPUArrays
A = CUDA.rand(10, 10)
A isa AbstractGPUArray
it returns true. However, it returns false for a sparse AbstractCuSparseMatrix array, such as CuSparseMatrixCSR type. If I do
A2 = CuSparseMatrixCSR(A)
A2 isa AbstractCuSparseMatrix
A2 isa AbstractGPUArray
the first returns true, while the second not.
I think that the support for sparse types is essential for developing external packages which use if conditions without import the whole CUDA.jl package. Such as the ExponentialUtilities.jl package, which have a special condition only for AbstractGPUArray types, which however fails if I insert a sparse array.
However, it returns false for a sparse AbstractCuSparseMatrix array, such as CuSparseMatrixCSR type.
That's intentional, because we don't have multiple inheritance. If we want CuSparseMatrixCSR to be <: AbstractSparseArray, we cannot also have it be a subtype of AbstractGPUArray (which is a direct subtype of AbstractArray).
So I'm not sure what you're suggesting here with 'Support for AbstractCuSparseMatrix'. A CUDA-specific type is never going to be used in GPUArrays, which sits at a higher level of abstraction. We could define AbstractSparseGPUArray <: AbstractSparseArray here and have the CUDA sparse array types be a subtype of that, but right now (without any sparse array functionality in GPUArrays) that won't buy you anything.
For example, here the ExponentialUtilities.jl package declares a function for AbstractGPUArray, which is called only if I insert a dense CuArray.
I need a way to declare a function for a sparse AbstractCuSparseMatrix, but the only way I found until now is to import the CUDA.jl package, which I don't know if it is a good idea.
What do you think?
I try to explain better what I mean.
I think that it is better to move these lines which are actually in the CUDA.jl package
https://github.com/JuliaGPU/CUDA.jl/blob/cea77a5aa60c0689bf8055f966722e5b3a3f8cf1/lib/cusparse/array.jl#L12-L14
into this package. Such as the current dense case, which is defined here
https://github.com/JuliaGPU/GPUArrays.jl/blob/0a42771ff10d49344e2640a24d7ba962b847e3f4/lib/GPUArraysCore/src/GPUArraysCore.jl#L18-L26
In this way, we can add also the AbstractCuSparseMatrix into the AnyGPUArray type.
In this way, we can add also the AbstractCuSparseMatrix into the AnyGPUArray type.
That's different from the AbstractGPUArray test you requested first (which is a subtype of DenseArray, so that wouldn't work). Maybe we shouldn't have done that, i.e., AbstractGPUArray<:AbstractArray instead, but I don't think we can safely change that now. But introducing a separate AbstractGPUSparseArray (which wouldn't be a subtype of AbstractGPUArray, but could be included in AnyGPUArray) seems like it could work.
I'm not sure how useful it would be though, because almost none of the AnyGPUArray functionality would be SparseArray-compatible. And we don't have any other GPU back-end that supports sparse arrays(rocSPARSE exists, but AFAIK AMDGPU.jl doesn't use it).