File indexing completed on 2025-01-18 10:13:56
0001
0002
0003
0004 #ifndef VECGEOM_MANAGEMENT_CUDAMANAGER_H_
0005 #define VECGEOM_MANAGEMENT_CUDAMANAGER_H_
0006
0007 #include "VecGeom/base/Cuda.h"
0008 #include "VecGeom/base/Global.h"
0009
0010 #include "VecGeom/base/Vector.h"
0011 #include "VecGeom/volumes/Box.h"
0012
0013 #ifdef VECGEOM_CUDA_INTERFACE
0014 #include "VecGeom/backend/cuda/Interface.h"
0015 #endif
0016
0017 #include <list>
0018 #include <map>
0019 #include <set>
0020
0021
0022 namespace vecgeom {
0023
0024 VECGEOM_DEVICE_FORWARD_DECLARE(class VPlacedVolume;);
0025 VECGEOM_DEVICE_FORWARD_DECLARE(void CudaManagerPrintGeometry(vecgeom::cuda::VPlacedVolume const *const world););
0026 VECGEOM_DEVICE_FORWARD_DECLARE(void InitDeviceCompactPlacedVolBufferPtr(void *););
0027
0028
0029
0030 namespace globaldevicegeomdata {
0031 inline VECCORE_ATT_DEVICE VPlacedVolume *gCompactPlacedVolBuffer = nullptr;
0032 inline VECCORE_ATT_DEVICE NavIndex_t *gNavIndex = nullptr;
0033 inline VECCORE_ATT_DEVICE int gMaxDepth = 0;
0034 }
0035
0036 #ifndef VECCORE_CUDA
0037 inline
0038 #endif
0039 namespace cxx {
0040
0041 #ifdef VECCORE_CUDA
0042
0043 class VUnplacedVolume;
0044 class VPlacedVolume;
0045 class LogicalVolume;
0046 class Transformation3D;
0047 template <typename Type>
0048 class Vector;
0049 #endif
0050
0051 class CudaManager {
0052
0053 private:
0054 bool synchronized_;
0055 int verbose_;
0056 int total_volumes_;
0057
0058 using Daughter_t = VPlacedVolume const *;
0059 using CudaDaughter_t = cuda::VPlacedVolume const *;
0060 using CudaDaughterPtr_t = DevicePtr<cuda::VPlacedVolume>;
0061
0062 std::set<VUnplacedVolume const *> unplaced_volumes_;
0063 std::set<LogicalVolume const *> logical_volumes_;
0064 std::set<VPlacedVolume const *> placed_volumes_;
0065 std::set<Transformation3D const *> transformations_;
0066 std::set<Vector<Daughter_t> *> daughters_;
0067
0068 typedef void const *CpuAddress;
0069 typedef DevicePtr<char> GpuAddress;
0070 typedef std::map<const CpuAddress, GpuAddress> MemoryMap;
0071 typedef std::map<GpuAddress, CpuAddress> PlacedVolumeMemoryMap;
0072 typedef std::map<GpuAddress, GpuAddress> GpuMemoryMap;
0073
0074 VPlacedVolume const *world_;
0075 DevicePtr<vecgeom::cuda::VPlacedVolume> world_gpu_;
0076 DevicePtr<vecgeom::cuda::VPlacedVolume> fPlacedVolumeBufferOnDevice;
0077 DevicePtr<NavIndex_t> fNavTableOnDevice;
0078
0079 private:
0080
0081
0082
0083
0084
0085
0086
0087
0088 MemoryMap memory_map_;
0089 GpuMemoryMap gpu_memory_map_;
0090
0091
0092
0093
0094 PlacedVolumeMemoryMap fGPUtoCPUmapForPlacedVolumes_;
0095
0096 std::list<GpuAddress> allocated_memory_;
0097
0098 public:
0099
0100
0101
0102 static CudaManager &Instance()
0103 {
0104 static CudaManager instance;
0105 return instance;
0106 }
0107
0108 VPlacedVolume const *world() const;
0109
0110 vecgeom::cuda::VPlacedVolume const *world_gpu() const;
0111
0112
0113
0114
0115 void LoadGeometry(VPlacedVolume const *const volume);
0116
0117 void LoadGeometry();
0118
0119
0120
0121
0122
0123
0124 DevicePtr<const vecgeom::cuda::VPlacedVolume> Synchronize();
0125
0126
0127
0128
0129 void CleanGpu();
0130
0131
0132
0133
0134 void Clear();
0135
0136
0137
0138
0139
0140 void PrintGeometry() const;
0141
0142
0143
0144
0145
0146
0147
0148 void set_verbose(const int verbose) { verbose_ = verbose; }
0149
0150 template <typename Type>
0151 GpuAddress Lookup(Type const *const key) const;
0152
0153 template <typename Type>
0154 GpuAddress Lookup(DevicePtr<Type> key) const;
0155
0156 DevicePtr<cuda::VUnplacedVolume> LookupUnplaced(VUnplacedVolume const *const host_ptr) const;
0157
0158 DevicePtr<cuda::LogicalVolume> LookupLogical(LogicalVolume const *const host_ptr) const;
0159
0160 DevicePtr<cuda::VPlacedVolume> LookupPlaced(VPlacedVolume const *const host_ptr) const;
0161 VPlacedVolume const *LookupPlacedCPUPtr(const void *address);
0162
0163 DevicePtr<cuda::Transformation3D> LookupTransformation(Transformation3D const *const host_ptr) const;
0164
0165 DevicePtr<cuda::Vector<CudaDaughter_t>> LookupDaughters(Vector<Daughter_t> *const host_ptr) const;
0166
0167 DevicePtr<CudaDaughter_t> LookupDaughterArray(Vector<Daughter_t> *const host_ptr) const;
0168
0169 private:
0170 CudaManager();
0171 CudaManager(CudaManager const &);
0172 CudaManager &operator=(CudaManager const &);
0173
0174
0175
0176
0177
0178 void ScanGeometry(VPlacedVolume const *const volume);
0179
0180
0181
0182
0183
0184 void AllocateGeometry();
0185
0186
0187
0188
0189
0190 template <typename Type>
0191 static CpuAddress ToCpuAddress(Type const *const ptr)
0192 {
0193 return static_cast<CpuAddress>(ptr);
0194 }
0195
0196
0197
0198
0199 template <typename Coll>
0200 bool AllocateCollectionOnCoproc(const char *verbose_title, const Coll &data, bool isplaced = false);
0201
0202
0203
0204
0205 bool AllocatePlacedVolumesOnCoproc();
0206
0207
0208 bool AllocateNavIndexOnCoproc();
0209
0210
0211
0212
0213
0214
0215 void CopyPlacedVolumes() const;
0216 };
0217
0218
0219
0220
0221
0222 inline VPlacedVolume const *CudaManager::LookupPlacedCPUPtr(const void *address)
0223 {
0224 const VPlacedVolume *cpu_ptr =
0225 (const VPlacedVolume *)fGPUtoCPUmapForPlacedVolumes_[GpuAddress(const_cast<void *>(address))];
0226 assert(cpu_ptr != NULL);
0227 return cpu_ptr;
0228 }
0229 }
0230 }
0231
0232 #endif