File indexing completed on 2024-04-06 12:15:40
0001 #ifndef HeterogeneousCore_AlpakaInterface_interface_VecArray_h
0002 #define HeterogeneousCore_AlpakaInterface_interface_VecArray_h
0003
0004
0005
0006
0007
0008 #include <utility>
0009
0010 #include <alpaka/alpaka.hpp>
0011
0012 namespace cms::alpakatools {
0013
0014 template <class T, int maxSize>
0015 class VecArray {
0016 public:
0017 using self = VecArray<T, maxSize>;
0018 using value_t = T;
0019
0020 inline constexpr int push_back_unsafe(const T &element) {
0021 auto previousSize = m_size;
0022 m_size++;
0023 if (previousSize < maxSize) {
0024 m_data[previousSize] = element;
0025 return previousSize;
0026 } else {
0027 --m_size;
0028 return -1;
0029 }
0030 }
0031
0032 template <class... Ts>
0033 constexpr int emplace_back_unsafe(Ts &&...args) {
0034 auto previousSize = m_size;
0035 m_size++;
0036 if (previousSize < maxSize) {
0037 (new (&m_data[previousSize]) T(std::forward<Ts>(args)...));
0038 return previousSize;
0039 } else {
0040 --m_size;
0041 return -1;
0042 }
0043 }
0044
0045 inline constexpr T const &back() const {
0046 if (m_size > 0) {
0047 return m_data[m_size - 1];
0048 } else
0049 return T();
0050 }
0051
0052 inline constexpr T &back() {
0053 if (m_size > 0) {
0054 return m_data[m_size - 1];
0055 } else
0056 return T();
0057 }
0058
0059
0060 template <typename TAcc>
0061 ALPAKA_FN_ACC int push_back(const TAcc &acc, const T &element) {
0062 auto previousSize = alpaka::atomicAdd(acc, &m_size, 1, alpaka::hierarchy::Blocks{});
0063 if (previousSize < maxSize) {
0064 m_data[previousSize] = element;
0065 return previousSize;
0066 } else {
0067 alpaka::atomicSub(acc, &m_size, 1, alpaka::hierarchy::Blocks{});
0068 return -1;
0069 }
0070 }
0071
0072 template <typename TAcc, class... Ts>
0073 ALPAKA_FN_ACC int emplace_back(const TAcc &acc, Ts &&...args) {
0074 auto previousSize = alpaka::atomicAdd(acc, &m_size, 1, alpaka::hierarchy::Blocks{});
0075 if (previousSize < maxSize) {
0076 (new (&m_data[previousSize]) T(std::forward<Ts>(args)...));
0077 return previousSize;
0078 } else {
0079 alpaka::atomicSub(acc, &m_size, 1, alpaka::hierarchy::Blocks{});
0080 return -1;
0081 }
0082 }
0083
0084 inline constexpr T pop_back() {
0085 if (m_size > 0) {
0086 auto previousSize = m_size--;
0087 return m_data[previousSize - 1];
0088 } else
0089 return T();
0090 }
0091
0092 inline constexpr T const *begin() const { return m_data; }
0093 inline constexpr T const *end() const { return m_data + m_size; }
0094 inline constexpr T *begin() { return m_data; }
0095 inline constexpr T *end() { return m_data + m_size; }
0096 inline constexpr int size() const { return m_size; }
0097 inline constexpr T &operator[](int i) { return m_data[i]; }
0098 inline constexpr const T &operator[](int i) const { return m_data[i]; }
0099 inline constexpr void reset() { m_size = 0; }
0100 inline static constexpr int capacity() { return maxSize; }
0101 inline constexpr T const *data() const { return m_data; }
0102 inline constexpr void resize(int size) { m_size = size; }
0103 inline constexpr bool empty() const { return 0 == m_size; }
0104 inline constexpr bool full() const { return maxSize == m_size; }
0105
0106 private:
0107 T m_data[maxSize];
0108
0109 int m_size;
0110 };
0111
0112 }
0113
0114 #endif