JuliaSparse / SuiteSparseGraphBLAS.jl

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

failed conversion from GBMatrix{Int} to GBMatrix{Float32} #97

Closed CarloLucibello closed 10 months ago

CarloLucibello commented 1 year ago

I couldn't find a way to convert an integer valued GBMatrix to a float one:

julia> using SuiteSparseGraphBlas

julia> x = GBMatrix([1,2], [2, 3], [1,2], fill=0)
2x3 GraphBLAS int64_t matrix, bitmap by row
  2 entries, memory: 264 bytes

    (1,2)   1
    (2,3)   2

julia> Float32.(x)
ERROR: cfunction: closures are not supported on this platform
Stacktrace:
 [1] unsafe_convert
   @ ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/src/types.jl:47 [inlined]
 [2] GrB_Matrix_apply(C::GBMatrix{Float32, Int64}, Mask::Ptr{Nothing}, accum::Ptr{Nothing}, op::SuiteSparseGraphBLAS.TypedUnaryOperator{DataType, Int64, Float32}, A::GBMatrix{Int64, Int64}, desc::Descriptor)
   @ SuiteSparseGraphBLAS.LibGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/lib/LibGraphBLAS_gen.jl:485
 [3] apply!(op::Type, C::GBMatrix{Float32, Int64}, A::GBMatrix{Int64, Int64}; mask::Nothing, accum::Nothing, desc::Nothing)
   @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/src/operations/map.jl:10
 [4] #apply#243
   @ ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/src/operations/map.jl:49 [inlined]
 [5] apply
   @ ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/src/operations/map.jl:44 [inlined]
 [6] copy
   @ ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/src/operations/broadcasts.jl:74 [inlined]
 [7] materialize(bc::Base.Broadcast.Broadcasted{SuiteSparseGraphBLAS.GBMatrixStyle, Nothing, Type{Float32}, Tuple{GBMatrix{Int64, Int64}}})
   @ Base.Broadcast ./broadcast.jl:860
 [8] top-level scope
   @ REPL[33]:1

julia> map(Float32, x)
ERROR: cfunction: closures are not supported on this platform
Stacktrace:
 [1] unsafe_convert
   @ ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/src/types.jl:47 [inlined]
 [2] GrB_Matrix_apply(C::GBMatrix{Float32, Nothing}, Mask::Ptr{Nothing}, accum::Ptr{Nothing}, op::SuiteSparseGraphBLAS.TypedUnaryOperator{DataType, Int64, Float32}, A::GBMatrix{Int64, Nothing}, desc::Descriptor)
   @ SuiteSparseGraphBLAS.LibGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/lib/LibGraphBLAS_gen.jl:485
 [3] apply!(op::Type, C::GBMatrix{Float32, Nothing}, A::GBMatrix{Int64, Nothing}; mask::Nothing, accum::Nothing, desc::Nothing)
   @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/src/operations/map.jl:10
 [4] apply(op::Type, A::GBMatrix{Int64, Nothing}; mask::Nothing, accum::Nothing, desc::Nothing)
   @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/src/operations/map.jl:49
 [5] #map#250
   @ ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/src/operations/map.jl:109 [inlined]
 [6] map(f::Type, A::GBMatrix{Int64, Nothing})
   @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/ijDcs/src/operations/map.jl:108
 [7] top-level scope
   @ REPL[81]:1

julia> convert(GBMatrix{Float32}, x)
ERROR: MethodError: Cannot `convert` an object of type 
  GBMatrix{Int64, Int64} to an object of type 
  GBMatrix{Float32}
Closest candidates are:
  convert(::Type{T}, ::Factorization) where T<:AbstractArray at ~/.julia/juliaup/julia-1.8.2+0.aarch64/share/julia/stdlib/v1.8/LinearAlgebra/src/factorization.jl:58
  convert(::Type{T}, ::T) where T<:AbstractArray at abstractarray.jl:16
  convert(::Type{T}, ::T) where T at Base.jl:61
Stacktrace:
 [1] top-level scope
   @ REPL[34]:1

julia> convert(GBMatrix{Float32, Float32}, x)
ERROR: MethodError: Cannot `convert` an object of type 
  GBMatrix{Int64,Int64} to an object of type 
  GBMatrix{Float32,Float32}
Closest candidates are:
  convert(::Type{T}, ::Factorization) where T<:AbstractArray at ~/.julia/juliaup/julia-1.8.2+0.aarch64/share/julia/stdlib/v1.8/LinearAlgebra/src/factorization.jl:58
  convert(::Type{T}, ::T) where T<:AbstractArray at abstractarray.jl:16
  convert(::Type{T}, ::T) where T at Base.jl:61
  ...
Stacktrace:
 [1] top-level scope
   @ REPL[35]:1
rayegun commented 1 year ago

On master does Float32.(x) not work?

CarloLucibello commented 1 year ago

Float32.(x) works on 0.9 indeed. The other attempts fail differently now

julia> x = GBMatrix([1,2], [2, 3], [1,2], fill=0)
2x3 GraphBLAS int64_t matrix, bitmap by row
  2 entries, memory: 264 bytes

    (1,2)   1
    (2,3)   2

julia> Float32.(x)
2x3 GraphBLAS float matrix, bitmap by row
  2 entries, memory: 238 bytes

    (1,2)    1
    (2,3)    2

julia> map(Float32, x)
ERROR: "Unsupported function SuiteSparseGraphBLAS.var\"#unaryopfn#10\"{DataType}. Closure functions are not supported."
Stacktrace:
  [1] #s77#9
    @ ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/types.jl:38 [inlined]
  [2] var"#s77#9"(F::Any, X::Any, Z::Any, ::Any, f::Any, #unused#::Type, #unused#::Any)
    @ SuiteSparseGraphBLAS ./none:0
  [3] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any})
    @ Core ./boot.jl:582
  [4] unsafe_convert(#unused#::Type{Ptr{SuiteSparseGraphBLAS.LibGraphBLAS.GB_UnaryOp_opaque}}, op::SuiteSparseGraphBLAS.TypedUnaryOperator{DataType, Int64, Float32})
    @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/types.jl:56
  [5] GrB_Matrix_apply(C::GBMatrix{Float32, Float32}, Mask::Ptr{Nothing}, accum::Ptr{Nothing}, op::SuiteSparseGraphBLAS.TypedUnaryOperator{DataType, Int64, Float32}, A::GBMatrix{Int64, Int64}, desc::Descriptor)
    @ SuiteSparseGraphBLAS.LibGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/lib/LibGraphBLAS_gen.jl:1284
  [6] apply!(op::Type, C::GBMatrix{Float32, Float32}, A::GBMatrix{Int64, Int64}; mask::Nothing, accum::Nothing, desc::Nothing)
    @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/operations/map.jl:10
  [7] apply(op::Type, A::GBMatrix{Int64, Int64}; mask::Nothing, accum::Nothing, desc::Nothing)
    @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/operations/map.jl:49
  [8] #map#502
    @ ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/operations/map.jl:109 [inlined]
  [9] map(f::Type, A::GBMatrix{Int64, Int64})
    @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/operations/map.jl:108
 [10] top-level scope
    @ REPL[21]:1

julia> convert(GBMatrix{Float32}, x)
ERROR: Abstract type Any does not have a definite size.
Stacktrace:
 [1] sizeof(x::Type)
   @ Base ./essentials.jl:473
 [2] _sizedjlmalloc(n::Int64, #unused#::Type{Any})
   @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/mem.jl:14
 [3] convert(::Type{GBMatrix{Float32}}, A::GBMatrix{Int64, Int64}; fill::Int64)
   @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/convert.jl:14
 [4] convert(::Type{GBMatrix{Float32}}, A::GBMatrix{Int64, Int64})
   @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/convert.jl:7
 [5] top-level scope
   @ REPL[22]:1

julia> convert(GBMatrix{Float32, Float32}, x)
ERROR: SuiteSparseGraphBLAS.NullPointerError
Stacktrace:
 [1] _packhypermatrix!(A::GBMatrix{Int64, Int64}, ptr::Vector{Int64}, idx1::Vector{Int64}, idx2::Vector{Int64}, values::Vector{Int64}; desc::Nothing, order::RowMajor, decrementindices::Bool, jumbled::Bool)
   @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/pack.jl:187
 [2] #unsafepack!#341
   @ ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/pack.jl:250 [inlined]
 [3] repack!
   @ ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/unpack.jl:509 [inlined]
 [4] repack! (repeats 6 times)
   @ ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/unpack.jl:507 [inlined]
 [5] convert(::Type{GBMatrix{Float32, Float32}}, A::GBMatrix{Int64, Int64}; fill::Int64)
   @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/convert.jl:17
 [6] convert(::Type{GBMatrix{Float32, Float32}}, A::GBMatrix{Int64, Int64})
   @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/convert.jl:7
 [7] top-level scope
   @ REPL[23]:1
CarloLucibello commented 1 year ago

I'll leave the issue open since all of these should work ideally

CarloLucibello commented 1 year ago

Another error when composing broadcasts:

julia> x = GBMatrix([1,2], [2, 3], [1,2])
2x3 GraphBLAS int64_t matrix, bitmap by row
  2 entries, memory: 264 bytes

    (1,2)   1
    (2,3)   2

julia> b = x .> 0
2x3 GraphBLAS bool matrix, bitmap by row
  2 entries, memory: 224 bytes

    (1,2)   1
    (2,3)   1

julia> Float32.(b)
2x3 GraphBLAS float matrix, bitmap by row
  2 entries, memory: 238 bytes

    (1,2)    1
    (2,3)    1

julia> Float32.(x .> 0)
ERROR: "Unsupported function SuiteSparseGraphBLAS.var\"#unaryopfn#10\"{DataType}. Closure functions are not supported."
Stacktrace:
  [1] #s77#9
    @ ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/types.jl:38 [inlined]
  [2] var"#s77#9"(F::Any, X::Any, Z::Any, ::Any, f::Any, #unused#::Type, #unused#::Any)
    @ SuiteSparseGraphBLAS ./none:0
  [3] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any})
    @ Core ./boot.jl:582
  [4] unsafe_convert(#unused#::Type{Ptr{SuiteSparseGraphBLAS.LibGraphBLAS.GB_UnaryOp_opaque}}, op::SuiteSparseGraphBLAS.TypedUnaryOperator{DataType, Bool, Float32})
    @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/types.jl:56
  [5] GrB_Matrix_apply(C::GBMatrix{Float32, Float32}, Mask::Ptr{Nothing}, accum::Ptr{Nothing}, op::SuiteSparseGraphBLAS.TypedUnaryOperator{DataType, Bool, Float32}, A::GBMatrix{Bool, Bool}, desc::Descriptor)
    @ SuiteSparseGraphBLAS.LibGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/lib/LibGraphBLAS_gen.jl:1284
  [6] apply!(op::Type, C::GBMatrix{Float32, Float32}, A::GBMatrix{Bool, Bool}; mask::Nothing, accum::Nothing, desc::Nothing)
    @ SuiteSparseGraphBLAS ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/operations/map.jl:10
  [7] #apply#495
    @ ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/operations/map.jl:49 [inlined]
  [8] apply
    @ ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/operations/map.jl:44 [inlined]
  [9] copy
    @ ~/.julia/packages/SuiteSparseGraphBLAS/hotIr/src/operations/broadcasts.jl:79 [inlined]
 [10] materialize(bc::Base.Broadcast.Broadcasted{SuiteSparseGraphBLAS.GBMatrixStyle, Nothing, Type{Float32}, Tuple{Base.Broadcast.Broadcasted{SuiteSparseGraphBLAS.GBMatrixStyle, Nothing, typeof(>), Tuple{GBMatrix{Int64, Int64}, Int64}}}})
    @ Base.Broadcast ./broadcast.jl:860
 [11] top-level scope
    @ REPL[79]:1
rayegun commented 10 months ago

Fixed in v0.10 as far as I can tell (please re-open if this is platform specific. It might be since closure cfunctions aren't supported on macOS ARM I believe).