Repository navigation
Commit 6de3742
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.51 parent 9b8decd commit 6de3742
5 files changed
Lines changed: 409 additions & 37 deletions
File tree
- Detectors/Base
- include/DetectorsBase
- src
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
108 | 115 | | |
109 | 116 | | |
110 | 117 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
105 | 106 | | |
106 | 107 | | |
107 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
108 | 140 | | |
109 | | - | |
| 141 | + | |
| 142 | + | |
110 | 143 | | |
111 | 144 | | |
112 | 145 | | |
| |||
182 | 215 | | |
183 | 216 | | |
184 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
185 | 222 | | |
186 | 223 | | |
187 | 224 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
| |||
66 | 69 | | |
67 | 70 | | |
68 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
69 | 192 | | |
70 | 193 | | |
71 | 194 | | |
| |||
76 | 199 | | |
77 | 200 | | |
78 | 201 | | |
79 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
80 | 209 | | |
81 | 210 | | |
82 | 211 | | |
83 | | - | |
| 212 | + | |
84 | 213 | | |
85 | 214 | | |
86 | 215 | | |
87 | | - | |
| 216 | + | |
88 | 217 | | |
89 | 218 | | |
90 | 219 | | |
| |||
101 | 230 | | |
102 | 231 | | |
103 | 232 | | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
104 | 236 | | |
105 | 237 | | |
106 | 238 | | |
| |||
113 | 245 | | |
114 | 246 | | |
115 | 247 | | |
116 | | - | |
117 | | - | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
118 | 256 | | |
119 | 257 | | |
120 | 258 | | |
121 | 259 | | |
122 | 260 | | |
123 | | - | |
| 261 | + | |
124 | 262 | | |
125 | | - | |
| 263 | + | |
126 | 264 | | |
127 | 265 | | |
128 | | - | |
| 266 | + | |
129 | 267 | | |
130 | 268 | | |
131 | 269 | | |
| |||
145 | 283 | | |
146 | 284 | | |
147 | 285 | | |
148 | | - | |
| 286 | + | |
149 | 287 | | |
150 | 288 | | |
151 | 289 | | |
| |||
375 | 513 | | |
376 | 514 | | |
377 | 515 | | |
378 | | - | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
379 | 523 | | |
380 | | - | |
| 524 | + | |
381 | 525 | | |
382 | 526 | | |
383 | 527 | | |
| |||
389 | 533 | | |
390 | 534 | | |
391 | 535 | | |
392 | | - | |
| 536 | + | |
393 | 537 | | |
394 | | - | |
| 538 | + | |
395 | 539 | | |
396 | | - | |
| 540 | + | |
397 | 541 | | |
398 | 542 | | |
399 | 543 | | |
| |||
408 | 552 | | |
409 | 553 | | |
410 | 554 | | |
411 | | - | |
| 555 | + | |
412 | 556 | | |
413 | 557 | | |
414 | 558 | | |
| |||
426 | 570 | | |
427 | 571 | | |
428 | 572 | | |
429 | | - | |
| 573 | + | |
430 | 574 | | |
431 | 575 | | |
432 | 576 | | |
433 | 577 | | |
434 | 578 | | |
435 | | - | |
436 | | - | |
| 579 | + | |
| 580 | + | |
437 | 581 | | |
438 | 582 | | |
439 | 583 | | |
| |||
470 | 614 | | |
471 | 615 | | |
472 | 616 | | |
473 | | - | |
474 | | - | |
475 | | - | |
476 | | - | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
477 | 621 | | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
478 | 625 | | |
479 | 626 | | |
480 | 627 | | |
| |||
0 commit comments