Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TGeoParallelWorld improvements #23

Merged
merged 5 commits into from
Sep 20, 2024

Conversation

sawenzel
Copy link
Collaborator

cherry-picking some recent TGeo improvements for immediate use in ALICE.

relevant PRs are here

root-project#16442
root-project#16470
root-project#16425

This should allow to speed up the safety calculation
in the parallel world (by pruning away uncessary safety calls
which are anyway beyond the limit).
Optimization of check if 2 geometry paths are the same, through
(a) simple comparison of the current level
(b) start node comparison at the leafs instead the root,
    because this will enhance early returns
Bug fix in Safety calculation:

Actually call ::Safety on the TGeoShape
**not** on the TGeoNode. Otherwise the coordinate transformation is done
twice which leads to wrong safety values.

Consistency fix in ::FindNextBoundary:

Apply the `IsMatchinsState` check also in the case of non-voxelised
navigation to be consistent with the voxelized treatment
This commit provides an (optional) optimization to reduce
the cost of safety evaluations associated with TGeoParallelWorld.

The approach consists in the introduction of additional cache-state
in TGeoNavigator. This state remembers the last location for which
TGeoParallelWorld::Safety was evaluated, together with the corresponding
value returned.

Since TGeoNavigator follows the evolution of tracks step-by-step,
we have some sort of history-locality. This means that the cache-state
has a high-probability of being relevant for multiple track locations in
a row.

The optimization proposed here is orthogonal to other improvements.
It adds on top of algorithmic improvements within TGeoParallelWorld
(bounding volume hierarchies, see separate PR), gaining a few extra percent
in speed.

By default, the new feature is not enabled by default for backward compatibility.
Once fully verified, we can make it the default mode.

That said, in ALICE simulations, the caching did not modify stepping
behaviour and yielded identical results compared to not doing the caching.
…utines

This commit provides a rewrite of key functions of TGeoParallelWorld, achieving:
(a) faster initialization
(b) faster execution of the Safety function (from ~O(N) to ~O(1))
(c) similar execution of FindNode/FindBoundary functions (~log(N))
(d) less memory consumption (better memory scalability)

The development for this commit was motivated from a use case in ALICE,
in which the parallel world "scene" can be very large (~100K volumes).
In this case, TGeoVoxelFinder takes very long
to construct and consumes a very large amount of memory (GBs). In addition,
the evaluation of the Safety function dominates the Geant simulation time.

The improvements in this commit are mainly achieved through:

* The use of a boundary volume hierarchy (BVH) as the base
acceleration entity, replacing TGeoVoxelFinder.
BVH are the standard in industry/computer-graphics, for what
concerns ray-object intersection tasks. The BVH is constructed
from axis-aligned bounding boxes and employed in the FindBoundary/FindNode
implementations

* The use of a 3D voxel grid (TGeoVoxelGrid) structure, able
to store properties "local" or in the vicinity of a cartesian coordinate P.
This structure allows to reduce the (typical) algorithmic complexity for "Safety" queries
to ~O(1) (with a constant factor determined by the voxel size). Filling of the 3D voxel grid cache
for Safety is done on-the-fly (using the BVH once).

Ideas for these improvements come from prior work in related libraries such as VecGeom.

-----

Implementation details:

* The implementation is, for now (until fully tested), provided in a backward compatible manner:

  - By default, nothing changes
  - Users have to activate the BVH mode via TGeoParallelWorld::SetAccelerationMode
  - Users may hence compare the 2 modes or choose the best depending on complexity and needs

* Functions for Safety, FindNode, FindBoundary dispatch to some internal implementation.
  This causes an extra lookup/jump, which can be removed once BVH is fully validated

* For the BVH, a well known open source implementation is included in header-only form.
  The headers are copied from https://github.com/madmann91/bvh commit 66e445b92f68801a6dd8ef943fe3038976ecb4ff.
  Some minor patching has been done in order to achieve compilation for C++17 (see README).

* A new class, TGeoVoxelGrid is provided for the cartesian VoxelGrid container.

----

Performance examples:

In a test with the ALICE simulation framework including the ITS + TPC detectors
with 48240 volumes on the parallel world, we see

* initialization time goes from TGeoVoxelFinder: 10s ---> BVH: 40ms
* Geant simulation time: 10s --> 2s
* memory usage: 3GB --> 1GB

Hence, this PR will make a big difference for the ALICE simulation program.
It was verified, that identical results (number of hits, steps, etc) are obtained
when going from TGeoVoxelFinder --> BVH+GRID.

----

Outlook:

Similar techniques could be applied to ordinary TGeoNavigator routines.
@sawenzel sawenzel merged commit 92d6aca into alisw:alice/v6-32-04 Sep 20, 2024
1 of 13 checks passed
@sawenzel sawenzel deleted the alice-v6-32-04 branch September 20, 2024 06:51
@sawenzel sawenzel restored the alice-v6-32-04 branch September 20, 2024 07:03
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.

1 participant