From 23b646c7f79f3b0e852ae4e5bd7952ca43135412 Mon Sep 17 00:00:00 2001 From: Erik Faulhaber <44124897+efaulhaber@users.noreply.github.com> Date: Wed, 26 Jun 2024 16:12:12 +0200 Subject: [PATCH] Use serial update by default on one thread --- src/nhs_grid.jl | 7 ++++++- test/neighborhood_search.jl | 14 ++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/nhs_grid.jl b/src/nhs_grid.jl index 783cf75..04149ff 100644 --- a/src/nhs_grid.jl +++ b/src/nhs_grid.jl @@ -77,7 +77,12 @@ struct GridNeighborhoodSearch{NDIMS, update_strategy, update_strategy = nothing) where {NDIMS} ELTYPE = typeof(search_radius) - if isnothing(update_strategy) + if isnothing(update_strategy) && Threads.nthreads == 1 + # Use serial update on one thread to avoid a second loop over all particles + # when `:parallel` is picked. + update_strategy = :serial + elseif isnothing(update_strategy) + # Automatically choose best available update option for this cell list update_strategy = first(supported_update_strategies(cell_list)) elseif !(update_strategy in supported_update_strategies(cell_list)) throw(ArgumentError("$update_strategy is not a valid update strategy for " * diff --git a/test/neighborhood_search.jl b/test/neighborhood_search.jl index ae21441..45a0b8d 100644 --- a/test/neighborhood_search.jl +++ b/test/neighborhood_search.jl @@ -158,13 +158,19 @@ max_corner = maximum(coords, dims = 2) .+ search_radius neighborhood_searches = [ - GridNeighborhoodSearch{NDIMS}(; search_radius, n_points), + GridNeighborhoodSearch{NDIMS}(; search_radius, n_points, + # Note that `:semi_parallel` is only the + # default on multiple threads. + update_strategy = :semi_parallel), GridNeighborhoodSearch{NDIMS}(; search_radius, n_points, update_strategy = :serial), GridNeighborhoodSearch{NDIMS}(; search_radius, n_points, cell_list = FullGridCellList(; min_corner, max_corner, - search_radius)), + search_radius), + # Note that `:parallel` is only the + # default on multiple threads. + update_strategy = :parallel), GridNeighborhoodSearch{NDIMS}(; search_radius, n_points, cell_list = FullGridCellList(; min_corner, max_corner, @@ -179,9 +185,9 @@ ] names = [ - "`GridNeighborhoodSearch`", + "`GridNeighborhoodSearch` with `:semi_parallel` update", "`GridNeighborhoodSearch` with `:serial` update", - "`GridNeighborhoodSearch` with `FullGridCellList` with `DynamicVectorOfVectors`", + "`GridNeighborhoodSearch` with `FullGridCellList` with `DynamicVectorOfVectors` and `:parallel` update", "`GridNeighborhoodSearch` with `FullGridCellList` with `DynamicVectorOfVectors` and `:semi_parallel` update", "`GridNeighborhoodSearch` with `FullGridCellList` with `Vector{Vector}`", "`PrecomputedNeighborhoodSearch`",