From 176f787b5989071cf33736a6b15ff8a998e2f062 Mon Sep 17 00:00:00 2001 From: Rene Gassmoeller Date: Wed, 9 Jun 2021 12:01:27 -0400 Subject: [PATCH] Only resort if necessary --- source/particles/particle_handler.cc | 30 +++++++++++++++++++++++++++- source/particles/property_pool.cc | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/source/particles/particle_handler.cc b/source/particles/particle_handler.cc index 52d47fc513..f41ada5324 100644 --- a/source/particles/particle_handler.cc +++ b/source/particles/particle_handler.cc @@ -1291,7 +1291,35 @@ namespace Particles // remove_particles also calls update_cached_numbers() remove_particles(particles_out_of_cell); - property_pool->sort_memory_slots(particles); + + // Sort the particle memory if we noticed during sorting + // the particles above that more than 10% of particle + // handles are non-consecutive. We do not want to do this + // if it is not necessary, because it involves a copy of + // all particle memory. + types::particle_index n_fragmented_handles = 0; + typename PropertyPool::Handle last_handle = + PropertyPool::invalid_handle; + for (const auto &particles_in_cell : particles) + for (const auto &particle : particles_in_cell) + { + if (last_handle == PropertyPool::invalid_handle) + { + last_handle = particle; + continue; + } + + if (particle != last_handle + 1) + ++n_fragmented_handles; + + last_handle = particle; + } + + if (n_fragmented_handles > n_locally_owned_particles() / 3) + { + property_pool->sort_memory_slots(particles); + } + } // namespace Particles diff --git a/source/particles/property_pool.cc b/source/particles/property_pool.cc index 94a1906964..950a70b8a5 100644 --- a/source/particles/property_pool.cc +++ b/source/particles/property_pool.cc @@ -178,7 +178,7 @@ namespace Particles std::vector sorted_ids(ids.size()); std::vector sorted_properties(properties.size()); - unsigned int i = 0; + Handle i = 0; for (auto &handles_in_cell : sorted_handles) for (auto &handle : handles_in_cell) { -- 2.39.5