File indexing completed on 2025-01-18 10:13:51
0001
0002
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 VECGEOM_FORCE_INLINE
0031 Array(Array<Type> const &other);
0032
0033 VECCORE_ATT_HOST_DEVICE
0034 VECGEOM_FORCE_INLINE
0035 Array(Type *data, unsigned int size);
0036
0037 VECCORE_ATT_HOST_DEVICE
0038 VECGEOM_FORCE_INLINE
0039 ~Array();
0040
0041 VECCORE_ATT_HOST_DEVICE
0042 VECGEOM_FORCE_INLINE
0043 Array &operator=(Array<Type> const &other);
0044
0045 VECCORE_ATT_HOST_DEVICE
0046 VECGEOM_FORCE_INLINE
0047 Type &operator[](const int index) { return fData[index]; }
0048
0049 VECCORE_ATT_HOST_DEVICE
0050 VECGEOM_FORCE_INLINE
0051 Type const &operator[](const int index) const { return fData[index]; }
0052
0053 VECCORE_ATT_HOST_DEVICE
0054 VECGEOM_FORCE_INLINE
0055 int size() const { return fSize; }
0056
0057 VECGEOM_FORCE_INLINE
0058 VECCORE_ATT_HOST_DEVICE
0059 void Allocate(const unsigned int size);
0060
0061 VECGEOM_FORCE_INLINE
0062 VECCORE_ATT_HOST_DEVICE
0063 void Deallocate();
0064
0065 typedef Type *iterator;
0066 typedef Type const *const_iterator;
0067
0068 VECCORE_ATT_HOST_DEVICE
0069 VECGEOM_FORCE_INLINE
0070 Type *begin() { return &fData[0]; }
0071
0072 VECCORE_ATT_HOST_DEVICE
0073 VECGEOM_FORCE_INLINE
0074 Type *end() { return &fData[fSize]; }
0075
0076 VECCORE_ATT_HOST_DEVICE
0077 VECGEOM_FORCE_INLINE
0078 Type const *cbegin() const { return &fData[0]; }
0079
0080 VECCORE_ATT_HOST_DEVICE
0081 VECGEOM_FORCE_INLINE
0082 Type const *cend() const { return &fData[fSize]; }
0083 };
0084
0085 template <typename Type>
0086 VECCORE_ATT_HOST_DEVICE
0087 Array<Type>::Array(const unsigned int initSize)
0088 {
0089 Allocate(initSize);
0090 }
0091
0092 template <typename Type>
0093 Array<Type>::Array(Array<Type> const &other)
0094 {
0095 Allocate(other.fSize);
0096 copy(other.fData, other.fData + other.fSize, fData);
0097 }
0098
0099 template <typename Type>
0100 VECGEOM_FORCE_INLINE
0101 VECCORE_ATT_HOST_DEVICE
0102 Array<Type>::Array(Type *data, unsigned int initSize) : fData(data), fSize(initSize), fAllocated(false)
0103 {
0104 }
0105
0106 template <typename Type>
0107 VECCORE_ATT_HOST_DEVICE
0108 Array<Type>::~Array()
0109 {
0110 #ifndef VECCORE_CUDA_DEVICE_COMPILATION
0111 if (fAllocated) vecCore::AlignedFree(fData);
0112 #endif
0113 }
0114
0115 template <typename Type>
0116 VECCORE_ATT_HOST_DEVICE
0117 void Array<Type>::Allocate(const unsigned int initSize)
0118 {
0119 Deallocate();
0120 fSize = initSize;
0121 if (initSize == 0) return;
0122
0123 #ifndef VECCORE_CUDA
0124 fData = static_cast<Type *>(vecCore::AlignedAlloc(kAlignmentBoundary, fSize * sizeof(Type)));
0125 #else
0126 fData = static_cast<Type *>(malloc(fSize * sizeof(Type)));
0127 #endif
0128 fAllocated = true;
0129 }
0130
0131 template <typename Type>
0132 VECCORE_ATT_HOST_DEVICE
0133 void Array<Type>::Deallocate()
0134 {
0135 if (fAllocated) {
0136 #ifndef VECCORE_CUDA
0137 vecCore::AlignedFree(fData);
0138 #else
0139 free(fData);
0140 #endif
0141 }
0142 fData = nullptr;
0143 fSize = 0;
0144 fAllocated = false;
0145 }
0146
0147 template <typename Type>
0148 VECGEOM_FORCE_INLINE
0149 VECCORE_ATT_HOST_DEVICE
0150 Array<Type> &Array<Type>::operator=(Array<Type> const &other)
0151 {
0152 #ifndef VECCORE_CUDA_DEVICE_COMPILATION
0153 Deallocate();
0154 Allocate(other.fSize);
0155 copy(other.fData, other.fData + other.fSize, fData);
0156 #else
0157 fData = other.fData;
0158 fSize = other.fSize;
0159 fAllocated = false;
0160 #endif
0161 return *this;
0162 }
0163 }
0164 }
0165
0166 #endif