JuliaSparse / SuiteSparseGraphBLAS.jl

Sparse, General Linear Algebra for Graphs!
MIT License
102 stars 16 forks source link

copyless conversion of full GBVector and GBMatrix into Arrays #107

Open CarloLucibello opened 1 year ago

CarloLucibello commented 1 year ago

Can we implement something like reinterpret here?

rayegun commented 1 year ago

Check out pack.jl and unpack.jl

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

rayegun commented 1 year ago

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.

CarloLucibello commented 1 year ago

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 commented 1 year ago

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

rayegun commented 1 year ago

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

rayegun commented 1 year ago

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:

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.