SuiteSparseGraphBLAS.jl icon indicating copy to clipboard operation
SuiteSparseGraphBLAS.jl copied to clipboard

copyless conversion of full GBVector and GBMatrix into Arrays

Open CarloLucibello opened this issue 1 year ago • 6 comments

Can we implement something like reinterpret here?

CarloLucibello avatar Nov 08 '22 14:11 CarloLucibello

Check out pack.jl and unpack.jl

I haven't rigorously documented them yet because they're difficult to make "safe"

rayegun avatar Nov 08 '22 16:11 rayegun

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.

rayegun avatar Nov 08 '22 16:11 rayegun

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)

CarloLucibello avatar Nov 08 '22 18:11 CarloLucibello

I'll leave the issue open until the API is finalized and documented

CarloLucibello avatar Nov 08 '22 18:11 CarloLucibello

So try to use methods without the _ prefix. But yeah I'll get this documented this week. It's relatively stable.

rayegun avatar Nov 08 '22 19:11 rayegun

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 of A, but it cannot do so for A, it is marked shallow.

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:

  1. You temporarily unpack a GraphBLAS matrix into a Julia matrix, and then call unsafepack! again. It is better to use tempunpack! for this purpose.
  2. You have memory created by :jlmalloc that you want to pass into GraphBLAS.

rayegun avatar Nov 08 '22 19:11 rayegun