vecmem 1.14.0
Loading...
Searching...
No Matches
device_vector.hpp
1/* VecMem project, part of the ACTS project (R&D line)
2 *
3 * (c) 2021-2025 CERN for the benefit of the ACTS project
4 *
5 * Mozilla Public License Version 2.0
6 */
7#pragma once
8
9// Local include(s).
10#include "vecmem/containers/data/vector_view.hpp"
11#include "vecmem/containers/details/reverse_iterator.hpp"
12#include "vecmem/memory/device_atomic_ref.hpp"
13#include "vecmem/utils/type_traits.hpp"
14#include "vecmem/utils/types.hpp"
15
16// System include(s).
17#include <cstddef>
18#include <type_traits>
19
20namespace vecmem {
21
22// Forward declaration(s).
23namespace edm {
24template <typename T, template <typename> class I>
25class device;
26}
27
35template <typename TYPE>
37
38 // Make @c vecmem::edm::device a friend of this class.
39 template <typename T, template <typename> class I>
40 friend class edm::device;
41
42public:
45
49 using size_type = unsigned int;
51 using difference_type = std::ptrdiff_t;
52
55 typename std::conditional<std::is_const<TYPE>::value, const size_type*,
56 size_type*>::type;
57
59 using reference = std::add_lvalue_reference_t<value_type>;
62 std::add_lvalue_reference_t<std::add_const_t<value_type>>;
64 using pointer = std::add_pointer_t<value_type>;
66 using const_pointer = std::add_pointer_t<std::add_const_t<value_type>>;
67
77
79
81 VECMEM_HOST_AND_DEVICE
84 device_vector(const device_vector& parent) = default;
86 template <typename OTHERTYPE,
87 std::enable_if_t<std::is_convertible<OTHERTYPE, TYPE>::value,
88 bool> = true>
89 VECMEM_HOST_AND_DEVICE device_vector(
90 const device_vector<OTHERTYPE>& parent);
91
93 VECMEM_HOST_AND_DEVICE device_vector& operator=(const device_vector& rhs);
94
96 template <typename OTHERTYPE,
97 std::enable_if_t<std::is_convertible<OTHERTYPE, TYPE>::value,
98 bool> = true>
99 VECMEM_HOST_AND_DEVICE device_vector& operator=(
101
104
106 VECMEM_HOST_AND_DEVICE
109 VECMEM_HOST_AND_DEVICE
111
113 VECMEM_HOST_AND_DEVICE
116 VECMEM_HOST_AND_DEVICE
118
120 VECMEM_HOST_AND_DEVICE
123 VECMEM_HOST_AND_DEVICE
124 const_reference front() const;
125
127 VECMEM_HOST_AND_DEVICE
128 reference back();
130 VECMEM_HOST_AND_DEVICE
131 const_reference back() const;
132
134 VECMEM_HOST_AND_DEVICE
135 pointer data();
137 VECMEM_HOST_AND_DEVICE
138 const_pointer data() const;
139
141
144
146 VECMEM_HOST_AND_DEVICE
147 void assign(size_type count, const_reference value);
149 template <
150 typename InputIt,
151 std::enable_if_t<details::is_iterator_of<InputIt, value_type>::value,
152 bool> = true>
153 VECMEM_HOST_AND_DEVICE void assign(InputIt other_begin, InputIt other_end) {
154
155 // This can only be done on a resizable vector.
156 assert(m_size != nullptr);
157
158 // Remove all previous elements.
159 clear();
160
161 // Create copies of all of the elements one-by-one. It's very
162 // inefficient, but we can't make any assumptions about the type of the
163 // input iterator received by this function.
165 for (InputIt itr = other_begin; itr != other_end; ++itr) {
166 construct(asize.fetch_add(1), *itr);
167 }
168 }
169
171 template <typename... Args>
172 VECMEM_HOST_AND_DEVICE reference emplace_back(Args&&... args);
174 VECMEM_HOST_AND_DEVICE
176
181 VECMEM_HOST_AND_DEVICE size_type bulk_append(size_type n);
182
187 VECMEM_HOST_AND_DEVICE size_type bulk_append(size_type n,
189
196 VECMEM_HOST_AND_DEVICE size_type bulk_append_implicit(size_type n);
197
205 VECMEM_HOST_AND_DEVICE size_type bulk_append_implicit_unsafe(size_type n);
206
208 VECMEM_HOST_AND_DEVICE
210
212 VECMEM_HOST_AND_DEVICE
213 void clear();
215 VECMEM_HOST_AND_DEVICE
219 VECMEM_HOST_AND_DEVICE
221
225 VECMEM_HOST_AND_DEVICE void resize_implicit(size_type new_size);
226
231 VECMEM_HOST_AND_DEVICE void resize_implicit_unsafe(size_type new_size);
232
234
237
239 VECMEM_HOST_AND_DEVICE
240 iterator begin();
243 VECMEM_HOST_AND_DEVICE
244 const_iterator begin() const;
247 VECMEM_HOST_AND_DEVICE
248 const_iterator cbegin() const;
249
251 VECMEM_HOST_AND_DEVICE
252 iterator end();
254 VECMEM_HOST_AND_DEVICE
255 const_iterator end() const;
257 VECMEM_HOST_AND_DEVICE
258 const_iterator cend() const;
259
261 VECMEM_HOST_AND_DEVICE
264 VECMEM_HOST_AND_DEVICE
267 VECMEM_HOST_AND_DEVICE
269
271 VECMEM_HOST_AND_DEVICE
275 VECMEM_HOST_AND_DEVICE
279 VECMEM_HOST_AND_DEVICE
281
283
286
288 VECMEM_HOST_AND_DEVICE
289 bool empty() const;
291 VECMEM_HOST_AND_DEVICE
292 size_type size() const;
294 VECMEM_HOST_AND_DEVICE
295 size_type max_size() const;
297 VECMEM_HOST_AND_DEVICE
298 size_type capacity() const;
299
301
302private:
304 VECMEM_HOST_AND_DEVICE
305 void construct(size_type pos, const_reference value);
307 VECMEM_HOST_AND_DEVICE
308 void destruct(size_type pos);
309
311 size_type m_capacity;
313 size_pointer m_size;
315 pointer m_ptr;
316
317}; // class device_vector
318
319} // namespace vecmem
320
321// Include the implementation.
322#include "vecmem/containers/impl/device_vector.ipp"
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 an std::vector in "device code".
Definition device_vector.hpp:36
VECMEM_HOST_AND_DEVICE size_type bulk_append(size_type n)
Default-construct a given number of elements at the end of the vector.
Definition device_vector.ipp:236
VECMEM_HOST_AND_DEVICE reference back()
Return the last element of the vector (non-const)
Definition device_vector.ipp:142
VECMEM_HOST_AND_DEVICE void assign(InputIt other_begin, InputIt other_end)
Assign new values to the vector (not thread-safe)
Definition device_vector.hpp:153
VECMEM_HOST_AND_DEVICE void clear()
Clear the vector (not thread-safe)
Definition device_vector.ipp:333
VECMEM_HOST_AND_DEVICE iterator begin()
Return a forward iterator pointing at the beginning of the vector.
Definition device_vector.ipp:418
VECMEM_HOST_AND_DEVICE reference operator[](size_type pos)
Return a specific element of the vector (non-const)
Definition device_vector.ipp:105
VECMEM_HOST_AND_DEVICE void resize_implicit_unsafe(size_type new_size)
Resize a vector in constant time, unsafely deallocating non-implicit lifetime types.
Definition device_vector.ipp:407
VECMEM_HOST_AND_DEVICE pointer data()
Access the underlying memory array (non-const)
Definition device_vector.ipp:163
std::add_pointer_t< value_type > pointer
Value pointer type.
Definition device_vector.hpp:64
device_vector(const device_vector &parent)=default
Copy constructor.
VECMEM_HOST_AND_DEVICE size_type bulk_append_implicit_unsafe(size_type n)
Reserve a fixed number of slots in the array in a way that technically is ill-formed.
Definition device_vector.ipp:303
VECMEM_HOST_AND_DEVICE reference emplace_back(Args &&... args)
Add a new element at the end of the vector (thread-safe)
VECMEM_HOST_AND_DEVICE device_vector & operator=(const device_vector &rhs)
Copy assignment operator from an identical type.
Definition device_vector.ipp:48
std::add_pointer_t< std::add_const_t< value_type > > const_pointer
Constant value pointer type.
Definition device_vector.hpp:66
VECMEM_HOST_AND_DEVICE size_type capacity() const
Return the current (fixed) capacity of the vector.
Definition device_vector.ipp:525
TYPE value_type
Type of the array elements.
Definition device_vector.hpp:47
std::ptrdiff_t difference_type
Pointer difference type.
Definition device_vector.hpp:51
VECMEM_HOST_AND_DEVICE const_reverse_iterator crend() const
Return a constant reverse iterator pointing at the beginning of the vector.
Definition device_vector.ipp:490
VECMEM_HOST_AND_DEVICE size_type size() const
Return the number of elements in the vector.
Definition device_vector.ipp:503
const_pointer const_iterator
Constant forward iterator type.
Definition device_vector.hpp:71
VECMEM_HOST_AND_DEVICE bool empty() const
Check whether the vector is empty.
Definition device_vector.ipp:497
VECMEM_HOST_AND_DEVICE void assign(size_type count, const_reference value)
Assign new values to the vector (not thread-safe)
Definition device_vector.ipp:175
VECMEM_HOST_AND_DEVICE reference front()
Return the first element of the vector (non-const)
Definition device_vector.ipp:121
VECMEM_HOST_AND_DEVICE device_vector & operator=(const device_vector< OTHERTYPE > &rhs)
Copy assignment operator from a different type.
VECMEM_HOST_AND_DEVICE void resize(size_type new_size)
Resize the vector (not thread-safe)
Definition device_vector.ipp:350
VECMEM_HOST_AND_DEVICE const_reverse_iterator crbegin() const
Return a constant reverse iterator pointing at the end of the vector.
Definition device_vector.ipp:470
VECMEM_HOST_AND_DEVICE size_type push_back(const_reference value)
Add a new element at the end of the vector (thread-safe)
Definition device_vector.ipp:216
VECMEM_HOST_AND_DEVICE size_type bulk_append_implicit(size_type n)
Reserve a fixed number of slots in the array in a standards-conformant way.
Definition device_vector.ipp:279
unsigned int size_type
Size type for the array.
Definition device_vector.hpp:49
VECMEM_HOST_AND_DEVICE iterator end()
Return a forward iterator pointing at the end of the vector.
Definition device_vector.ipp:438
pointer iterator
Forward iterator type.
Definition device_vector.hpp:69
std::add_lvalue_reference_t< value_type > reference
Value reference type.
Definition device_vector.hpp:59
typename std::conditional< std::is_const< TYPE >::value, const size_type *, size_type * >::type size_pointer
Pointer type to the size of the array.
Definition device_vector.hpp:56
VECMEM_HOST_AND_DEVICE size_type max_size() const
Return the maximum (fixed) number of elements in the vector.
Definition device_vector.ipp:519
std::add_lvalue_reference_t< std::add_const_t< value_type > > const_reference
Constant value reference type.
Definition device_vector.hpp:62
VECMEM_HOST_AND_DEVICE const_iterator cend() const
Return a constant forward iterator pointing at the end of the vector.
Definition device_vector.ipp:450
VECMEM_HOST_AND_DEVICE reference at(size_type pos)
Return a specific element of the vector in a "safe way" (non-const)
Definition device_vector.ipp:83
VECMEM_HOST_AND_DEVICE const_iterator cbegin() const
Return a constant forward iterator pointing at the beginning of the vector.
Definition device_vector.ipp:431
VECMEM_HOST_AND_DEVICE size_type pop_back()
Remove the last element of the vector (not thread-safe)
Definition device_vector.ipp:316
VECMEM_HOST_AND_DEVICE reverse_iterator rend()
Return a reverse iterator pointing at the beginning of the vector.
Definition device_vector.ipp:477
VECMEM_HOST_AND_DEVICE reverse_iterator rbegin()
Return a reverse iterator pointing at the end of the vector.
Definition device_vector.ipp:457
VECMEM_HOST_AND_DEVICE void resize_implicit(size_type new_size)
Resize a vector of implicit lifetime types.
Definition device_vector.ipp:390
Technical base type for device<schema<VARTYPES...>,INTERFACE>
Definition device_vector.hpp:25
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