JuliaSparse / SuiteSparseGraphBLAS.jl

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

Cannot extract user defined values from a GBVector either by subscripting or by using findnz #115

Closed macd closed 10 months ago

macd commented 1 year ago

using I, V = findnz(d) on a GBVector with a user defined struct gives a value vector with #undef as the following shows.

macd@macd-NUC9:~/jlang/graphblas/handson$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.10.0-DEV.777 (2023-03-09)
 _/ |\__'_|_|_|\__'_|  |  Commit f288f8886b (3 days old master)
|__/                   |

julia @v1.10> using SuiteSparseGraphBLAS

julia @v1.10> mutable struct BF_Tuple3
                  w::Float64   #  w corresponds to a path weight.
                  h::UInt      #  h corresponds to a path size or number of hops.
                  pi::UInt     # pi corresponds to the penultimate vertex along a path.
                               # vertex indexed as 1, 2, 3, ... , V, and pi = 0 (as nil)
                               # for u=v, and pi = typemax(UInt64) (as inf) for (u,v) not in E
              end

julia @v1.10> function BF_Tuple3()
                  return BF_Tuple3(0.0, 0, 0)
              end
BF_Tuple3

julia @v1.10> import Base: show, zero, +, min, ==, isapprox, isfinite

julia @v1.10> const zero_BF_Tuple3 = BF_Tuple3()
BF_Tuple3(0.0, 0x0000000000000000, 0x0000000000000000)

julia @v1.10> const BF_identity = BF_Tuple3(typemax(Float64), typemax(UInt64), typemax(UInt64))
BF_Tuple3(Inf, 0xffffffffffffffff, 0xffffffffffffffff)

julia @v1.10> const BF_terminal = BF_Tuple3(typemin(Float64), typemin(UInt64), typemin(UInt64))
BF_Tuple3(-Inf, 0x0000000000000000, 0x0000000000000000)

julia @v1.10> zero(BF_Tuple3) = zero_BF_Tuple3
zero (generic function with 28 methods)

julia @v1.10> d = GBVector{BF_Tuple3}(7)
7x1 GraphBLAS BF_Tuple3 matrix, sparse by col
  no entries, memory: 272 bytes

julia @v1.10> d[1] = BF_Tuple3()
BF_Tuple3(0.0, 0x0000000000000000, 0x0000000000000000)

julia @v1.10> I, V = findnz(d)
(UInt64[0x0000000000000001], BF_Tuple3[#undef])

julia @v1.10> d[1]
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getproperty
   @ ./Base.jl:37 [inlined]
 [2] unsafe_convert
   @ ./refvalue.jl:42 [inlined]
 [3] GrB_Matrix_extractElement_UDT
   @ ~/jlang/SuiteSparseGraphBLAS.jl/lib/LibGraphBLAS_gen.jl:5016 [inlined]
 [4] getindex(v::GBVector{BF_Tuple3, BF_Tuple3}, i::Int64)
   @ SuiteSparseGraphBLAS ~/jlang/SuiteSparseGraphBLAS.jl/src/abstractgbarray.jl:895
 [5] top-level scope
   @ REPL[12]:1
rayegun commented 10 months ago

You cannot use a mutable struct, it must be isbitstype(BF_Tuple3) == true. I will try to document this better.