|
| 1 | +function asArray(f::Function, A::GBVecOrMat{T}; dropzeros=false, freeunpacked=false) where {T} |
| 2 | + if gbget(A, SPARSITY_STATUS) != GBDENSE |
| 3 | + X = similar(A) |
| 4 | + if X isa GBVector |
| 5 | + X[:] = zero(T) |
| 6 | + else |
| 7 | + X[:,:] = zero(T) |
| 8 | + end |
| 9 | + #I don't like this, it defeats the purpose of this method, which is to make no copies. |
| 10 | + # But somehow maintaining the input A in its original form is key to the to_vec implementation |
| 11 | + # for ChainRules. Temporarily it's fine, it's no worse than it originally was. |
| 12 | + # TODO: fix this issue with the ChainRules code. |
| 13 | + A = eadd(X, A) |
| 14 | + end |
| 15 | + array = _unpackdensematrix!(A) |
| 16 | + result = try |
| 17 | + f(array, A) |
| 18 | + finally |
| 19 | + if freeunpacked |
| 20 | + ccall(:jl_free, Cvoid, (Ptr{T},), pointer(array)) |
| 21 | + else |
| 22 | + _packdensematrix!(A, array) |
| 23 | + if dropzeros |
| 24 | + select!(nonzeros, A) |
| 25 | + end |
| 26 | + end |
| 27 | + end |
| 28 | + return result |
| 29 | +end |
| 30 | + |
| 31 | +function asSparseMatrixCSC(f::Function, A::GBMatrix{T}; freeunpacked=false) where {T} |
| 32 | + colptr, rowidx, values = _unpackcscmatrix!(A) |
| 33 | + array = SparseMatrixCSC{T, libgb.GrB_Index}(size(A, 1), size(A, 2), colptr, rowidx, values) |
| 34 | + result = try |
| 35 | + f(array, A) |
| 36 | + finally |
| 37 | + if freeunpacked |
| 38 | + ccall(:jl_free, Cvoid, (Ptr{libgb.GrB_Index},), pointer(colptr)) |
| 39 | + ccall(:jl_free, Cvoid, (Ptr{libgb.GrB_Index},), pointer(rowidx)) |
| 40 | + ccall(:jl_free, Cvoid, (Ptr{T},), pointer(values)) |
| 41 | + else |
| 42 | + _packcscmatrix!(A, colptr, rowidx, values) |
| 43 | + end |
| 44 | + end |
| 45 | + return result |
| 46 | +end |
| 47 | + |
| 48 | +function asSparseVector(f::Function, A::GBVector{T}; freeunpacked=false) where {T} |
| 49 | + colptr, rowidx, values = _unpackcscmatrix!(A) |
| 50 | + vector = SparseVector{T, libgb.GrB_Index}(size(A, 1), rowidx, values) |
| 51 | + result = try |
| 52 | + f(vector, A) |
| 53 | + finally |
| 54 | + if freeunpacked |
| 55 | + ccall(:jl_free, Cvoid, (Ptr{libgb.GrB_Index},), pointer(colptr)) |
| 56 | + ccall(:jl_free, Cvoid, (Ptr{libgb.GrB_Index},), pointer(rowidx)) |
| 57 | + ccall(:jl_free, Cvoid, (Ptr{T},), pointer(values)) |
| 58 | + else |
| 59 | + _packcscmatrix!(A, colptr, rowidx, values) |
| 60 | + end |
| 61 | + end |
| 62 | + return result |
| 63 | +end |
| 64 | + |
| 65 | + |
| 66 | +function Base.Matrix(A::GBMatrix) |
| 67 | + return asArray(A) do arr, _ |
| 68 | + return copy(arr) |
| 69 | + end |
| 70 | +end |
| 71 | + |
| 72 | +function Matrix!(A::GBMatrix) |
| 73 | + return asArray(A; freeunpacked=true) do arr, _ |
| 74 | + return copy(arr) |
| 75 | + end |
| 76 | +end |
| 77 | + |
| 78 | +function Base.Vector(v::GBVector) |
| 79 | + return asArray(v) do vec, _ |
| 80 | + return copy(vec) |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | +function Vector!(v::GBVector) |
| 85 | + return asArray(v; freeunpacked=true) do vec, _ |
| 86 | + return copy(vec) |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +function SparseArrays.SparseMatrixCSC(A::GBMatrix) |
| 91 | + return asArray(A) do arr, _ |
| 92 | + return copy(arr) |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | +function SparseArrays.SparseVector(v::GBVector) |
| 97 | + return asArray(v) do arr, _ |
| 98 | + return copy(arr) |
| 99 | + end |
| 100 | +end |
0 commit comments