Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

copyless conversion of full GBVector and GBMatrix into Arrays #107

Open
CarloLucibello opened this issue Nov 8, 2022 · 6 comments
Open

Comments

@CarloLucibello
Copy link

Can we implement something like reinterpret here?

@rayegun
Copy link
Member

rayegun commented Nov 8, 2022

Check out pack.jl and unpack.jl

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

@rayegun
Copy link
Member

rayegun commented Nov 8, 2022

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
Copy link
Author

CarloLucibello commented Nov 8, 2022

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
Copy link
Author

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

@rayegun
Copy link
Member

rayegun commented Nov 8, 2022

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

@rayegun
Copy link
Member

rayegun commented Nov 8, 2022

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants