vecmem 1.28.0
Loading...
Searching...
No Matches
device_vector.hpp
1/* VecMem project, part of the ACTS project (R&D line)
2 *
3 * (c) 2021-2026 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 other specializations of the class a friend of this class.
39 template <typename OTHERTYPE>
40 friend class device_vector;
41 // Make @c vecmem::edm::device a friend of this class.
42 template <typename T, template <typename> class I>
43 friend class edm::device;
44
45public:
48
52 using size_type = unsigned int;
54 using difference_type = std::ptrdiff_t;
55
58 typename std::conditional<std::is_const<TYPE>::value, const size_type*,
59 size_type*>::type;
60
62 using reference = std::add_lvalue_reference_t<value_type>;
65 std::add_lvalue_reference_t<std::add_const_t<value_type>>;
67 using pointer = std::add_pointer_t<value_type>;
69 using const_pointer = std::add_pointer_t<std::add_const_t<value_type>>;
70
80
82
84 VECMEM_HOST_AND_DEVICE
87 device_vector(const device_vector& parent) = default;
89 template <typename OTHERTYPE,
90 std::enable_if_t<details::is_same_nc<TYPE, OTHERTYPE>::value,
91 bool> = true>
92 VECMEM_HOST_AND_DEVICE device_vector(
93 const device_vector<OTHERTYPE>& parent);
94
96 VECMEM_HOST_AND_DEVICE device_vector& operator=(const device_vector& rhs);
97
99 template <typename OTHERTYPE,
100 std::enable_if_t<std::is_convertible<OTHERTYPE, TYPE>::value,
101 bool> = true>
102 VECMEM_HOST_AND_DEVICE device_vector& operator=(
104
107
109 VECMEM_HOST_AND_DEVICE
112 VECMEM_HOST_AND_DEVICE
114
116 VECMEM_HOST_AND_DEVICE
119 VECMEM_HOST_AND_DEVICE
121
123 VECMEM_HOST_AND_DEVICE
126 VECMEM_HOST_AND_DEVICE
127 const_reference front() const;
128
130 VECMEM_HOST_AND_DEVICE
131 reference back();
133 VECMEM_HOST_AND_DEVICE
134 const_reference back() const;
135
137 VECMEM_HOST_AND_DEVICE
138 pointer data();
140 VECMEM_HOST_AND_DEVICE
141 const_pointer data() const;
142
144
147
149 VECMEM_HOST_AND_DEVICE
150 void assign(size_type count, const_reference value);
152 template <
153 typename InputIt,
154 std::enable_if_t<details::is_iterator_of<InputIt, value_type>::value,
155 bool> = true>
156 VECMEM_HOST_AND_DEVICE void assign(InputIt other_begin, InputIt other_end) {
157
158 // This can only be done on a resizable vector.
159 assert(m_size != nullptr);
160
161 // Remove all previous elements.
162 clear();
163
164 // Create copies of all of the elements one-by-one. It's very
165 // inefficient, but we can't make any assumptions about the type of the
166 // input iterator received by this function.
168 for (InputIt itr = other_begin; itr != other_end; ++itr) {
169 construct(asize.fetch_add(1), *itr);
170 }
171 }
172
174 template <typename... Args>
175 VECMEM_HOST_AND_DEVICE reference emplace_back(Args&&... args);
177 VECMEM_HOST_AND_DEVICE
179
184 VECMEM_HOST_AND_DEVICE size_type bulk_append(size_type n);
185
190 VECMEM_HOST_AND_DEVICE size_type bulk_append(size_type n,
192
199 VECMEM_HOST_AND_DEVICE size_type bulk_append_implicit(size_type n);
200
208 VECMEM_HOST_AND_DEVICE size_type bulk_append_implicit_unsafe(size_type n);
209
211 VECMEM_HOST_AND_DEVICE
213
215 VECMEM_HOST_AND_DEVICE
216 void clear();
218 VECMEM_HOST_AND_DEVICE
222 VECMEM_HOST_AND_DEVICE
224
228 VECMEM_HOST_AND_DEVICE void resize_implicit(size_type new_size);
229
234 VECMEM_HOST_AND_DEVICE void resize_implicit_unsafe(size_type new_size);
235
237
240
242 VECMEM_HOST_AND_DEVICE
243 iterator begin();
246 VECMEM_HOST_AND_DEVICE
247 const_iterator begin() const;
250 VECMEM_HOST_AND_DEVICE
251 const_iterator cbegin() const;
252
254 VECMEM_HOST_AND_DEVICE
255 iterator end();
257 VECMEM_HOST_AND_DEVICE
258 const_iterator end() const;
260 VECMEM_HOST_AND_DEVICE
261 const_iterator cend() const;
262
264 VECMEM_HOST_AND_DEVICE
267 VECMEM_HOST_AND_DEVICE
270 VECMEM_HOST_AND_DEVICE
272
274 VECMEM_HOST_AND_DEVICE
278 VECMEM_HOST_AND_DEVICE
282 VECMEM_HOST_AND_DEVICE
284
286
289
291 VECMEM_HOST_AND_DEVICE
292 bool empty() const;
294 VECMEM_HOST_AND_DEVICE
295 size_type size() const;
297 VECMEM_HOST_AND_DEVICE
298 size_type max_size() const;
300 VECMEM_HOST_AND_DEVICE
301 size_type capacity() const;
302
304
305private:
307 VECMEM_HOST_AND_DEVICE
308 void construct(size_type pos, const_reference value);
310 VECMEM_HOST_AND_DEVICE
311 void destruct(size_type pos);
312
314 size_type m_capacity;
316 size_pointer m_size;
318 pointer m_ptr;
319
320}; // class device_vector
321
322} // namespace vecmem
323
324// Include the implementation.
325#include "vecmem/containers/impl/device_vector.ipp"
An allocator class that wraps a memory resource.
Definition allocator.hpp:37
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:156
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:67
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:69
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:50
std::ptrdiff_t difference_type
Pointer difference type.
Definition device_vector.hpp:54
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:74
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:52
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:72
std::add_lvalue_reference_t< value_type > reference
Value reference type.
Definition device_vector.hpp:62
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:59
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:65
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