Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-22 09:10:00

0001 /// \file Array.h
0002 /// \author Johannes de Fine Licht (johannes.definelicht@cern.ch)
0003 
0004 #ifndef VECGEOM_BASE_ARRAY_H_
0005 #define VECGEOM_BASE_ARRAY_H_
0006 
0007 #include "VecGeom/base/Global.h"
0008 
0009 #include "VecGeom/base/AlignedBase.h"
0010 #include "VecGeom/backend/scalar/Backend.h"
0011 
0012 namespace vecgeom {
0013 inline namespace VECGEOM_IMPL_NAMESPACE {
0014 
0015 template <typename Type>
0016 class Array : public AlignedBase {
0017 
0018 private:
0019   Type *fData        = nullptr;
0020   unsigned int fSize = 0;
0021   bool fAllocated    = false;
0022 
0023 public:
0024   Array() = default;
0025 
0026   VECCORE_ATT_HOST_DEVICE
0027   VECGEOM_FORCE_INLINE
0028   Array(const unsigned int size);
0029 
0030   VECCORE_ATT_HOST_DEVICE
0031   VECGEOM_FORCE_INLINE
0032   Array(const unsigned int size, AlignedAllocator &a);
0033 
0034   VECGEOM_FORCE_INLINE
0035   Array(Array<Type> const &other);
0036 
0037   VECCORE_ATT_HOST_DEVICE
0038   VECGEOM_FORCE_INLINE
0039   Array(Type *data, unsigned int size);
0040 
0041   VECCORE_ATT_HOST_DEVICE
0042   VECGEOM_FORCE_INLINE
0043   ~Array();
0044 
0045   VECCORE_ATT_HOST_DEVICE
0046   VECGEOM_FORCE_INLINE
0047   Array &operator=(Array<Type> const &other);
0048 
0049   VECCORE_ATT_HOST_DEVICE
0050   VECGEOM_FORCE_INLINE
0051   Type &operator[](const int index) { return fData[index]; }
0052 
0053   VECCORE_ATT_HOST_DEVICE
0054   VECGEOM_FORCE_INLINE
0055   Type const &operator[](const int index) const { return fData[index]; }
0056 
0057   VECCORE_ATT_HOST_DEVICE
0058   VECGEOM_FORCE_INLINE
0059   int size() const { return fSize; }
0060 
0061   template <typename... Args>
0062   VECCORE_ATT_HOST_DEVICE VECGEOM_FORCE_INLINE static size_t aligned_sizeof_data(const size_t initSize,
0063                                                                                  const Args... args);
0064 
0065   VECCORE_ATT_HOST_DEVICE
0066   VECGEOM_FORCE_INLINE
0067   void Allocate(const unsigned int size);
0068 
0069   VECCORE_ATT_HOST_DEVICE
0070   VECGEOM_FORCE_INLINE
0071   void Deallocate();
0072 
0073   typedef Type *iterator;
0074   typedef Type const *const_iterator;
0075 
0076   VECCORE_ATT_HOST_DEVICE
0077   VECGEOM_FORCE_INLINE
0078   Type *begin() { return &fData[0]; }
0079 
0080   VECCORE_ATT_HOST_DEVICE
0081   VECGEOM_FORCE_INLINE
0082   Type *end() { return &fData[fSize]; }
0083 
0084   VECCORE_ATT_HOST_DEVICE
0085   VECGEOM_FORCE_INLINE
0086   Type const *cbegin() const { return &fData[0]; }
0087 
0088   VECCORE_ATT_HOST_DEVICE
0089   VECGEOM_FORCE_INLINE
0090   Type const *cend() const { return &fData[fSize]; }
0091 };
0092 
0093 template <typename Type>
0094 VECCORE_ATT_HOST_DEVICE Array<Type>::Array(const unsigned int initSize)
0095 {
0096   Allocate(initSize);
0097 }
0098 
0099 template <typename Type>
0100 VECCORE_ATT_HOST_DEVICE Array<Type>::Array(const unsigned int initSize, AlignedAllocator &a)
0101     : fSize(initSize), fAllocated(false)
0102 {
0103   fData = a.aligned_alloc<Type>(initSize, kAlignmentBoundary);
0104 }
0105 
0106 template <typename Type>
0107 Array<Type>::Array(Array<Type> const &other)
0108 {
0109   Allocate(other.fSize);
0110   copy(other.fData, other.fData + other.fSize, fData);
0111 }
0112 
0113 template <typename Type>
0114 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE Array<Type>::Array(Type *data, unsigned int initSize)
0115     : fData(data), fSize(initSize), fAllocated(false)
0116 {
0117 }
0118 
0119 template <typename Type>
0120 VECCORE_ATT_HOST_DEVICE Array<Type>::~Array()
0121 {
0122 #ifndef VECCORE_CUDA_DEVICE_COMPILATION
0123   if (fAllocated) vecCore::AlignedFree(fData);
0124 #endif
0125 }
0126 
0127 template <class Type>
0128 template <class... Args>
0129 VECCORE_ATT_HOST_DEVICE size_t Array<Type>::aligned_sizeof_data(const size_t numElements, const Args... args)
0130 {
0131   return (AlignedAllocator::aligned_sizeof<Type>(numElements, kAlignmentBoundary, args...));
0132 }
0133 
0134 template <typename Type>
0135 VECCORE_ATT_HOST_DEVICE void Array<Type>::Allocate(const unsigned int initSize)
0136 {
0137   Deallocate();
0138   fSize = initSize;
0139   if (initSize == 0) return;
0140 
0141 #ifndef VECCORE_CUDA
0142   fData = static_cast<Type *>(vecCore::AlignedAlloc(kAlignmentBoundary, fSize * sizeof(Type)));
0143 #else
0144   fData = static_cast<Type *>(malloc(fSize * sizeof(Type))); // new Type[fSize];
0145 #endif
0146   fAllocated = true;
0147 }
0148 
0149 template <typename Type>
0150 VECCORE_ATT_HOST_DEVICE void Array<Type>::Deallocate()
0151 {
0152   if (fAllocated) {
0153 #ifndef VECCORE_CUDA
0154     vecCore::AlignedFree(fData);
0155 #else
0156     free(fData);
0157 #endif
0158   }
0159   fData      = nullptr;
0160   fSize      = 0;
0161   fAllocated = false;
0162 }
0163 
0164 template <typename Type>
0165 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE Array<Type> &Array<Type>::operator=(Array<Type> const &other)
0166 {
0167 #ifndef VECCORE_CUDA_DEVICE_COMPILATION
0168   Deallocate();
0169   Allocate(other.fSize);
0170   copy(other.fData, other.fData + other.fSize, fData);
0171 #else
0172   fData      = other.fData;
0173   fSize      = other.fSize;
0174   fAllocated = false;
0175 #endif
0176   return *this;
0177 }
0178 } // namespace VECGEOM_IMPL_NAMESPACE
0179 } // namespace vecgeom
0180 
0181 #endif // VECGEOM_BASE_ARRAY_H_