Skip to content

Commit b7ff87c

Browse files
committed
control garbage collector, add finding libjulia
1 parent 2c31b44 commit b7ff87c

File tree

4 files changed

+220
-14
lines changed

4 files changed

+220
-14
lines changed

cjetreconstruction/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
cmake_minimum_required(VERSION 3.12)
77
project(JetReconstructionBenchmarks)
8+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
89

910
find_package(HepMC3 REQUIRED)
1011
find_package(JetReconstruction REQUIRED)
12+
find_package(Julia REQUIRED) # Find Julia for GC utilities
1113

1214
set(CMAKE_CXX_STANDARD 17)
1315

@@ -18,8 +20,8 @@ add_executable(cjetreconstruction-finder
1820
src/cjetreconstruction-utils.cc
1921
)
2022

21-
23+
target_include_directories(cjetreconstruction-finder PRIVATE ${Julia_INCLUDE_DIRS})
2224
target_link_libraries(cjetreconstruction-finder
2325
HepMC3::HepMC3
24-
JetReconstruction::JetReconstruction
26+
JetReconstruction::JetReconstruction ${Julia_LIBRARY}
2527
)

cjetreconstruction/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# JetReconstruction.jl C-bindings Test Applications
22

3+
Benchmark application using the JetReconstruction.jl C-bindings.
34

45
## Compilation
56

@@ -11,3 +12,11 @@ cmake -S . -B build
1112
# JetReconstruction_DIR=<JetReconstruction-installation-location> cmake -S . -B build
1213
cmake --build build
1314
```
15+
16+
> [!NOTE]
17+
> Make sure to find the same version of Julia that was used to compile the JetReconstruction package.
18+
> Custom search path can be set with `Julia_ROOT`, for instance:
19+
>
20+
> ```sh
21+
> JULIA_ROOT=${HOME}/.julia/juliaup/julia-nightly/ cmake -S . -B build
22+
> ```
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# File taken from JuliaInteroplibcxxwrap-julia v0.13.4
2+
# MIT License
3+
# Copyright (c) 2015: Bart Janssens
4+
5+
# Original FindJulia.cmake from https://github.com/QuantStack/xtensor-julia-cookiecutter/blob/master/%7B%7Bcookiecutter.github_project_name%7D%7D/cmake/FindJulia.cmake
6+
7+
if(Julia_FOUND)
8+
return()
9+
endif()
10+
11+
####################
12+
# Julia Executable #
13+
####################
14+
15+
if(Julia_PREFIX)
16+
message(STATUS "Adding path ${Julia_PREFIX} to search path")
17+
list(APPEND CMAKE_PREFIX_PATH ${Julia_PREFIX})
18+
message(STATUS "THIS BRANCH")
19+
else()
20+
find_program(Julia_EXECUTABLE julia DOC "Julia executable")
21+
message(STATUS "Found Julia executable: " ${Julia_EXECUTABLE})
22+
endif()
23+
24+
#################
25+
# Julia Version #
26+
#################
27+
28+
if(Julia_EXECUTABLE)
29+
execute_process(
30+
COMMAND "${Julia_EXECUTABLE}" --startup-file=no --version
31+
OUTPUT_VARIABLE Julia_VERSION_STRING
32+
)
33+
else()
34+
find_file(Julia_VERSION_INCLUDE julia_version.h PATH_SUFFIXES include/julia)
35+
file(READ ${Julia_VERSION_INCLUDE} Julia_VERSION_STRING)
36+
string(REGEX MATCH "JULIA_VERSION_STRING.*" Julia_VERSION_STRING ${Julia_VERSION_STRING})
37+
endif()
38+
39+
string(
40+
REGEX REPLACE ".*([0-9]+\\.[0-9]+\\.[0-9]+).*" "\\1"
41+
Julia_VERSION_STRING "${Julia_VERSION_STRING}"
42+
)
43+
44+
MESSAGE(STATUS "Julia_VERSION_STRING: ${Julia_VERSION_STRING}")
45+
46+
##################
47+
# Julia Includes #
48+
##################
49+
50+
set(JULIA_HOME_NAME "Sys.BINDIR")
51+
if(${Julia_VERSION_STRING} VERSION_LESS "0.7.0")
52+
set(JULIA_HOME_NAME "JULIA_HOME")
53+
else()
54+
set(USING_LIBDL "using Libdl")
55+
endif()
56+
57+
if(DEFINED ENV{JULIA_INCLUDE_DIRS})
58+
set(Julia_INCLUDE_DIRS $ENV{JULIA_INCLUDE_DIRS}
59+
CACHE STRING "Location of Julia include files")
60+
elseif(Julia_EXECUTABLE)
61+
execute_process(
62+
COMMAND ${Julia_EXECUTABLE} --startup-file=no -E "julia_include_dir = joinpath(match(r\"(.*)(bin)\",${JULIA_HOME_NAME}).captures[1],\"include\",\"julia\")\n
63+
if !isdir(julia_include_dir) # then we're running directly from build\n
64+
julia_base_dir_aux = splitdir(splitdir(${JULIA_HOME_NAME})[1])[1] # useful for running-from-build\n
65+
julia_include_dir = joinpath(julia_base_dir_aux, \"usr\", \"include\" )\n
66+
julia_include_dir *= \";\" * joinpath(julia_base_dir_aux, \"src\", \"support\" )\n
67+
julia_include_dir *= \";\" * joinpath(julia_base_dir_aux, \"src\" )\n
68+
end\n
69+
julia_include_dir"
70+
OUTPUT_VARIABLE Julia_INCLUDE_DIRS
71+
)
72+
73+
string(REGEX REPLACE "\"" "" Julia_INCLUDE_DIRS "${Julia_INCLUDE_DIRS}")
74+
string(REGEX REPLACE "\n" "" Julia_INCLUDE_DIRS "${Julia_INCLUDE_DIRS}")
75+
set(Julia_INCLUDE_DIRS ${Julia_INCLUDE_DIRS}
76+
CACHE PATH "Location of Julia include files")
77+
elseif(Julia_PREFIX)
78+
set(Julia_INCLUDE_DIRS ${Julia_PREFIX}/include/julia)
79+
endif()
80+
set(Julia_INCLUDE_DIRS ${Julia_INCLUDE_DIRS};$ENV{includedir})
81+
MESSAGE(STATUS "Julia_INCLUDE_DIRS: ${Julia_INCLUDE_DIRS}")
82+
83+
###################
84+
# Julia Libraries #
85+
###################
86+
87+
if(WIN32)
88+
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES} .a;.dll)
89+
endif()
90+
91+
if(Julia_EXECUTABLE)
92+
execute_process(
93+
COMMAND ${Julia_EXECUTABLE} --startup-file=no -E "${USING_LIBDL}\nabspath(Libdl.dlpath((ccall(:jl_is_debugbuild, Cint, ()) != 0) ? \"libjulia-debug\" : \"libjulia\"))"
94+
OUTPUT_VARIABLE Julia_LIBRARY
95+
)
96+
97+
string(REGEX REPLACE "\"" "" Julia_LIBRARY "${Julia_LIBRARY}")
98+
string(REGEX REPLACE "\n" "" Julia_LIBRARY "${Julia_LIBRARY}")
99+
string(STRIP "${Julia_LIBRARY}" Julia_LIBRARY)
100+
101+
if(WIN32)
102+
get_filename_component(Julia_LIBRARY_DIR ${Julia_LIBRARY} DIRECTORY)
103+
get_filename_component(Julia_LIBRARY_DIR ${Julia_LIBRARY_DIR} DIRECTORY)
104+
find_library(win_Julia_LIBRARY
105+
NAMES libjulia.dll.a
106+
PATHS "${Julia_LIBRARY_DIR}/lib"
107+
NO_DEFAULT_PATH
108+
)
109+
set(Julia_LIBRARY "${win_Julia_LIBRARY}")
110+
endif()
111+
112+
set(Julia_LIBRARY "${Julia_LIBRARY}"
113+
CACHE PATH "Julia library")
114+
else()
115+
find_library(Julia_LIBRARY NAMES libjulia.${Julia_VERSION_STRING}.dylib julia libjulia.dll.a libjulia CMAKE_FIND_ROOT_PATH_BOTH)
116+
endif()
117+
118+
get_filename_component(Julia_LIBRARY_DIR ${Julia_LIBRARY} DIRECTORY)
119+
120+
MESSAGE(STATUS "Julia_LIBRARY_DIR: ${Julia_LIBRARY_DIR}")
121+
MESSAGE(STATUS "Julia_LIBRARY: ${Julia_LIBRARY}")
122+
123+
##############
124+
# JULIA_HOME #
125+
##############
126+
127+
if(Julia_EXECUTABLE)
128+
execute_process(
129+
COMMAND ${Julia_EXECUTABLE} --startup-file=no -E "${JULIA_HOME_NAME}"
130+
OUTPUT_VARIABLE JULIA_HOME
131+
)
132+
133+
string(REGEX REPLACE "\"" "" JULIA_HOME "${JULIA_HOME}")
134+
string(REGEX REPLACE "\n" "" JULIA_HOME "${JULIA_HOME}")
135+
136+
MESSAGE(STATUS "JULIA_HOME: ${JULIA_HOME}")
137+
138+
###################
139+
# libLLVM version #
140+
###################
141+
142+
execute_process(
143+
COMMAND ${Julia_EXECUTABLE} --startup-file=no -E "Base.libllvm_version"
144+
OUTPUT_VARIABLE Julia_LLVM_VERSION
145+
)
146+
147+
string(REGEX REPLACE "\"" "" Julia_LLVM_VERSION "${Julia_LLVM_VERSION}")
148+
string(REGEX REPLACE "\n" "" Julia_LLVM_VERSION "${Julia_LLVM_VERSION}")
149+
150+
MESSAGE(STATUS "Julia_LLVM_VERSION: ${Julia_LLVM_VERSION}")
151+
endif()
152+
153+
##################################
154+
# Check for Existence of Headers #
155+
##################################
156+
157+
find_path(Julia_MAIN_HEADER julia.h HINTS ${Julia_INCLUDE_DIRS})
158+
159+
#######################################
160+
# Determine if we are on 32 or 64 bit #
161+
#######################################
162+
163+
if(Julia_EXECUTABLE)
164+
execute_process(
165+
COMMAND ${Julia_EXECUTABLE} --startup-file=no -E "Sys.WORD_SIZE"
166+
OUTPUT_VARIABLE Julia_WORD_SIZE
167+
)
168+
string(REGEX REPLACE "\n" "" Julia_WORD_SIZE "${Julia_WORD_SIZE}")
169+
MESSAGE(STATUS "Julia_WORD_SIZE: ${Julia_WORD_SIZE}")
170+
endif()
171+
172+
if($ENV{target} MATCHES "^i686.*")
173+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse -msse2")
174+
endif()
175+
176+
###########################
177+
# FindPackage Boilerplate #
178+
###########################
179+
180+
include(FindPackageHandleStandardArgs)
181+
find_package_handle_standard_args(Julia
182+
REQUIRED_VARS Julia_LIBRARY Julia_LIBRARY_DIR Julia_INCLUDE_DIRS Julia_MAIN_HEADER
183+
VERSION_VAR Julia_VERSION_STRING
184+
FAIL_MESSAGE "Julia not found"
185+
)

cjetreconstruction/src/cjetreconstruction-finder.cc

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@
2121
#include "popl.hpp"
2222

2323
#include "JetReconstruction.h"
24+
#ifdef JETRECONSTRUCTION_COMPILER_PACKAGECOMPILER
25+
extern "C" {
26+
#include "julia_init.h"
27+
}
28+
#endif
2429

2530
#include "HepMC3/GenEvent.h"
2631
#include "HepMC3/GenParticle.h"
2732
#include "HepMC3/ReaderAscii.h"
2833

2934
#include "cjetreconstruction-utils.hh"
3035

31-
#ifdef JETRECONSTRUCTION_COMPILER_PACKAGECOMPILER
32-
extern "C" {
33-
#include "julia_init.h"
34-
}
35-
#endif
36+
#include <julia.h> // for jl_gc functions
3637

3738
using namespace std;
3839
using namespace popl;
@@ -48,7 +49,7 @@ void sorted_by_pt(jetreconstruction_JetsResult &jets) {
4849
}
4950

5051
jetreconstruction_ClusterSequence
51-
run_clustering(std::vector<jetreconstruction_PseudoJet> input_particles,
52+
run_clustering(std::vector<jetreconstruction_PseudoJet>& input_particles,
5253
jetreconstruction_RecoStrategy strategy,
5354
jetreconstruction_JetAlgorithm algorithm, double R, double p) {
5455

@@ -89,6 +90,8 @@ int main(int argc, char *argv[]) {
8990
auto njets_option = opts.add<Value<int>>("", "njets", "njets value for exclusive jets");
9091
auto dump_option = opts.add<Value<string>>("d", "dump", "Filename to dump jets to");
9192
auto debug_clusterseq_option = opts.add<Switch>("c", "debug-clusterseq", "Dump cluster sequence jet and history content");
93+
auto gc_option = opts.add<Switch>("", "gc", "Enable garbage collector");
94+
9295

9396

9497
opts.parse(argc, argv);
@@ -119,6 +122,9 @@ int main(int argc, char *argv[]) {
119122
exit(EXIT_FAILURE);
120123
}
121124

125+
std::cout << "Previous GC state: " << jl_gc_is_enabled() << std::endl;
126+
jl_gc_enable(gc_option->count());
127+
std::cout << "Current GC state: " << jl_gc_is_enabled() << std::endl;
122128
// read in input events
123129
//----------------------------------------------------------
124130
auto events = read_input_events(input_file.c_str(), maxevents);
@@ -190,6 +196,9 @@ int main(int argc, char *argv[]) {
190196
double time_lowest = 1.0e20;
191197
for (long trial = 0; trial < trials; ++trial) {
192198
std::cout << "Trial " << trial << " ";
199+
if (jl_gc_is_enabled() == 0) {
200+
jl_gc_collect(JL_GC_FULL);
201+
}
193202
auto start_t = std::chrono::steady_clock::now();
194203
for (size_t ievt = skip_events_option->value(); ievt < events.size(); ++ievt) {
195204
auto cluster_sequence =
@@ -225,12 +234,12 @@ int main(int argc, char *argv[]) {
225234
}
226235

227236
// Dump the cluster sequence history content as well?
228-
if (debug_clusterseq_option->is_set()) {
229-
// dump_clusterseq(cluster_sequence);
237+
if (debug_clusterseq_option->is_set()) {
238+
// dump_clusterseq(cluster_sequence);
230239
}
231-
jetreconstruction_ClusterSequence_free_members(&cluster_sequence);
232-
jetreconstruction_JetsResult_free_members(&final_jets);
233240
}
241+
jetreconstruction_ClusterSequence_free_members(&cluster_sequence);
242+
jetreconstruction_JetsResult_free_members(&final_jets);
234243
}
235244
auto stop_t = std::chrono::steady_clock::now();
236245
auto elapsed = stop_t - start_t;
@@ -255,8 +264,9 @@ int main(int argc, char *argv[]) {
255264
std::cout << "Time per event " << mean_per_event << " +- " << sigma_per_event << " us" << endl;
256265
std::cout << "Lowest time per event " << time_lowest << " us" << endl;
257266

267+
const int ret_code = 0;
258268
#ifdef JETRECONSTRUCTION_COMPILER_PACKAGECOMPILER
259-
shutdown_julia(0);
269+
shutdown_julia(ret_code);
260270
#endif
261-
return 0;
271+
return ret_code;
262272
}

0 commit comments

Comments
 (0)