Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:26:00

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