vecmem 1.18.0
Loading...
Searching...
No Matches
jagged_vector_buffer.ipp
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/details/aligned_multiple_placement.hpp"
12
13// System include(s).
14#include <algorithm>
15#include <cassert>
16#include <cstddef>
17#include <memory>
18#include <numeric>
19#include <vector>
20
21namespace {
22
24template <typename TYPE>
27allocate_jagged_buffer_outer_memory(
29 vecmem::memory_resource& resource) {
30
31 if (size == 0) {
32 return nullptr;
33 } else {
36 resource, size);
37 }
38}
39} // namespace
40
41namespace vecmem {
42namespace data {
43
48template <typename TYPE>
50
51template <typename TYPE>
52template <typename OTHERTYPE,
53 std::enable_if_t<std::is_convertible<TYPE, OTHERTYPE>::value, bool> >
59
60template <typename TYPE>
61template <typename SIZE_TYPE, typename SIZE_ALLOC,
62 std::enable_if_t<std::is_integral<SIZE_TYPE>::value &&
63 std::is_unsigned<SIZE_TYPE>::value,
64 bool> >
66 const std::vector<SIZE_TYPE, SIZE_ALLOC>& capacities,
67 memory_resource& resource, memory_resource* host_access_resource,
68 buffer_type type)
69 : base_type(capacities.size(), nullptr),
71 (host_access_resource == nullptr ? 0 : capacities.size()), resource)),
72 m_outer_host_memory(::allocate_jagged_buffer_outer_memory<TYPE>(
73 capacities.size(),
74 (host_access_resource == nullptr ? resource
76
77 // Determine the allocation size.
79 TYPE>::value_type::size_type;
80 const std::size_t total_elements = std::accumulate(
81 capacities.begin(), capacities.end(), static_cast<std::size_t>(0));
82
83 // Helper pointers to the "inner data".
84 header_t* header_ptr = nullptr;
85 TYPE* data_ptr = nullptr;
86
87 // Allocate the "inner memory" for a fixed size buffer.
88 if (type == buffer_type::fixed_size && total_elements != 0) {
89 m_inner_memory = vecmem::make_unique_alloc<char[]>(
90 resource, total_elements * sizeof(TYPE));
91 data_ptr = reinterpret_cast<TYPE*>(m_inner_memory.get());
92 }
93 // Allocate the "inner memory" for a resizable buffer.
94 else if (type == buffer_type::resizable && capacities.size() != 0) {
95 std::tie(m_inner_memory, header_ptr, data_ptr) =
96 details::aligned_multiple_placement<header_t, TYPE>(
97 resource, capacities.size(), total_elements);
98 }
99
100 // Set up the base object.
102 capacities.size(),
103 ((host_access_resource != nullptr) ? m_outer_memory.get()
104 : m_outer_host_memory.get()),
105 m_outer_host_memory.get()});
106
107 // Set up the vecmem::vector_view objects in the host accessible memory.
108 std::ptrdiff_t ptrdiff = 0;
109 for (std::size_t i = 0; i < capacities.size(); ++i) {
110 if (header_ptr != nullptr) {
112 static_cast<typename value_type::size_type>(capacities[i]),
114 } else {
116 static_cast<typename value_type::size_type>(capacities[i]),
117 data_ptr + ptrdiff);
118 }
119 ptrdiff += capacities[i];
120 }
121}
122
123} // namespace data
124
125template <typename TYPE>
131
132template <typename TYPE>
135
136 return data;
137}
138
139} // namespace vecmem
An allocator class that wraps a memory resource.
Definition allocator.hpp:37
Object owning all the data of a jagged vector.
Definition jagged_vector_buffer.hpp:30
typename base_type::size_type size_type
Use the base class's size_type.
Definition jagged_vector_buffer.hpp:36
typename base_type::value_type value_type
Use the base class's value_type.
Definition jagged_vector_buffer.hpp:38
jagged_vector_buffer()
Make sure that the template type does not have a custom destructor.
Definition jagged_vector_buffer.ipp:49
VECMEM_HOST_AND_DEVICE pointer host_ptr() const
Access the host accessible array describing the inner vectors.
Definition jagged_vector_view.ipp:105
VECMEM_HOST_AND_DEVICE jagged_vector_view & operator=(const jagged_vector_view< OTHERTYPE > &rhs)
Assignment operator from a "slightly different" object.
VECMEM_HOST std::vector< typename vector_view< T >::size_type > get_capacities(const jagged_vector_view< T > &data)
Get the capacities of the inner vectors of a jagged vector.
Definition jagged_vector_view.ipp:111
buffer_type
"Overall type" for a buffer object
Definition buffer_type.hpp:13
@ fixed_size
The buffer has a fixed number of elements.
@ resizable
The buffer is resizable/expandable.
Main namespace for the vecmem classes/functions.
Definition atomic_ref.hpp:16
unique_alloc_ptr< T > make_unique_alloc(memory_resource &m)
Create a unique allocation pointer to a type.
Definition unique_ptr.hpp:185
std::unique_ptr< T, details::unique_alloc_deleter< T > > unique_alloc_ptr
A unique pointer type for trivial types.
Definition unique_ptr.hpp:69
VECMEM_HOST data::vector_view< T > get_data(array< T, N > &a)
Helper function creating a vecmem::data::vector_view object.
Definition array.ipp:217