Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-10 09:29:09

0001 /// \file BVHManager.h
0002 /// \author Guilherme Amadio
0003 
0004 #ifndef VECGEOM_MANAGEMENT_BVHMANAGER_H_
0005 #define VECGEOM_MANAGEMENT_BVHMANAGER_H_
0006 
0007 #include "VecGeom/base/Config.h"
0008 #include "VecGeom/base/Cuda.h"
0009 #include "VecGeom/base/BVH.h"
0010 #include "VecGeom/volumes/LogicalVolume.h"
0011 
0012 #ifdef VECGEOM_ENABLE_CUDA
0013 #include "VecGeom/backend/cuda/Interface.h"
0014 #endif
0015 
0016 namespace vecgeom {
0017 inline namespace VECGEOM_IMPL_NAMESPACE {
0018 template <typename Real_t>
0019 inline std::vector<BVH<Real_t> *> hBVH;
0020 #ifdef VECGEOM_ENABLE_CUDA
0021 template <typename Real_t>
0022 inline __device__ BVH<Real_t> *dBVH;
0023 #endif
0024 
0025 // Macro allowing downstream codes to use GetDeviceBVH
0026 #define VECGEOM_BVHMANAGER_DEVICE
0027 
0028 /**
0029  * @brief The @c BVHManager class is a singleton class to manage the association between
0030  * logical volumes and their bounding volume hierarchies, using the logical volumes' ids.
0031  */
0032 class BVHManager {
0033 
0034 // to avoid changing the user code via templates, the precision is chosen at compile-time in the BVHManager and
0035 // BVHNavigator
0036 #ifdef VECGEOM_BVH_SINGLE
0037   using Real_t = float;
0038 #else
0039   using Real_t = double;
0040 #endif
0041 
0042 public:
0043   BVHManager() = delete;
0044 
0045   /**
0046    * Initializes the bounding volume hierarchies for all logical volumes in the geometry.
0047    * Since it uses the ABBoxManager to fetch the pre-computed bounding boxes for each logical volume,
0048    * it must be called after the bounding boxes have already been computed. The depth is not specified,
0049    * to allow the BVH class to choose the depth dynamically based on the number of children of each
0050    * logical volume. This function is called automatically when the geometry is closed.
0051    *
0052    * The BVHManager assumes all volumes have an associated BVH, but only BVHs for volumes whose
0053    * navigator is set to the BVHNavigatorV are actually accessed at runtime.
0054    */
0055   static void Init();
0056 
0057   /** Initializes bounding volume hierarchies on the GPU. */
0058   static cuda::BVH<Real_t> const *DeviceInit();
0059 
0060   /** Access the device BVH pointer if CUDA is enabled. **/
0061   static cuda::BVH<Real_t> const *GetDeviceBVH();
0062 
0063   VECCORE_ATT_HOST_DEVICE
0064   static BVH<Real_t> const *GetBVH(int id)
0065   {
0066 #ifdef VECCORE_CUDA_DEVICE_COMPILATION
0067     return &cuda::dBVH<Real_t>[id];
0068 #else
0069     return hBVH<Real_t>[id];
0070 #endif
0071   }
0072 
0073   VECCORE_ATT_HOST_DEVICE
0074   static BVH<Real_t> const *GetBVH(LogicalVolume const *v) { return GetBVH(v->id()); }
0075 };
0076 
0077 } // namespace VECGEOM_IMPL_NAMESPACE
0078 } // namespace vecgeom
0079 
0080 #endif