JuhaHeiskala / DirectQhull.jl

MIT License
11 stars 5 forks source link

Invalid pointer when precompiled with SnoopPrecompile #12

Closed thchr closed 1 year ago

thchr commented 1 year ago

I tried using SnoopPrecompile.jl on a method that called Voronoi from DirectQhull.jl. Unfortunately, it seems there is some pointer loaded during module initialization that does not persist between different loads of the package: I wonder if these pointers should be initialized in an __init__ function?

Representative error message below:

ERROR: pointerref: invalid pointer
Stacktrace:
  [1] unsafe_load (repeats 2 times)
    @ .\pointer.jl:111 [inlined]
  [2] qh_visit_voronoi(qh_ptr::Ptr{DirectQhull.qhT}, ridges::DirectQhull.RidgesT, vertex_ptr::Ptr{DirectQhull.QHvertexT{HD}}, vertexA_ptr::Ptr{DirectQhull.QHvertexT{HD}}, centers_ptr::Ptr{DirectQhull.QHsetT{Ptr{DirectQhull.QHfacetT{HD}}}}, unbounded::UInt32)
    @ DirectQhull C:\Users\tchr\.julia\packages\DirectQhull\Ngrfk\src\DirectQhull.jl:1334
  [3] qh_eachvoronoi_all
    @ C:\Users\tchr\.julia\packages\DirectQhull\Ngrfk\src\DirectQhull.jl:677 [inlined]
  [4] qh_get_voronoi_diagram(qh_ptr::Ptr{DirectQhull.qhT}, num_input_pnts::Int64, #unused#::Val{3})
    @ DirectQhull C:\Users\tchr\.julia\packages\DirectQhull\Ngrfk\src\DirectQhull.jl:1382
  [5] DirectQhull.Voronoi(pnts::Matrix{Float64}, qhull_options::Vector{String})
    @ DirectQhull C:\Users\tchr\.julia\packages\DirectQhull\Ngrfk\src\DirectQhull.jl:532
  [6] Voronoi
    @ C:\Users\tchr\.julia\packages\DirectQhull\Ngrfk\src\DirectQhull.jl:508 [inlined]
  [7] wignerseitz(basis::ReciprocalBasis{2}; merge::Bool, Nmax::Int64)
    @ Brillouin.WignerSeitz C:\Users\tchr\Dropbox (MIT)\Projects\photonic-topo-sym\julia\Brillouin\src\WignerSeitz.jl:119
  [8] wignerseitz
    @ C:\Users\tchr\Dropbox (MIT)\Projects\photonic-topo-sym\julia\Brillouin\src\WignerSeitz.jl:104 [inlined]
  [9] _precompile(sgnum::Int64, Rs::Vector{SVector{2, Float64}}, Dᵛ::Val{2})
    @ Main .\REPL[6]:5
 [10] macro expansion
    @ .\REPL[8]:2 [inlined]
 [11] top-level scope
    @ .\timing.jl:273 [inlined]
 [12] top-level scope
    @ .\REPL[8]:0
JuhaHeiskala commented 1 year ago

Well, I can't infer from that error message what could cause it. The qhull memory is reserved and initialized in the Voronoi constructor. The failing unsafe_load comes from a julia function (qh_visit_voronoi) that is given to qhull as cfunction. The invalid pointer should be allocated inside qhull and given to qh_visit_voronoi as parameter. Maybe the SnoopPrecompile has some incompatibility with the giving julia function as parameter to qhull. At least that is only thing I can think of now.

thchr commented 1 year ago

After trying for ~1 hour, I somehow also cannot reproduce this anymore: sorry about the noise - I'll close this again.

For posterity, a possible MWE could have been something like this (but works fine for me now...):

  1. Generate a new project, say DebugSnoop.jl, with dependencies on SnoopPrecompile.jl and DirectQhull.jl
  2. Put this in /src/DebugSnoop.jl

    module SnoopDebug
    
    using SnoopPrecompile
    using DirectQhull
    
    f(x) =  Voronoi(x .* rand(2,5))
    @precompile_all_calls begin
       f(2)
    end
    end # module
  3. See if using DebugSnoop crashes - now it doesn't.
  4. Check which methods got precompile by doing (before using DebugSnoop):
    using SnoopPrecompile
    SnoopPrecompile.verbose[] = true
    include("src/DebugSnoop.jl")