ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
grid_collection.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 CERN for the benefit of the ACTS project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s).
12#include "detray/definitions/detail/qualifiers.hpp"
13#include "detray/utils/grid/concepts.hpp"
14#include "detray/utils/grid/grid.hpp"
15
16// VecMem include(s).
17#include <vecmem/memory/memory_resource.hpp>
18
19// System include(s).
20#include <cstddef>
21#include <type_traits>
22
23namespace detray {
24
30template <concepts::grid grid_t, typename = void>
32
34template <typename axes_t, typename bin_t,
35 template <std::size_t> class serializer_t>
39 using const_grid_t =
41 using multi_axis_t = typename grid_t::axes_type;
42
43 public:
45 using value_type = grid_t;
48
53 typename multi_axis_t::edge_offset_container_type;
55 using edges_container_type = typename multi_axis_t::edges_container_type;
57 template <typename T>
58 using vector_type = typename multi_axis_t::template vector_type<T>;
59
60 private:
62 struct iterator {
63 using difference_type = std::ptrdiff_t;
64 using value_type = grid_t;
65 using pointer = grid_t *;
66 using reference = grid_t &;
67 using iterator_category = detray::ranges::bidirectional_iterator_tag;
68
70 DETRAY_HOST_DEVICE
71 constexpr explicit iterator(const grid_collection &grid_coll, dindex i = 0u)
72 : m_i{i}, m_grid_coll{grid_coll} {}
73
74 constexpr iterator(const iterator &other) = default;
75 constexpr iterator(iterator &&other) noexcept = default;
76 constexpr iterator &operator=(const iterator &rhs) noexcept = default;
77
79 DETRAY_HOST_DEVICE
80 constexpr auto operator==(const iterator &rhs) const -> bool {
81 return (m_i == rhs.m_i);
82 }
83
85 DETRAY_HOST_DEVICE
86 constexpr auto operator++() -> iterator & {
87 ++m_i;
88 return *this;
89 }
90
92 DETRAY_HOST_DEVICE
93 constexpr auto operator--() -> iterator & {
94 --m_i;
95 return *this;
96 }
97
100 DETRAY_HOST_DEVICE
101 constexpr auto operator*() const -> value_type { return m_grid_coll[m_i]; }
102
103 private:
105 dindex m_i;
107 const grid_collection &m_grid_coll;
108 };
109
110 public:
112 using view_type = dmulti_view<dvector_view<size_type>,
113 detail::get_view_t<bin_container_type>,
114 detail::get_view_t<edge_offset_container_type>,
115 detail::get_view_t<edges_container_type>>;
116
119 dmulti_view<dvector_view<const size_type>,
120 detail::get_view_t<const bin_container_type>,
121 detail::get_view_t<const edge_offset_container_type>,
122 detail::get_view_t<const edges_container_type>>;
123
126 dmulti_buffer<dvector_buffer<size_type>,
127 detail::get_buffer_t<bin_container_type>,
128 detail::get_buffer_t<edge_offset_container_type>,
129 detail::get_buffer_t<edges_container_type>>;
130
132 grid_collection() = default;
133
136 DETRAY_HOST
137 explicit grid_collection(vecmem::memory_resource *resource)
138 : m_bin_offsets(resource),
139 m_bins(resource),
140 m_bin_edge_offsets(resource),
141 m_bin_edges(resource) {}
142
148 DETRAY_HOST_DEVICE
150 edge_offset_container_type &&edge_offs,
151 edges_container_type &&edges)
152 : m_bin_offsets(std::move(offs)),
153 m_bins(std::move(bins)),
154 m_bin_edge_offsets(std::move(edge_offs)),
155 m_bin_edges(std::move(edges)) {}
156
159 template <concepts::device_view coll_view_t>
160 DETRAY_HOST_DEVICE explicit grid_collection(coll_view_t &view)
161 : m_bin_offsets(detail::get<0>(view.m_view)),
162 m_bins(detail::get<1>(view.m_view)),
163 m_bin_edge_offsets(detail::get<2>(view.m_view)),
164 m_bin_edges(detail::get<3>(view.m_view)) {}
165
168 DETRAY_HOST_DEVICE grid_collection(grid_collection &&other) noexcept
169 : m_bin_offsets(std::move(other.m_bin_offsets)),
170 m_bins(std::move(other.m_bins)),
171 m_bin_edge_offsets(std::move(other.m_bin_edge_offsets)),
172 m_bin_edges(std::move(other.m_bin_edges)) {}
173
177 DETRAY_HOST_DEVICE grid_collection &operator=(
178 grid_collection &&other) noexcept {
179 if (this != &other) {
180 m_bin_offsets = std::move(other.m_bin_offsets);
181 m_bins = std::move(other.m_bins);
182 m_bin_edge_offsets = std::move(other.m_bin_edge_offsets);
183 m_bin_edges = std::move(other.m_bin_edges);
184 }
185 return *this;
186 }
187
189 DETRAY_HOST_DEVICE
190 constexpr auto size() const noexcept -> dindex {
191 return static_cast<dindex>(m_bin_offsets.size());
192 }
193
195 DETRAY_HOST_DEVICE
196 constexpr auto begin() const { return iterator{*this, 0u}; }
197
199 DETRAY_HOST_DEVICE
200 constexpr auto end() const { return iterator{*this, size()}; }
201
203 DETRAY_HOST_DEVICE
204 constexpr auto empty() const noexcept -> bool {
205 return m_bin_offsets.empty();
206 }
207
210 DETRAY_HOST_DEVICE
211 constexpr void resize(std::size_t /*unused*/) noexcept { /*Not defined*/ }
212
215 DETRAY_HOST_DEVICE
216 constexpr void reserve(std::size_t /*unused*/) noexcept { /*Not defined*/ }
217
219 DETRAY_HOST_DEVICE
220 constexpr void clear() noexcept {
221 m_bin_offsets.clear();
222 m_bins.clear();
223 m_bin_edge_offsets.clear();
224 m_bin_edges.clear();
225 }
226
229 template <typename... Args>
230 DETRAY_HOST_DEVICE constexpr void insert(Args &&.../*unused*/) noexcept {
231 /*Not defined*/
232 }
233
235 DETRAY_HOST_DEVICE
236 constexpr auto offsets() const -> const vector_type<size_type> & {
237 return m_bin_offsets;
238 }
239
241 DETRAY_HOST_DEVICE
242 constexpr auto bin_storage() const -> const bin_container_type & {
243 return m_bins;
244 }
245
247 DETRAY_HOST_DEVICE
248 constexpr auto axes_storage() const -> const edge_offset_container_type & {
249 return m_bin_edge_offsets;
250 }
251
253 DETRAY_HOST_DEVICE
254 constexpr auto bin_edges_storage() const -> const edges_container_type & {
255 return m_bin_edges;
256 }
257
261 DETRAY_HOST_DEVICE
262 auto operator[](const size_type i) const -> grid_t {
263 const size_type axes_offset{grid_t::dim * i};
264 return grid_t(&m_bins,
265 multi_axis_t(m_bin_edge_offsets, m_bin_edges, axes_offset),
266 m_bin_offsets[i]);
267 }
268
272 DETRAY_HOST_DEVICE
273 auto at(const size_type i) const -> grid_t {
274 const size_type axes_offset{grid_t::dim * i};
275 return grid_t(&m_bins,
276 multi_axis_t(m_bin_edge_offsets, m_bin_edges, axes_offset),
277 m_bin_offsets.at(i));
278 }
279
281 DETRAY_HOST auto get_data() -> view_type {
282 return view_type{detray::get_data(m_bin_offsets), detray::get_data(m_bins),
283 detray::get_data(m_bin_edge_offsets),
284 detray::get_data(m_bin_edges)};
285 }
286
288 DETRAY_HOST
289 auto get_data() const -> const_view_type {
290 return const_view_type{
291 detray::get_data(m_bin_offsets), detray::get_data(m_bins),
292 detray::get_data(m_bin_edge_offsets), detray::get_data(m_bin_edges)};
293 }
294
297 template <typename other_grid_t>
298 requires std::constructible_from<typename grid_t::template type<true>,
299 other_grid_t>
300 DETRAY_HOST constexpr auto push_back(const other_grid_t &gr) noexcept(false)
301 -> void {
302 // Current offset into the global bin storage for the new grid
303 m_bin_offsets.push_back(static_cast<size_type>(m_bins.size()));
304
305 // Add the bins of the new grid to the collection
306 insert_bin_data(m_bins, gr.bins());
307
308 // Add the bin edge offsets of the new grid to the collection
309 // (how to lookup the axis bin edges)
310 const auto &bin_edge_offsets = gr.axes().bin_edge_offsets();
311 m_bin_edge_offsets.insert(m_bin_edge_offsets.end(),
312 bin_edge_offsets.begin(), bin_edge_offsets.end());
313
314 // Current offset into the global bin edges storage
315 auto bin_edges_offset{static_cast<dindex>(m_bin_edges.size())};
316
317 // Update the bin edges index offset for the axes in the grid collection
318 const auto start_idx{m_bin_edge_offsets.size() - grid_t::dim};
319 for (std::size_t i = start_idx; i < m_bin_edge_offsets.size(); ++i) {
320 auto &bin_entry_range = m_bin_edge_offsets.at(i);
321 bin_entry_range.shift(bin_edges_offset);
322 }
323
324 // Add the bin edges of the new grid to the collection
325 const auto &bin_edges = gr.axes().bin_edges();
326 m_bin_edges.insert(m_bin_edges.end(), bin_edges.begin(), bin_edges.end());
327 }
328
329 private:
331 DETRAY_HOST void insert_bin_data(
332 vector_type<typename grid_t::bin_type> &bin_data,
333 const typename grid_t::template type<true>::bin_storage &grid_bins) {
334 bin_data.insert(bin_data.end(), grid_bins.begin(), grid_bins.end());
335 }
336
339 template <typename container_t>
340 DETRAY_HOST void insert_bin_data(
341 detray::detail::dynamic_bin_container<bin_t, container_t> &bin_data,
342 const grid_t::template type<true>::bin_storage &grid_bins) {
343 bin_data.append(grid_bins);
344 }
345
347 vector_type<size_type> m_bin_offsets{};
349 bin_container_type m_bins{};
351 edge_offset_container_type m_bin_edge_offsets{};
353 edges_container_type m_bin_edges{};
354};
355
356} // namespace detray
constexpr auto offsets() const -> const vector_type< size_type > &
Definition grid_collection.hpp:236
auto get_data() -> view_type
Definition grid_collection.hpp:281
dmulti_view< dvector_view< size_type >, detail::get_view_t< bin_container_type >, detail::get_view_t< edge_offset_container_type >, detail::get_view_t< edges_container_type > > view_type
Vecmem based grid collection view type.
Definition grid_collection.hpp:112
auto get_data() const -> const_view_type
Definition grid_collection.hpp:289
grid_collection()=default
Make grid collection default constructible: Empty.
constexpr auto size() const noexcept -> dindex
Definition grid_collection.hpp:190
typename multi_axis_t::edges_container_type edges_container_type
Contains all bin edges for all axes.
Definition grid_collection.hpp:55
constexpr auto empty() const noexcept -> bool
Definition grid_collection.hpp:204
grid_collection(coll_view_t &view)
Device-side construction from a vecmem based view type.
Definition grid_collection.hpp:160
typename multi_axis_t::edge_offset_container_type edge_offset_container_type
Offsets into the bin edges container.
Definition grid_collection.hpp:52
grid_collection & operator=(grid_collection &&other) noexcept
Move assignment.
Definition grid_collection.hpp:177
auto operator[](const size_type i) const -> grid_t
Create grid from container pointers - const.
Definition grid_collection.hpp:262
typename grid_t::bin_container_type bin_container_type
Backend storage type for the grid.
Definition grid_collection.hpp:50
constexpr auto axes_storage() const -> const edge_offset_container_type &
Definition grid_collection.hpp:248
grid_collection(vector_type< size_type > &&offs, bin_container_type &&bins, edge_offset_container_type &&edge_offs, edges_container_type &&edges)
Create grid collection from existing data - move.
Definition grid_collection.hpp:149
constexpr void resize(std::size_t) noexcept
Resize the underlying containers.
Definition grid_collection.hpp:211
constexpr auto bin_storage() const -> const bin_container_type &
Definition grid_collection.hpp:242
constexpr auto push_back(const other_grid_t &gr) noexcept(false) -> void
Add a new grid.
Definition grid_collection.hpp:300
constexpr void clear() noexcept
Removes all data from the grid collection containers.
Definition grid_collection.hpp:220
grid_t value_type
Non-owning grid type stored in the collection.
Definition grid_collection.hpp:45
grid_collection(grid_collection &&other) noexcept
Move constructor.
Definition grid_collection.hpp:168
grid_collection(vecmem::memory_resource *resource)
Create empty grid collection from specific vecmem memory resource.
Definition grid_collection.hpp:137
constexpr auto bin_edges_storage() const -> const edges_container_type &
Definition grid_collection.hpp:254
constexpr auto end() const
Definition grid_collection.hpp:200
constexpr void reserve(std::size_t) noexcept
Reserve memory.
Definition grid_collection.hpp:216
dindex size_type
Index type used to address collection elements.
Definition grid_collection.hpp:47
dmulti_buffer< dvector_buffer< size_type >, detail::get_buffer_t< bin_container_type >, detail::get_buffer_t< edge_offset_container_type >, detail::get_buffer_t< edges_container_type > > buffer_type
Vecmem based buffer type.
Definition grid_collection.hpp:125
auto at(const size_type i) const -> grid_t
Create grid from container pointers with range check.
Definition grid_collection.hpp:273
constexpr void insert(Args &&...) noexcept
Insert a number of grids.
Definition grid_collection.hpp:230
typename multi_axis_t::template vector_type< T > vector_type
Container type used for vectors of type T in this collection.
Definition grid_collection.hpp:58
dmulti_view< dvector_view< const size_type >, detail::get_view_t< const bin_container_type >, detail::get_view_t< const edge_offset_container_type >, detail::get_view_t< const edges_container_type > > const_view_type
Vecmem based grid collection view type.
Definition grid_collection.hpp:118
constexpr auto begin() const
Definition grid_collection.hpp:196
A collection of grids that can be moved to device as part of the detector.
Definition grid_collection.hpp:31
An N-dimensional grid for object storage.
Definition grid.hpp:38
typename bin_storage::bin_container_type bin_container_type
Definition grid.hpp:76
static constexpr bool is_owning
Whether this grid owns its axis and bin storage.
Definition grid.hpp:69
static constexpr unsigned int dim
Definition grid.hpp:41
Detray include(s).
Definition array_operators.hpp:18
unsigned int dindex
Default index type.
Definition indexing.hpp:18
constexpr array_t< scalar1_t, 2 > operator*(const array_t< scalar1_t, 2 > &a, scalar2_t s)
Multiply an array-backed vector by a scalar.
Definition array_operators.hpp:26
const auto & get(const detray::tuple< Ts... > &t) noexcept
Get a tuple element by index from a const tuple.
Definition tuple.hpp:152
constexpr bool operator==(const Fastor::Tensor< S, N > &lhs, const Fastor::Tensor< S, N > &rhs)
Compare two Fastor vectors component-wise for exact equality.
Definition fastor.hpp:35