Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:11

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 // Detray core include(s).
0010 #include "detray/definitions/containers.hpp"
0011 #include "detray/definitions/indexing.hpp"
0012 #include "detray/geometry/mask.hpp"
0013 #include "detray/geometry/shapes.hpp"
0014 #include "detray/geometry/surface_descriptor.hpp"
0015 #include "detray/navigation/intersection/ray_intersector.hpp"
0016 #include "detray/tracks/ray.hpp"
0017 
0018 // Detray benchmark include(s)
0019 #include "detray/benchmarks/types.hpp"
0020 
0021 // Detray test include(s)
0022 #include "detray/test/common/track_generators.hpp"
0023 #include "detray/test/utils/planes_along_direction.hpp"
0024 
0025 // Google Benchmark include(s)
0026 #include <benchmark/benchmark.h>
0027 
0028 // Use the detray:: namespace implicitly.
0029 using namespace detray;
0030 
0031 using bench_algebra = benchmarks::algebra;
0032 
0033 using ray_generator_t = uniform_track_generator<detail::ray<bench_algebra>>;
0034 
0035 static const unsigned int theta_steps = 100u;
0036 static const unsigned int phi_steps = 100u;
0037 
0038 static const dvector<benchmarks::scalar> dists = {1.f, 2.f, 3.f, 4.f, 5.f,
0039                                                   6.f, 7.f, 8.f, 9.f, 10.f};
0040 
0041 /// This benchmark runs intersection with the planar intersector
0042 void BM_INTERSECT_PLANES(benchmark::State &state) {
0043   unsigned int sfhit = 0u;
0044   unsigned int sfmiss = 0u;
0045 
0046   auto [plane_descs, transforms] = test::planes_along_direction(
0047       dists, vector::normalize(benchmarks::vector3{1.f, 1.f, 1.f}));
0048   constexpr mask<rectangle2D, bench_algebra> rect{0u, 10.f, 20.f};
0049 
0050   // Iterate through uniformly distributed momentum directions
0051   auto ray_generator = ray_generator_t{};
0052   ray_generator.config().theta_steps(theta_steps).phi_steps(phi_steps);
0053 
0054   for (auto _ : state) {
0055     benchmark::DoNotOptimize(sfhit);
0056     benchmark::DoNotOptimize(sfmiss);
0057 
0058     // Iterate through uniformly distributed momentum directions
0059     for (const auto ray : ray_generator) {
0060       for (const auto &desc : plane_descs) {
0061         auto pi = ray_intersector<rectangle2D, bench_algebra>{};
0062         auto is = pi(ray, desc, rect, transforms[desc.transform()]);
0063 
0064         benchmark::DoNotOptimize(sfhit);
0065         benchmark::DoNotOptimize(sfmiss);
0066         if (is.is_inside()) {
0067           ++sfhit;
0068         } else {
0069           ++sfmiss;
0070         }
0071         benchmark::ClobberMemory();
0072       }
0073     }
0074   }
0075 }
0076 
0077 BENCHMARK(BM_INTERSECT_PLANES)
0078 #ifdef DETRAY_BENCHMARK_MULTITHREAD
0079     ->ThreadRange(1, benchmark::CPUInfo::Get().num_cpus)
0080 #endif
0081     ->Unit(benchmark::kMillisecond);
0082 
0083 namespace {
0084 
0085 enum class mask_id : unsigned int {
0086   e_rectangle2D = 0,
0087   e_cylinder2D = 1,
0088   e_conc_cylinder3 = 2,
0089 };
0090 
0091 enum class material_id : unsigned int {
0092   e_material_slab = 0,
0093 };
0094 
0095 }  // namespace
0096 
0097 using mask_link_t = dtyped_index<mask_id, dindex>;
0098 using material_link_t = dtyped_index<material_id, dindex>;
0099 
0100 using surface_desc_t = surface_descriptor<mask_link_t, material_link_t>;
0101 using intersection_t = intersection2D<surface_desc_t, bench_algebra>;
0102 
0103 /// This benchmark runs intersection with the cylinder intersector
0104 void BM_INTERSECT_CYLINDERS(benchmark::State &state) {
0105   using cylinder_mask = mask<cylinder2D, bench_algebra>;
0106 
0107   unsigned int sfhit = 0u;
0108   unsigned int sfmiss = 0u;
0109   dvector<cylinder_mask> cylinders;
0110 
0111   for (benchmarks::scalar r : dists) {
0112     cylinders.push_back(cylinder_mask{0u, r, -10.f, 10.f});
0113   }
0114 
0115   benchmarks::transform3 trf{};
0116 
0117   mask_link_t mask_link{mask_id::e_cylinder2D, 0};
0118   material_link_t material_link{material_id::e_material_slab, 0};
0119   surface_desc_t sf_desc(0u, mask_link, material_link, 0u,
0120                          surface_id::e_sensitive);
0121 
0122   // Iterate through uniformly distributed momentum directions
0123   auto ray_generator = ray_generator_t{};
0124   ray_generator.config().theta_steps(theta_steps).phi_steps(phi_steps);
0125 
0126   for (auto _ : state) {
0127     benchmark::DoNotOptimize(sfhit);
0128     benchmark::DoNotOptimize(sfmiss);
0129 
0130     // Iterate through uniformly distributed momentum directions
0131     for (const auto ray : ray_generator) {
0132       for (const auto &cylinder : cylinders) {
0133         auto ci = ray_intersector<cylinder2D, bench_algebra>{};
0134         auto inters = ci(ray, sf_desc, cylinder, trf);
0135 
0136         benchmark::DoNotOptimize(sfhit);
0137         benchmark::DoNotOptimize(sfmiss);
0138         for (const auto &sfi : inters) {
0139           if (sfi.is_inside()) {
0140             ++sfhit;
0141           } else {
0142             ++sfmiss;
0143           }
0144         }
0145         benchmark::ClobberMemory();
0146       }
0147     }
0148   }
0149 }
0150 
0151 BENCHMARK(BM_INTERSECT_CYLINDERS)
0152 #ifdef DETRAY_BENCHMARKS_MULTITHREAD
0153     ->ThreadRange(1, benchmark::CPUInfo::Get().num_cpus)
0154 #endif
0155     ->Unit(benchmark::kMillisecond);
0156 
0157 /// This benchmark runs intersection with a specialized portal cylinder
0158 /// intersector
0159 void BM_INTERSECT_PORTAL_CYLINDERS(benchmark::State &state) {
0160   using cylinder_mask = mask<concentric_cylinder2D, bench_algebra>;
0161 
0162   unsigned int sfhit = 0u;
0163   unsigned int sfmiss = 0u;
0164   dvector<cylinder_mask> cylinders;
0165 
0166   for (benchmarks::scalar r : dists) {
0167     cylinders.push_back(cylinder_mask{0u, r, -10.f, 10.f});
0168   }
0169 
0170   benchmarks::transform3 trf{};
0171 
0172   mask_link_t mask_link{mask_id::e_cylinder2D, 0u};
0173   material_link_t material_link{material_id::e_material_slab, 0u};
0174   surface_desc_t sf_desc(0u, mask_link, material_link, 0u,
0175                          surface_id::e_sensitive);
0176 
0177   // Iterate through uniformly distributed momentum directions
0178   auto ray_generator = ray_generator_t{};
0179   ray_generator.config().theta_steps(theta_steps).phi_steps(phi_steps);
0180 
0181   for (auto _ : state) {
0182     benchmark::DoNotOptimize(sfhit);
0183     benchmark::DoNotOptimize(sfmiss);
0184 
0185     // Iterate through uniformly distributed momentum directions
0186     for (const auto ray : ray_generator) {
0187       for (const auto &cylinder : cylinders) {
0188         auto cpi = ray_intersector<concentric_cylinder2D, bench_algebra>{};
0189         auto is = cpi(ray, sf_desc, cylinder, trf);
0190 
0191         benchmark::DoNotOptimize(sfhit);
0192         benchmark::DoNotOptimize(sfmiss);
0193         if (is.is_inside()) {
0194           ++sfhit;
0195         } else {
0196           ++sfmiss;
0197         }
0198         benchmark::ClobberMemory();
0199       }
0200     }
0201   }
0202 }
0203 
0204 BENCHMARK(BM_INTERSECT_PORTAL_CYLINDERS)
0205 #ifdef DETRAY_BENCHMARK_MULTITHREAD
0206     ->ThreadRange(1, benchmark::CPUInfo::Get().num_cpus)
0207 #endif
0208     ->Unit(benchmark::kMillisecond);