Skip to content

Commit a72d726

Browse files
sawenzelclaude
andcommitted
Add a test that multithreaded o2-sim gives the same output for 1 and N threads
This adds the script o2-sim-mt-equality.sh and a ctest that runs it. - The script runs the same seeded workload with --isMT -j 1 and -j N and compares the kinematics and hits per event and branch. - It reports the first difference and exits with 1 when the outputs differ. - The ctest replaces the disabled o2sim_G4_mt test and is registered only for Geant4 built with multithreading. Usage: o2-sim-mt-equality.sh -n 5 -j 4 --field 0 HALL MAG DIPO COMP PIPE ABSO SHIL TPC https://its.cern.ch/jira/browse/O2-7305 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3765577 commit a72d726

2 files changed

Lines changed: 121 additions & 20 deletions

File tree

‎run/CMakeLists.txt‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,26 +230,21 @@ set_tests_properties(o2sim_G4
230230
G4)
231231
set_property(TEST o2sim_G4 APPEND PROPERTY ENVIRONMENT ${G4ENV})
232232

233-
# # note that the MT is currently only supported in the non FairMQ version
234-
# # switched off
235-
# o2_add_test_command(NAME o2sim_G4_mt
236-
# WORKING_DIRECTORY ${SIMTESTDIR}
237-
# TIMEOUT 400
238-
# COMMAND $<TARGET_FILE:${o2simSerialExecutable}>
239-
# COMMAND_LINE_ARGS -n
240-
# 1
241-
# -e
242-
# TGeant4
243-
# --isMT
244-
# on
245-
# -o
246-
# o2simG4MT
247-
# ENVIRONMENT "${SIMENV}"
248-
# LABELS "g4;sim;long")
249-
#set_tests_properties(o2sim_G4_mt
250-
# PROPERTIES PASS_REGULAR_EXPRESSION
251-
# "Macro finished succesfully")
252-
#set_property(TEST o2sim_G4_mt APPEND PROPERTY ENVIRONMENT ${G4ENV})
233+
# multithreaded Geant4 must give the same output for 1 and 2 transport threads
234+
if(Geant4_multithreaded_FOUND)
235+
o2_add_test_command(NAME o2sim_G4_mt_equality
236+
WORKING_DIRECTORY ${SIMTESTDIR}
237+
TIMEOUT 600
238+
COMMAND ${CMAKE_SOURCE_DIR}/run/o2-sim-mt-equality.sh
239+
COMMAND_LINE_ARGS --workdir
240+
${SIMTESTDIR}/mt_equality
241+
ENVIRONMENT "${SIMENV}"
242+
LABELS "g4;sim;long")
243+
set_tests_properties(o2sim_G4_mt_equality
244+
PROPERTIES PASS_REGULAR_EXPRESSION
245+
"EQUALITY TEST PASSED")
246+
set_property(TEST o2sim_G4_mt_equality APPEND PROPERTY ENVIRONMENT ${G4ENV})
247+
endif()
253248

254249

255250
o2_add_test(CheckStackG4
@@ -360,6 +355,7 @@ set_tests_properties(o2sim_G4_checklogs
360355
PROPERTIES FIXTURES_REQUIRED G4)
361356
endif()
362357

358+
install(PROGRAMS o2-sim-mt-equality.sh DESTINATION ${CMAKE_INSTALL_BINDIR})
363359
install(FILES o2-sim-client.py PERMISSIONS GROUP_READ GROUP_EXECUTE OWNER_EXECUTE OWNER_WRITE OWNER_READ WORLD_EXECUTE WORLD_READ DESTINATION ${CMAKE_INSTALL_BINDIR})
364360
install(DIRECTORY SimExamples/
365361
DESTINATION examples

‎run/o2-sim-mt-equality.sh‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env bash
2+
# Checks that multithreaded Geant4 in o2-sim gives the same output for 1 and N transport threads.
3+
#
4+
# Usage: o2-sim-mt-equality.sh [-n events] [-j threads] [-g generator] [--field kG] [--seed seed] [--workdir dir] [module ...]
5+
#
6+
# Runs the same seeded workload with --isMT -j 1 and --isMT -j N and compares the kinematics and hit files
7+
# event by event and branch by branch. Exits with 1 at the first difference. Without modules, the passive
8+
# geometry with ITS is used.
9+
10+
nev=2
11+
nthreads=2
12+
gen=pythia8pp
13+
field=-5
14+
seed=4711
15+
workdir=
16+
modules=()
17+
while [ $# -gt 0 ]; do
18+
case $1 in
19+
-n) nev=$2; shift 2 ;;
20+
-j) nthreads=$2; shift 2 ;;
21+
-g) gen=$2; shift 2 ;;
22+
--field) field=$2; shift 2 ;;
23+
--seed) seed=$2; shift 2 ;;
24+
--workdir) workdir=$2; shift 2 ;;
25+
-h|--help) sed -n '2,9p' $0; exit 0 ;;
26+
*) modules+=("$1"); shift ;;
27+
esac
28+
done
29+
[ ${#modules[@]} -eq 0 ] && modules=(HALL MAG DIPO COMP PIPE ABSO SHIL ITS)
30+
o2sim=o2-sim
31+
[ -x "$O2_ROOT/bin/o2-sim" ] && o2sim=$O2_ROOT/bin/o2-sim
32+
[ -z "$workdir" ] && workdir=$(mktemp -d ${TMPDIR:-/tmp}/o2sim-mt-equality-XXXXXX)
33+
mkdir -p $workdir && cd $workdir || exit 1
34+
35+
# content hash per event and branch of the o2sim tree, independent of the basket layout
36+
cat > hashtree.C <<'EOF'
37+
void hashtree(const char* fname)
38+
{
39+
TFile f(fname);
40+
auto t = (TTree*)f.Get("o2sim");
41+
if (!t) {
42+
return;
43+
}
44+
std::vector<std::tuple<TClass*, void*, std::string>> objs;
45+
objs.reserve(t->GetListOfBranches()->GetEntries()); // the branch addresses point into objs
46+
for (auto b : *t->GetListOfBranches()) {
47+
auto br = (TBranch*)b;
48+
TClass* cl = nullptr;
49+
EDataType dt;
50+
br->GetExpectedType(cl, dt);
51+
if (!cl) {
52+
continue;
53+
}
54+
objs.emplace_back(cl, cl->New(), br->GetName());
55+
br->SetAddress(&std::get<1>(objs.back()));
56+
}
57+
for (Long64_t i = 0; i < t->GetEntries(); ++i) {
58+
t->GetEntry(i);
59+
for (auto& [cl, obj, name] : objs) {
60+
TBufferFile buf(TBuffer::kWrite);
61+
buf.WriteObjectAny(obj, cl);
62+
TMD5 md5;
63+
md5.Update((UChar_t*)buf.Buffer(), buf.Length());
64+
md5.Final();
65+
printf("HASH event %lld branch %s %s\n", i, name.c_str(), md5.AsString());
66+
}
67+
}
68+
}
69+
EOF
70+
71+
run() { # <threads>
72+
local dir=mt$1
73+
mkdir -p $dir && cd $dir
74+
$o2sim --isMT 1 -j $1 -n $nev -g $gen -e TGeant4 --seed $seed --field $field -m ${modules[@]} \
75+
--configKeyValues "SimCutParams.trackSeed=true" > sim.log 2>&1
76+
local rc=$?
77+
if [ $rc -ne 0 ] || ! grep -q "SIMULATION RETURNED SUCCESFULLY" sim.log; then
78+
echo "o2-sim with $1 threads failed (exit code $rc), see $workdir/$dir"
79+
exit 1
80+
fi
81+
for f in o2sim_Kine.root o2sim_Hits*.root; do
82+
[ -f $f ] && root -l -b -q "../hashtree.C(\"$f\")" 2>/dev/null | grep "^HASH" | sed "s/^HASH/$f/" > $f.hashes
83+
done
84+
cd ..
85+
}
86+
87+
echo "o2-sim MT equality: ${modules[*]}, $nev events of $gen, field $field kG, 1 against $nthreads threads, in $workdir"
88+
run 1
89+
run $nthreads
90+
status=0
91+
for h in mt1/*.hashes; do
92+
f=$(basename $h)
93+
if [ ! -s $h ]; then
94+
echo "${f%.hashes}: no entries"
95+
status=1
96+
elif cmp -s $h mt$nthreads/$f; then
97+
echo "${f%.hashes}: identical ($(wc -l < $h) event-branch hashes)"
98+
else
99+
echo "${f%.hashes}: DIFFERS, first at $(diff $h mt$nthreads/$f | grep -m1 '^<' | cut -d' ' -f3-6)"
100+
status=1
101+
fi
102+
done
103+
[ $(ls mt$nthreads/*.hashes | wc -l) -eq $(ls mt1/*.hashes | wc -l) ] || { echo "the runs wrote different output files"; status=1; }
104+
[ $status -eq 0 ] && echo "EQUALITY TEST PASSED" || echo "EQUALITY TEST FAILED"
105+
exit $status

0 commit comments

Comments
 (0)