Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-26 08:22:01

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2020-2026 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 #pragma once
0009 
0010 // Project include(s).
0011 #include "traccc/definitions/qualifiers.hpp"
0012 
0013 // Detray include(s)
0014 #include <detray/definitions/algorithms.hpp>
0015 #include <detray/utils/invalid_values.hpp>
0016 
0017 // VecMem include(s).
0018 #include <vecmem/containers/data/jagged_vector_buffer.hpp>
0019 #include <vecmem/containers/data/jagged_vector_view.hpp>
0020 #include <vecmem/containers/data/vector_buffer.hpp>
0021 #include <vecmem/containers/data/vector_view.hpp>
0022 #include <vecmem/containers/device_vector.hpp>
0023 #include <vecmem/containers/jagged_device_vector.hpp>
0024 
0025 // System include(s).
0026 #include <algorithm>
0027 #include <limits>
0028 
0029 namespace traccc {
0030 
0031 /** A replace populator that swaps whatever current value in the
0032  * bin with the new one.
0033  *
0034  * @tparam value_t the type of a single stored object
0035  *
0036  * @note bare_value and store_value are identicial in this case
0037  **/
0038 template <template <typename...> class vector_t = vecmem::vector,
0039           template <typename...> class jagged_vector_t = vecmem::jagged_vector,
0040           template <typename, std::size_t> class array_t = std::array,
0041           typename value_t = unsigned int, bool kSORT = false,
0042           unsigned int kDIM = 1u>
0043 struct replace_populator {
0044   DETRAY_HOST_DEVICE
0045   explicit replace_populator(
0046       const value_t invalid = detray::detail::invalid_value<value_t>())
0047       : m_invalid(invalid) {}
0048 
0049   value_t m_invalid;
0050 
0051   using bare_value = value_t;
0052   using store_value = value_t;
0053   using serialized_storage = vector_t<store_value>;
0054 
0055   using vector_view_type = vecmem::data::vector_view<store_value>;
0056   using const_vector_view_type = vecmem::data::vector_view<const store_value>;
0057   using vector_data_type = vecmem::data::vector_view<store_value>;
0058   using const_vector_data_type = vecmem::data::vector_view<const store_value>;
0059   using vector_buffer_type = vecmem::data::vector_buffer<store_value>;
0060   using buffer_size_type = typename vector_view_type::size_type;
0061 
0062   /** Swap the stored value with a new bare value
0063    *
0064    * @param stored the stored value for the population
0065    * @param bvalue the new value to be added
0066    **/
0067   DETRAY_HOST_DEVICE
0068   void operator()(store_value &stored, bare_value &&bvalue) const {
0069     stored = std::move(bvalue);
0070   }
0071 
0072   /** Create a sequence of bare values, independent of the store_value.
0073    *
0074    * @param stored the stored value
0075    *
0076    * @return a sequence of bare values
0077    */
0078   DETRAY_HOST_DEVICE
0079   vector_t<bare_value> sequence(store_value &stored) const {
0080     if (stored != m_invalid) {
0081       return {stored};
0082     }
0083     return {};
0084   }
0085 
0086   /** Shift operation for unified memory block
0087    *
0088    * @param stored the stored value
0089    * @param offset is the shift offset
0090    *
0091    **/
0092   DETRAY_HOST_DEVICE
0093   void shift(store_value &stored, const bare_value &offset) const {
0094     stored += offset;
0095   }
0096 
0097   /** Return an initialized bin value
0098    */
0099   DETRAY_HOST_DEVICE
0100   store_value init() const { return m_invalid; }
0101 };
0102 
0103 /** A complete populator that adds values to the internal
0104  * store array until it is completed, ignored afterwards.
0105  *
0106  * @tparam kDIM the dimension of the underlying stored array
0107  * @tparam kSORT a sorting flag
0108  * @tparam value_t the type of a single stored object
0109  * @tparam m_invalid the chosen invalid type
0110  *
0111  * @note bare_value and store_value are different in this case
0112  **/
0113 
0114 template <template <typename...> class vector_t = vecmem::vector,
0115           template <typename...> class jagged_vector_t = vecmem::jagged_vector,
0116           template <typename, std::size_t> class array_t = std::array,
0117           typename value_t = unsigned int, bool kSORT = false,
0118           unsigned int kDIM = 1u>
0119 struct complete_populator {
0120   DETRAY_HOST_DEVICE
0121   explicit complete_populator(
0122       const value_t invalid = detray::detail::invalid_value<value_t>())
0123       : m_invalid(invalid) {}
0124 
0125   value_t m_invalid;
0126 
0127   using bare_value = value_t;
0128   using store_value = array_t<bare_value, kDIM>;
0129   using serialized_storage = vector_t<store_value>;
0130 
0131   using vector_view_type = vecmem::data::vector_view<store_value>;
0132   using const_vector_view_type = vecmem::data::vector_view<const store_value>;
0133   using vector_data_type = vecmem::data::vector_view<store_value>;
0134   using const_vector_data_type = vecmem::data::vector_view<const store_value>;
0135   using vector_buffer_type = vecmem::data::vector_buffer<store_value>;
0136   using buffer_size_type = typename vector_view_type::size_type;
0137 
0138   /** Complete the stored value with a new bare value - for host
0139    *
0140    * @param stored the stored value for the population
0141    * @param bvalue the new value to be added
0142    **/
0143   DETRAY_HOST_DEVICE
0144   void operator()(store_value &stored, bare_value &&bvalue) const {
0145     for (auto &val : stored) {
0146       if (val == m_invalid) {
0147         val = std::move(bvalue);
0148         break;
0149       }
0150     }
0151     if constexpr (kSORT) {
0152       detray::sequential_sort(stored.begin(), stored.end());
0153     }
0154   }
0155 
0156   /** Create a sequence of bare values, independent of the store_value.
0157    *
0158    * @param stored the stored value
0159    *
0160    * @return a sequence of bare values, @note it will ignore invalid entries
0161    */
0162   DETRAY_HOST_DEVICE
0163   vector_t<bare_value> sequence(store_value &stored) const {
0164     vector_t<bare_value> s;
0165     s.reserve(kDIM);
0166     for (const auto &val : stored) {
0167       if (val != m_invalid) {
0168         s.push_back(val);
0169       }
0170     }
0171     return s;
0172   }
0173 
0174   /** Shift operation for unified memory block
0175    *
0176    * @param stored the stored value
0177    * @param offset is the shift offset
0178    *
0179    **/
0180   DETRAY_HOST_DEVICE
0181   void shift(store_value &stored, const bare_value &offset) const {
0182     std::ranges::for_each(stored, [&](auto &d) { d += offset; });
0183   }
0184 
0185   /** Return an initialized bin value
0186    **/
0187   DETRAY_HOST_DEVICE
0188   store_value init() const {
0189     store_value init_bin;
0190     for (auto &val : init_bin) {
0191       val = m_invalid;
0192     }
0193     return init_bin;
0194   }
0195 };
0196 
0197 /** An attach populator that adds the new value to the
0198  *
0199  * @tparam kSORT the sorting directive
0200  * @tparam value_t the type of a single stored object
0201  *
0202  * @note bare_value and store_value are identicial in this case
0203  **/
0204 template <template <typename...> class vector_t = vecmem::vector,
0205           template <typename...> class jagged_vector_t = vecmem::jagged_vector,
0206           template <typename, std::size_t> class array_t = std::array,
0207           typename value_t = unsigned int, bool kSORT = false,
0208           unsigned int kDIM = 1u>
0209 struct attach_populator {
0210   DETRAY_HOST_DEVICE
0211   explicit attach_populator(
0212       const value_t invalid = detray::detail::invalid_value<value_t>())
0213       : m_invalid(invalid) {}
0214 
0215   value_t m_invalid;
0216 
0217   using bare_value = value_t;
0218   using store_value = vector_t<bare_value>;
0219   using serialized_storage = jagged_vector_t<bare_value>;
0220 
0221   using vector_view_type = vecmem::data::jagged_vector_view<bare_value>;
0222   using const_vector_view_type =
0223       vecmem::data::jagged_vector_view<const bare_value>;
0224   using vector_data_type = vecmem::data::jagged_vector_data<bare_value>;
0225   using const_vector_data_type =
0226       vecmem::data::jagged_vector_data<const bare_value>;
0227   using vector_buffer_type = vecmem::data::jagged_vector_buffer<bare_value>;
0228   using buffer_size_type = std::vector<typename vector_view_type::size_type>;
0229 
0230   /** Add a new value to the stored value - for host vector
0231    *
0232    * @param stored the stored value for the population
0233    * @param bvalue the new value to be added
0234    **/
0235   DETRAY_HOST
0236   void operator()(store_value &stored, bare_value &&bvalue) const {
0237     stored.push_back(std::move(bvalue));
0238     if (kSORT) {
0239       std::ranges::sort(stored);
0240     }
0241   }
0242 
0243   /** Add a new value to the stored value - for device vector
0244    *
0245    * @param stored the stored value for the population
0246    * @param bvalue the new value to be added
0247    **/
0248 #if defined(__CUDACC__)  // to resolve ambiguoty from host side
0249   DETRAY_DEVICE
0250   void operator()(store_value stored, bare_value &&bvalue) const {
0251     stored.push_back(std::move(bvalue));
0252   }
0253 #endif
0254 
0255   /** Create a sequence of bare values, independent of the store_value.
0256    *
0257    * @param stored the stored value
0258    *
0259    * @return a sequence of bare values
0260    */
0261   DETRAY_HOST_DEVICE
0262   vector_t<bare_value> sequence(store_value &stored) const { return stored; }
0263 
0264   /** Shift operation for unified memory block
0265    *
0266    * @param stored the stored value
0267    * @param offset is the shift offset
0268    *
0269    **/
0270   DETRAY_HOST_DEVICE
0271   void shift(store_value &stored, const bare_value &offset) const {
0272     std::ranges::for_each(stored, [&](auto &d) { d += offset; });
0273   }
0274 
0275   /** Return an initialized bin value
0276    **/
0277   DETRAY_HOST_DEVICE
0278   store_value init() const { return {}; }
0279 };
0280 
0281 }  // namespace traccc