kahypar / KaHyPar.jl

KaHyPar.jl is a Julia interface to the KaHyPar multilevel hypergraph partitioning package.
Other
21 stars 7 forks source link

Cint limited to Int32 #30

Open andreadegirolamo99 opened 4 months ago

andreadegirolamo99 commented 4 months ago

Hi :)

I am trying to create hypergraphs with pretty huge hyperedge weights. As a consequence, when these get too large I get this error log:

ERROR: InexactError: trunc(Int32, 1099511627776)
Stacktrace:
  [1] throw_inexacterror(f::Symbol, #unused#::Type{Int32}, val::Int64)
    @ Core ./boot.jl:634
  [2] checked_trunc_sint
    @ ./boot.jl:656 [inlined]
  [3] toInt32
    @ ./boot.jl:693 [inlined]
  [4] Int32(x::Int64)
    @ Core ./boot.jl:783
  [5] _broadcast_getindex_evalf
    @ ./broadcast.jl:683 [inlined]
  [6] _broadcast_getindex
    @ ./broadcast.jl:656 [inlined]
  [7] getindex
    @ ./broadcast.jl:610 [inlined]
  [8] copy
    @ ./broadcast.jl:912 [inlined]
  [9] materialize
    @ ./broadcast.jl:873 [inlined]
 [10] KaHyPar.HyperGraph(A::SparseArrays.SparseMatrixCSC{Float64, Int64}, vertex_weights::Vector{Float64}, edge_weights::Vector{Any})
    @ KaHyPar ~/.julia/packages/KaHyPar/63UEh/src/KaHyPar.jl:82

This is due to the fact that all constants in kahypar_h.jl, including kahypar_hyperedge_weight_t, are declared as Cint or Cuint, corresponding to julia's Int32, which is not enough bits to represent these larger integers. In kahypar_h.jl, lines 1-5:

const kahypar_hypernode_id_t = Cuint
const kahypar_hyperedge_id_t = Cuint
const kahypar_hypernode_weight_t = Cint
const kahypar_hyperedge_weight_t = Cint
const kahypar_partition_id_t = Cint

For now, I have temporarily solved this issue with a try-catch-else clause:

try 
    Int32(my_huge_number)
catch
else
    # my_huge_number can be used for computation
end

Could this be solved by parsing a larger integer, e.g. Int64, to C?

jalving commented 4 months ago

good question @andreadegirolamo99. I am not sure whether the KaHyPar C interface assumes node and edge weights need to be int32 or not. Can the int type be 64 bit @SebastianSchlag?

SebastianSchlag commented 4 months ago

Hey @andreadegirolamo99, thank for reaching out and sorry for the delayed response. It is correct that KaHyPar uses 32bit integers by default.

However, if you build it from source, it should be relatively easy to get a version that supported 64bit edge weights. The main typedefs for node IDs, edge IDs, node weights and edge weights are located in the definitions.h file located in the kahypar-shared-resources submodule.

Those would need to be changed to int64_t along with the typedefs of the C interface in libkahypar.h.