Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:19

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 // Project include(s)
0010 #include "detray/navigation/caching_navigator.hpp"
0011 #include "detray/propagator/actors.hpp"
0012 #include "detray/propagator/rk_stepper.hpp"
0013 #include "detray/tracks/tracks.hpp"
0014 #include "detray/utils/type_list.hpp"
0015 
0016 // Detray IO include(s)
0017 #include "detray/io/frontend/detector_reader.hpp"
0018 
0019 // Detray benchmark include(s)
0020 #include "detray/benchmarks/cpu/propagation_benchmark.hpp"
0021 #include "detray/benchmarks/types.hpp"
0022 
0023 // Detray test include(s)
0024 #include "detray/test/common/bfield.hpp"
0025 #include "detray/test/common/track_generators.hpp"
0026 
0027 // Detray tools include(s)
0028 #include "detray/options/detector_io_options.hpp"
0029 #include "detray/options/parse_options.hpp"
0030 #include "detray/options/propagation_options.hpp"
0031 #include "detray/options/track_generator_options.hpp"
0032 
0033 // Vecmem include(s)
0034 #include <vecmem/memory/host_memory_resource.hpp>
0035 
0036 // System include(s)
0037 #include <algorithm>
0038 #include <string>
0039 
0040 namespace po = boost::program_options;
0041 
0042 using namespace detray;
0043 
0044 int main(int argc, char** argv) {
0045   // Use the most general type to be able to read in all detector files
0046   using detector_t = detray::detector<detray::benchmarks::default_metadata>;
0047   using bench_algebra = typename detector_t::algebra_type;
0048   using scalar = dscalar<bench_algebra>;
0049   using vector3 = dvector3D<bench_algebra>;
0050 
0051   using free_track_parameters_t = free_track_parameters<bench_algebra>;
0052   using uniform_gen_t =
0053       detail::random_numbers<scalar, std::uniform_real_distribution<scalar>>;
0054   using track_generator_t =
0055       random_track_generator<free_track_parameters_t, uniform_gen_t>;
0056 
0057   using field_t = bfield::const_field_t<scalar>;
0058   using stepper_t = rk_stepper<typename field_t::view_t, bench_algebra>;
0059   using empty_chain_t = actor_chain<>;
0060   using default_chain = actor_chain<actor::parameter_updater<
0061       bench_algebra, actor::pointwise_material_interactor<bench_algebra>>>;
0062 
0063   // Host memory resource
0064   vecmem::host_memory_resource host_mr;
0065 
0066   // Constant magnetic field
0067   vector3 B{0.f, 0.f, 2.f * unit<scalar>::T};
0068 
0069   // Number of tracks in the different benchmark cases
0070   std::vector<int> n_tracks{10,     100,    500,     1000,   5000,
0071                             10'000, 50'000, 100'000, 250'000};
0072 
0073   //
0074   // Configuration
0075   //
0076 
0077   // Google benchmark specific options
0078   ::benchmark::Initialize(&argc, argv);
0079 
0080   // Specific options for this test
0081   po::options_description desc("\ndetray propagation benchmark options");
0082 
0083   desc.add_options()("context", po::value<dindex>(),
0084                      "Index of the geometry context")(
0085       "bknd_name", po::value<std::string>(), "Name of the Processor")(
0086       "sort_tracks", "Sort track samples by theta angle");
0087 
0088   // Configs to be filled
0089   detray::io::detector_reader_config reader_cfg{};
0090   track_generator_t::configuration trk_cfg{};
0091   propagation::config prop_cfg{};
0092   detray::benchmarks::benchmark_base::configuration bench_cfg{};
0093 
0094   // Read options from commandline
0095   po::variables_map vm = detray::options::parse_options(
0096       desc, argc, argv, reader_cfg, trk_cfg, prop_cfg);
0097 
0098   // Custom options
0099   bool do_sort{(vm.count("sort_tracks") != 0)};
0100 
0101   // The geometry context to be used
0102   detector_t::geometry_context gctx;
0103   if (vm.count("context") != 0u) {
0104     gctx = detector_t::geometry_context{vm["context"].as<dindex>()};
0105   }
0106   std::string proc_name{"unknown"};
0107   if (vm.count("bknd_name") != 0u) {
0108     proc_name = vm["bknd_name"].as<std::string>();
0109   }
0110 
0111   // String that describes the detector setup
0112   std::string setup_str{};
0113   auto add_delim = [](std::string& str) { str += ", "; };
0114   if (vm.count("grid_file") == 0u) {
0115     setup_str += "no grids";
0116   }
0117   if (vm.count("material_file") == 0u) {
0118     if (!setup_str.empty()) {
0119       add_delim(setup_str);
0120     }
0121     setup_str += "no mat.";
0122   }
0123 
0124   //
0125   // Prepare data
0126   //
0127 
0128   // Read the detector geometry
0129   reader_cfg.do_check(true);
0130 
0131   const auto [det, names] =
0132       detray::io::read_detector<detector_t>(host_mr, reader_cfg);
0133   const std::string& det_name = det.name(names);
0134 
0135   // Generate the track samples
0136   auto track_samples =
0137       detray::benchmarks::generate_track_samples<track_generator_t>(
0138           &host_mr, n_tracks, trk_cfg, do_sort);
0139 
0140   // Create a constant b-field
0141   auto bfield = create_const_field<scalar>(B);
0142 
0143   // Build actor states
0144   dtuple<> empty_state{};
0145 
0146   actor::parameter_updater_state<bench_algebra> updater_state{prop_cfg};
0147   actor::pointwise_material_interactor<bench_algebra>::state interactor_state{};
0148 
0149   auto actor_states =
0150       detail::make_tuple<dtuple>(updater_state, interactor_state);
0151 
0152   //
0153   // Register benchmarks
0154   //
0155 
0156   // Number of warmup tracks
0157   const int n_max_tracks{*std::ranges::max_element(n_tracks)};
0158   bench_cfg.n_warmup(
0159       static_cast<int>(std::ceil(0.1f * static_cast<float>(n_max_tracks))));
0160 
0161   if (prop_cfg.stepping.do_covariance_transport) {
0162     detray::benchmarks::register_benchmark<
0163         detray::benchmarks::host_propagation_bm, stepper_t, default_chain>(
0164         det_name + "_W_COV_TRANSPORT", bench_cfg, prop_cfg, det, bfield,
0165         &actor_states, track_samples, n_tracks);
0166   } else {
0167     detray::benchmarks::register_benchmark<
0168         detray::benchmarks::host_propagation_bm, stepper_t, empty_chain_t>(
0169         det_name, bench_cfg, prop_cfg, det, bfield, &empty_state, track_samples,
0170         n_tracks);
0171 
0172     if (!setup_str.empty()) {
0173       add_delim(setup_str);
0174     }
0175     setup_str += "no cov.";
0176   }
0177 
0178   // These fields are needed by the plotting scripts, even if undefined
0179   ::benchmark::AddCustomContext("Backend", "CPU");
0180   ::benchmark::AddCustomContext("Backend Name", proc_name);
0181   ::benchmark::AddCustomContext("Algebra-plugin",
0182                                 detray::types::get_name<bench_algebra>());
0183   ::benchmark::AddCustomContext("Detector Setup", setup_str);
0184 
0185   // Run benchmarks
0186   ::benchmark::RunSpecifiedBenchmarks();
0187   ::benchmark::Shutdown();
0188 }