Skip to content

Commit c441191

Browse files
committed
Mutex-free TGeo material budget for multithreaded users
GeometryManager::meanMaterialBudget(Ext) used the shared TGeo navigator under sTGMutex, serializing all callers (Propagator TGeo branch, LUT building). Add opt-in per-thread navigators: * GeometryManager::setMaxThreads(n): wraps TGeoManager::SetMaxThreads and enables per-thread navigation; returns the effective capacity. * GeometryManager::getThreadNavigator(): thread_local navigator, created on first use, removed at thread exit. nullptr if not enabled. Throws std::runtime_error if more threads than declared ask for one. * meanMaterialBudget / meanMaterialBudgetExt (and thus Propagator, unchanged) take the calling thread's navigator when enabled, otherwise the shared one under the mutex, as before. meanMaterialBudgetExt now navigates via a TGeoNavigator instead of the gGeoManager global API. * getOriginalMatrix / getOriginalMatrixFromPath navigate the same way (they also no longer re-lock sTGMutex when getOriginalMatrix(symname) falls back to getOriginalMatrixFromPath, which self-deadlocked). * rebuildVoxels(): the voxels, shared by all navigators and rebuilt lazily after TGeoPhysicalNode::Align, are rebuilt serially (also in setMaxThreads). * MatLayerCylSet LUT build uses the same mechanism and limits its parallelism to the enabled capacity. * Unit test testGeometryManagerMT. How to use in code calling Propagator (or meanMaterialBudget) from threads: 1. Load the geometry (and apply alignment). 2. Serially, BEFORE starting the worker threads, call once o2::base::GeometryManager::setMaxThreads(nThreads); with nThreads >= the maximum number of threads which will ever use TGeo in the process (workers other than the calling thread). 3. Call Propagator methods (USEMatCorrTGeo or TGeo fall-back without LUT) from the threads as usual: no further change needed. Without step 2 behaviour is unchanged (thread-safe but mutexed). MatCorrType::USEMatCorrLUT never touches TGeo. Corner cases to avoid (TGeoManager is not designed for them): * setMaxThreads can be called effectively once per geometry: TGeo would reset the thread slots of threads already navigating. Repeated calls with n <= capacity are no-ops, a larger n is refused with an error. * TGeo thread slots (TGeoManager::ThreadId) are never released: every distinct thread which navigated keeps its slot after exit. With thread churn (short-lived threads) use a thread pool, otherwise slots run out and getThreadNavigator throws. Never silently fall back to the shared navigator in multithread mode: it would index the per-volume thread data with the calling thread's slot, possibly out of range. * Direct use of the gGeoManager navigation API (cd, InitTrack, GetCurrentNode, ...) from a worker thread is safe only after that thread has called getThreadNavigator() (or called setMaxThreads): GetCurrentNavigator returns nullptr for threads without a navigator. Prefer calling the methods of the navigator returned by getThreadNavigator(). Any TGeo navigation from a new thread consumes a slot. * TGeoPhysicalNode::Align after setMaxThreads: call rebuildVoxels() while no thread navigates, otherwise the lazy rebuild races between the threads. * Replacing gGeoManager disables multithreading (detected via IsMultiThread(), not only the pointer) and the stale navigators are dropped. The slot map is static in TGeo and not reset: threads which navigated before must not be reused and enabling again may find slots already consumed (warning issued). * setMaxThreads, rebuildVoxels and geometry replacement need all other threads to be idle. Co-Authored-By: Claude Sonnet 5.5
1 parent cf1343c commit c441191

5 files changed

Lines changed: 409 additions & 37 deletions

File tree

‎Detectors/Base/CMakeLists.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ o2_add_test(
105105
PUBLIC_LINK_LIBRARIES O2::DetectorsBase
106106
LABELS detectorsbase)
107107

108+
o2_add_test(
109+
GeometryManagerMT
110+
SOURCES test/testGeometryManagerMT.cxx
111+
COMPONENT_NAME DetectorsBase
112+
PUBLIC_LINK_LIBRARIES O2::DetectorsBase
113+
LABELS detectorsbase)
114+
108115
o2_add_test(
109116
O2Tessellated
110117
SOURCES test/testO2Tessellated.cxx

‎Detectors/Base/include/DetectorsBase/GeometryManager.h‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "GPUCommonLogger.h" // for LOG
2727
#include "MathUtils/Cartesian.h"
2828
#include "DetectorsBase/MatCell.h"
29+
#include <atomic>
2930
#include <mutex>
3031
#include <vector>
3132
class TGeoHMatrix; // lines 11-11
@@ -105,8 +106,40 @@ class GeometryManager : public TObject
105106
ClassDefNV(MatBudgetExt, 1);
106107
};
107108

109+
/// Multithreaded TGeo navigation (used by Propagator and MatLayerCylSet).
110+
///
111+
/// setMaxThreads(n): enable per-thread navigation for up to n threads in addition to the calling one.
112+
/// It wraps TGeoManager::SetMaxThreads, which is one-way and not re-entrant, hence:
113+
/// * call it once, serially, after the geometry is loaded/aligned and BEFORE the threads start navigating,
114+
/// with n >= the maximum number of threads which will ever call TGeo (the TGeo thread slots are never
115+
/// released: a slot is consumed by every distinct thread which navigated, even after it exited);
116+
/// * repeated calls with n <= current capacity are no-ops; a larger n is refused (error logged), since TGeo
117+
/// would reset the thread slots of the threads already navigating;
118+
/// * after TGeoPhysicalNode::Align, call rebuildVoxels() while no thread is navigating;
119+
/// * replacing gGeoManager disables multithreading (threads that navigated before must not be reused).
120+
/// Returns the effective capacity (0 if not enabled).
121+
/// While not enabled, everything behaves as before: the shared TGeo navigator is used under a mutex.
122+
/// Once enabled, a thread navigates through its own navigator, obtained by getThreadNavigator(). This is done
123+
/// internally by meanMaterialBudget(Ext), getOriginalMatrix* and thus Propagator. Direct use of the gGeoManager
124+
/// navigation API (cd, InitTrack, GetCurrentNode, ...) is safe only in a thread which has called
125+
/// getThreadNavigator() before (or which called setMaxThreads): gGeoManager->GetCurrentNavigator() returns
126+
/// nullptr for any other thread, and any TGeo navigation from a new thread consumes one of the thread slots.
127+
/// Prefer calling the methods of the navigator returned by getThreadNavigator().
128+
static int setMaxThreads(int nThreads);
129+
static int getMaxThreads(); // capacity set by setMaxThreads for the current geometry, 0 if not enabled
130+
131+
/// Voxel structures are shared by all navigators and rebuilt lazily at the first navigation after
132+
/// TGeoPhysicalNode::Align. Do it explicitly (serially, with no thread navigating). Called by setMaxThreads.
133+
static void rebuildVoxels();
134+
135+
/// Navigator owned by the calling thread (created on the first call, released at the thread exit), to be
136+
/// passed to meanMaterialBudget(..., nav). Returns nullptr if multithreading is not enabled (=> mutexed shared
137+
/// navigator). Throws std::runtime_error if more threads than declared in setMaxThreads() ask for a navigator.
138+
static TGeoNavigator* getThreadNavigator();
139+
108140
/// Mean material budget between two points. Pass a navigator owned by the calling thread to
109-
/// run lock-free from several threads; with nav = nullptr the shared navigator is used under
141+
/// run lock-free from several threads. With nav = nullptr: the calling thread's own navigator if setMaxThreads()
142+
/// was called, otherwise the shared navigator is used under
110143
/// a mutex, as before.
111144
static o2::base::MatBudget meanMaterialBudget(float x0, float y0, float z0, float x1, float y1, float z1,
112145
TGeoNavigator* nav = nullptr);
@@ -182,6 +215,10 @@ class GeometryManager : public TObject
182215
static constexpr UInt_t sDetOffsetLarge = 17; /// detector identifier will start from this bit for detectors after the FOC
183216
static constexpr UInt_t sSensorMaskLarge = (0x1 << sDetOffsetLarge) - 1; /// mask=max sensitive volumes allowed per detector (131071)
184217
static std::mutex sTGMutex;
218+
static std::atomic<int> sMaxThreads; // TGeo multithreading capacity, 0: not enabled
219+
static std::atomic<int> sGeneration; // bumped when multithreading is (re)enabled or invalidated
220+
static TGeoManager* sGeo; // TGeoManager multithreading was enabled for
221+
static bool isMTActive(); // multithreading enabled for the current gGeoManager (resets stale state)
185222

186223
ClassDefOverride(GeometryManager, 0); // Manager of geometry information for alignment
187224
};

‎Detectors/Base/src/GeometryManager.cxx‎

Lines changed: 169 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include <TGeoNavigator.h> // for TGeoNavigator
2020
#include <TGeoNode.h> // for TGeoNode
2121
#include <TGeoPhysicalNode.h> // for TGeoPhysicalNode, TGeoPNEntry
22+
#include <TGeoVolume.h>
23+
#include <TGeoVoxelFinder.h>
24+
#include <stdexcept>
2225
#include <string>
2326
#include <cassert>
2427
#include <cstddef> // for NULL
@@ -66,6 +69,126 @@ using namespace o2::base;
6669
/// the look-up table mapping unique volume indices to symbolic volume names. For that, it
6770
/// collects several static methods
6871
std::mutex GeometryManager::sTGMutex;
72+
std::atomic<int> GeometryManager::sMaxThreads{0};
73+
std::atomic<int> GeometryManager::sGeneration{0};
74+
TGeoManager* GeometryManager::sGeo = nullptr;
75+
76+
//_____________________________________________________________________________________
77+
bool GeometryManager::isMTActive()
78+
{
79+
// A TGeoManager which was not put in multithread mode by setMaxThreads (e.g. a geometry loaded after
80+
// the previous one was deleted, possibly at the same address) is detected by the IsMultiThread() check.
81+
if (sMaxThreads.load() == 0) {
82+
return false;
83+
}
84+
if (gGeoManager && gGeoManager == sGeo && gGeoManager->IsMultiThread()) {
85+
return true;
86+
}
87+
std::lock_guard<std::mutex> guard(sTGMutex);
88+
if (sMaxThreads.load() != 0 && !(gGeoManager && gGeoManager == sGeo && gGeoManager->IsMultiThread())) {
89+
LOG(warning) << "TGeoManager was replaced: per-thread TGeo navigation is disabled until GeometryManager::setMaxThreads is called again";
90+
sMaxThreads = 0;
91+
sGeo = nullptr;
92+
sGeneration++; // invalidates the navigators held by threads
93+
}
94+
return false;
95+
}
96+
97+
//_____________________________________________________________________________________
98+
int GeometryManager::getMaxThreads()
99+
{
100+
return isMTActive() ? sMaxThreads.load() : 0;
101+
}
102+
103+
//_____________________________________________________________________________________
104+
void GeometryManager::rebuildVoxels()
105+
{
106+
// TGeoVoxelFinder rebuilds its voxels lazily at the first navigation into a volume flagged by
107+
// SetNeedRebuild() (e.g. mothers of TGeoPhysicalNode::Align-ed nodes). The voxel structures are shared
108+
// between all navigators, so this has to be done serially, not concurrently from the worker threads.
109+
if (!gGeoManager) {
110+
LOG(fatal) << "geometry is not loaded";
111+
}
112+
std::lock_guard<std::mutex> guard(sTGMutex);
113+
TIter next(gGeoManager->GetListOfVolumes());
114+
while (auto* vol = static_cast<TGeoVolume*>(next())) {
115+
if (auto* voxels = vol->GetVoxels(); voxels && voxels->NeedRebuild()) {
116+
voxels->Voxelize(); // resets the NeedRebuild flag
117+
vol->FindOverlaps();
118+
}
119+
}
120+
}
121+
122+
//_____________________________________________________________________________________
123+
int GeometryManager::setMaxThreads(int nThreads)
124+
{
125+
if (!gGeoManager) {
126+
LOG(fatal) << "geometry is not loaded";
127+
}
128+
if (nThreads < 1) {
129+
return getMaxThreads();
130+
}
131+
if (isMTActive()) {
132+
const int cur = sMaxThreads.load();
133+
if (nThreads > cur) {
134+
// TGeoManager::SetMaxThreads would clear the thread slots (and the per-volume thread data) of the
135+
// threads which already navigate, so the capacity cannot be raised once it is enabled.
136+
LOG(error) << "GeometryManager::setMaxThreads(" << nThreads << ") refused: multithreaded navigation was already enabled for "
137+
<< cur << " threads and cannot be extended. Call setMaxThreads once with the maximum number of threads";
138+
}
139+
return cur;
140+
}
141+
rebuildVoxels();
142+
std::lock_guard<std::mutex> guard(sTGMutex);
143+
gGeoManager->SetMaxThreads(nThreads); // one-way in TGeoManager
144+
// The map of thread slots is static in TGeoManager (it is not reset with the geometry, and the threads cache
145+
// their slot), so slots used with a previous geometry stay consumed. The calling thread takes a slot first.
146+
const int ownerSlot = TGeoManager::ThreadId();
147+
if (ownerSlot > nThreads) {
148+
LOG(warning) << "TGeo thread slots were already consumed (calling thread has slot " << ownerSlot
149+
<< ", capacity " << nThreads << "): per-thread navigators will not be available";
150+
}
151+
sGeo = gGeoManager;
152+
sGeneration++;
153+
sMaxThreads = nThreads;
154+
return nThreads;
155+
}
156+
157+
//_____________________________________________________________________________________
158+
TGeoNavigator* GeometryManager::getThreadNavigator()
159+
{
160+
struct Holder {
161+
TGeoNavigator* nav = nullptr;
162+
int gen = -1; // value of sGeneration the navigator belongs to
163+
~Holder()
164+
{
165+
if (nav) {
166+
std::lock_guard<std::mutex> guard(sTGMutex);
167+
if (gen == sGeneration.load() && gGeoManager && gGeoManager == sGeo) { // else the navigator died with its geometry
168+
gGeoManager->RemoveNavigator(nav); // the TGeo thread slot of this thread stays consumed
169+
}
170+
}
171+
}
172+
};
173+
thread_local Holder h;
174+
if (!isMTActive()) {
175+
return nullptr;
176+
}
177+
if (h.nav && h.gen == sGeneration.load()) {
178+
return h.nav;
179+
}
180+
h.nav = nullptr; // none yet, or the one from the previous geometry
181+
std::lock_guard<std::mutex> guard(sTGMutex);
182+
h.gen = sGeneration.load();
183+
// The slot (TGeoManager::ThreadId) is what indexes the per-volume thread data: it must be within the
184+
// reserved range, otherwise TGeo would access beyond its arrays.
185+
if (TGeoManager::ThreadId() > gGeoManager->GetMaxThreads()) {
186+
throw std::runtime_error("GeometryManager: more threads use TGeo than declared in GeometryManager::setMaxThreads(" +
187+
std::to_string(sMaxThreads.load()) + ")");
188+
}
189+
h.nav = gGeoManager->AddNavigator();
190+
return h.nav;
191+
}
69192

70193
//______________________________________________________________________
71194
Bool_t GeometryManager::getOriginalMatrix(const char* symname, TGeoHMatrix& m)
@@ -76,15 +199,21 @@ Bool_t GeometryManager::getOriginalMatrix(const char* symname, TGeoHMatrix& m)
76199
;
77200
return kFALSE;
78201
}
79-
std::lock_guard<std::mutex> guard(sTGMutex);
202+
// navigator of the calling thread if setMaxThreads was called, otherwise the shared one under the mutex
203+
std::unique_lock<std::mutex> guard(sTGMutex, std::defer_lock);
204+
TGeoNavigator* nav = getThreadNavigator();
205+
if (!nav) {
206+
guard.lock();
207+
nav = gGeoManager->GetCurrentNavigator();
208+
}
80209
if (!gGeoManager->GetListOfPhysicalNodes()) {
81210
LOG(warning) << "gGeoManager doesn't contain any aligned nodes!";
82211

83-
if (!gGeoManager->cd(symname)) {
212+
if (!nav->cd(symname)) {
84213
LOG(error) << "Volume path " << symname << " not valid!";
85214
return kFALSE;
86215
} else {
87-
m = *gGeoManager->GetCurrentMatrix();
216+
m = *nav->GetCurrentMatrix();
88217
return kTRUE;
89218
}
90219
}
@@ -101,6 +230,9 @@ Bool_t GeometryManager::getOriginalMatrix(const char* symname, TGeoHMatrix& m)
101230
path = symname;
102231
}
103232

233+
if (guard.owns_lock()) { // getOriginalMatrixFromPath takes the (non-recursive) mutex itself
234+
guard.unlock();
235+
}
104236
return getOriginalMatrixFromPath(path, m);
105237
}
106238

@@ -113,19 +245,25 @@ Bool_t GeometryManager::getOriginalMatrixFromPath(const char* path, TGeoHMatrix&
113245
LOG(error) << "Can't get the original global matrix! gGeoManager doesn't exist or it is still opened!";
114246
return kFALSE;
115247
}
116-
std::lock_guard<std::mutex> guard(sTGMutex);
117-
if (!gGeoManager->CheckPath(path)) {
248+
// navigator of the calling thread if setMaxThreads was called, otherwise the shared one under the mutex
249+
std::unique_lock<std::mutex> guard(sTGMutex, std::defer_lock);
250+
TGeoNavigator* nav = getThreadNavigator();
251+
if (!nav) {
252+
guard.lock();
253+
nav = gGeoManager->GetCurrentNavigator();
254+
}
255+
if (!nav->CheckPath(path)) {
118256
LOG(error) << "Volume path " << path << " not valid!";
119257
return kFALSE;
120258
}
121259

122260
TIter next(gGeoManager->GetListOfPhysicalNodes());
123-
gGeoManager->cd(path);
261+
nav->cd(path);
124262

125-
while (gGeoManager->GetLevel()) {
263+
while (nav->GetLevel()) {
126264
TGeoPhysicalNode* physNode = nullptr;
127265
next.Reset();
128-
TGeoNode* node = gGeoManager->GetCurrentNode();
266+
TGeoNode* node = nav->GetCurrentNode();
129267

130268
while ((physNode = (TGeoPhysicalNode*)next())) {
131269
if (physNode->GetNode() == node) {
@@ -145,7 +283,7 @@ Bool_t GeometryManager::getOriginalMatrixFromPath(const char* path, TGeoHMatrix&
145283

146284
m.MultiplyLeft(lm);
147285

148-
gGeoManager->CdUp();
286+
nav->CdUp();
149287
}
150288
return kTRUE;
151289
}
@@ -375,9 +513,15 @@ GeometryManager::MatBudgetExt GeometryManager::meanMaterialBudgetExt(float x0, f
375513
for (int i = 3; i--;) {
376514
dir[i] *= invlen;
377515
}
378-
std::lock_guard<std::mutex> guard(sTGMutex);
516+
// per-thread navigator if setMaxThreads was called, otherwise the shared one under the mutex
517+
std::unique_lock<std::mutex> guard(sTGMutex, std::defer_lock);
518+
TGeoNavigator* nav = getThreadNavigator();
519+
if (!nav) {
520+
guard.lock();
521+
nav = gGeoManager->GetCurrentNavigator();
522+
}
379523
// Initialize start point and direction
380-
TGeoNode* currentnode = gGeoManager->InitTrack(startD, dir);
524+
TGeoNode* currentnode = nav->InitTrack(startD, dir);
381525
if (!currentnode) {
382526
LOG(error) << "start point out of geometry: " << x0 << ':' << y0 << ':' << z0;
383527
return MatBudgetExt(); // return empty struct
@@ -389,11 +533,11 @@ GeometryManager::MatBudgetExt GeometryManager::meanMaterialBudgetExt(float x0, f
389533

390534
// Locate next boundary within length without computing safety.
391535
// Propagate either with length (if no boundary found) or just cross boundary
392-
gGeoManager->FindNextBoundaryAndStep(length, kFALSE);
536+
nav->FindNextBoundaryAndStep(length, kFALSE);
393537
Double_t stepTot = 0.0; // Step made
394-
Double_t step = gGeoManager->GetStep();
538+
Double_t step = nav->GetStep();
395539
// If no boundary within proposed length, return current step data
396-
if (!gGeoManager->IsOnBoundary()) {
540+
if (!nav->IsOnBoundary()) {
397541
budStep.meanX2X0 = budStep.length / budStep.meanX2X0;
398542
return MatBudgetExt(budStep);
399543
}
@@ -408,7 +552,7 @@ GeometryManager::MatBudgetExt GeometryManager::meanMaterialBudgetExt(float x0, f
408552
if (nzero > 3) {
409553
// This means navigation has problems on one boundary
410554
// Try to cross by making a small step
411-
const double* curPos = gGeoManager->GetCurrentPoint();
555+
const double* curPos = nav->GetCurrentPoint();
412556
LOG(warning) << "Cannot cross boundary at (" << curPos[0] << ',' << curPos[1] << ',' << curPos[2] << ')';
413557
budTotal.normalize(stepTot);
414558
budTotal.nCross = -1; // flag failed navigation
@@ -426,14 +570,14 @@ GeometryManager::MatBudgetExt GeometryManager::meanMaterialBudgetExt(float x0, f
426570
if (step >= length) {
427571
break;
428572
}
429-
currentnode = gGeoManager->GetCurrentNode();
573+
currentnode = nav->GetCurrentNode();
430574
if (!currentnode) {
431575
break;
432576
}
433577
length -= step;
434578
accountMaterial(currentnode->GetVolume()->GetMedium()->GetMaterial(), budStep);
435-
gGeoManager->FindNextBoundaryAndStep(length, kFALSE);
436-
step = gGeoManager->GetStep();
579+
nav->FindNextBoundaryAndStep(length, kFALSE);
580+
step = nav->GetStep();
437581
}
438582
budTotal.normalize(stepTot);
439583
return MatBudgetExt(budTotal);
@@ -470,11 +614,14 @@ o2::base::MatBudget GeometryManager::meanMaterialBudget(float x0, float y0, floa
470614
for (int i = 3; i--;) {
471615
dir[i] *= invlen;
472616
}
473-
// A caller that passes its own navigator owns it exclusively, so no locking is needed. A caller
474-
// that passes none shares gGeoManager's current navigator and must still serialize. Deciding
475-
// this from the argument keeps the choice local: it does not depend on -- and cannot be broken
476-
// by -- process-global state such as TGeoManager::GetMaxThreads().
617+
// A caller that passes its own navigator (or, with multithreading enabled by setMaxThreads, gets the one of
618+
// its thread) owns it exclusively, so no locking is needed. Otherwise the shared navigator is used under a mutex.
619+
// In multithread mode a thread without navigator must never touch the shared one (GetCurrentNavigator would
620+
// return nullptr for it and the per-volume thread data is indexed by the thread slot).
477621
std::unique_lock<std::mutex> guard(sTGMutex, std::defer_lock);
622+
if (!nav) {
623+
nav = getThreadNavigator();
624+
}
478625
if (!nav) {
479626
guard.lock();
480627
nav = gGeoManager->GetCurrentNavigator();

0 commit comments

Comments
 (0)