From 7ef81e86c113af7176f64937f16a70bf04cc2d7a Mon Sep 17 00:00:00 2001 From: Andreas Stefl Date: Sun, 21 Apr 2024 14:20:42 +0200 Subject: [PATCH] perf: Benchmark `StraightLineStepper` (#3134) Adds a benchmark for the `StraightLineStepper` similar as the benchmarks for `EigenStepper` and `AtlasStepper`. --- Tests/Benchmarks/CMakeLists.txt | 5 +- .../StraightLineStepperBenchmark.cpp | 123 ++++++++++++++++++ 2 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 Tests/Benchmarks/StraightLineStepperBenchmark.cpp diff --git a/Tests/Benchmarks/CMakeLists.txt b/Tests/Benchmarks/CMakeLists.txt index e452add9ed2..3bd4a69f07e 100644 --- a/Tests/Benchmarks/CMakeLists.txt +++ b/Tests/Benchmarks/CMakeLists.txt @@ -26,5 +26,6 @@ add_benchmark(BinUtility BinUtilityBenchmark.cpp) add_benchmark(EigenStepper EigenStepperBenchmark.cpp) add_benchmark(SolenoidField SolenoidFieldBenchmark.cpp) add_benchmark(SurfaceIntersection SurfaceIntersectionBenchmark.cpp) -add_benchmark(RayFrustumBenchmark RayFrustumBenchmark.cpp) -add_benchmark(AnnulusBoundsBenchmark AnnulusBoundsBenchmark.cpp) +add_benchmark(RayFrustum RayFrustumBenchmark.cpp) +add_benchmark(AnnulusBounds AnnulusBoundsBenchmark.cpp) +add_benchmark(StraightLineStepper StraightLineStepperBenchmark.cpp) diff --git a/Tests/Benchmarks/StraightLineStepperBenchmark.cpp b/Tests/Benchmarks/StraightLineStepperBenchmark.cpp new file mode 100644 index 00000000000..5d2262c6068 --- /dev/null +++ b/Tests/Benchmarks/StraightLineStepperBenchmark.cpp @@ -0,0 +1,123 @@ +// This file is part of the Acts project. +// +// Copyright (C) 2017-2019 CERN for the benefit of the Acts project +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include "Acts/Definitions/Units.hpp" +#include "Acts/EventData/ParticleHypothesis.hpp" +#include "Acts/EventData/TrackParameters.hpp" +#include "Acts/Geometry/GeometryContext.hpp" +#include "Acts/MagneticField/MagneticFieldContext.hpp" +#include "Acts/Propagator/Propagator.hpp" +#include "Acts/Propagator/StraightLineStepper.hpp" +#include "Acts/Tests/CommonHelpers/BenchmarkTools.hpp" +#include "Acts/Utilities/Logger.hpp" + +#include + +#include + +namespace po = boost::program_options; +using namespace Acts; +using namespace Acts::UnitLiterals; + +int main(int argc, char* argv[]) { + unsigned int toys = 1; + double ptInGeV = 1; + double BzInT = 1; + double maxPathInM = 1; + unsigned int lvl = Acts::Logging::INFO; + bool withCov = true; + + // Create a test context + GeometryContext tgContext = GeometryContext(); + MagneticFieldContext mfContext = MagneticFieldContext(); + + try { + po::options_description desc("Allowed options"); + // clang-format off + desc.add_options() + ("help", "produce help message") + ("toys",po::value(&toys)->default_value(20000),"number of tracks to propagate") + ("pT",po::value(&ptInGeV)->default_value(1),"transverse momentum in GeV") + ("path",po::value(&maxPathInM)->default_value(5),"maximum path length in m") + ("cov",po::value(&withCov)->default_value(true),"propagation with covariance matrix") + ("verbose",po::value(&lvl)->default_value(Acts::Logging::INFO),"logging level"); + // clang-format on + po::variables_map vm; + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + if (vm.count("help") != 0u) { + std::cout << desc << std::endl; + return 0; + } + } catch (std::exception& e) { + std::cerr << "error: " << e.what() << std::endl; + return 1; + } + + ACTS_LOCAL_LOGGER( + getDefaultLogger("ATLAS_Stepper", Acts::Logging::Level(lvl))); + + // print information about profiling setup + ACTS_INFO("propagating " << toys << " tracks with pT = " << ptInGeV + << "GeV in a " << BzInT << "T B-field"); + + using Stepper = StraightLineStepper; + using Propagator = Propagator; + using Covariance = BoundSquareMatrix; + + Stepper stepper; + Propagator propagator(stepper); + + PropagatorOptions<> options(tgContext, mfContext); + options.pathLimit = maxPathInM * UnitConstants::m; + + Vector4 pos4(0, 0, 0, 0); + Vector3 dir(1, 0, 0); + Covariance cov; + // clang-format off + cov << 10_mm, 0, 0, 0, 0, 0, + 0, 10_mm, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, + 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 1_e / 10_GeV, 0, + 0, 0, 0, 0, 0, 0; + // clang-format on + + std::optional covOpt = std::nullopt; + if (withCov) { + covOpt = cov; + } + CurvilinearTrackParameters pars(pos4, dir, +1 / ptInGeV, covOpt, + ParticleHypothesis::pion()); + + double totalPathLength = 0; + std::size_t numSteps = 0; + std::size_t numIters = 0; + const auto propagationBenchResult = Acts::Test::microBenchmark( + [&] { + auto r = propagator.propagate(pars, options).value(); + if (totalPathLength == 0.) { + ACTS_DEBUG("reached position " + << r.endParameters->position(tgContext).transpose() + << " in " << r.steps << " steps"); + } + totalPathLength += r.pathLength; + numSteps += r.steps; + ++numIters; + return r; + }, + 1, toys); + + ACTS_INFO("Execution stats: " << propagationBenchResult); + ACTS_INFO("average path length = " << totalPathLength / numIters / 1_mm + << "mm"); + ACTS_INFO("average number of steps = " << 1.0 * numSteps / numIters); + + return 0; +}