vecmem 1.24.0
Loading...
Searching...
No Matches
copy.hpp
1/*
2 * VecMem project, part of the ACTS project (R&D line)
3 *
4 * (c) 2021-2025 CERN for the benefit of the ACTS project
5 *
6 * Mozilla Public License Version 2.0
7 */
8#pragma once
9
10// VecMem include(s).
11#include "vecmem/containers/data/jagged_vector_buffer.hpp"
12#include "vecmem/containers/data/jagged_vector_view.hpp"
13#include "vecmem/containers/data/vector_buffer.hpp"
14#include "vecmem/containers/data/vector_view.hpp"
15#include "vecmem/containers/vector.hpp"
16#include "vecmem/edm/buffer.hpp"
17#include "vecmem/edm/details/schema_traits.hpp"
18#include "vecmem/edm/host.hpp"
19#include "vecmem/edm/view.hpp"
20#include "vecmem/memory/memory_resource.hpp"
21#include "vecmem/utils/abstract_event.hpp"
22#include "vecmem/utils/async_size.hpp"
23#include "vecmem/utils/async_sizes.hpp"
24#include "vecmem/utils/attributes.hpp"
25#include "vecmem/vecmem_core_export.hpp"
26
27// System include(s).
28#include <cstddef>
29#include <memory>
30#include <type_traits>
31#include <vector>
32
33namespace vecmem {
34
46
47public:
54 struct type {
56 enum copy_type {
58 host_to_device = 0,
60 device_to_host = 1,
62 host_to_host = 2,
64 device_to_device = 3,
66 unknown = 4,
68 count = 5
69 }; // enum copy_type
70 }; // struct type
71
73 using event_type = std::unique_ptr<abstract_event>;
74
77
79 virtual ~copy() = default;
80
82 template <typename TYPE>
83 VECMEM_NODISCARD event_type setup(data::vector_view<TYPE> data) const;
84
86 template <typename TYPE>
88 int value) const;
89
91 template <typename TYPE>
93 const data::vector_view<TYPE>& data, memory_resource& resource,
94 type::copy_type cptype = type::unknown) const;
95
97 template <typename TYPE>
98 VECMEM_NODISCARD event_type
99 operator()(const data::vector_view<std::add_const_t<TYPE>>& from,
101 type::copy_type cptype = type::unknown) const;
102
104 template <typename TYPE, typename ALLOC>
105 VECMEM_NODISCARD event_type
106 operator()(const data::vector_view<std::add_const_t<TYPE>>& from,
107 std::vector<TYPE, ALLOC>& to,
108 type::copy_type cptype = type::unknown) const;
109
111 template <typename TYPE>
112 typename data::vector_view<TYPE>::size_type get_size(
113 const data::vector_view<TYPE>& data) const;
114
117 template <typename TYPE>
120 memory_resource& pinnedHostMr) const;
121
123
126
128 template <typename TYPE>
129 VECMEM_NODISCARD event_type
131
133 template <typename TYPE>
135 int value) const;
136
138 template <typename TYPE>
140 const data::jagged_vector_view<TYPE>& data, memory_resource& resource,
141 memory_resource* host_access_resource = nullptr,
142 type::copy_type cptype = type::unknown) const;
143
145 template <typename TYPE>
146 VECMEM_NODISCARD event_type
147 operator()(const data::jagged_vector_view<std::add_const_t<TYPE>>& from,
149 type::copy_type cptype = type::unknown) const;
150
152 template <typename TYPE, typename ALLOC1, typename ALLOC2>
153 VECMEM_NODISCARD event_type
154 operator()(const data::jagged_vector_view<std::add_const_t<TYPE>>& from,
155 std::vector<std::vector<TYPE, ALLOC2>, ALLOC1>& to,
156 type::copy_type cptype = type::unknown) const;
157
159 template <typename TYPE>
160 std::vector<typename data::vector_view<TYPE>::size_type> get_sizes(
161 const data::jagged_vector_view<TYPE>& data) const;
162
164 template <typename TYPE>
165 VECMEM_NODISCARD event_type set_sizes(
166 const std::vector<typename data::vector_view<TYPE>::size_type>& sizes,
168
171 template <typename TYPE>
174 memory_resource& pinnedHostMr) const;
175
177
180
182 template <typename SCHEMA>
183 VECMEM_NODISCARD event_type setup(edm::view<SCHEMA> data) const;
184
186 template <typename... VARTYPES>
188 int value) const;
189
191 template <typename... VARTYPES>
192 edm::buffer<edm::details::remove_cv_t<edm::schema<VARTYPES...>>> to(
194 memory_resource& resource,
195 memory_resource* host_access_resource = nullptr,
196 type::copy_type cptype = type::unknown) const;
197
199 template <typename... VARTYPES>
200 VECMEM_NODISCARD event_type operator()(
201 const edm::view<edm::details::add_const_t<edm::schema<VARTYPES...>>>&
202 from,
204 type::copy_type cptype = type::unknown) const;
205
207 template <typename... VARTYPES, template <typename> class INTERFACE>
208 VECMEM_NODISCARD event_type operator()(
209 const edm::view<edm::details::add_const_t<edm::schema<VARTYPES...>>>&
210 from,
212 type::copy_type cptype = type::unknown) const;
213
215 template <typename... VARTYPES>
216 typename edm::view<edm::schema<VARTYPES...>>::size_type get_size(
217 const edm::view<edm::schema<VARTYPES...>>& data) const;
218
220 template <typename... VARTYPES>
221 VECMEM_NODISCARD
222 async_size<typename edm::view<edm::schema<VARTYPES...>>::size_type>
224 memory_resource& pinnedHostMr) const;
225
227 template <typename... VARTYPES>
228 std::vector<data::vector_view<int>::size_type> get_sizes(
229 const edm::view<edm::schema<VARTYPES...>>& data) const;
230
232 template <typename... VARTYPES>
235 memory_resource& pinnedHostMr) const;
236
238
239protected:
241 virtual void do_copy(std::size_t size, const void* from, void* to,
242 type::copy_type cptype) const;
244 virtual void do_memset(std::size_t size, void* ptr, int value) const;
246 VECMEM_NODISCARD virtual event_type create_event() const;
247
248private:
250 template <typename TYPE>
251 bool copy_view_impl(const data::vector_view<std::add_const_t<TYPE>>& from,
253 type::copy_type cptype) const;
255 template <typename TYPE>
256 bool copy_view_impl(
257 const data::jagged_vector_view<std::add_const_t<TYPE>>& from,
260 template <typename TYPE>
261 void copy_views_impl(
262 const std::vector<typename data::vector_view<TYPE>::size_type>& sizes,
263 const data::vector_view<std::add_const_t<TYPE>>* from,
266 template <typename TYPE>
267 void copy_views_contiguous_impl(
268 const std::vector<typename data::vector_view<TYPE>::size_type>& sizes,
269 const data::vector_view<std::add_const_t<TYPE>>* from,
272 template <typename TYPE>
273 static bool is_contiguous(const data::vector_view<TYPE>* data,
274 std::size_t size);
276 template <std::size_t INDEX, typename... VARTYPES>
277 void memset_impl(edm::view<edm::schema<VARTYPES...>> data, int value) const;
279 template <std::size_t INDEX, typename... VARTYPES,
280 template <typename> class INTERFACE>
281 void resize_impl(
282 const edm::view<edm::details::add_const_t<edm::schema<VARTYPES...>>>&
283 from,
285 type::copy_type cptype) const;
287 template <std::size_t INDEX, typename... VARTYPES>
288 void copy_sizes_impl(
289 const edm::view<edm::details::add_const_t<edm::schema<VARTYPES...>>>&
290 from,
293 template <std::size_t INDEX, typename... VARTYPES>
294 void copy_payload_impl(
295 const edm::view<edm::details::add_const_t<edm::schema<VARTYPES...>>>&
296 from,
299 template <std::size_t INDEX, typename... VARTYPES>
300 std::vector<data::vector_view<int>::size_type> get_sizes_impl(
303 template <std::size_t INDEX, typename... VARTYPES>
306 memory_resource& pinnedHostMr) const;
307
308}; // class copy
309
310} // namespace vecmem
311
312// Include the implementation.
313#include "vecmem/utils/impl/copy.ipp"
An allocator class that wraps a memory resource.
Definition allocator.hpp:37
Return type for asynchronous size retrievals.
Definition async_size.hpp:25
Class implementing (synchronous) host <-> device memory copies.
Definition copy.hpp:45
VECMEM_NODISCARD event_type setup(data::jagged_vector_view< TYPE > data) const
Copy the internal state of a jagged vector buffer to the target device.
VECMEM_NODISCARD event_type set_sizes(const std::vector< typename data::vector_view< TYPE >::size_type > &sizes, data::jagged_vector_view< TYPE > data) const
Helper function for setting the sizes of a resizable jagged vector.
VECMEM_NODISCARD event_type operator()(const data::vector_view< std::add_const_t< TYPE > > &from, data::vector_view< TYPE > to, type::copy_type cptype=type::unknown) const
Copy a 1-dimensional vector's data between two existing memory blocks.
VECMEM_NODISCARD event_type setup(data::vector_view< TYPE > data) const
Set up the internal state of a vector buffer correctly on a device.
VECMEM_NODISCARD event_type operator()(const edm::view< edm::details::add_const_t< edm::schema< VARTYPES... > > > &from, edm::host< edm::schema< VARTYPES... >, INTERFACE > &to, type::copy_type cptype=type::unknown) const
Copy from a view, into a host container.
VECMEM_NODISCARD event_type operator()(const data::jagged_vector_view< std::add_const_t< TYPE > > &from, std::vector< std::vector< TYPE, ALLOC2 >, ALLOC1 > &to, type::copy_type cptype=type::unknown) const
Copy a jagged vector's data into a vector object.
VECMEM_NODISCARD event_type operator()(const data::vector_view< std::add_const_t< TYPE > > &from, std::vector< TYPE, ALLOC > &to, type::copy_type cptype=type::unknown) const
Copy a 1-dimensional vector's data into a vector object.
VECMEM_NODISCARD async_size< typename edm::view< edm::schema< VARTYPES... > >::size_type > get_size(const edm::view< edm::schema< VARTYPES... > > &data, memory_resource &pinnedHostMr) const
Get the (outer) size of a (resizable) SoA container asynchronously.
std::unique_ptr< abstract_event > event_type
Event type used by the copy class.
Definition copy.hpp:73
VECMEM_NODISCARD event_type memset(edm::view< edm::schema< VARTYPES... > > data, int value) const
Set all bytes of the container to some value.
VECMEM_NODISCARD event_type operator()(const edm::view< edm::details::add_const_t< edm::schema< VARTYPES... > > > &from, edm::view< edm::schema< VARTYPES... > > to, type::copy_type cptype=type::unknown) const
Copy between two views.
VECMEM_NODISCARD event_type memset(data::jagged_vector_view< TYPE > data, int value) const
Set all bytes of the jagged vector to some value.
VECMEM_NODISCARD event_type operator()(const data::jagged_vector_view< std::add_const_t< TYPE > > &from, data::jagged_vector_view< TYPE > to, type::copy_type cptype=type::unknown) const
Copy a jagged vector's data between two existing allocations.
VECMEM_NODISCARD async_sizes< data::vector_view< int >::size_type > get_sizes(const edm::view< edm::schema< VARTYPES... > > &data, memory_resource &pinnedHostMr) const
Get the (inner) size of a (resizable) SoA container asynchronously.
VECMEM_NODISCARD async_sizes< typename data::vector_view< TYPE >::size_type > get_sizes(const data::jagged_vector_view< TYPE > &data, memory_resource &pinnedHostMr) const
Helper function for getting the sizes of a resizable jagged vector asynchronously.
VECMEM_NODISCARD async_size< typename data::vector_view< TYPE >::size_type > get_size(const data::vector_view< TYPE > &data, memory_resource &pinnedHostMr) const
Helper function for getting the size of a resizable 1D buffer asynchonously.
VECMEM_NODISCARD event_type setup(edm::view< SCHEMA > data) const
Set up the internal state of a buffer correctly on a device.
VECMEM_NODISCARD event_type memset(data::vector_view< TYPE > data, int value) const
Set all bytes of the vector to some value.
virtual ~copy()=default
Virtual destructor.
Object owning all the data of a jagged vector.
Definition jagged_vector_buffer.hpp:30
A view for jagged vectors.
Definition jagged_vector_view.hpp:45
Object owning the data held by it.
Definition vector_buffer.hpp:29
Class holding data about a 1 dimensional vector/array.
Definition vector_view.hpp:38
Technical base type for buffer<schema<VARTYPES...>>
Definition buffer.hpp:28
Technical base type for host<schema<VARTYPES...>,INTERFACE>
Definition host.hpp:28
Technical base type for view<schema<VARTYPES...>>
Definition view.hpp:29
Main namespace for the vecmem classes/functions.
Definition atomic_ref.hpp:16
Wrapper struct around the copy_type enumeration.
Definition copy.hpp:54
copy_type
Types of memory copies to handle.
Definition copy.hpp:56
Meta type describing the "schema" of an SoA container.
Definition schema.hpp:46