Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2021-2026 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 // core
0009 #include "traccc/geometry/detector.hpp"
0010 #include "traccc/geometry/detector_buffer.hpp"
0011 #include "traccc/geometry/host_detector.hpp"
0012 #include "traccc/utils/propagation.hpp"
0013 
0014 // algorithms
0015 #include "traccc/gbts_seeding/gbts_seeding_config.hpp"
0016 #include "traccc/seeding/seeding_algorithm.hpp"
0017 #include "traccc/seeding/track_params_estimation.hpp"
0018 #include "traccc/sycl/gbts_seeding/gbts_seeding_algorithm.hpp"
0019 #include "traccc/sycl/seeding/seed_parameter_estimation_algorithm.hpp"
0020 #include "traccc/sycl/seeding/triplet_seeding_algorithm.hpp"
0021 #include "traccc/sycl/utils/make_magnetic_field.hpp"
0022 
0023 // io
0024 #include "traccc/io/read_detector.hpp"
0025 #include "traccc/io/read_spacepoints.hpp"
0026 #include "traccc/io/utils.hpp"
0027 
0028 // performance
0029 #include "traccc/efficiency/seeding_performance_writer.hpp"
0030 #include "traccc/performance/collection_comparator.hpp"
0031 #include "traccc/performance/soa_comparator.hpp"
0032 #include "traccc/performance/timer.hpp"
0033 
0034 // options
0035 #include "traccc/options/accelerator.hpp"
0036 #include "traccc/options/detector.hpp"
0037 #include "traccc/options/input_data.hpp"
0038 #include "traccc/options/magnetic_field.hpp"
0039 #include "traccc/options/performance.hpp"
0040 #include "traccc/options/program_options.hpp"
0041 #include "traccc/options/track_gbts_seeding.hpp"
0042 #include "traccc/options/track_seeding.hpp"
0043 
0044 // examples
0045 #include "traccc/examples/make_magnetic_field.hpp"
0046 
0047 // Vecmem include(s)
0048 #include <vecmem/memory/host_memory_resource.hpp>
0049 #include <vecmem/memory/sycl/device_memory_resource.hpp>
0050 #include <vecmem/memory/sycl/host_memory_resource.hpp>
0051 #include <vecmem/memory/sycl/shared_memory_resource.hpp>
0052 #include <vecmem/utils/sycl/async_copy.hpp>
0053 
0054 // System include(s).
0055 #include <exception>
0056 #include <iomanip>
0057 #include <iostream>
0058 
0059 int seq_run(const traccc::opts::detector& detector_opts,
0060             const traccc::opts::magnetic_field& bfield_opts,
0061             const traccc::opts::track_seeding& seeding_opts,
0062             const traccc::opts::track_gbts_seeding& seeding_gbts_opts,
0063             const traccc::opts::input_data& input_opts,
0064             const traccc::opts::performance& performance_opts,
0065             const traccc::opts::accelerator& accelerator_opts,
0066             std::unique_ptr<const traccc::Logger> ilogger, bool usingGBTS) {
0067   TRACCC_LOCAL_LOGGER(std::move(ilogger));
0068 
0069   // Creating sycl queue object
0070   vecmem::sycl::queue_wrapper vecmem_queue;
0071   traccc::sycl::queue_wrapper traccc_queue{vecmem_queue.queue()};
0072   TRACCC_INFO("Running on device: " << vecmem_queue.device_name());
0073 
0074   // Memory resources used by the application.
0075   vecmem::host_memory_resource host_mr;
0076   vecmem::sycl::host_memory_resource sycl_host_mr{vecmem_queue};
0077   vecmem::sycl::shared_memory_resource shared_mr{vecmem_queue};
0078   vecmem::sycl::device_memory_resource device_mr{vecmem_queue};
0079   traccc::memory_resource mr{device_mr, &sycl_host_mr};
0080 
0081   // Copy object for asynchronous data transfers.
0082   vecmem::sycl::async_copy copy{vecmem_queue};
0083 
0084   // Performance writer
0085   traccc::seeding_performance_writer sd_performance_writer(
0086       traccc::seeding_performance_writer::config{},
0087       logger().clone("SeedingPerformanceWriter"));
0088 
0089   // Output stats
0090   uint64_t n_spacepoints = 0;
0091   uint64_t n_seeds = 0;
0092   uint64_t n_seeds_sycl = 0;
0093 
0094   /*****************************
0095    * Build a geometry
0096    *****************************/
0097 
0098   // Construct a Detray detector object, if supported by the configuration.
0099   traccc::host_detector host_det;
0100   traccc::io::read_detector(host_det, host_mr, detector_opts.detector_file,
0101                             detector_opts.material_file,
0102                             detector_opts.grid_file);
0103 
0104   const traccc::detector_buffer detector_buffer =
0105       traccc::buffer_from_host_detector(host_det, device_mr, copy);
0106   vecmem_queue.synchronize();
0107 
0108   const traccc::vector3 field_vec(seeding_opts);
0109   const auto host_field = traccc::details::make_magnetic_field(bfield_opts);
0110   const auto device_field =
0111       traccc::sycl::make_magnetic_field(host_field, traccc_queue);
0112 
0113   // GBTS seeding configuration
0114   const traccc::gbts_seedfinder_config gbts_config(seeding_gbts_opts);
0115 
0116   // Seeding algorithm
0117   const traccc::seedfinder_config seedfinder_config(seeding_opts);
0118   const traccc::seedfilter_config seedfilter_config(seeding_opts);
0119   const traccc::spacepoint_grid_config spacepoint_grid_config(seeding_opts);
0120   traccc::host::seeding_algorithm sa(seedfinder_config, spacepoint_grid_config,
0121                                      seedfilter_config, host_mr,
0122                                      logger().clone("HostSeedingAlg"));
0123   const traccc::track_params_estimation_config track_params_estimation_config;
0124   traccc::host::track_params_estimation tp(
0125       track_params_estimation_config, host_mr,
0126       logger().clone("HostTrackParEstAlg"));
0127 
0128   traccc::sycl::triplet_seeding_algorithm sa_sycl{
0129       seedfinder_config,
0130       spacepoint_grid_config,
0131       seedfilter_config,
0132       mr,
0133       copy,
0134       traccc_queue,
0135       logger().clone("SyclSeedingAlg")};
0136   traccc::sycl::gbts_seeding_algorithm gbts_sa_sycl(
0137       gbts_config, mr, copy, traccc_queue,
0138       logger().clone("SyclGbtsSeedingAlg"));
0139   traccc::sycl::seed_parameter_estimation_algorithm tp_sycl{
0140       track_params_estimation_config, mr, copy, traccc_queue,
0141       logger().clone("SyclTrackParEstAlg")};
0142 
0143   traccc::performance::timing_info elapsedTimes;
0144 
0145   // Loop over events
0146   for (std::size_t event = input_opts.skip;
0147        event < input_opts.events + input_opts.skip; ++event) {
0148     // Instantiate host containers/collections
0149     traccc::edm::measurement_collection::host measurements_per_event{host_mr};
0150     traccc::edm::spacepoint_collection::host spacepoints_per_event{host_mr};
0151     traccc::host::seeding_algorithm::output_type seeds{host_mr};
0152     traccc::host::track_params_estimation::output_type params{&host_mr};
0153 
0154     // Instantiate sycl containers/collections
0155     traccc::edm::seed_collection::buffer seeds_sycl_buffer;
0156     traccc::bound_track_parameters_collection_types::buffer params_sycl_buffer(
0157         0, *mr.host);
0158 
0159     {  // Start measuring wall time
0160       traccc::performance::timer wall_t("Wall time", elapsedTimes);
0161 
0162       /*-----------------
0163         hit file reading
0164         -----------------*/
0165 
0166       {
0167         traccc::performance::timer t("Hit reading  (cpu)", elapsedTimes);
0168         // Read the hits from the relevant event file
0169         traccc::io::read_spacepoints(
0170             spacepoints_per_event, measurements_per_event, event,
0171             input_opts.directory,
0172             (input_opts.use_acts_geom_source ? &host_det : nullptr), nullptr,
0173             nullptr, input_opts.format);
0174 
0175       }  // stop measuring hit reading timer
0176 
0177       /*----------------------------
0178            Seeding algorithm
0179         ----------------------------*/
0180 
0181       /// SYCL
0182 
0183       // Copy the measurements and spacepoint and module data to the
0184       // device.
0185       traccc::edm::measurement_collection::buffer measurements_sycl_buffer(
0186           static_cast<unsigned int>(measurements_per_event.size()), mr.main);
0187       copy(vecmem::get_data(measurements_per_event), measurements_sycl_buffer)
0188           ->wait();
0189       traccc::edm::spacepoint_collection::buffer spacepoints_sycl_buffer(
0190           static_cast<unsigned int>(spacepoints_per_event.size()), mr.main);
0191       copy(vecmem::get_data(spacepoints_per_event), spacepoints_sycl_buffer)
0192           ->wait();
0193 
0194       {
0195         traccc::performance::timer t("Seeding (sycl)", elapsedTimes);
0196         // Reconstruct the spacepoints into seeds.
0197         if (usingGBTS) {
0198           seeds_sycl_buffer =
0199               gbts_sa_sycl(spacepoints_sycl_buffer, measurements_sycl_buffer);
0200         } else {
0201           seeds_sycl_buffer = sa_sycl(spacepoints_sycl_buffer);
0202         }
0203         vecmem_queue.synchronize();
0204       }  // stop measuring seeding sycl timer
0205 
0206       // CPU
0207 
0208       if (accelerator_opts.compare_with_cpu) {
0209         traccc::performance::timer t("Seeding  (cpu)", elapsedTimes);
0210         seeds = sa(vecmem::get_data(spacepoints_per_event));
0211       }  // stop measuring seeding cpu timer
0212 
0213       /*----------------------------
0214         Track params estimation
0215         ----------------------------*/
0216 
0217       // SYCL
0218 
0219       {
0220         traccc::performance::timer t("Track params (sycl)", elapsedTimes);
0221         params_sycl_buffer =
0222             tp_sycl(device_field, measurements_sycl_buffer,
0223                     spacepoints_sycl_buffer, seeds_sycl_buffer);
0224       }  // stop measuring track params sycl timer
0225 
0226       // CPU
0227       if (accelerator_opts.compare_with_cpu) {
0228         traccc::performance::timer t("Track params  (cpu)", elapsedTimes);
0229         params = tp(vecmem::get_data(measurements_per_event),
0230                     vecmem::get_data(spacepoints_per_event),
0231                     vecmem::get_data(seeds), field_vec);
0232       }  // stop measuring track params cpu timer
0233 
0234     }  // Stop measuring wall time
0235 
0236     /*----------------------------------
0237       compare seeds from cpu and sycl
0238       ----------------------------------*/
0239 
0240     // Copy the seeds to the host for comparison.
0241     traccc::edm::seed_collection::host seeds_sycl{host_mr};
0242     traccc::bound_track_parameters_collection_types::host params_sycl{&host_mr};
0243     copy(seeds_sycl_buffer, seeds_sycl)->wait();
0244     copy(params_sycl_buffer, params_sycl)->wait();
0245 
0246     if (accelerator_opts.compare_with_cpu) {
0247       // Show which event we are currently presenting the results for.
0248       TRACCC_INFO("===>>> Event " << event << " <<<===");
0249 
0250       // Compare the seeds made on the host and on the device
0251       traccc::soa_comparator<traccc::edm::seed_collection> compare_seeds{
0252           "seeds",
0253           traccc::details::comparator_factory<
0254               traccc::edm::seed_collection::const_device::const_proxy_type>{
0255               vecmem::get_data(spacepoints_per_event),
0256               vecmem::get_data(spacepoints_per_event)}};
0257       compare_seeds(vecmem::get_data(seeds), vecmem::get_data(seeds_sycl));
0258 
0259       // Compare the track parameters made on the host and on the device.
0260       traccc::collection_comparator<traccc::bound_track_parameters<>>
0261           compare_track_parameters{"track parameters"};
0262       compare_track_parameters(vecmem::get_data(params),
0263                                vecmem::get_data(params_sycl));
0264     }
0265 
0266     /*----------------
0267          Statistics
0268       ---------------*/
0269 
0270     n_spacepoints += spacepoints_per_event.size();
0271     n_seeds_sycl += seeds_sycl.size();
0272     n_seeds += seeds.size();
0273 
0274     /*------------
0275       Writer
0276       ------------*/
0277 
0278     if (performance_opts.run) {
0279       traccc::event_data evt_data(input_opts.directory, event, host_mr,
0280                                   input_opts.use_acts_geom_source, &host_det,
0281                                   input_opts.format, false);
0282 
0283       sd_performance_writer.write(
0284           vecmem::get_data(seeds_sycl), vecmem::get_data(spacepoints_per_event),
0285           vecmem::get_data(measurements_per_event), evt_data);
0286     }
0287   }
0288 
0289   if (performance_opts.run) {
0290     sd_performance_writer.finalize();
0291   }
0292 
0293   TRACCC_INFO("==> Statistics ... ");
0294   TRACCC_INFO("- read    " << n_spacepoints << " spacepoints");
0295   TRACCC_INFO("- created  (cpu)  " << n_seeds << " seeds");
0296   TRACCC_INFO("- created (sycl) " << n_seeds_sycl << " seeds");
0297   TRACCC_INFO("==>Elapsed times... " << elapsedTimes);
0298 
0299   return 0;
0300 }
0301 
0302 // The main routine
0303 //
0304 int main(int argc, char* argv[]) {
0305   std::unique_ptr<const traccc::Logger> logger = traccc::getDefaultLogger(
0306       "TracccExampleSeedingSycl", traccc::Logging::Level::INFO);
0307 
0308   // Program options.
0309   traccc::opts::detector detector_opts;
0310   traccc::opts::magnetic_field bfield_opts;
0311   traccc::opts::input_data input_opts;
0312   traccc::opts::track_seeding seeding_opts;
0313   traccc::opts::track_gbts_seeding seeding_gbts_opts;
0314   traccc::opts::performance performance_opts;
0315   traccc::opts::accelerator accelerator_opts;
0316   traccc::opts::program_options program_opts{
0317       "Full Tracking Chain Using SYCL (without clusterization)",
0318       {detector_opts, bfield_opts, input_opts, seeding_opts, seeding_gbts_opts,
0319        performance_opts, accelerator_opts},
0320       argc,
0321       argv,
0322       logger->cloneWithSuffix("Options")};
0323 
0324   // Run the application.
0325   return seq_run(detector_opts, bfield_opts, seeding_opts, seeding_gbts_opts,
0326                  input_opts, performance_opts, accelerator_opts,
0327                  logger->clone(), seeding_gbts_opts.useGBTS);
0328 }