Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-15 09:27:31

0001 /// \file AOS3D.h
0002 /// \author Johannes de Fine Licht (johannes.definelicht@cern.ch)
0003 
0004 #ifndef VECGEOM_BASE_AOS3D_H_
0005 #define VECGEOM_BASE_AOS3D_H_
0006 
0007 #include "VecGeom/base/Cuda.h"
0008 #include "VecGeom/base/Global.h"
0009 
0010 #include "VecGeom/base/Container3D.h"
0011 #include "VecGeom/base/Vector3D.h"
0012 #include "VecGeom/backend/scalar/Backend.h"
0013 #ifdef VECGEOM_CUDA_INTERFACE
0014 #include "VecGeom/backend/cuda/Interface.h"
0015 #endif
0016 
0017 namespace vecgeom {
0018 
0019 VECGEOM_DEVICE_FORWARD_DECLARE(template <typename Type> class AOS3D;);
0020 
0021 inline namespace VECGEOM_IMPL_NAMESPACE {
0022 
0023 template <typename T>
0024 class AOS3D : Container3D<AOS3D<T>> {
0025 
0026 private:
0027   bool fAllocated{false};
0028   size_t fSize{0};
0029   size_t fCapacity{0};
0030   Vector3D<T> *fContent{nullptr};
0031 
0032   typedef Vector3D<T> Vec_t;
0033 
0034 public:
0035   typedef T value_type;
0036 
0037   VECCORE_ATT_HOST_DEVICE
0038   AOS3D(Vector3D<T> *data, size_t size);
0039 
0040   VECCORE_ATT_HOST_DEVICE
0041   AOS3D(size_t size);
0042 
0043   /// @brief Construct aligned data using specialized allocator.
0044   /// @details To allocate the full object, call this constructor with placement new.
0045   /// @param size Size of the internal arrays
0046   /// @param a Aligned allocator, pre-initialized to fit the content
0047   VECCORE_ATT_HOST_DEVICE
0048   AOS3D(size_t size, AlignedAllocator &a);
0049 
0050   AOS3D() = default;
0051 
0052   AOS3D(AOS3D<T> const &other);
0053 
0054   VECCORE_ATT_HOST_DEVICE
0055   AOS3D &operator=(AOS3D<T> const &other);
0056 
0057   VECCORE_ATT_HOST_DEVICE
0058   ~AOS3D();
0059 
0060   VECCORE_ATT_HOST_DEVICE
0061   VECGEOM_FORCE_INLINE
0062   size_t size() const;
0063 
0064   /// @brief Compute size of a buffer to hold the aligned vector content
0065   /// @param initSize Number of elements
0066   /// @return Size to allocate
0067   VECCORE_ATT_HOST_DEVICE
0068   VECGEOM_FORCE_INLINE
0069   static size_t aligned_sizeof_data(size_t initSize);
0070 
0071   VECCORE_ATT_HOST_DEVICE
0072   VECGEOM_FORCE_INLINE
0073   size_t capacity() const;
0074 
0075   VECGEOM_FORCE_INLINE
0076   void resize(size_t newSize);
0077 
0078   VECCORE_ATT_HOST_DEVICE
0079   VECGEOM_FORCE_INLINE
0080   void reserve(size_t newCapacity);
0081 
0082   VECGEOM_FORCE_INLINE
0083   void clear();
0084 
0085   // Element access methods. Can be used to manipulate content.
0086 
0087   VECCORE_ATT_HOST_DEVICE
0088   VECGEOM_FORCE_INLINE
0089   Vector3D<T> operator[](size_t index) const;
0090 
0091   VECCORE_ATT_HOST_DEVICE
0092   VECGEOM_FORCE_INLINE
0093   Vector3D<T> &operator[](size_t index);
0094 
0095   VECCORE_ATT_HOST_DEVICE
0096   VECGEOM_FORCE_INLINE
0097   Vector3D<T> *content();
0098 
0099   VECCORE_ATT_HOST_DEVICE
0100   VECGEOM_FORCE_INLINE
0101   Vector3D<T> const *content() const;
0102 
0103   VECCORE_ATT_HOST_DEVICE
0104   VECGEOM_FORCE_INLINE
0105   T x(size_t index) const;
0106 
0107   VECCORE_ATT_HOST_DEVICE
0108   VECGEOM_FORCE_INLINE
0109   T &x(size_t index);
0110 
0111   VECCORE_ATT_HOST_DEVICE
0112   VECGEOM_FORCE_INLINE
0113   T y(size_t index) const;
0114 
0115   VECCORE_ATT_HOST_DEVICE
0116   VECGEOM_FORCE_INLINE
0117   T &y(size_t index);
0118 
0119   VECCORE_ATT_HOST_DEVICE
0120   VECGEOM_FORCE_INLINE
0121   T z(size_t index) const;
0122 
0123   VECCORE_ATT_HOST_DEVICE
0124   VECGEOM_FORCE_INLINE
0125   T &z(size_t index);
0126 
0127   VECCORE_ATT_HOST_DEVICE
0128   VECGEOM_FORCE_INLINE
0129   void set(size_t index, T x, T y, T z);
0130 
0131   VECCORE_ATT_HOST_DEVICE
0132   VECGEOM_FORCE_INLINE
0133   void set(size_t index, Vector3D<T> const &vec);
0134 
0135   VECCORE_ATT_HOST_DEVICE
0136   VECGEOM_FORCE_INLINE
0137   void push_back(T x, T y, T z);
0138 
0139   VECCORE_ATT_HOST_DEVICE
0140   VECGEOM_FORCE_INLINE
0141   void push_back(Vector3D<T> const &vec);
0142 
0143 #ifdef VECGEOM_CUDA_INTERFACE
0144   DevicePtr<cuda::AOS3D<T>> CopyToGpu(DevicePtr<cuda::Vector3D<T>> contentGpu) const;
0145 #endif
0146 
0147 private:
0148   VECCORE_ATT_HOST_DEVICE
0149   void Deallocate();
0150 };
0151 
0152 template <typename T>
0153 VECCORE_ATT_HOST_DEVICE AOS3D<T>::AOS3D(Vector3D<T> *in_content, size_t in_size)
0154     : fSize(in_size), fCapacity(fSize), fContent(in_content)
0155 {
0156 }
0157 
0158 template <typename T>
0159 VECCORE_ATT_HOST_DEVICE AOS3D<T>::AOS3D(size_t sz, AlignedAllocator &a) : fSize(sz), fCapacity(sz)
0160 {
0161   fContent = a.aligned_alloc<Vector3D<T>>(sz, kAlignmentBoundary);
0162 }
0163 
0164 template <typename T>
0165 VECCORE_ATT_HOST_DEVICE AOS3D<T>::AOS3D(size_t sz) : fSize(sz), fCapacity(sz)
0166 {
0167   if (fCapacity > 0) reserve(fCapacity);
0168 }
0169 
0170 template <typename T>
0171 AOS3D<T>::AOS3D(AOS3D<T> const &rhs) : fSize(rhs.fSize), fCapacity(rhs.fCapacity)
0172 {
0173   *this = rhs;
0174 }
0175 
0176 template <typename T>
0177 VECCORE_ATT_HOST_DEVICE AOS3D<T> &AOS3D<T>::operator=(AOS3D<T> const &rhs)
0178 {
0179 #ifndef VECCORE_CUDA_DEVICE_COMPILATION
0180   clear();
0181   if (rhs.fAllocated) {
0182     reserve(rhs.fCapacity);
0183     copy(rhs.fContent, rhs.fContent + rhs.fSize, fContent);
0184   } else {
0185     fContent   = rhs.fContent;
0186     fAllocated = false;
0187     fCapacity  = rhs.fCapacity;
0188   }
0189   fSize = rhs.fSize;
0190 #else
0191   fAllocated = false;
0192   fSize      = rhs.fSize;
0193   fCapacity  = rhs.fCapacity;
0194   fContent   = rhs.fContent;
0195 #endif
0196   return *this;
0197 }
0198 
0199 template <typename T>
0200 VECCORE_ATT_HOST_DEVICE AOS3D<T>::~AOS3D()
0201 {
0202   Deallocate();
0203 }
0204 
0205 template <typename T>
0206 VECCORE_ATT_HOST_DEVICE size_t AOS3D<T>::size() const
0207 {
0208   return fSize;
0209 }
0210 
0211 template <typename T>
0212 VECCORE_ATT_HOST_DEVICE VECGEOM_FORCE_INLINE size_t AOS3D<T>::aligned_sizeof_data(size_t initSize)
0213 {
0214   return initSize * sizeof(Vector3D<T>) + kAlignmentBoundary;
0215 }
0216 
0217 template <typename T>
0218 VECCORE_ATT_HOST_DEVICE size_t AOS3D<T>::capacity() const
0219 {
0220   return fCapacity;
0221 }
0222 
0223 template <typename T>
0224 void AOS3D<T>::resize(size_t newSize)
0225 {
0226   VECGEOM_ASSERT(newSize <= fCapacity);
0227   fSize = newSize;
0228 }
0229 
0230 template <typename T>
0231 VECCORE_ATT_HOST_DEVICE void AOS3D<T>::reserve(size_t newCapacity)
0232 {
0233   fCapacity         = newCapacity;
0234   Vec_t *contentNew = fCapacity > 0 ? AlignedAllocate<Vec_t>(fCapacity) : nullptr;
0235   fSize             = (fSize > fCapacity) ? fCapacity : fSize;
0236   if (fContent && fSize > 0) {
0237     copy(fContent, fContent + fSize, contentNew);
0238   }
0239   Deallocate();
0240   fContent   = contentNew;
0241   fAllocated = fContent != nullptr;
0242 }
0243 
0244 template <typename T>
0245 void AOS3D<T>::clear()
0246 {
0247   Deallocate();
0248   fContent   = nullptr;
0249   fAllocated = false;
0250   fSize      = 0;
0251   fCapacity  = 0;
0252 }
0253 
0254 template <typename T>
0255 VECCORE_ATT_HOST_DEVICE void AOS3D<T>::Deallocate()
0256 {
0257   if (fAllocated) {
0258     AlignedFree(fContent);
0259   }
0260 }
0261 
0262 template <typename T>
0263 VECCORE_ATT_HOST_DEVICE Vector3D<T> AOS3D<T>::operator[](size_t index) const
0264 {
0265   return fContent[index];
0266 }
0267 
0268 template <typename T>
0269 VECCORE_ATT_HOST_DEVICE Vector3D<T> &AOS3D<T>::operator[](size_t index)
0270 {
0271   return fContent[index];
0272 }
0273 
0274 template <typename T>
0275 VECCORE_ATT_HOST_DEVICE Vector3D<T> *AOS3D<T>::content()
0276 {
0277   return fContent;
0278 }
0279 
0280 template <typename T>
0281 VECCORE_ATT_HOST_DEVICE Vector3D<T> const *AOS3D<T>::content() const
0282 {
0283   return fContent;
0284 }
0285 
0286 template <typename T>
0287 VECCORE_ATT_HOST_DEVICE T AOS3D<T>::x(size_t index) const
0288 {
0289   return (fContent[index])[0];
0290 }
0291 
0292 template <typename T>
0293 VECCORE_ATT_HOST_DEVICE T &AOS3D<T>::x(size_t index)
0294 {
0295   return (fContent[index])[0];
0296 }
0297 
0298 template <typename T>
0299 VECCORE_ATT_HOST_DEVICE T AOS3D<T>::y(size_t index) const
0300 {
0301   return (fContent[index])[1];
0302 }
0303 
0304 template <typename T>
0305 VECCORE_ATT_HOST_DEVICE T &AOS3D<T>::y(size_t index)
0306 {
0307   return (fContent[index])[1];
0308 }
0309 
0310 template <typename T>
0311 VECCORE_ATT_HOST_DEVICE T AOS3D<T>::z(size_t index) const
0312 {
0313   return (fContent[index])[2];
0314 }
0315 
0316 template <typename T>
0317 VECCORE_ATT_HOST_DEVICE T &AOS3D<T>::z(size_t index)
0318 {
0319   return (fContent[index])[2];
0320 }
0321 
0322 template <typename T>
0323 VECCORE_ATT_HOST_DEVICE void AOS3D<T>::set(size_t index, T in_x, T in_y, T in_z)
0324 {
0325   (fContent[index])[0] = in_x;
0326   (fContent[index])[1] = in_y;
0327   (fContent[index])[2] = in_z;
0328 }
0329 
0330 template <typename T>
0331 VECCORE_ATT_HOST_DEVICE void AOS3D<T>::set(size_t index, Vector3D<T> const &vec)
0332 {
0333   fContent[index] = vec;
0334 }
0335 
0336 template <typename T>
0337 VECCORE_ATT_HOST_DEVICE void AOS3D<T>::push_back(T in_x, T in_y, T in_z)
0338 {
0339   (fContent[fSize])[0] = in_x;
0340   (fContent[fSize])[1] = in_y;
0341   (fContent[fSize])[2] = in_z;
0342   ++fSize;
0343 }
0344 
0345 template <typename T>
0346 VECCORE_ATT_HOST_DEVICE void AOS3D<T>::push_back(Vector3D<T> const &vec)
0347 {
0348   fContent[fSize] = vec;
0349   ++fSize;
0350 }
0351 
0352 #ifdef VECGEOM_CUDA_INTERFACE
0353 
0354 template <typename T>
0355 DevicePtr<cuda::AOS3D<T>> AOS3D<T>::CopyToGpu(DevicePtr<cuda::Vector3D<T>> contentGpu) const
0356 {
0357   contentGpu.ToDevice(fContent, fSize);
0358 
0359   DevicePtr<cuda::AOS3D<T>> gpu_ptr;
0360   gpu_ptr.Allocate();
0361   gpu_ptr.Construct(contentGpu, fSize);
0362 }
0363 
0364 #endif // VECGEOM_CUDA_INTERFACE
0365 } // namespace VECGEOM_IMPL_NAMESPACE
0366 } // End namespace vecgeom
0367 
0368 #endif // VECGEOM_BASE_AOS3D_H_