Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2022-2026 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 // Local include(s).
0009 #include "traccc/examples/sycl/full_chain_algorithm.hpp"
0010 
0011 // Project include(s).
0012 #include "traccc/sycl/utils/make_magnetic_field.hpp"
0013 
0014 namespace traccc::sycl {
0015 namespace details {
0016 
0017 struct full_chain_algorithm_data {
0018   /// The vecmem SYCL queue object
0019   vecmem::sycl::queue_wrapper m_queue;
0020   /// Wrapper around the SYCL queue object
0021   queue_wrapper m_queue_wrapper{m_queue.queue()};
0022 };
0023 
0024 }  // namespace details
0025 
0026 full_chain_algorithm::full_chain_algorithm(
0027     vecmem::memory_resource& host_mr,
0028     const clustering_config& clustering_config,
0029     const seedfinder_config& finder_config,
0030     const spacepoint_grid_config& grid_config,
0031     const seedfilter_config& filter_config,
0032     const gbts_seedfinder_config& gbts_config,
0033     const track_params_estimation_config& track_params_estimation_config,
0034     const finding_algorithm::config_type& finding_config,
0035     const fitting_algorithm::config_type& fitting_config,
0036     const detector_design_description::host& det_descr,
0037     const detector_conditions_description::host& det_cond,
0038     const magnetic_field& field, host_detector* detector,
0039     std::unique_ptr<const traccc::Logger> log, bool useGBTS)
0040     : messaging(log->clone()),
0041       m_data(std::make_unique<details::full_chain_algorithm_data>()),
0042       m_host_mr(host_mr),
0043       m_pinned_host_mr(m_data->m_queue),
0044       m_cached_pinned_host_mr(m_pinned_host_mr),
0045       m_device_mr{m_data->m_queue},
0046       m_cached_device_mr{m_device_mr},
0047       m_copy{m_data->m_queue},
0048       m_field_vec{0.f, 0.f, finder_config.bFieldInZ},
0049       m_field{sycl::make_magnetic_field(field, m_data->m_queue_wrapper)},
0050       m_det_descr(det_descr),
0051       m_det_cond(det_cond),
0052       m_device_det_descr(
0053           [&]() {
0054             // number of elements in the detector design description
0055             std::vector<unsigned int> sizes(det_descr.size());
0056             for (std::size_t i = 0; i < det_descr.size(); ++i) {
0057               auto this_design = det_descr.at(i);
0058               // now for each element, set the size to the largest size of
0059               // that element across all modules
0060               sizes[i] = std::max(static_cast<unsigned int>(
0061                                       ((this_design.bin_edges_x()).size())),
0062                                   static_cast<unsigned int>(
0063                                       ((this_design.bin_edges_y()).size())));
0064             }
0065             return sizes;
0066           }(),
0067           m_cached_device_mr, &m_cached_pinned_host_mr,
0068           vecmem::data::buffer_type::resizable),
0069       m_device_det_cond(
0070           static_cast<detector_conditions_description::buffer::size_type>(
0071               m_det_cond.get().size()),
0072           m_cached_device_mr),
0073       m_detector(detector),
0074       m_device_detector{},
0075       m_clusterization{{m_cached_device_mr, &m_cached_pinned_host_mr},
0076                        m_copy,
0077                        m_data->m_queue_wrapper,
0078                        clustering_config,
0079                        log->clone("ClusteringAlg")},
0080       m_measurement_sorting({m_cached_device_mr, &m_cached_pinned_host_mr},
0081                             m_copy, m_data->m_queue_wrapper,
0082                             log->clone("MeasSortingAlg")),
0083       m_spacepoint_formation{{m_cached_device_mr, &m_cached_pinned_host_mr},
0084                              m_copy,
0085                              m_data->m_queue_wrapper,
0086                              log->clone("SpFormationAlg")},
0087       m_seeding{finder_config,
0088                 grid_config,
0089                 filter_config,
0090                 {m_cached_device_mr, &m_cached_pinned_host_mr},
0091                 m_copy,
0092                 m_data->m_queue_wrapper,
0093                 log->clone("SeedingAlg")},
0094       m_gbts_seeding{gbts_config,
0095                      {m_cached_device_mr, &m_cached_pinned_host_mr},
0096                      m_copy,
0097                      m_data->m_queue_wrapper,
0098                      log->clone("GbtsAlg")},
0099       m_track_parameter_estimation{
0100           track_params_estimation_config,
0101           {m_cached_device_mr, &m_cached_pinned_host_mr},
0102           m_copy,
0103           m_data->m_queue_wrapper,
0104           log->clone("TrackParEstAlg")},
0105       m_finding{finding_config,
0106                 {m_cached_device_mr, &m_cached_pinned_host_mr},
0107                 m_copy,
0108                 m_data->m_queue_wrapper,
0109                 log->clone("TrackFindingAlg")},
0110       m_fitting{fitting_config,
0111                 {m_cached_device_mr, &m_cached_pinned_host_mr},
0112                 m_copy,
0113                 m_data->m_queue_wrapper,
0114                 log->clone("TrackFittingAlg")},
0115       m_clustering_config(clustering_config),
0116       m_finder_config(finder_config),
0117       m_grid_config(grid_config),
0118       m_filter_config(filter_config),
0119       m_gbts_config(gbts_config),
0120       m_track_params_estimation_config(track_params_estimation_config),
0121       m_finding_config(finding_config),
0122       m_fitting_config(fitting_config),
0123       usingGBTS(useGBTS) {
0124   // Tell the user what device is being used.
0125   TRACCC_INFO("Using SYCL device: " << m_data->m_queue.device_name());
0126 
0127   // Copy the detector (description) to the device.
0128   m_copy.setup(m_device_det_descr)->wait();
0129   m_copy(vecmem::get_data(m_det_descr.get()), m_device_det_descr)->wait();
0130   m_copy(vecmem::get_data(m_det_cond.get()), m_device_det_cond)->wait();
0131   if (m_detector != nullptr) {
0132     m_device_detector =
0133         traccc::buffer_from_host_detector(*m_detector, m_device_mr, m_copy);
0134   }
0135 }
0136 
0137 full_chain_algorithm::full_chain_algorithm(const full_chain_algorithm& parent)
0138     : messaging(parent.logger().clone()),
0139       m_data(std::make_unique<details::full_chain_algorithm_data>()),
0140       m_host_mr(parent.m_host_mr),
0141       m_pinned_host_mr(m_data->m_queue),
0142       m_cached_pinned_host_mr(m_pinned_host_mr),
0143       m_device_mr{m_data->m_queue},
0144       m_cached_device_mr{m_device_mr},
0145       m_copy{m_data->m_queue},
0146       m_field_vec{parent.m_field_vec},
0147       m_field{parent.m_field},
0148       m_det_descr(parent.m_det_descr),
0149       m_det_cond(parent.m_det_cond),
0150       m_device_det_descr(
0151           [&]() {
0152             // number of elements in the detector design description
0153             std::vector<unsigned int> sizes(parent.m_det_descr.get().size());
0154             for (std::size_t i = 0; i < parent.m_det_descr.get().size(); ++i) {
0155               auto this_design = parent.m_det_descr.get().at(i);
0156               // now for each element, set the size to the largest size of
0157               // that element across all modules
0158               sizes[i] = std::max(static_cast<unsigned int>(
0159                                       ((this_design.bin_edges_x()).size())),
0160                                   static_cast<unsigned int>(
0161                                       ((this_design.bin_edges_y()).size())));
0162             }
0163             return sizes;
0164           }(),
0165           m_cached_device_mr, &m_cached_pinned_host_mr,
0166           vecmem::data::buffer_type::resizable),
0167       m_device_det_cond(
0168           static_cast<detector_conditions_description::buffer::size_type>(
0169               m_det_cond.get().size()),
0170           m_cached_device_mr),
0171       m_detector(parent.m_detector),
0172       m_device_detector{},
0173       m_clusterization{{m_cached_device_mr, &m_cached_pinned_host_mr},
0174                        m_copy,
0175                        m_data->m_queue_wrapper,
0176                        parent.m_clustering_config,
0177                        parent.logger().clone("ClusteringAlg")},
0178       m_measurement_sorting({m_cached_device_mr, &m_cached_pinned_host_mr},
0179                             m_copy, m_data->m_queue_wrapper,
0180                             parent.logger().clone("MeasSortingAlg")),
0181       m_spacepoint_formation{{m_cached_device_mr, &m_cached_pinned_host_mr},
0182                              m_copy,
0183                              m_data->m_queue_wrapper,
0184                              parent.logger().clone("SpFormationAlg")},
0185       m_seeding{parent.m_finder_config,
0186                 parent.m_grid_config,
0187                 parent.m_filter_config,
0188                 {m_cached_device_mr, &m_cached_pinned_host_mr},
0189                 m_copy,
0190                 m_data->m_queue_wrapper,
0191                 parent.logger().clone("SeedingAlg")},
0192       m_gbts_seeding{parent.m_gbts_config,
0193                      {m_cached_device_mr, &m_cached_pinned_host_mr},
0194                      m_copy,
0195                      m_data->m_queue_wrapper,
0196                      parent.logger().clone("GbtsAlg")},
0197       m_track_parameter_estimation{
0198           parent.m_track_params_estimation_config,
0199           {m_cached_device_mr, &m_cached_pinned_host_mr},
0200           m_copy,
0201           m_data->m_queue_wrapper,
0202           parent.logger().clone("TrackParEstAlg")},
0203       m_finding{parent.m_finding_config,
0204                 {m_cached_device_mr, &m_cached_pinned_host_mr},
0205                 m_copy,
0206                 m_data->m_queue_wrapper,
0207                 parent.logger().clone("FindingAlg")},
0208       m_fitting{parent.m_fitting_config,
0209                 {m_cached_device_mr, &m_cached_pinned_host_mr},
0210                 m_copy,
0211                 m_data->m_queue_wrapper,
0212                 parent.logger().clone("FittingAlg")},
0213       m_clustering_config(parent.m_clustering_config),
0214       m_finder_config(parent.m_finder_config),
0215       m_grid_config(parent.m_grid_config),
0216       m_filter_config(parent.m_filter_config),
0217       m_gbts_config(parent.m_gbts_config),
0218       m_track_params_estimation_config(parent.m_track_params_estimation_config),
0219       m_finding_config(parent.m_finding_config),
0220       m_fitting_config(parent.m_fitting_config),
0221       usingGBTS(parent.usingGBTS) {
0222   // Copy the detector (description) to the device.
0223   m_copy.setup(m_device_det_descr)->wait();
0224   m_copy(vecmem::get_data(m_det_descr.get()), m_device_det_descr)->wait();
0225   m_copy(vecmem::get_data(m_det_cond.get()), m_device_det_cond)->wait();
0226   if (m_detector != nullptr) {
0227     m_device_detector =
0228         traccc::buffer_from_host_detector(*m_detector, m_device_mr, m_copy);
0229   }
0230 }
0231 
0232 full_chain_algorithm::~full_chain_algorithm() = default;
0233 
0234 full_chain_algorithm::output_type full_chain_algorithm::operator()(
0235     const edm::silicon_cell_collection::host& cells) const {
0236   // Create device copy of input collections
0237   edm::silicon_cell_collection::buffer cells_buffer{
0238       static_cast<unsigned int>(cells.size()), m_cached_device_mr};
0239   m_copy(vecmem::get_data(cells), cells_buffer)->wait();
0240 
0241   // Execute the algorithms.
0242   const auto unsorted_measurements =
0243       m_clusterization(cells_buffer, m_device_det_descr, m_device_det_cond);
0244   const measurement_sorting_algorithm::output_type measurements =
0245       m_measurement_sorting(unsorted_measurements);
0246 
0247   // If we have a Detray detector, run the seeding, track
0248   // finding and fitting.
0249   if (m_detector != nullptr) {
0250     // Run the seed-finding.
0251     const spacepoint_formation_algorithm::output_type spacepoints =
0252         m_spacepoint_formation(m_device_detector, measurements);
0253     triplet_seeding_algorithm::output_type seeds;
0254     if (usingGBTS) {
0255       seeds = m_gbts_seeding(spacepoints, measurements);
0256     } else {
0257       seeds = m_seeding(spacepoints);
0258     }
0259     const seed_parameter_estimation_algorithm::output_type track_params =
0260         m_track_parameter_estimation(m_field, measurements, spacepoints, seeds);
0261 
0262     // Run the track finding.
0263     const finding_algorithm::output_type track_candidates =
0264         m_finding(m_device_detector, m_field, measurements, track_params);
0265 
0266     // Run the track fitting (asynchronously).
0267     const fitting_algorithm::output_type track_states =
0268         m_fitting(m_device_detector, m_field, track_candidates);
0269 
0270     // Copy a limited amount of result data back to the host.
0271     const auto host_tracks =
0272         m_copy.to(track_states.tracks, m_cached_pinned_host_mr, nullptr,
0273                   vecmem::copy::type::device_to_host);
0274     output_type result{m_host_mr.get()};
0275     vecmem::copy host_copy;
0276     host_copy(host_tracks, result)->wait();
0277     return result;
0278   }
0279   // If not, copy the measurements back to the host, and return
0280   // a dummy object.
0281   else {
0282     // Copy the measurements back to the host.
0283     edm::measurement_collection::host measurements_host(m_host_mr.get());
0284     m_copy(measurements, measurements_host)->wait();
0285 
0286     // Return an empty object.
0287     return output_type{m_host_mr.get()};
0288   }
0289 }
0290 
0291 bound_track_parameters_collection_types::host full_chain_algorithm::seeding(
0292     const edm::silicon_cell_collection::host& cells) const {
0293   // Create device copy of input collections
0294   edm::silicon_cell_collection::buffer cells_buffer{
0295       static_cast<unsigned int>(cells.size()), m_cached_device_mr};
0296   m_copy(vecmem::get_data(cells), cells_buffer)->wait();
0297 
0298   // Execute the algorithms.
0299   const auto unsorted_measurements =
0300       m_clusterization(cells_buffer, m_device_det_descr, m_device_det_cond);
0301   const measurement_sorting_algorithm::output_type measurements =
0302       m_measurement_sorting(unsorted_measurements);
0303 
0304   // If we have a Detray detector, run the seeding, track
0305   // finding and fitting.
0306   if (m_detector != nullptr) {
0307     // Run the seed-finding.
0308     const spacepoint_formation_algorithm::output_type spacepoints =
0309         m_spacepoint_formation(m_device_detector, measurements);
0310     triplet_seeding_algorithm::output_type seeds;
0311     if (usingGBTS) {
0312       seeds = m_gbts_seeding(spacepoints, measurements);
0313     } else {
0314       seeds = m_seeding(spacepoints);
0315     }
0316     const seed_parameter_estimation_algorithm::output_type track_params =
0317         m_track_parameter_estimation(m_field, measurements, spacepoints, seeds);
0318 
0319     // Copy a limited amount of result data back to the host.
0320     const auto host_seeds = m_copy.to(track_params, m_cached_pinned_host_mr,
0321                                       vecmem::copy::type::device_to_host);
0322     bound_track_parameters_collection_types::host result{&(m_host_mr.get())};
0323     vecmem::copy host_copy;
0324     host_copy(host_seeds, result)->wait();
0325     return result;
0326   }
0327   // If not, copy the measurements back to the host, and return
0328   // a dummy object.
0329   else {
0330     // Copy the measurements back to the host.
0331     edm::measurement_collection::host measurements_host(m_host_mr.get());
0332     m_copy(measurements, measurements_host)->wait();
0333 
0334     // Return an empty object.
0335     return {};
0336   }
0337 }
0338 
0339 }  // namespace traccc::sycl