Skip to content

Commit

Permalink
Use random, but sorted tracks for benchmarks and copy the detector to…
Browse files Browse the repository at this point in the history
… device
  • Loading branch information
niermann999 committed Nov 15, 2024
1 parent 9c0f9af commit 2d2a7cb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 20 deletions.
43 changes: 36 additions & 7 deletions tests/benchmarks/cpu/benchmark_propagator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,50 @@ auto toy_cfg =
toy_det_config{}.n_brl_layers(4u).n_edc_layers(7u).do_check(false);

void fill_tracks(vecmem::vector<free_track_parameters<algebra_t>> &tracks,
const std::size_t theta_steps, const std::size_t phi_steps) {
// Set momentum of tracks
const scalar mom_mag{10.f * unit<scalar>::GeV};
const std::size_t n_tracks, bool do_sort = true) {
using scalar_t = dscalar<algebra_t>;
using uniform_gen_t =
detail::random_numbers<scalar_t,
std::uniform_real_distribution<scalar_t>>;
using trk_generator_t =
random_track_generator<free_track_parameters<algebra_t>, uniform_gen_t>;

trk_generator_t::configuration trk_gen_cfg{};
trk_gen_cfg.seed(42u);
trk_gen_cfg.n_tracks(n_tracks);
trk_gen_cfg.phi_range(-constant<scalar_t>::pi, constant<scalar_t>::pi);
trk_gen_cfg.eta_range(-3.f, 3.f);
trk_gen_cfg.mom_range(1.f * unit<scalar_t>::GeV,
100.f * unit<scalar_t>::GeV);
trk_gen_cfg.origin({0.f, 0.f, 0.f});
trk_gen_cfg.origin_stddev({0.f * unit<scalar_t>::mm,
0.f * unit<scalar_t>::mm,
0.f * unit<scalar_t>::mm});

// Iterate through uniformly distributed momentum directions
for (auto traj : uniform_track_generator<free_track_parameters<algebra_t>>(
phi_steps, theta_steps, mom_mag)) {
for (auto traj : trk_generator_t{trk_gen_cfg}) {
tracks.push_back(traj);
}

if (do_sort) {
// Sort by theta angle
const auto traj_comp = [](const auto &lhs, const auto &rhs) {
constexpr auto pi_2{constant<scalar_t>::pi_2};
return math::fabs(pi_2 - getter::theta(lhs.dir())) <
math::fabs(pi_2 - getter::theta(lhs.dir()));
// return getter::theta(lhs.dir()) < getter::theta(lhs.dir());
};

std::ranges::sort(tracks, traj_comp);
}
}

template <propagate_option opt>
static void BM_PROPAGATOR_CPU(benchmark::State &state) {

std::size_t n_tracks{static_cast<std::size_t>(state.range(0)) *
static_cast<std::size_t>(state.range(0))};

// Create the toy geometry and bfield
auto [det, names] = build_toy_detector(host_mr, toy_cfg);
test::vector3 B{0.f, 0.f, 2.f * unit<scalar>::T};
Expand All @@ -109,8 +139,7 @@ static void BM_PROPAGATOR_CPU(benchmark::State &state) {

// Get tracks
vecmem::vector<free_track_parameters<algebra_t>> tracks(&host_mr);
fill_tracks(tracks, static_cast<std::size_t>(state.range(0)),
static_cast<std::size_t>(state.range(0)));
fill_tracks(tracks, n_tracks);

total_tracks += tracks.size();

Expand Down
56 changes: 43 additions & 13 deletions tests/benchmarks/cuda/benchmark_propagator_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,61 @@ auto toy_cfg =
toy_det_config{}.n_brl_layers(4u).n_edc_layers(7u).do_check(false);

void fill_tracks(vecmem::vector<free_track_parameters<algebra_t>> &tracks,
const std::size_t theta_steps, const std::size_t phi_steps) {
// Set momentum of tracks
const scalar mom_mag{10.f * unit<scalar>::GeV};
const std::size_t n_tracks, bool do_sort = true) {
using scalar_t = dscalar<algebra_t>;
using uniform_gen_t =
detail::random_numbers<scalar_t,
std::uniform_real_distribution<scalar_t>>;
using trk_generator_t =
random_track_generator<free_track_parameters<algebra_t>, uniform_gen_t>;

trk_generator_t::configuration trk_gen_cfg{};
trk_gen_cfg.seed(42u);
trk_gen_cfg.n_tracks(n_tracks);
trk_gen_cfg.phi_range(-constant<scalar_t>::pi, constant<scalar_t>::pi);
trk_gen_cfg.eta_range(-3.f, 3.f);
trk_gen_cfg.mom_range(1.f * unit<scalar_t>::GeV,
100.f * unit<scalar_t>::GeV);
trk_gen_cfg.origin({0.f, 0.f, 0.f});
trk_gen_cfg.origin_stddev({0.f * unit<scalar_t>::mm,
0.f * unit<scalar_t>::mm,
0.f * unit<scalar_t>::mm});

// Iterate through uniformly distributed momentum directions
for (auto traj : uniform_track_generator<free_track_parameters<algebra_t>>(
phi_steps, theta_steps, mom_mag)) {
for (auto traj : trk_generator_t{trk_gen_cfg}) {
tracks.push_back(traj);
}

if (do_sort) {
// Sort by theta angle
const auto traj_comp = [](const auto &lhs, const auto &rhs) {
constexpr auto pi_2{constant<scalar_t>::pi_2};
return math::fabs(pi_2 - getter::theta(lhs.dir())) <
math::fabs(pi_2 - getter::theta(lhs.dir()));
// return getter::theta(lhs.dir()) < getter::theta(lhs.dir());
};

std::ranges::sort(tracks, traj_comp);
}
}

template <propagate_option opt>
static void BM_PROPAGATOR_CUDA(benchmark::State &state) {

std::size_t n_tracks{static_cast<std::size_t>(state.range(0)) *
static_cast<std::size_t>(state.range(0))};

// Create the toy geometry
auto [det, names] = build_toy_detector(bp_mng_mr, toy_cfg);
auto [det, names] = build_toy_detector(host_mr, toy_cfg);
test::vector3 B{0.f, 0.f, 2.f * unit<scalar>::T};
auto bfield = bfield::create_const_field(B);

// Get detector data
auto det_data = detray::get_data(det);

// vecmem copy helper object
vecmem::cuda::copy copy;
vecmem::cuda::copy cuda_cpy;

// Copy detector to device
auto det_buff = detray::get_buffer(det, dev_mr, cuda_cpy);
auto det_view = detray::get_data(det_buff);

std::size_t total_tracks = 0;

Expand All @@ -68,8 +99,7 @@ static void BM_PROPAGATOR_CUDA(benchmark::State &state) {

// Get tracks
vecmem::vector<free_track_parameters<algebra_t>> tracks(&bp_mng_mr);
fill_tracks(tracks, static_cast<std::size_t>(state.range(0)),
static_cast<std::size_t>(state.range(0)));
fill_tracks(tracks, n_tracks);

total_tracks += tracks.size();

Expand All @@ -79,7 +109,7 @@ static void BM_PROPAGATOR_CUDA(benchmark::State &state) {
auto tracks_data = vecmem::get_data(tracks);

// Run the propagator test for GPU device
propagator_benchmark(det_data, bfield, tracks_data, opt);
propagator_benchmark(det_view, bfield, tracks_data, opt);
}

state.counters["TracksPropagated"] = benchmark::Counter(
Expand Down

0 comments on commit 2d2a7cb

Please sign in to comment.