SuiteSparseGraphBLAS.jl
SuiteSparseGraphBLAS.jl copied to clipboard
copyless conversion of full GBVector and GBMatrix into Arrays
Can we implement something like reinterpret
here?
Check out pack.jl and unpack.jl
I haven't rigorously documented them yet because they're difficult to make "safe"
If you need both at the same time (the reinterpret and the original) I don't have a great option other than repacking and holding onto the pointers. If you don't modify then they "should" remain the same. But you could easily segfault on GC.
Thanks, that is what I was looking for.
julia> a = rand(2,2)
2×2 Matrix{Float64}:
0.823637 0.396913
0.44517 0.924361
julia> x = SuiteSparseGraphBLAS.pack(a)
2x2 GraphBLAS double matrix, full by col
4 entries, memory: 208 bytes
(1,1) 0.823637
(2,1) 0.44517
(1,2) 0.396913
(2,2) 0.924361
julia> a[1,1] = 5
5
julia> x
2x2 GraphBLAS double matrix, full by col
4 entries, memory: 208 bytes
(1,1) 5
(2,1) 0.44517
(1,2) 0.396913
(2,2) 0.924361
The other direction is with
z = SuiteSparseGraphBLAS._unpackdensematrix!(x)
I'll leave the issue open until the API is finalized and documented
So try to use methods without the _
prefix. But yeah I'll get this documented this week. It's relatively stable.
Specifically the semantics of pack
are different from unsafepack!
:
pack
returns a GBShallow[Vector | Matrix]
. This keeps your original matrix/vector A
alive, and provides a GraphBLAS view of it. There are some limitations here:
- Don't resize
A
, you could easily segfault. - You cannot modify
A
from within GraphBLAS. Specifically using it as output for any function is invalid. GraphBLAS must be able to modify the sparsity ofA
, but it cannot do so forA
, it is markedshallow
.
Once you've packed A
it still exists as before, no need to unpack it, after its lifetime has ended it will clean itself up without freeing A
.
unsafepack!
is different. SuiteSparse:GraphBLAS takes ownership with unsafepack!
. It is only safe to use this (and unsafeunpack!
in two specific situations:
- You temporarily unpack a GraphBLAS matrix into a Julia matrix, and then call
unsafepack!
again. It is better to usetempunpack!
for this purpose. - You have memory created by
:jlmalloc
that you want to pass into GraphBLAS.