Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple benchmark #611

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions benchmark/benchmark.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using TrixiParticles
using BenchmarkTools

# Suppress the output of the simulation
function suppress_simulation_output(f)
open("/dev/null", "w") do devnull
redirect_stdout(devnull) do
redirect_stderr(devnull) do
f()
end
end
end
end

# Benchmark using @btime and suppress output
result = @belapsed suppress_simulation_output(() -> trixi_include(@__MODULE__,
joinpath(examples_dir(),
"fluid",
"dam_break_2d.jl"),
saving_callback=nothing,
saving_paper=nothing,
fluid_particle_spacing=0.01))

# Print the timing result in nanoseconds
println(result)
33 changes: 33 additions & 0 deletions benchmark/plot_benchmark.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using CSV
using DataFrames
using CairoMakie
using FileIO

csv_file = "benchmark_results.csv"

if !isfile(csv_file)
println("Error: File '$csv_file' does not exist. Please make sure the file is available and try again.")
return
end

df = CSV.read(csv_file, DataFrame, header=1, skipto=3)

longest_time = maximum(df.Time)
df.Speedup = longest_time ./ df.Time

x_ticks = df.Threads

fig = Figure(size=(800, 600))
ax = Axis(fig[1, 1],
title="Strong Scaling Speedup",
xlabel="Number of Threads",
ylabel="Speedup",
xticks=(x_ticks, string.(x_ticks)))

line_plot = lines!(ax, df.Threads, df.Speedup, color=:blue, label="Speedup Curve")
scatter_plot = scatter!(ax, df.Threads, df.Speedup, color=:red, markersize=10,
label="Data Points")

axislegend(ax, position=:rb)

save("benchmark_speedup.svg", fig)
64 changes: 64 additions & 0 deletions benchmark/run_strong_scaling.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash

######################################################################################
# Benchmark Script
#
# This script automates the benchmarking process for TrixiParticles by running the
# benchmark script (`benchmark/benchmark.jl`) with varying thread counts. It starts
# from a minimum number of threads (default: 1) and doubles the number of threads
# each iteration until the maximum number of threads (based on CPU or overridden)
# is reached.
#
# Output:
# - The results are saved in a CSV file called "benchmark_results.csv", with columns:
# - Threads: Number of threads used during the benchmark run.
# - Time: The time taken to complete the benchmark (in seconds).
# - Units are saved in the second row, indicating the time in seconds.
#
# Environment Variables:
# - OVERRIDE_MAX_THREADS: Optionally, the maximum number of threads can be overridden
# by setting this environment variable. If not set, the system's CPU thread count
# will be used.
# - OVERRIDE_MIN_THREADS: Optionally, the minimum number of threads can be overridden
# by setting this environment variable. If not set, the default is 1.
#
# Example Usage:
# - Run with default thread settings:
# ./benchmark_script.sh
# - Override maximum threads:
# OVERRIDE_MAX_THREADS=8 ./benchmark_script.sh
# - Override both minimum and maximum threads:
# OVERRIDE_MIN_THREADS=2 OVERRIDE_MAX_THREADS=8 ./benchmark_script.sh
######################################################################################

# Get the total number of CPU threads available on the system
DEFAULT_MAX_THREADS=$(julia -e 'println(Sys.CPU_THREADS)')
DEFAULT_MIN_THREADS=1

MAX_THREADS=${OVERRIDE_MAX_THREADS:-$DEFAULT_MAX_THREADS}
MIN_THREADS=${OVERRIDE_MIN_THREADS:-$DEFAULT_MIN_THREADS}

# Initialize the output file with headers and units
OUTPUT_FILE="benchmark_results.csv"
echo "Threads,Time" > $OUTPUT_FILE
echo "[1],[s]" >> $OUTPUT_FILE

CURRENT_THREADS=$MIN_THREADS

while [ $CURRENT_THREADS -le $MAX_THREADS ]; do
echo "Running benchmark with $CURRENT_THREADS threads..."

# Run the benchmark using the specified number of threads and capture the timing result
BENCHMARK_TIME=$(julia --project=. --threads=$CURRENT_THREADS benchmark/benchmark.jl)

# Append the number of threads and timing result to the CSV file
echo "$CURRENT_THREADS,$BENCHMARK_TIME" >> $OUTPUT_FILE

CURRENT_THREADS=$((CURRENT_THREADS * 2))
done

echo "Benchmarking complete. Results saved to $OUTPUT_FILE."

julia plot_benchmark.jl

echo "Plotting complete. Plot saved as benchmark_speedup.svg."
7 changes: 7 additions & 0 deletions benchmark_results.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Threads,Time_ns
4,44.130856019
8,26.929279594
16,17.867562992
32,12.621583571
64,11.243286411
128,22.692239428
Loading