vecmem 1.14.0
Loading...
Searching...
No Matches
device_array.ipp
1/* VecMem project, part of the ACTS project (R&D line)
2 *
3 * (c) 2021-2024 CERN for the benefit of the ACTS project
4 *
5 * Mozilla Public License Version 2.0
6 */
7#pragma once
8
9// HIP include(s)
10#if defined(__HIP_DEVICE_COMPILE__)
11#include <hip/hip_runtime.h>
12#endif
13
14// System include(s).
15#include <cassert>
16
17namespace vecmem {
18
19template <typename T, std::size_t N>
20VECMEM_HOST_AND_DEVICE device_array<T, N>::device_array(
22 : m_ptr(data.ptr()) {
23
24 assert(data.size() >= N);
25}
26
27template <typename T, std::size_t N>
28template <typename OTHERTYPE,
29 std::enable_if_t<details::is_same_nc<T, OTHERTYPE>::value, bool> >
30VECMEM_HOST_AND_DEVICE device_array<T, N>::device_array(
32 : m_ptr(data.ptr()) {
33
34 assert(data.size() >= N);
35}
36
37template <typename T, std::size_t N>
38VECMEM_HOST_AND_DEVICE device_array<T, N>::device_array(
39 const device_array& parent)
40 : m_ptr(parent.m_ptr) {}
41
42template <typename T, std::size_t N>
44 const device_array& rhs) {
45
46 // Prevent self-assignment.
47 if (this == &rhs) {
48 return *this;
49 }
50
51 // Copy the other object's payload.
52 m_ptr = rhs.m_ptr;
53
54 // Return a reference to this object.
55 return *this;
56}
57
58template <typename T, std::size_t N>
59VECMEM_HOST_AND_DEVICE auto device_array<T, N>::at(size_type pos) -> reference {
60
61 // Check if the index is valid.
62 assert(pos < N);
63
64 // Return a reference to the vector element.
65 return m_ptr[pos];
66}
67
68template <typename T, std::size_t N>
69VECMEM_HOST_AND_DEVICE auto device_array<T, N>::at(size_type pos) const
71
72 // Check if the index is valid.
73 assert(pos < N);
74
75 // Return a reference to the vector element.
76 return m_ptr[pos];
77}
78
79template <typename T, std::size_t N>
80VECMEM_HOST_AND_DEVICE auto device_array<T, N>::operator[](size_type pos)
81 -> reference {
82
83 // Return a reference to the vector element.
84 return m_ptr[pos];
85}
86
87template <typename T, std::size_t N>
88VECMEM_HOST_AND_DEVICE auto device_array<T, N>::operator[](size_type pos) const
90
91 // Return a reference to the vector element.
92 return m_ptr[pos];
93}
94
95template <typename T, std::size_t N>
96VECMEM_HOST_AND_DEVICE auto device_array<T, N>::front() -> reference {
97
98 // Make sure that there is at least one element in the vector.
99 static_assert(N > 0, "Cannot return first element of empty array");
100
101 // Return a reference to the first element of the vector.
102 return m_ptr[0];
103}
104
105template <typename T, std::size_t N>
106VECMEM_HOST_AND_DEVICE auto device_array<T, N>::front() const
107 -> const_reference {
108
109 // Make sure that there is at least one element in the vector.
110 static_assert(N > 0, "Cannot return first element of empty array");
111
112 // Return a reference to the first element of the vector.
113 return m_ptr[0];
114}
115
116template <typename T, std::size_t N>
117VECMEM_HOST_AND_DEVICE auto device_array<T, N>::back() -> reference {
118
119 // Make sure that there is at least one element in the vector.
120 static_assert(N > 0, "Cannot return last element of empty array");
121
122 // Return a reference to the last element of the vector.
123 return m_ptr[N - 1];
124}
125
126template <typename T, std::size_t N>
127VECMEM_HOST_AND_DEVICE auto device_array<T, N>::back() const
128 -> const_reference {
129
130 // Make sure that there is at least one element in the vector.
131 static_assert(N > 0, "Cannot return last element of empty array");
132
133 // Return a reference to the last element of the vector.
134 return m_ptr[N - 1];
135}
136
137template <typename T, std::size_t N>
138VECMEM_HOST_AND_DEVICE auto device_array<T, N>::data() -> pointer {
139
140 return m_ptr;
141}
142
143template <typename T, std::size_t N>
144VECMEM_HOST_AND_DEVICE auto device_array<T, N>::data() const -> const_pointer {
145
146 return m_ptr;
147}
148
149template <typename T, std::size_t N>
150VECMEM_HOST_AND_DEVICE auto device_array<T, N>::begin() -> iterator {
151
152 return iterator(m_ptr);
153}
154
155template <typename T, std::size_t N>
156VECMEM_HOST_AND_DEVICE auto device_array<T, N>::begin() const
157 -> const_iterator {
158
159 return const_iterator(m_ptr);
160}
161
162template <typename T, std::size_t N>
163VECMEM_HOST_AND_DEVICE auto device_array<T, N>::cbegin() const
164 -> const_iterator {
165
166 return begin();
167}
168
169template <typename T, std::size_t N>
170VECMEM_HOST_AND_DEVICE auto device_array<T, N>::end() -> iterator {
171
172 return iterator(m_ptr + N);
173}
174
175template <typename T, std::size_t N>
176VECMEM_HOST_AND_DEVICE auto device_array<T, N>::end() const -> const_iterator {
177
178 return const_iterator(m_ptr + N);
179}
180
181template <typename T, std::size_t N>
182VECMEM_HOST_AND_DEVICE auto device_array<T, N>::cend() const -> const_iterator {
183
184 return end();
185}
186
187template <typename T, std::size_t N>
188VECMEM_HOST_AND_DEVICE auto device_array<T, N>::rbegin() -> reverse_iterator {
189
190 return reverse_iterator(end());
191}
192
193template <typename T, std::size_t N>
194VECMEM_HOST_AND_DEVICE auto device_array<T, N>::rbegin() const
196
197 return const_reverse_iterator(end());
198}
199
200template <typename T, std::size_t N>
201VECMEM_HOST_AND_DEVICE auto device_array<T, N>::crbegin() const
203
204 return rbegin();
205}
206
207template <typename T, std::size_t N>
208VECMEM_HOST_AND_DEVICE auto device_array<T, N>::rend() -> reverse_iterator {
209
210 return reverse_iterator(begin());
211}
212
213template <typename T, std::size_t N>
214VECMEM_HOST_AND_DEVICE auto device_array<T, N>::rend() const
216
217 return const_reverse_iterator(begin());
218}
219
220template <typename T, std::size_t N>
221VECMEM_HOST_AND_DEVICE auto device_array<T, N>::crend() const
223
224 return rend();
225}
226
227template <typename T, std::size_t N>
228VECMEM_HOST_AND_DEVICE constexpr bool device_array<T, N>::empty() const {
229
230 return N == 0;
231}
232
233template <typename T, std::size_t N>
234VECMEM_HOST_AND_DEVICE constexpr auto device_array<T, N>::size() const
235 -> size_type {
236
237 return N;
238}
239
240template <typename T, std::size_t N>
241VECMEM_HOST_AND_DEVICE constexpr auto device_array<T, N>::max_size() const
242 -> size_type {
243
244 return size();
245}
246
247} // namespace vecmem
Class holding data about a 1 dimensional vector/array.
Definition vector_view.hpp:38
Type mimicking std::reverse_iterator.
Definition reverse_iterator.hpp:25
Class mimicking a host-filled std::array in "device code".
Definition device_array.hpp:28
VECMEM_HOST_AND_DEVICE iterator end()
Return a forward iterator pointing at the end of the array.
Definition device_array.ipp:170
VECMEM_HOST_AND_DEVICE reference at(size_type pos)
Return a specific element of the array in a "safe way" (non-const)
Definition device_array.ipp:59
VECMEM_HOST_AND_DEVICE pointer data()
Access the underlying memory array (non-const)
Definition device_array.ipp:138
VECMEM_HOST_AND_DEVICE const_reverse_iterator crend() const
Return a constant reverse iterator pointing at the beginning of the array.
Definition device_array.ipp:221
VECMEM_HOST_AND_DEVICE reference front()
Return the first element of the array (non-const)
Definition device_array.ipp:96
VECMEM_HOST_AND_DEVICE reference operator[](size_type pos)
Return a specific element of the array (non-const)
Definition device_array.ipp:80
VECMEM_HOST_AND_DEVICE constexpr bool empty() const
Check whether the array is empty.
Definition device_array.ipp:228
value_type * pointer
Value pointer type.
Definition device_array.hpp:46
const value_type * const_pointer
Constant value pointer type.
Definition device_array.hpp:48
value_type & reference
Value reference type.
Definition device_array.hpp:42
std::size_t size_type
Size type for the array.
Definition device_array.hpp:37
VECMEM_HOST_AND_DEVICE const_reverse_iterator crbegin() const
Return a constant reverse iterator pointing at the end of the array.
Definition device_array.ipp:201
VECMEM_HOST_AND_DEVICE const_iterator cbegin() const
Return a constant forward iterator pointing at the beginning of the array.
Definition device_array.ipp:163
const_pointer const_iterator
Constant forward iterator type.
Definition device_array.hpp:53
VECMEM_HOST_AND_DEVICE reverse_iterator rbegin()
Return a reverse iterator pointing at the end of the array.
Definition device_array.ipp:188
VECMEM_HOST_AND_DEVICE const_iterator cend() const
Return a constant forward iterator pointing at the end of the array.
Definition device_array.ipp:182
VECMEM_HOST_AND_DEVICE device_array & operator=(const device_array &rhs)
Copy assignment operator.
Definition device_array.ipp:43
VECMEM_HOST_AND_DEVICE device_array(const data::vector_view< value_type > &data)
Constructor, on top of a previously allocated/filled block of memory.
Definition device_array.ipp:20
VECMEM_HOST_AND_DEVICE constexpr size_type max_size() const
Return the maximum (fixed) number of elements in the array.
Definition device_array.ipp:241
VECMEM_HOST_AND_DEVICE constexpr size_type size() const
Return the number of elements in the array.
Definition device_array.ipp:234
const value_type & const_reference
Constant value reference type.
Definition device_array.hpp:44
VECMEM_HOST_AND_DEVICE reverse_iterator rend()
Return a reverse iterator pointing at the beginning of the array.
Definition device_array.ipp:208
VECMEM_HOST_AND_DEVICE reference back()
Return the last element of the array (non-const)
Definition device_array.ipp:117
pointer iterator
Forward iterator type.
Definition device_array.hpp:51
VECMEM_HOST_AND_DEVICE iterator begin()
Return a forward iterator pointing at the beginning of the array.
Definition device_array.ipp:150
Main namespace for the vecmem classes/functions.
Definition atomic_ref.hpp:16
std::vector< T, vecmem::polymorphic_allocator< T > > vector
Alias type for vectors with our polymorphic allocator.
Definition vector.hpp:35