Skip to content

Commit

Permalink
ref: Generalize propagation benchmark functionality (#404)
Browse files Browse the repository at this point in the history
Generalize the benchmark functionality to different detectors and actor setups. Also splits some common benchmark functionality into a detray benchmark library, like e.g. the generation of track samples.

The CPU benchmarks have also been switched to use dynamic scheduling for load balancing.

Also adds the charge conjugation operation to the pdg particle, so that the charge hypothesis can be updated when test tracks are generated with randomized charge (this was triggering an assertion in the benchmarks otherwise)
  • Loading branch information
niermann999 authored Jan 8, 2025
1 parent 7816acc commit ac8e293
Show file tree
Hide file tree
Showing 24 changed files with 1,289 additions and 471 deletions.
2 changes: 1 addition & 1 deletion core/include/detray/builders/material_map_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ struct add_sf_material_map {

template <typename coll_t, typename index_t, typename mat_factory_t,
typename bin_data_t, std::size_t DIM, typename material_store_t,
typename scalar_t>
concepts::scalar scalar_t>
DETRAY_HOST inline std::pair<typename materials_t::id, dindex> operator()(
[[maybe_unused]] const coll_t& coll,
[[maybe_unused]] const index_t& index,
Expand Down
23 changes: 20 additions & 3 deletions core/include/detray/definitions/pdg_particle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,37 @@ struct pdg_particle {
m_charge(static_cast<scalar_t>(charge)) {}

DETRAY_HOST_DEVICE
std::int32_t pdg_num() const { return m_pdg_num; }
constexpr std::int32_t pdg_num() const { return m_pdg_num; }

DETRAY_HOST_DEVICE
scalar_type mass() const { return m_mass; }
constexpr scalar_type mass() const { return m_mass; }

DETRAY_HOST_DEVICE
scalar_type charge() const { return m_charge; }
constexpr scalar_type charge() const { return m_charge; }

private:
std::int32_t m_pdg_num;
scalar_type m_mass;
scalar_type m_charge;
};

/// Apply the charge conjugation operator to a particle hypothesis @param ptc
template <concepts::scalar scalar_t>
DETRAY_HOST_DEVICE constexpr pdg_particle<scalar_t> charge_conjugation(
const pdg_particle<scalar_t>& ptc) {
return (ptc.charge() != 0)
? detray::pdg_particle<scalar_t>{-ptc.pdg_num(), ptc.mass(),
-ptc.charge()}
: ptc;
}

/// @returns an updated particle hypothesis according to the track qop
template <concepts::scalar scalar_t, typename track_t>
DETRAY_HOST_DEVICE constexpr pdg_particle<scalar_t> update_particle_hypothesis(
const pdg_particle<scalar_t>& ptc, const track_t& params) {
return (ptc.charge() * params.qop() > 0.f) ? ptc : charge_conjugation(ptc);
}

// Macro for declaring the particle
#define DETRAY_DECLARE_PARTICLE(PARTICLE_NAME, PDG_NUM, MASS, CHARGE) \
template <concepts::scalar scalar_t> \
Expand Down
2 changes: 1 addition & 1 deletion core/include/detray/navigation/intersection_kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ struct intersection_update {
/// @return the intersection
template <typename mask_group_t, typename mask_range_t, typename traj_t,
typename intersection_t, typename transform_container_t,
typename scalar_t>
concepts::scalar scalar_t>
DETRAY_HOST_DEVICE inline bool operator()(
const mask_group_t &mask_group, const mask_range_t &mask_range,
const traj_t &traj, intersection_t &sfi,
Expand Down
20 changes: 14 additions & 6 deletions core/include/detray/propagator/actor_chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class actor_chain {
public:
/// Types of the actors that are registered in the chain
using actor_list_type = dtuple<actors_t...>;
// Tuple of actor states
using state_tuple = dtuple<typename actors_t::state...>;
// Type of states tuple that is used in the propagator
using state = dtuple<typename actors_t::state &...>;

Expand All @@ -52,8 +54,7 @@ class actor_chain {
return m_actors;
}

/// @returns a tuple of default constructible actor states and a
/// corresponding tuple of references
/// @returns a tuple of default constructible actor states
DETRAY_HOST_DEVICE
static constexpr auto make_actor_states() {
// Only possible if each state is default initializable
Expand All @@ -66,10 +67,10 @@ class actor_chain {
}

/// @returns a tuple of reference for every state in the tuple @param t
DETRAY_HOST_DEVICE static constexpr state make_ref_tuple(
DETRAY_HOST_DEVICE static constexpr state setup_actor_states(
dtuple<typename actors_t::state...> &t) {
return make_ref_tuple(t,
std::make_index_sequence<sizeof...(actors_t)>{});
return setup_actor_states(
t, std::make_index_sequence<sizeof...(actors_t)>{});
}

private:
Expand Down Expand Up @@ -110,7 +111,7 @@ class actor_chain {

/// @returns a tuple of reference for every state in the tuple @param t
template <std::size_t... indices>
DETRAY_HOST_DEVICE static constexpr state make_ref_tuple(
DETRAY_HOST_DEVICE static constexpr state setup_actor_states(
dtuple<typename actors_t::state...> &t,
std::index_sequence<indices...> /*ids*/) {
return detray::tie(detail::get<indices>(t)...);
Expand All @@ -125,6 +126,7 @@ template <>
class actor_chain<> {

public:
using state_tuple = dtuple<>;
/// Empty states replaces a real actor states container
struct state {};

Expand All @@ -137,6 +139,12 @@ class actor_chain<> {
propagator_state_t & /*p_state*/) const {
/*Do nothing*/
}

/// @returns an empty state
DETRAY_HOST_DEVICE static constexpr state setup_actor_states(
const state_tuple &) {
return {};
}
};

} // namespace detray
35 changes: 35 additions & 0 deletions tests/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,47 @@
#
# Mozilla Public License Version 2.0

# Set the common C++ flags.
include(detray-compiler-options-cpp)
include_directories(
SYSTEM
$<TARGET_PROPERTY:covfie::core,INTERFACE_INCLUDE_DIRECTORIES>
)
include_directories(
SYSTEM
$<TARGET_PROPERTY:dfelibs::dfelibs,INTERFACE_INCLUDE_DIRECTORIES>
)

# Set up a common benchmark library.
file(
GLOB _detray_benchmarks_headers
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
"include/detray/benchmarks/*.hpp"
)

add_library(detray_benchmarks INTERFACE "${_detray_benchmarks_headers}")
add_library(detray::benchmarks ALIAS detray_benchmarks)

target_include_directories(
detray_benchmarks
INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include"
)

target_link_libraries(
detray_benchmarks
INTERFACE benchmark::benchmark vecmem::core detray::core detray::test_utils
)

unset(_detray_benchmarks_headers)

# Set up the host/cpu benchmarks.
if(DETRAY_BUILD_HOST)
add_subdirectory(cpu)
add_subdirectory(include/detray/benchmarks/cpu)
endif()

# Set up all of the "device" benchmarks.
if(DETRAY_BUILD_CUDA)
add_subdirectory(cuda)
add_subdirectory(include/detray/benchmarks/device)
endif()
19 changes: 15 additions & 4 deletions tests/benchmarks/cpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ message(STATUS "Building detray host benchmarks")
option(DETRAY_BENCHMARK_MULTITHREAD "Enable multithreaded benchmarks" OFF)
option(DETRAY_BENCHMARK_PRINTOUTS "Enable printouts in the benchmarks" OFF)

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

# Macro setting up the CPU benchmarks for a specific algebra plugin.
macro(detray_add_cpu_benchmark algebra)
# Build the benchmark executable.
detray_add_executable(benchmark_cpu_${algebra}
"benchmark_propagator.cpp"
"find_volume.cpp"
"grid.cpp"
"grid2.cpp"
"intersect_all.cpp"
"intersect_surfaces.cpp"
"masks.cpp"
LINK_LIBRARIES benchmark::benchmark benchmark::benchmark_main vecmem::core
LINK_LIBRARIES benchmark::benchmark benchmark::benchmark_main vecmem::core detray::benchmarks
detray::core_${algebra} detray::test_utils
)

Expand All @@ -48,9 +47,21 @@ macro(detray_add_cpu_benchmark algebra)
)
endif()

# Build the benchmark executable for the propagation
detray_add_executable( benchmark_cpu_propagation_${algebra}
"propagation.cpp"
LINK_LIBRARIES detray::benchmark_cpu benchmark::benchmark_main
vecmem::core detray::core_${algebra} detray::test_utils
)

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

if(OpenMP_CXX_FOUND)
target_link_libraries(
detray_benchmark_cpu_${algebra}
detray_benchmark_cpu_propagation_${algebra}
PRIVATE OpenMP::OpenMP_CXX
)
endif()
Expand Down
182 changes: 0 additions & 182 deletions tests/benchmarks/cpu/benchmark_propagator.cpp

This file was deleted.

Loading

0 comments on commit ac8e293

Please sign in to comment.