-
Notifications
You must be signed in to change notification settings - Fork 8
Add benchmarks #18
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
Merged
Merged
Add benchmarks #18
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b58f977
Add benchmarks
efaulhaber c78229b
Add benchmarks to CI tests
efaulhaber 42a82d4
Add benchmarks to tests
efaulhaber 656389e
Add test dependencies
efaulhaber b201ca0
Fix typo
efaulhaber 6f26448
Merge branch 'main' into ef/benchmarks
efaulhaber b51c2dd
Add `PrecomputedNeighborhoodSearch` to benchmark plot
efaulhaber 39cdd47
Fix typo
efaulhaber d9d366e
Merge branch 'main' into ef/benchmarks
efaulhaber 692f7c1
Migrate to v0.3
efaulhaber ed9e991
Reformat
efaulhaber 7fcf61c
Fix benchmarks
efaulhaber 561d83b
Merge branch 'main' into ef/benchmarks
svchb eb64d9c
Merge branch 'main' into ef/benchmarks
svchb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| include("count_neighbors.jl") | ||
| include("n_body.jl") | ||
|
|
||
| include("plot.jl") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using PointNeighbors | ||
| using BenchmarkTools | ||
|
|
||
| """ | ||
| benchmark_count_neighbors(neighborhood_search, coordinates; parallel = true) | ||
|
|
||
| A very cheap and simple neighborhood search benchmark, only counting the neighbors of each | ||
| particle. For each particle-neighbor pair, only an array entry is incremented. | ||
|
|
||
| Due to the minimal computational cost, differences between neighborhood search | ||
| implementations are highlighted. On the other hand, this is the least realistic benchmark. | ||
|
|
||
| For a computationally heavier benchmark, see [`benchmark_n_body`](@ref). | ||
| """ | ||
| function benchmark_count_neighbors(neighborhood_search, coordinates; parallel = true) | ||
| n_neighbors = zeros(Int, size(coordinates, 2)) | ||
|
|
||
| function count_neighbors!(n_neighbors, coordinates, neighborhood_search, parallel) | ||
| n_neighbors .= 0 | ||
|
|
||
| for_particle_neighbor(coordinates, coordinates, neighborhood_search, | ||
| parallel = parallel) do i, _, _, _ | ||
| n_neighbors[i] += 1 | ||
| end | ||
| end | ||
|
|
||
| return @belapsed $count_neighbors!($n_neighbors, $coordinates, | ||
| $neighborhood_search, $parallel) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using PointNeighbors | ||
| using BenchmarkTools | ||
|
|
||
| """ | ||
| benchmark_n_body(neighborhood_search, coordinates; parallel = true) | ||
|
|
||
| A simple neighborhood search benchmark, computing the right-hand side of an n-body | ||
| simulation with a cutoff (corresponding to the search radius of `neighborhood_search`). | ||
|
|
||
| This is a more realistic benchmark for particle-based simulations than | ||
| [`benchmark_count_neighbors`](@ref). | ||
| However, due to the higher computational cost, differences between neighborhood search | ||
| implementations are less pronounced. | ||
| """ | ||
| function benchmark_n_body(neighborhood_search, coordinates; parallel = true) | ||
| mass = 1e10 * (rand(size(coordinates, 2)) .+ 1) | ||
| G = 6.6743e-11 | ||
|
|
||
| dv = similar(coordinates) | ||
|
|
||
| function compute_acceleration!(dv, coordinates, mass, G, neighborhood_search, parallel) | ||
| dv .= 0.0 | ||
|
|
||
| for_particle_neighbor(coordinates, coordinates, neighborhood_search, | ||
| parallel = parallel) do i, j, pos_diff, distance | ||
| # Only consider particles with a distance > 0 | ||
| distance < sqrt(eps()) && return | ||
|
|
||
| dv_ = -G * mass[j] * pos_diff / distance^3 | ||
|
|
||
| for dim in axes(dv, 1) | ||
| @inbounds dv[dim, i] += dv_[dim] | ||
| end | ||
| end | ||
|
|
||
| return dv | ||
| end | ||
|
|
||
| return @belapsed $compute_acceleration!($dv, $coordinates, $mass, $G, | ||
| $neighborhood_search, $parallel) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| using Plots | ||
| using BenchmarkTools | ||
|
|
||
| # Generate a rectangular point cloud | ||
| include("../test/point_cloud.jl") | ||
|
|
||
| """ | ||
| plot_benchmarks(benchmark, n_points_per_dimension, iterations; | ||
| seed = 1, perturbation_factor_position = 1.0, | ||
| parallel = true, title = "") | ||
|
|
||
| Run a benchmark for with several neighborhood searches multiple times for increasing numbers | ||
| of points and plot the results. | ||
|
|
||
| # Arguments | ||
| - `benchmark`: The benchmark function. See [`benchmark_count_neighbors`](@ref) | ||
| and [`benchmark_n_body`](@ref). | ||
| - `n_points_per_dimension`: Initial resolution as tuple. The product is the initial number | ||
| of points. For example, use `(100, 100)` for a 2D benchmark or | ||
| `(10, 10, 10)` for a 3D benchmark. | ||
| - `iterations`: Number of refinement iterations | ||
|
|
||
| # Keywords | ||
| - `parallel = true`: Loop over all points in parallel | ||
| - `title = ""`: Title of the plot | ||
| - `seed = 1`: Seed to perturb the point positions. Different seeds yield | ||
| slightly different point positions. | ||
| - `perturbation_factor_position = 1.0`: Perturb point positions by this factor. A factor of | ||
| `1.0` corresponds to points being moved by | ||
| a maximum distance of `0.5` along each axis. | ||
| """ | ||
| function plot_benchmarks(benchmark, n_points_per_dimension, iterations; | ||
| parallel = true, title = "", | ||
| seed = 1, perturbation_factor_position = 1.0) | ||
| neighborhood_searches_names = ["TrivialNeighborhoodSearch";; | ||
| "GridNeighborhoodSearch";; | ||
| "ProcomputedNeighborhoodSearch"] | ||
|
|
||
| # Multiply number of points in each iteration (roughly) by this factor | ||
| scaling_factor = 4 | ||
| per_dimension_factor = scaling_factor^(1 / length(n_points_per_dimension)) | ||
| sizes = [round.(Int, n_points_per_dimension .* per_dimension_factor^(iter - 1)) | ||
| for iter in 1:iterations] | ||
|
|
||
| n_particles = prod.(sizes) | ||
| times = zeros(iterations, length(neighborhood_searches_names)) | ||
|
|
||
| for iter in 1:iterations | ||
| coordinates = point_cloud(sizes[iter], seed = seed, | ||
| perturbation_factor_position = perturbation_factor_position) | ||
|
|
||
| search_radius = 3.0 | ||
|
|
||
| neighborhood_searches = [ | ||
| TrivialNeighborhoodSearch{size(coordinates, 1)}(search_radius, | ||
| axes(coordinates, 2)), | ||
| GridNeighborhoodSearch{size(coordinates, 1)}(search_radius, | ||
| size(coordinates, 2)), | ||
| PrecomputedNeighborhoodSearch{size(coordinates, 1)}(search_radius, | ||
| size(coordinates, 2)), | ||
| ] | ||
|
|
||
| for i in eachindex(neighborhood_searches) | ||
| neighborhood_search = neighborhood_searches[i] | ||
| initialize!(neighborhood_search, coordinates, coordinates) | ||
|
|
||
| time = benchmark(neighborhood_search, coordinates, parallel = parallel) | ||
| times[iter, i] = time | ||
| time_string = BenchmarkTools.prettytime(time * 1e9) | ||
| println("$(neighborhood_searches_names[i])") | ||
| println("with $(join(sizes[iter], "x")) = $(prod(sizes[iter])) particles finished in $time_string\n") | ||
| end | ||
| end | ||
|
|
||
| plot(n_particles, times, | ||
| xaxis = :log, yaxis = :log, | ||
| xticks = (n_particles, n_particles), | ||
| xlabel = "#particles", ylabel = "Runtime [s]", | ||
| legend = :outerright, size = (750, 400), dpi = 200, | ||
| label = neighborhood_searches_names, title = title) | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Check that all benchmarks are running without errors. | ||
| # Note that these are only smoke tests, not verifying the result. | ||
| # Also note that these tests are run without coverage checks, since we want to | ||
| # cover everything with unit tests. | ||
| @testset verbose=true "Benchmarks" begin | ||
| include("../benchmarks/benchmarks.jl") | ||
|
|
||
| @testset verbose=true "$(length(size))D" for size in [(50,), (10, 10), (5, 5, 5)] | ||
svchb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @testset verbose=true "`benchmark_count_neighbors`" begin | ||
| @test_nowarn_mod plot_benchmarks(benchmark_count_neighbors, size, 2) | ||
| end | ||
|
|
||
| @testset verbose=true "`benchmark_n_body`" begin | ||
| @test_nowarn_mod plot_benchmarks(benchmark_n_body, size, 2) | ||
| end | ||
| end | ||
| end; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| include("test_util.jl") | ||
|
|
||
| const POINTNEIGHBORS_TEST = lowercase(get(ENV, "POINTNEIGHBORS_TEST", "all")) | ||
|
|
||
| @testset verbose=true "PointNeighbors.jl Tests" begin | ||
| include("nhs_trivial.jl") | ||
| include("nhs_grid.jl") | ||
| include("neighborhood_search.jl") | ||
| end | ||
| if POINTNEIGHBORS_TEST in ("all", "unit") | ||
| include("unittest.jl") | ||
| end | ||
|
|
||
| if POINTNEIGHBORS_TEST in ("all", "benchmarks") | ||
| include("benchmarks.jl") | ||
| end | ||
| end; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Separate file that can be executed to only run unit tests. | ||
| # Include `test_util.jl` first. | ||
| @testset verbose=true "Unit Tests" begin | ||
| include("nhs_trivial.jl") | ||
| include("nhs_grid.jl") | ||
| include("neighborhood_search.jl") | ||
| end; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.