Benchmarks

This example times the shape functions with the methodology of the shape benchmarks of VecGeom, so that the results can be compared with it.

Note that

You can also download this example as a Jupyter notebook and a plain Julia source file.

Table of contents

Loading the modules

using Geom4hep, Printf
using CairoMakie: CairoMakie, Figure, Axis, barplot!

Benchmarking a shape

benchmark_shape places the shape in a world box, samples points in the box (half of them inside the shape, as VecGeom) and directions (with 80% of the rays from outside hitting the shape), and times each shape function. :shape calls the function on the shape directly, :model through the dispatch of a GeometryModel, as the navigator does. The result has one row per mode and function, with the time per call in ns.

rows = benchmark_shape(Torus{Float64}(1.2, 3.1, 5, 0, 2π); npoints=1024, nrep=10, verbose=false)
for r in rows
    @printf "%-6s %-16s %8.1f ns\n" r.mode r.op r.ns
end
shape  inside                7.7 ns
shape  contains              8.2 ns
shape  distance_to_in      827.7 ns
shape  safety_to_in          7.3 ns
shape  distance_to_out     434.7 ns
shape  safety_to_out         8.3 ns
model  inside                8.2 ns
model  contains              8.5 ns
model  distance_to_in      842.0 ns
model  safety_to_in          7.7 ns
model  distance_to_out     441.9 ns
model  safety_to_out         7.9 ns

The VecGeom suite

vecgeom_suite has the setups of the VecGeom benchmark programs (test/benchmark/*Benchmark.cpp) for the shapes of Geom4hep: the same shape parameters, world box, placement and numbers of points.

suite = vecgeom_suite(Float64)
[(b.name, b.vecgeom) for b in suite][1:6]
6-element Vector{Tuple{String, String}}:
 ("Box", "BoxBenchmark")
 ("Trd", "TrdBenchmark")
 ("Trap type 0", "TrapezoidBenchmark -type 0")
 ("Trap type 2", "TrapezoidBenchmark -type 2")
 ("Trap type 3", "TrapezoidBenchmark -type 3")
 ("Trap type 4", "TrapezoidBenchmark -type 4")

Running the suite gives the time per call of each function for each shape:

results = Dict(b.name => benchmark_shape(b; verbose=false) for b in suite)
ops = (:inside, :distance_to_in, :distance_to_out, :safety_to_in, :safety_to_out)
@printf "%-18s" "case"; foreach(op -> @printf("%16s", op), ops); println()
for b in suite
    @printf "%-18s" b.name
    for op in ops
        @printf "%16.1f" only(r.ns for r in results[b.name] if r.mode == :shape && r.op == op)
    end
    println()
end
case                        inside  distance_to_in distance_to_out    safety_to_in   safety_to_out
Box                            3.2            33.5             4.1            14.6             2.9
Trd                           13.6            38.8             8.3            13.7             3.8
Trap type 0                   17.0            32.2            17.7            17.8            13.6
Trap type 2                   17.1            33.5            17.9            17.8            13.5
Trap type 3                   17.1            32.7            17.7            18.1            13.7
Trap type 4                   16.6            33.2            17.8            17.8            13.4
Trap type 5                   16.7            33.3            17.8            18.2            13.6
Tube                           7.6            19.6             6.9             3.6             2.7
Cone                           8.9            31.4            32.7             6.1             4.5
CutTube                       18.9           112.1            52.1            29.0            26.3
Sphere                         6.7            44.7            16.5             7.3             4.6
Orb                            7.8            38.7            10.8             5.9             4.7
Polycone type 0               11.1            44.0            52.1            23.0            26.1
Polycone type 1                9.1            24.5            26.2             6.6            15.5
Polycone type 2               11.1            37.9            47.1            19.3            28.8
Polycone type 3               11.4            46.1            59.8            19.9            27.1
Polyhedron type 0             48.2           179.7           245.6            83.0           106.5
Polyhedron type 1             50.3           247.5           247.0            89.8           115.2
Polyhedron type 2             44.4           248.5           189.4            74.6           106.3
Polyhedron type 3             54.7           256.3           252.6            95.4           116.9
Polyhedron type 4             69.1           568.8           378.7           160.0           121.5
Polyhedron type 5             45.8           169.2           253.7            88.7           116.5
Torus                         10.4           755.2           447.2             9.3             7.6
MultiUnion                   309.0          1533.2          1223.0           529.1           526.4

The times per call of distance_to_in, for each shape:

names = [b.name for b in suite]
times = [only(r.ns for r in results[n] if r.mode == :shape && r.op == :distance_to_in) for n in names]
fig = Figure(size=(900, 400))
ax = Axis(fig[1, 1], ylabel="ns per call", yscale=log10, xticks=(1:length(names), names),
          xticklabelrotation=π/3, title="distance_to_in")
barplot!(ax, 1:length(names), times)
fig

Comparison with VecGeom

The script examples/Benchmarks.jl of the repository runs the suite and, given a VecGeom build configured with -DBUILD_TESTING=ON -DVECGEOM_TEST_BENCHMARK=ON, the VecGeom benchmark programs with the same numbers of points and repetitions, and prints the ratios of the times:

julia --project=examples examples/Benchmarks.jl --vecgeom <vecgeom-build>/test/benchmark

Note that the timings of these examples, run while building the documentation on a shared machine, are only indicative.


This page was generated using Literate.jl.