From cec918abe4d415eb06a3c92fecbb1f88f9aa4422 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 11 Dec 2020 18:03:12 -0700 Subject: [PATCH] Make Particles::Property pool a template . --- include/deal.II/particles/particle.h | 26 +++++++----- include/deal.II/particles/particle_accessor.h | 4 +- include/deal.II/particles/particle_handler.h | 4 +- include/deal.II/particles/property_pool.h | 6 ++- source/particles/particle.cc | 29 ++++++++------ source/particles/particle_handler.cc | 12 +++--- source/particles/property_pool.cc | 40 ++++++++++++++----- 7 files changed, 76 insertions(+), 45 deletions(-) diff --git a/include/deal.II/particles/particle.h b/include/deal.II/particles/particle.h index dcf43716ea..3b107b2fc5 100644 --- a/include/deal.II/particles/particle.h +++ b/include/deal.II/particles/particle.h @@ -190,8 +190,8 @@ namespace Particles * contains serialized data of the same length and type that is allocated * by @p property_pool. */ - Particle(const void *& begin_data, - PropertyPool *const property_pool = nullptr); + Particle(const void *& begin_data, + PropertyPool *const property_pool = nullptr); /** * Move constructor for Particle, creates a particle from an existing @@ -360,7 +360,7 @@ namespace Particles * allocated in the new property pool. */ void - set_property_pool(PropertyPool &property_pool); + set_property_pool(PropertyPool &property_pool); /** * Return whether this particle has a valid property pool and a valid @@ -478,12 +478,12 @@ namespace Particles * A pointer to the property pool. Necessary to translate from the * handle to the actual memory locations. */ - PropertyPool *property_pool; + PropertyPool *property_pool; /** * A handle to all particle properties */ - PropertyPool::Handle properties; + typename PropertyPool::Handle properties; }; /* ---------------------- inline and template functions ------------------ */ @@ -523,7 +523,7 @@ namespace Particles { unsigned int n_properties = 0; if ((property_pool != nullptr) && - (properties != PropertyPool::invalid_handle)) + (properties != PropertyPool::invalid_handle)) n_properties = get_properties().size(); ar &location &reference_location &id &n_properties; @@ -591,13 +591,16 @@ namespace Particles template inline void - Particle::set_property_pool(PropertyPool &new_property_pool) + Particle::set_property_pool( + PropertyPool &new_property_pool) { // First, we do want to save any properties that may // have previously been set, and copy them over to the memory allocated // on the new pool - PropertyPool::Handle new_handle = PropertyPool::invalid_handle; - if (property_pool != nullptr && properties != PropertyPool::invalid_handle) + typename PropertyPool::Handle new_handle = + PropertyPool::invalid_handle; + if (property_pool != nullptr && + properties != PropertyPool::invalid_handle) { new_handle = new_property_pool.allocate_properties_array(); @@ -611,7 +614,8 @@ namespace Particles // If the particle currently has a reference to properties, then // release those. - if (property_pool != nullptr && properties != PropertyPool::invalid_handle) + if (property_pool != nullptr && + properties != PropertyPool::invalid_handle) property_pool->deallocate_properties_array(properties); @@ -639,7 +643,7 @@ namespace Particles Particle::has_properties() const { return (property_pool != nullptr) && - (properties != PropertyPool::invalid_handle); + (properties != PropertyPool::invalid_handle); } } // namespace Particles diff --git a/include/deal.II/particles/particle_accessor.h b/include/deal.II/particles/particle_accessor.h index c3083bfbde..5cbd0f20c6 100644 --- a/include/deal.II/particles/particle_accessor.h +++ b/include/deal.II/particles/particle_accessor.h @@ -127,7 +127,7 @@ namespace Particles * function is after particle transfer to a new process. */ void - set_property_pool(PropertyPool &property_pool); + set_property_pool(PropertyPool &property_pool); /** * Return whether this particle has a valid property pool and a valid @@ -384,7 +384,7 @@ namespace Particles template inline void ParticleAccessor::set_property_pool( - PropertyPool &new_property_pool) + PropertyPool &new_property_pool) { Assert(particle != map->end(), ExcInternalError()); diff --git a/include/deal.II/particles/particle_handler.h b/include/deal.II/particles/particle_handler.h index 3d459a5614..c7d8632b24 100644 --- a/include/deal.II/particles/particle_handler.h +++ b/include/deal.II/particles/particle_handler.h @@ -680,7 +680,7 @@ namespace Particles * Return a reference to the property pool that owns all particle * properties, and organizes them physically. */ - PropertyPool & + PropertyPool & get_property_pool() const; /** @@ -805,7 +805,7 @@ namespace Particles * precedes the declaration of the `particles` and `ghost_particles` * members. */ - std::unique_ptr property_pool; + std::unique_ptr> property_pool; /** * Set of particles currently living in the local domain, organized by diff --git a/include/deal.II/particles/property_pool.h b/include/deal.II/particles/property_pool.h index 0ca579208d..f733b65128 100644 --- a/include/deal.II/particles/property_pool.h +++ b/include/deal.II/particles/property_pool.h @@ -44,6 +44,7 @@ namespace Particles * memory with varying sizes per particle (this memory would not be managed by * this class). */ + template class PropertyPool { public: @@ -142,10 +143,13 @@ namespace Particles std::vector currently_available_handles; }; + + /* ---------------------- inline and template functions ------------------ */ + template inline ArrayView - PropertyPool::get_properties(const Handle handle) + PropertyPool::get_properties(const Handle handle) { const std::vector::size_type data_index = (handle != invalid_handle) ? handle * n_properties : 0; diff --git a/source/particles/particle.cc b/source/particles/particle.cc index 984b327dad..3a81b08718 100644 --- a/source/particles/particle.cc +++ b/source/particles/particle.cc @@ -27,7 +27,7 @@ namespace Particles , reference_location(numbers::signaling_nan>()) , id(0) , property_pool(nullptr) - , properties(PropertyPool::invalid_handle) + , properties(PropertyPool::invalid_handle) {} @@ -40,7 +40,7 @@ namespace Particles , reference_location(reference_location) , id(id) , property_pool(nullptr) - , properties(PropertyPool::invalid_handle) + , properties(PropertyPool::invalid_handle) {} @@ -51,7 +51,7 @@ namespace Particles , reference_location(particle.get_reference_location()) , id(particle.get_id()) , property_pool(particle.property_pool) - , properties(PropertyPool::invalid_handle) + , properties(PropertyPool::invalid_handle) { if (particle.has_properties()) { @@ -70,8 +70,9 @@ namespace Particles template - Particle::Particle(const void *& data, - PropertyPool *const new_property_pool) + Particle::Particle( + const void *& data, + PropertyPool *const new_property_pool) { const types::particle_index *id_data = static_cast(data); @@ -88,7 +89,7 @@ namespace Particles if (property_pool != nullptr) properties = property_pool->allocate_properties_array(); else - properties = PropertyPool::invalid_handle; + properties = PropertyPool::invalid_handle; // See if there are properties to load if (has_properties()) @@ -113,7 +114,7 @@ namespace Particles , property_pool(std::move(particle.property_pool)) , properties(std::move(particle.properties)) { - particle.properties = PropertyPool::invalid_handle; + particle.properties = PropertyPool::invalid_handle; } @@ -142,7 +143,7 @@ namespace Particles my_properties.begin()); } else - properties = PropertyPool::invalid_handle; + properties = PropertyPool::invalid_handle; } return *this; } @@ -165,7 +166,7 @@ namespace Particles // We stole the rhs's properties, so we need to invalidate // the handle the rhs holds lest it releases the memory that // we still reference here. - particle.properties = PropertyPool::invalid_handle; + particle.properties = PropertyPool::invalid_handle; } return *this; } @@ -175,7 +176,8 @@ namespace Particles template Particle::~Particle() { - if (property_pool != nullptr && properties != PropertyPool::invalid_handle) + if (property_pool != nullptr && + properties != PropertyPool::invalid_handle) property_pool->deallocate_properties_array(properties); } @@ -183,7 +185,8 @@ namespace Particles void Particle::free_properties() { - if (property_pool != nullptr && properties != PropertyPool::invalid_handle) + if (property_pool != nullptr && + properties != PropertyPool::invalid_handle) property_pool->deallocate_properties_array(properties); } @@ -274,7 +277,7 @@ namespace Particles Assert(property_pool != nullptr, ExcInternalError()); // If we haven't allocated memory yet, do so now - if (properties == PropertyPool::invalid_handle) + if (properties == PropertyPool::invalid_handle) properties = property_pool->allocate_properties_array(); const ArrayView property_values = @@ -304,7 +307,7 @@ namespace Particles Assert(property_pool != nullptr, ExcInternalError()); // If this particle has no properties yet, allocate and initialize them. - if (properties == PropertyPool::invalid_handle) + if (properties == PropertyPool::invalid_handle) { properties = property_pool->allocate_properties_array(); diff --git a/source/particles/particle_handler.cc b/source/particles/particle_handler.cc index 539415d8c4..797c2f1e52 100644 --- a/source/particles/particle_handler.cc +++ b/source/particles/particle_handler.cc @@ -52,8 +52,8 @@ namespace Particles std::vector> unpack_particles( const boost::iterator_range::const_iterator> - & data_range, - PropertyPool &property_pool) + & data_range, + PropertyPool &property_pool) { std::vector> particles; @@ -88,7 +88,7 @@ namespace Particles ParticleHandler::ParticleHandler() : triangulation() , mapping() - , property_pool(std::make_unique(0)) + , property_pool(std::make_unique>(0)) , particles() , ghost_particles() , global_number_of_particles(0) @@ -109,7 +109,7 @@ namespace Particles const unsigned int n_properties) : triangulation(&triangulation, typeid(*this).name()) , mapping(&mapping, typeid(*this).name()) - , property_pool(std::make_unique(n_properties)) + , property_pool(std::make_unique>(n_properties)) , particles() , ghost_particles() , global_number_of_particles(0) @@ -137,7 +137,7 @@ namespace Particles mapping = &new_mapping; // Create the memory pool that will store all particle properties - property_pool = std::make_unique(n_properties); + property_pool = std::make_unique>(n_properties); // Create the grid cache to cache the information about the triangulation // that is used to locate the particles into subdomains and cells @@ -879,7 +879,7 @@ namespace Particles template - PropertyPool & + PropertyPool & ParticleHandler::get_property_pool() const { return *property_pool; diff --git a/source/particles/property_pool.cc b/source/particles/property_pool.cc index 0ddb28dfda..f204d83a01 100644 --- a/source/particles/property_pool.cc +++ b/source/particles/property_pool.cc @@ -20,24 +20,31 @@ DEAL_II_NAMESPACE_OPEN namespace Particles { - const PropertyPool::Handle PropertyPool::invalid_handle = - static_cast(-1); + template + const typename PropertyPool::Handle + PropertyPool::invalid_handle = static_cast(-1); - PropertyPool::PropertyPool(const unsigned int n_properties_per_slot) + + template + PropertyPool::PropertyPool( + const unsigned int n_properties_per_slot) : n_properties(n_properties_per_slot) {} - PropertyPool::~PropertyPool() + + template + PropertyPool::~PropertyPool() { clear(); } + template void - PropertyPool::clear() + PropertyPool::clear() { if (n_properties > 0) { @@ -63,8 +70,9 @@ namespace Particles - PropertyPool::Handle - PropertyPool::allocate_properties_array() + template + typename PropertyPool::Handle + PropertyPool::allocate_properties_array() { Handle handle = invalid_handle; if (n_properties > 0) @@ -86,8 +94,9 @@ namespace Particles + template void - PropertyPool::deallocate_properties_array(Handle &handle) + PropertyPool::deallocate_properties_array(Handle &handle) { Assert( handle != invalid_handle, @@ -109,18 +118,29 @@ namespace Particles + template void - PropertyPool::reserve(const std::size_t size) + PropertyPool::reserve(const std::size_t size) { properties.reserve(size * n_properties); } + template unsigned int - PropertyPool::n_properties_per_slot() const + PropertyPool::n_properties_per_slot() const { return n_properties; } + + + // Instantiate the class for all reasonable template arguments + template class PropertyPool<1, 1>; + template class PropertyPool<1, 2>; + template class PropertyPool<1, 3>; + template class PropertyPool<2, 2>; + template class PropertyPool<2, 3>; + template class PropertyPool<3, 3>; } // namespace Particles DEAL_II_NAMESPACE_CLOSE -- 2.39.5