Skip to content

Conversation

@m-fila
Copy link
Member

@m-fila m-fila commented Nov 11, 2024

Adding C bindings and ahead of time compilation to shared library with PackageCompiler.jl or juliac

C components have jetreconstruction_(or JETRECONSTRUCTION_) prefix

For now the C API includes only:

  • jet_reconstruct
  • exclusive_jets, inclusive_jets
  • enums and structs used in their signatures. When needed new structs with C-like layout are introduced (for instance replacing Vector{T} field with Ptr{T} and Csize_t fields for C-facing structs)

The C header file has to be written manually and keep in sync with Julia code. Technically the whole compile could be a separate julia package possibly even living in a separate repo, but I think it'd be harder to keep in sync this way


To do:

  • write to an output pointer from inclusive_jets and exclusive_jets
  • add error handling
  • add missing docstrings
  • add precompilation statements
  • cache all combinations of strategy and algorithm in precompilation
  • have actual test
  • install JetReconstructionConfig.cmake
  • compilation with juliac
  • add GenKt
  • add recombination schemas
  • support Lorentz vectors
  • support EEJet

@codecov
Copy link

codecov bot commented Nov 11, 2024

Codecov Report

❌ Patch coverage is 65.55556% with 31 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.14%. Comparing base (d3c97f3) to head (7db31ad).

Files with missing lines Patch % Lines
src/C_JetReconstruction/C_JetReconstruction.jl 65.55% 31 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #88      +/-   ##
==========================================
- Coverage   80.00%   79.14%   -0.86%     
==========================================
  Files          21       22       +1     
  Lines        1315     1405      +90     
==========================================
+ Hits         1052     1112      +60     
- Misses        263      293      +30     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@m-fila m-fila changed the title add submodule for C bindings, library compilation with PackageCompiler [WIP] add submodule for C bindings, library compilation with PackageCompiler Nov 11, 2024
@m-fila m-fila force-pushed the clib branch 2 times, most recently from 321f097 to 382b53c Compare November 18, 2024 07:59
@m-fila
Copy link
Member Author

m-fila commented Nov 20, 2024

The CI for Julia 1.9 fails as documenting@ccallable functions was fixed only in 1.11 😢

I couldn't think of a good way of error handling.
What I propose here is to:

  • use status codes from enum with:
    • value for success,
    • values for each standard Julia exception,
    • single value for the remaining cases.
  • print the exception and backtrace to stderr so it's not lost

I also couldn't find a reasonable way to translate Julia exceptions to C++ exceptions without going through C-bottleneck

@Moelf
Copy link
Member

Moelf commented Nov 20, 2024

We don't need to support 1.9 I guess, but I hope we can backport JuliaLang/julia#51587 to 1.10.

@graeme-a-stewart
Copy link
Member

Yeah, I would agree we can drop support for 1.9 now, but the LTS release (1.10) we should try to support

@graeme-a-stewart
Copy link
Member

Just report this crashes on OS X with

[ Info: PackageCompiler: Executing /Users/graemes/.julia/dev/JetReconstruction/compile/precompile_execution.jl => /var/folders/kq/jgwv_wcj3ll2n6qwj06pp6jr0000gn/T/jl_packagecompiler_0hx01x/jl_dsyOUt
julia(84816,0x1f264cf40) malloc: *** error for object 0x134add910: pointer being freed was not allocated
julia(84816,0x1f264cf40) malloc: *** set a breakpoint in malloc_error_break to debug

[84816] signal 6: Abort trap: 6
in expression starting at /Users/graemes/.julia/dev/JetReconstruction/compile/precompile_execution.jl:33

I'm investigating!

@m-fila
Copy link
Member Author

m-fila commented Nov 21, 2024

Thanks for checking. I think that was because the results_ptrpointee was managed by Julia and got garbage collected. Should be fixed now

@graeme-a-stewart
Copy link
Member

Yes, now it works - thanks for the quick fix @m-fila!

@m-fila
Copy link
Member Author

m-fila commented Mar 11, 2025

Added compiling with juliac from Julia nightlies. Unfortunately LoopVectorization isn't compatible with juliac and would have to be removed altogether from the project.
Also any kind of trimming produces a lot of errors and aborts the compilation.

With the nightlies I get a lot of warnings like this. They might be false positives though:

WARNING: Detected access to binding `JetReconstruction.##meta#64` in a world prior to its definition world.
  Julia 1.12 has introduced more strict world age semantics for global bindings.
  !!! This code may malfunction under Revise.
  !!! This code will error in future versions of Julia.
Hint: Add an appropriate `invokelatest` around the access to this binding.

Taking all into account some results

PackageCompiler juliac
Compilation time (s) ~730 ~90
Lib size (M) 283 247
Deps size (M) 279 0

with juliac the explicit initialization and shutdown of julia runtime is gone. The pre-compilation is also gone (I didn't manage to reduce completely the overheads of first execution with PackageCompiler either with precompilation-statements, precompilations-execution or precompilation-execution on the test suite). Also now the compiled object doesn't bring any exclusive dependencies that should be kept around it.

@Moelf
Copy link
Member

Moelf commented Mar 11, 2025

sounds good!

Added compiling with juliac from Julia nightlies. Unfortunately LoopVectorization isn't compatible with juliac and would have to be removed altogether from the project.

for this, I guess time to retry #84

@m-fila
Copy link
Member Author

m-fila commented Mar 12, 2025

for this, I guess time to retry #84

Thankfully SIMD and its deps seem to be working fine with juliac

@Moelf
Copy link
Member

Moelf commented Mar 12, 2025

I guess makes sense, it's just injecting LLVM IRs

@m-fila
Copy link
Member Author

m-fila commented Mar 17, 2025

PackageCompiler with LoopVectorization compiles and runs but somehow is much slower ( more than x5) even than than a simple loop with no extra optimization

@m-fila m-fila force-pushed the clib branch 2 times, most recently from 63ba759 to 78bb48b Compare June 27, 2025 14:40
@m-fila m-fila changed the title [WIP] add submodule for C bindings, library compilation with PackageCompiler [WIP] add submodule for C bindings, static compilation Jul 2, 2025
@m-fila m-fila force-pushed the clib branch 6 times, most recently from fbc136f to d33f275 Compare July 3, 2025 21:34
@Moelf
Copy link
Member

Moelf commented Sep 5, 2025

@m-fila
Copy link
Member Author

m-fila commented Sep 5, 2025

I think we don't have anything like this here. From what I understood their function was type-unstable from the begging as returned type was decided by enum which isn't very nice even without juliac involved.

We use enums to select which function (strategy, algorithm) but it's stable as in the end we get the same type, if a function can select return type (jet selection) we do so by passing Type explicitly no enums involved.
In the C-interface here we only have single jet type PseudoJet.

@Moelf
Copy link
Member

Moelf commented Sep 5, 2025

interesting, I thought for sure CInt -> which jet clustering algo is gonna make Julia function not "grounded"

@m-fila
Copy link
Member Author

m-fila commented Sep 5, 2025

For us the biggest problems with trimming are:

  • no catching exceptions - this is the real elephant in the room here. If any Julia exception reaches C-layer then it's terminate, yet --trim=safe can't catch exceptions. In that example they too throw error if
  • no logging allowed
  • no cpu-features quering (no LoopVectorization)

From what I understood support for exceptions and logging in static analysis might be added in future

@m-fila
Copy link
Member Author

m-fila commented Sep 5, 2025

Also to keep track, the build organization can be rewritten with https://github.com/JuliaLang/JuliaC.jl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants