diff --git a/tests/benchmarks/cuda/CMakeLists.txt b/tests/benchmarks/cuda/CMakeLists.txt index a101f0854..b15c8356d 100644 --- a/tests/benchmarks/cuda/CMakeLists.txt +++ b/tests/benchmarks/cuda/CMakeLists.txt @@ -15,17 +15,14 @@ enable_language(CUDA) # Set the CUDA build flags. include(detray-compiler-options-cuda) -# Look for openMP, which is used for the CPU benchmark -find_package(OpenMP) - -# make unit tests for multiple algebras -# Currently vc and smatrix is not supported -set(algebras "array") +# Build benchmarks for multiple algebra plugins +# Currently vc and smatrix is not supported on device +set(algebra_plugins "array") if(DETRAY_EIGEN_PLUGIN) - list(APPEND algebras "eigen") + list(APPEND algebra_plugins "eigen") endif() -foreach(algebra ${algebras}) +foreach(algebra ${algebra_plugins}) detray_add_executable(benchmark_cuda_propagation_${algebra} "propagation.cpp" LINK_LIBRARIES detray::benchmark_cuda detray::core detray::algebra_${algebra} vecmem::cuda detray::test_utils @@ -40,11 +37,4 @@ foreach(algebra ${algebras}) detray_benchmark_cuda_propagation_${algebra} PRIVATE "-march=native" "-ftree-vectorize" ) - - if(OpenMP_CXX_FOUND) - target_link_libraries( - detray_benchmark_cuda_propagation_${algebra} - PRIVATE OpenMP::OpenMP_CXX - ) - endif() endforeach() diff --git a/tests/tools/src/cpu/CMakeLists.txt b/tests/tools/src/cpu/CMakeLists.txt index ea630ea83..16a514b17 100644 --- a/tests/tools/src/cpu/CMakeLists.txt +++ b/tests/tools/src/cpu/CMakeLists.txt @@ -58,19 +58,19 @@ if(DETRAY_BUILD_BENCHMARKS) # Build the propagation benchmark executable. macro(detray_add_propagation_benchmark algebra) - detray_add_executable(propagation_benchmark_${algebra} + detray_add_executable(propagation_benchmark_cpu_${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} + detray_propagation_benchmark_cpu_${algebra} PRIVATE "-march=native" "-ftree-vectorize" ) if(OpenMP_CXX_FOUND) target_link_libraries( - detray_propagation_benchmark_${algebra} + detray_propagation_benchmark_cpu_${algebra} PRIVATE OpenMP::OpenMP_CXX ) endif() diff --git a/tests/tools/src/cuda/CMakeLists.txt b/tests/tools/src/cuda/CMakeLists.txt index 271577341..f323db92e 100644 --- a/tests/tools/src/cuda/CMakeLists.txt +++ b/tests/tools/src/cuda/CMakeLists.txt @@ -26,3 +26,27 @@ detray_add_executable(material_validation_cuda LINK_LIBRARIES GTest::gtest GTest::gtest_main Boost::program_options detray::test_cuda detray::tools ) + +# Build benchmarks for multiple algebra plugins +# Currently vc and smatrix is not supported on device +set(algebra_plugins "array") +if(DETRAY_EIGEN_PLUGIN) + list(APPEND algebra_plugins "eigen") +endif() + +foreach(algebra ${algebra_plugins}) + detray_add_executable(propagation_benchmark_cuda_${algebra} + "propagation_benchmark_cuda.cpp" + LINK_LIBRARIES detray::benchmark_cuda detray::core_${algebra} vecmem::cuda detray::tools detray::test_utils + ) + + target_compile_definitions( + detray_propagation_benchmark_cuda_${algebra} + PRIVATE ${algebra}=${algebra} + ) + + target_compile_options( + detray_propagation_benchmark_cuda_${algebra} + PRIVATE "-march=native" "-ftree-vectorize" + ) +endforeach() diff --git a/tests/tools/src/cuda/propagation_benchmark_cuda.cpp b/tests/tools/src/cuda/propagation_benchmark_cuda.cpp new file mode 100644 index 000000000..ba0995f8f --- /dev/null +++ b/tests/tools/src/cuda/propagation_benchmark_cuda.cpp @@ -0,0 +1,163 @@ +/** 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/device/cuda/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 + +// System include(s) +#include +#include + +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; + using test_algebra = typename detector_t::algebra_type; + using scalar = dscalar; + using vector3 = dvector3D; + using free_track_parameters_t = free_track_parameters; + using uniform_gen_t = + detail::random_numbers>; + using track_generator_t = + random_track_generator; + + using field_bknd_t = bfield::const_bknd_t; + + // Host and device memory resources + vecmem::host_memory_resource host_mr; + vecmem::cuda::device_memory_resource dev_mr; + + // Constant magnetic field + vector3 B{0.f, 0.f, 2.f * unit::T}; + + // Number of tracks in the different benchmark cases + std::vector 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 window; + desc.add_options()("context", po::value(), + "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()}; + } + + // + // Prepare data + // + + // Read the detector geometry + reader_cfg.do_check(true); + + const auto [det, names] = + detray::io::read_detector(host_mr, reader_cfg); + const std::string& det_name = det.name(names); + + // Generate the track samples + auto track_samples = + detray::benchmarks::generate_track_samples( + &host_mr, n_tracks, trk_cfg, do_sort); + + // Create a constant b-field + auto bfield = bfield::create_const_field(B); + + // Build actor states + dtuple<> empty_state{}; + + parameter_transporter::state transporter_state{}; + pointwise_material_interactor::state interactor_state{}; + parameter_resetter::state resetter_state{}; + + auto actor_states = detail::make_tuple( + 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(std::ceil(0.1f * static_cast(n_max_tracks)))); + + if (prop_cfg.stepping.do_covariance_transport) { + detray::benchmarks::register_benchmark< + detray::benchmarks::cuda_propagation_bm, + detray::benchmarks::cuda_propagator_type< + test::default_metadata, field_bknd_t, + detray::benchmarks::default_chain>>( + det_name + "_W_COV_TRANSPORT", bench_cfg, prop_cfg, det, bfield, + &actor_states, track_samples, n_tracks, &dev_mr); + } else { + detray::benchmarks::register_benchmark< + detray::benchmarks::cuda_propagation_bm, + detray::benchmarks::cuda_propagator_type< + test::default_metadata, field_bknd_t, + detray::benchmarks::empty_chain>>( + det_name, bench_cfg, prop_cfg, det, bfield, &empty_state, + track_samples, n_tracks, &dev_mr); + } + + // Run benchmarks + ::benchmark::Initialize(&argc, argv); + ::benchmark::RunSpecifiedBenchmarks(); + ::benchmark::Shutdown(); +}