Skip to content

Commit

Permalink
Add CPU benchmark cli tools
Browse files Browse the repository at this point in the history
  • Loading branch information
niermann999 committed Dec 18, 2024
1 parent 2e93b55 commit 4558c20
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 6 deletions.
4 changes: 2 additions & 2 deletions tests/include/detray/test/common/detector_scan_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace detray::test {
template <typename track_generator_t>
struct detector_scan_config : public test::fixture_base<>::configuration {
using base_type = test::fixture_base<>;
using scalar_type = typename base_type::scalar_type;
using vector3_type = typename base_type::vector3_type;
using scalar_type = typename base_type::scalar;
using vector3_type = typename base_type::vector3;
using trk_gen_config_t = typename track_generator_t::configuration;

/// Name of the test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace detray::test {
/// @brief Configuration for a detector scan test.
struct material_validation_config : public test::fixture_base<>::configuration {
using base_type = test::fixture_base<>;
using scalar_type = typename base_type::scalar_type;
using vector3_type = typename base_type::vector3_type;
using scalar_type = typename base_type::scalar;
using vector3_type = typename base_type::vector3;

/// Name of the test
std::string m_name{"material_validation"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace detray::test {
struct navigation_validation_config
: public test::fixture_base<>::configuration {
using base_type = test::fixture_base<>;
using scalar_type = typename base_type::scalar_type;
using vector3_type = typename base_type::vector3_type;
using scalar_type = typename base_type::scalar;
using vector3_type = typename base_type::vector3;

/// Name of the test
std::string m_name{"navigation_validation"};
Expand Down
1 change: 1 addition & 0 deletions tests/tools/include/detray/options/propagation_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ void configure_options<detray::stepping::config>(

cfg.path_limit = path_limit * unit<float>::m;
}
cfg.do_covariance_transport = false;
if (vm.count("covariance_transport")) {
cfg.do_covariance_transport = true;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/tools/src/cpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,46 @@ detray_add_executable(material_validation
Boost::program_options detray::tools detray::test_utils
detray::svgtools
)

if(DETRAY_BUILD_BENCHMARKS)
# Look for openMP, which is used for the CPU propagation benchmark
find_package(OpenMP)

# Build the propagation benchmark executable.
macro(detray_add_propagation_benchmark algebra)
detray_add_executable(propagation_benchmark_${algebra}
"propagation_benchmark.cpp"
LINK_LIBRARIES Boost::program_options benchmark::benchmark benchmark::benchmark_main vecmem::core detray::core_${algebra} detray::benchmarks detray::benchmark_cpu detray::tools detray::detectors
)

target_compile_options(
detray_propagation_benchmark_${algebra}
PRIVATE "-march=native" "-ftree-vectorize"
)

if(OpenMP_CXX_FOUND)
target_link_libraries(
detray_propagation_benchmark_${algebra}
PRIVATE OpenMP::OpenMP_CXX
)
endif()
endmacro()

# Build the array benchmark.
detray_add_propagation_benchmark( array )

# Build the Eigen benchmark executable.
if(DETRAY_EIGEN_PLUGIN)
detray_add_propagation_benchmark( eigen )
endif()

# Build the SMatrix benchmark executable.
if(DETRAY_SMATRIX_PLUGIN)
detray_add_propagation_benchmark( smatrix )
endif()

# Build the Vc benchmark executable.
if(DETRAY_VC_AOS_PLUGIN)
detray_add_propagation_benchmark( vc_aos )
endif()
endif()
171 changes: 171 additions & 0 deletions tests/tools/src/cpu/propagation_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/** Detray library, part of the ACTS project (R&D line)
*
* (c) 2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

// Project include(s)
#include "detray/detectors/bfield.hpp"
#include "detray/navigation/navigator.hpp"
#include "detray/propagator/actor_chain.hpp"
#include "detray/propagator/actors/aborters.hpp"
#include "detray/propagator/actors/parameter_resetter.hpp"
#include "detray/propagator/actors/parameter_transporter.hpp"
#include "detray/propagator/actors/pointwise_material_interactor.hpp"
#include "detray/propagator/rk_stepper.hpp"
#include "detray/tracks/tracks.hpp"

// Detray IO include(s)
#include "detray/io/frontend/detector_reader.hpp"

// Detray benchmark include(s)
#include "detray/benchmarks/cpu/propagation_benchmark.hpp"

// Detray test include(s).
#include "detray/test/utils/simulation/event_generator/track_generators.hpp"
#include "detray/test/utils/types.hpp"

// Detray test include(s)
#include "detray/options/detector_io_options.hpp"
#include "detray/options/parse_options.hpp"
#include "detray/options/propagation_options.hpp"
#include "detray/options/track_generator_options.hpp"

// Vecmem include(s)
#include <vecmem/memory/host_memory_resource.hpp>

//#include "detray/detectors/toy_metadata.hpp"
//#include "detray/test/utils/detectors/build_toy_detector.hpp"

// System include(s)
#include <algorithm>
#include <string>

namespace po = boost::program_options;

using namespace detray;

int main(int argc, char** argv) {

// Use the most general type to be able to read in all detector files
// using detector_t = detray::detector<detray::toy_metadata>;
using detector_t = detray::detector<test::default_metadata>;

using test_algebra = typename detector_t::algebra_type;
using scalar = dscalar<test_algebra>;
using vector3 = dvector3D<test_algebra>;
using free_track_parameters_t = free_track_parameters<test_algebra>;
using uniform_gen_t =
detail::random_numbers<scalar, std::uniform_real_distribution<scalar>>;
using track_generator_t =
random_track_generator<free_track_parameters_t, uniform_gen_t>;

using field_t = bfield::const_field_t<scalar>;
using stepper_t = rk_stepper<typename field_t::view_t, test_algebra>;
using empty_chain_t = actor_chain<>;
using default_chain =
actor_chain<dtuple, parameter_transporter<test_algebra>,
pointwise_material_interactor<test_algebra>,
parameter_resetter<test_algebra>>;

// Host memory resource
vecmem::host_memory_resource host_mr;

// Constant magnetic field
vector3 B{0.f, 0.f, 2.f * unit<scalar>::T};

// Number of tracks in the different benchmark cases
std::vector<int> n_tracks{8 * 8, 16 * 16, 32 * 32, 64 * 64,
128 * 128, 256 * 256, 512 * 512};

//
// Configuration
//

// Specific options for this test
po::options_description desc("\ndetray propagation benchmark options");

std::vector<dindex> window;
desc.add_options()("context", po::value<dindex>(),
"Number of the geometry context")(
"sort_tracks", "Sort track samples by theta angle");

// Configs to be filled
detray::io::detector_reader_config reader_cfg{};
track_generator_t::configuration trk_cfg{};
propagation::config prop_cfg{};
detray::benchmarks::benchmark_base::configuration bench_cfg{};

// Read options from commandline
po::variables_map vm = detray::options::parse_options(
desc, argc, argv, reader_cfg, trk_cfg, prop_cfg);

// General options
bool do_sort{(vm.count("sort_tracks") != 0)};

// The geometry context to be displayed
detector_t::geometry_context gctx;
if (vm.count("context")) {
gctx = detector_t::geometry_context{vm["context"].as<dindex>()};
}

//
// Prepare data
//

// Read the detector geometry
reader_cfg.do_check(true);

const auto [det, names] =
detray::io::read_detector<detector_t>(host_mr, reader_cfg);

// toy_det_config toy_cfg{};
// toy_cfg.use_material_maps(false).n_brl_layers(4u).n_edc_layers(7u);
// const auto [det, names] = build_toy_detector(host_mr, toy_cfg);
const std::string& det_name = det.name(names);

// Generate the track samples
auto track_samples =
detray::benchmarks::generate_track_samples<track_generator_t>(
&host_mr, n_tracks, trk_cfg, do_sort);

// Create a constant b-field
auto bfield = bfield::create_const_field<scalar>(B);

// Build actor states
dtuple<> empty_state{};

parameter_transporter<test_algebra>::state transporter_state{};
pointwise_material_interactor<test_algebra>::state interactor_state{};
parameter_resetter<test_algebra>::state resetter_state{};

auto actor_states = detail::make_tuple<dtuple>(
transporter_state, interactor_state, resetter_state);

//
// Register benchmarks
//

// Number of warmup tracks
const int n_max_tracks{*std::ranges::max_element(n_tracks)};
bench_cfg.n_warmup(
static_cast<int>(std::ceil(0.1f * static_cast<float>(n_max_tracks))));

if (prop_cfg.stepping.do_covariance_transport) {
detray::benchmarks::register_benchmark<
detray::benchmarks::host_propagation_bm, stepper_t, default_chain>(
det_name + "_W_COV_TRANSPORT", bench_cfg, prop_cfg, det, bfield,
&actor_states, track_samples, n_tracks);
} else {
detray::benchmarks::register_benchmark<
detray::benchmarks::host_propagation_bm, stepper_t, empty_chain_t>(
det_name, bench_cfg, prop_cfg, det, bfield, &empty_state,
track_samples, n_tracks);
}

// Run benchmarks
::benchmark::Initialize(&argc, argv);
::benchmark::RunSpecifiedBenchmarks();
::benchmark::Shutdown();
}

0 comments on commit 4558c20

Please sign in to comment.