Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-17 08:35:27

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 // Local include(s).
0010 #include "traccc/examples/hip/full_chain_algorithm.hpp"
0011 
0012 #include "hip_error_check.hpp"
0013 #include "traccc/examples/hip/tbb_await.hpp"
0014 
0015 // Project include(s).
0016 #include "traccc/hip/utils/make_magnetic_field.hpp"
0017 #include "traccc/seeding/detail/track_params_estimation_config.hpp"
0018 
0019 // VecMem include(s).
0020 #include <vecmem/utils/hip/copy.hpp>
0021 
0022 // HIP include(s).
0023 #include <hip/hip_runtime_api.h>
0024 
0025 // System include(s).
0026 #include <iostream>
0027 #include <stdexcept>
0028 
0029 namespace traccc::hip {
0030 
0031 namespace {
0032 await_function_type get_await_function(await_strategy await_mode) {
0033   switch (await_mode) {
0034     case await_strategy::sync_event:
0035       return await_sync_event;
0036     case await_strategy::callback:
0037       return tbb_await_callback;
0038     default:
0039       throw std::invalid_argument("Unknown await strategy");
0040   }
0041 }
0042 }  // namespace
0043 
0044 full_chain_algorithm::shared_data::shared_data(
0045     vecmem::memory_resource& host_mr,
0046     const detector_design_description::host& det_descr,
0047     const detector_conditions_description::host& det_cond,
0048     const magnetic_field& field, const host_detector* detector)
0049     : m_device_mr(),
0050       m_field(make_magnetic_field(field)),
0051       m_device_det_descr(
0052           [&]() {
0053             // number of elements in the detector design description
0054             std::vector<unsigned int> sizes(det_descr.size());
0055             for (std::size_t i = 0; i < det_descr.size(); ++i) {
0056               auto this_design = det_descr.at(i);
0057               // now for each element, set the size to the largest size of
0058               // that element across all modules
0059               sizes[i] = std::max(static_cast<unsigned int>(
0060                                       ((this_design.bin_edges_x()).size())),
0061                                   static_cast<unsigned int>(
0062                                       ((this_design.bin_edges_y()).size())));
0063             }
0064             return sizes;
0065           }(),
0066           m_device_mr, &host_mr, vecmem::data::buffer_type::resizable),
0067       m_device_det_cond(
0068           static_cast<detector_conditions_description::buffer::size_type>(
0069               det_cond.size()),
0070           m_device_mr),
0071       m_detector(detector) {
0072   // Copy the detector (description) to the device.
0073   vecmem::hip::copy copy;
0074   copy.setup(m_device_det_descr)->wait();
0075   copy(vecmem::get_data(det_descr), m_device_det_descr)->wait();
0076   copy(vecmem::get_data(det_cond), m_device_det_cond)->wait();
0077   if (m_detector != nullptr) {
0078     m_device_detector =
0079         traccc::buffer_from_host_detector(*m_detector, m_device_mr, copy);
0080   }
0081 }
0082 
0083 full_chain_algorithm::full_chain_algorithm(
0084     vecmem::memory_resource& host_mr,
0085     const clustering_config& clustering_config,
0086     const seedfinder_config& finder_config,
0087     const spacepoint_grid_config& grid_config,
0088     const seedfilter_config& filter_config,
0089     const gbts_seedfinder_config& gbts_config,
0090     const track_params_estimation_config& track_params_estimation_config,
0091     const finding_algorithm::config_type& finding_config,
0092     const fitting_algorithm::config_type& fitting_config,
0093     const shared_data& data, std::unique_ptr<const traccc::Logger> logger,
0094     bool useGBTS, await_strategy await_mode)
0095     : messaging(logger->clone()),
0096       m_host_mr(host_mr),
0097       m_pinned_host_mr(),
0098       m_cached_pinned_host_mr(m_pinned_host_mr),
0099       m_vecmem_stream{},
0100       m_stream{m_vecmem_stream.stream()},
0101       m_device_mr(),
0102       m_cached_device_mr(m_device_mr),
0103       m_copy(m_stream.hipStream()),
0104       m_await_function(get_await_function(await_mode)),
0105       m_field(data.m_field),
0106       m_device_det_descr(data.m_device_det_descr),
0107       m_device_det_cond(data.m_device_det_cond),
0108       m_detector(data.m_detector),
0109       m_device_detector(data.m_device_detector),
0110       m_clusterization({m_cached_device_mr, &m_cached_pinned_host_mr}, m_copy,
0111                        m_stream, clustering_config,
0112                        logger->cloneWithSuffix("ClusteringAlg"),
0113                        m_await_function),
0114       m_measurement_sorting({m_cached_device_mr, &m_cached_pinned_host_mr},
0115                             m_copy, m_stream,
0116                             logger->cloneWithSuffix("MeasSortingAlg")),
0117       m_spacepoint_formation(
0118           {m_cached_device_mr, &m_cached_pinned_host_mr}, m_copy, m_stream,
0119           logger->cloneWithSuffix("SpFormationAlg"), m_await_function),
0120       m_seeding(finder_config, grid_config, filter_config,
0121                 {m_cached_device_mr, &m_cached_pinned_host_mr}, m_copy,
0122                 m_stream, logger->cloneWithSuffix("SeedingAlg"),
0123                 m_await_function),
0124       m_gbts_seeding(
0125           gbts_config, {m_cached_device_mr, &m_cached_pinned_host_mr}, m_copy,
0126           m_stream, logger->cloneWithSuffix("GbtsAlg"), m_await_function),
0127       m_track_parameter_estimation(
0128           track_params_estimation_config,
0129           {m_cached_device_mr, &m_cached_pinned_host_mr}, m_copy, m_stream,
0130           logger->cloneWithSuffix("TrackParEstAlg"), m_await_function),
0131       m_finding(finding_config, {m_cached_device_mr, &m_cached_pinned_host_mr},
0132                 m_copy, m_stream, logger->cloneWithSuffix("TrackFindingAlg"),
0133                 nullptr, m_await_function),
0134       m_fitting(fitting_config, {m_cached_device_mr, &m_cached_pinned_host_mr},
0135                 m_copy, m_stream, logger->cloneWithSuffix("TrackFittingAlg"),
0136                 m_await_function),
0137       usingGBTS(useGBTS) {
0138   // Tell the user what device is being used.
0139   int device = 0;
0140   HIP_ERROR_CHECK(hipGetDevice(&device));
0141   hipDeviceProp_t props;
0142   HIP_ERROR_CHECK(hipGetDeviceProperties(&props, device));
0143   std::cout << "Using HIP device: " << props.name << " [id: " << device
0144             << ", bus: " << props.pciBusID << ", device: " << props.pciDeviceID
0145             << "]" << std::endl;
0146 }
0147 
0148 full_chain_algorithm::~full_chain_algorithm() = default;
0149 
0150 full_chain_algorithm::output_type full_chain_algorithm::operator()(
0151     const edm::silicon_cell_collection::host& cells) const {
0152   // Create device copy of input collections
0153   edm::silicon_cell_collection::buffer cells_buffer(
0154       static_cast<unsigned int>(cells.size()), m_cached_device_mr);
0155   m_copy(vecmem::ignore_event, vecmem::get_data(cells), cells_buffer);
0156 
0157   // Run the clusterization (asynchronously).
0158   const auto unsorted_measurements =
0159       m_clusterization(cells_buffer, m_device_det_descr, m_device_det_cond);
0160   const measurement_sorting_algorithm::output_type measurements =
0161       m_measurement_sorting(unsorted_measurements);
0162 
0163   // If we have a Detray detector, run the seeding, track finding and fitting.
0164   if (m_detector != nullptr) {
0165     // Run the seed-finding (asynchronously).
0166     const spacepoint_formation_algorithm::output_type spacepoints =
0167         m_spacepoint_formation(m_device_detector, measurements);
0168 
0169     triplet_seeding_algorithm::output_type seeds;
0170     if (usingGBTS) {
0171       seeds = m_gbts_seeding(spacepoints, measurements);
0172     } else {
0173       seeds = m_seeding(spacepoints);
0174     }
0175     const seed_parameter_estimation_algorithm::output_type track_params =
0176         m_track_parameter_estimation(m_field, measurements, spacepoints, seeds);
0177 
0178     // Run the track finding (asynchronously).
0179     const finding_algorithm::output_type track_candidates =
0180         m_finding(m_device_detector, m_field, measurements, track_params);
0181 
0182     // Copy a limited amount of result data back to the host.
0183     const auto host_tracks =
0184         m_copy.to(track_candidates.tracks, m_cached_pinned_host_mr, nullptr,
0185                   vecmem::copy::type::device_to_host);
0186     output_type result{m_host_mr};
0187     vecmem::copy host_copy;
0188     host_copy(host_tracks, result)->wait();
0189     return result;
0190 
0191   }
0192   // If not, copy the measurements back to the host, and return a dummy
0193   // object.
0194   else {
0195     // Copy the measurements back to the host.
0196     edm::measurement_collection::host measurements_host(m_host_mr);
0197     m_copy(measurements, measurements_host)->wait();
0198 
0199     // Return an empty object.
0200     return output_type{m_host_mr};
0201   }
0202 }
0203 
0204 bound_track_parameters_collection_types::host full_chain_algorithm::seeding(
0205     const edm::silicon_cell_collection::host& cells) const {
0206   // Create device copy of input collections
0207   edm::silicon_cell_collection::buffer cells_buffer(
0208       static_cast<unsigned int>(cells.size()), m_cached_device_mr);
0209   m_copy(vecmem::ignore_event, vecmem::get_data(cells), cells_buffer);
0210 
0211   // Run the clusterization (asynchronously).
0212   const auto unsorted_measurements =
0213       m_clusterization(cells_buffer, m_device_det_descr, m_device_det_cond);
0214   const measurement_sorting_algorithm::output_type measurements =
0215       m_measurement_sorting(unsorted_measurements);
0216 
0217   // If we have a Detray detector, run the seeding, track finding and fitting.
0218   if (m_detector != nullptr) {
0219     // Run the seed-finding (asynchronously).
0220     const spacepoint_formation_algorithm::output_type spacepoints =
0221         m_spacepoint_formation(m_device_detector, measurements);
0222 
0223     triplet_seeding_algorithm::output_type seeds;
0224     if (usingGBTS) {
0225       seeds = m_gbts_seeding(spacepoints, measurements);
0226     } else {
0227       seeds = m_seeding(spacepoints);
0228     }
0229     const seed_parameter_estimation_algorithm::output_type track_params =
0230         m_track_parameter_estimation(m_field, measurements, spacepoints, seeds);
0231 
0232     // Copy a limited amount of result data back to the host.
0233     const auto host_seeds = m_copy.to(track_params, m_cached_pinned_host_mr,
0234                                       vecmem::copy::type::device_to_host);
0235     bound_track_parameters_collection_types::host result{&m_host_mr};
0236     vecmem::copy host_copy;
0237     host_copy(host_seeds, result)->wait();
0238     return result;
0239 
0240   }
0241   // If not, copy the measurements back to the host, and return a dummy
0242   // object.
0243   else {
0244     // Copy the measurements back to the host.
0245     edm::measurement_collection::host measurements_host(m_host_mr);
0246     m_copy(measurements, measurements_host)->wait();
0247 
0248     // Return an empty object.
0249     return {};
0250   }
0251 }
0252 
0253 }  // namespace traccc::hip