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/device/cuda/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<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_bknd_t = bfield::const_bknd_t<scalar>;
0058 
0059   // Host and device memory resources
0060   vecmem::host_memory_resource host_mr;
0061   vecmem::cuda::device_memory_resource dev_mr;
0062 
0063   // Constant magnetic field
0064   vector3 B{0.f, 0.f, 2.f * unit<scalar>::T};
0065 
0066   // Number of tracks in the different benchmark cases
0067   std::vector<int> n_tracks{10,     100,    500,     1000,   5000,
0068                             10'000, 50'000, 100'000, 250'000};
0069 
0070   //
0071   // Configuration
0072   //
0073 
0074   // Google benchmark specific options
0075   ::benchmark::Initialize(&argc, argv);
0076 
0077   // Specific options for this test
0078   po::options_description desc("\ndetray propagation benchmark options");
0079 
0080   desc.add_options()("context", po::value<dindex>(),
0081                      "Index of the geometry context")(
0082       "bknd_name", po::value<std::string>(), "Name of the Processor")(
0083       "sort_tracks", "Sort track samples by theta angle");
0084 
0085   // Configs to be filled
0086   detray::io::detector_reader_config reader_cfg{};
0087   track_generator_t::configuration trk_cfg{};
0088   propagation::config prop_cfg{};
0089   detray::benchmarks::benchmark_base::configuration bench_cfg{};
0090 
0091   // Read options from commandline
0092   po::variables_map vm = detray::options::parse_options(
0093       desc, argc, argv, reader_cfg, trk_cfg, prop_cfg);
0094 
0095   // Custom options
0096   bool do_sort{(vm.count("sort_tracks") != 0)};
0097 
0098   // The geometry context to be used
0099   detector_t::geometry_context gctx;
0100   if (vm.count("context") != 0u) {
0101     gctx = detector_t::geometry_context{vm["context"].as<dindex>()};
0102   }
0103   std::string proc_name{"unknown"};
0104   if (vm.count("bknd_name") != 0u) {
0105     proc_name = vm["bknd_name"].as<std::string>();
0106   }
0107 
0108   // String that describes the detector setup
0109   std::string setup_str{};
0110   auto add_delim = [](std::string& str) { str += ", "; };
0111   if (vm.count("grid_file") == 0u) {
0112     setup_str += "no grids";
0113   }
0114   if (vm.count("material_file") == 0u) {
0115     if (!setup_str.empty()) {
0116       add_delim(setup_str);
0117     }
0118     setup_str += "no mat.";
0119   }
0120 
0121   //
0122   // Prepare data
0123   //
0124 
0125   // Read the detector geometry
0126   reader_cfg.do_check(true);
0127 
0128   const auto [det, names] =
0129       detray::io::read_detector<detector_t>(host_mr, reader_cfg);
0130   const std::string& det_name = det.name(names);
0131 
0132   // Generate the track samples
0133   auto track_samples =
0134       detray::benchmarks::generate_track_samples<track_generator_t>(
0135           &host_mr, n_tracks, trk_cfg, do_sort);
0136 
0137   // Create a constant b-field
0138   auto bfield = create_const_field<scalar>(B);
0139 
0140   // Build actor states
0141   dtuple<> empty_state{};
0142 
0143   actor::parameter_updater_state<bench_algebra> updater_state{prop_cfg};
0144   actor::pointwise_material_interactor<bench_algebra>::state interactor_state{};
0145 
0146   auto actor_states =
0147       detail::make_tuple<dtuple>(updater_state, interactor_state);
0148 
0149   //
0150   // Register benchmarks
0151   //
0152 
0153   // Number of warmup tracks
0154   const int n_max_tracks{*std::ranges::max_element(n_tracks)};
0155   bench_cfg.n_warmup(
0156       static_cast<int>(std::ceil(0.1f * static_cast<float>(n_max_tracks))));
0157 
0158   if (prop_cfg.stepping.do_covariance_transport) {
0159     detray::benchmarks::register_benchmark<
0160         detray::benchmarks::cuda_propagation_bm,
0161         detray::benchmarks::cuda_propagator_type<
0162             detray::benchmarks::default_metadata, field_bknd_t,
0163             detray::benchmarks::default_chain>>(
0164         det_name + "_W_COV_TRANSPORT", bench_cfg, prop_cfg, det, bfield,
0165         &actor_states, track_samples, n_tracks, &dev_mr);
0166   } else {
0167     detray::benchmarks::register_benchmark<
0168         detray::benchmarks::cuda_propagation_bm,
0169         detray::benchmarks::cuda_propagator_type<
0170             detray::benchmarks::default_metadata, field_bknd_t,
0171             detray::benchmarks::empty_chain>>(det_name, bench_cfg, prop_cfg,
0172                                               det, bfield, &empty_state,
0173                                               track_samples, n_tracks, &dev_mr);
0174 
0175     if (!setup_str.empty()) {
0176       add_delim(setup_str);
0177     }
0178     setup_str += "no cov.";
0179   }
0180 
0181   // These fields are needed by the plotting scripts, even if undefined
0182   ::benchmark::AddCustomContext("Backend", "CUDA");
0183   ::benchmark::AddCustomContext("Backend Name", proc_name);
0184   ::benchmark::AddCustomContext("Algebra-plugin",
0185                                 detray::types::get_name<bench_algebra>());
0186   ::benchmark::AddCustomContext("Detector Setup", setup_str);
0187 
0188   // Run benchmarks
0189   ::benchmark::RunSpecifiedBenchmarks();
0190   ::benchmark::Shutdown();
0191 }