From: Daniel Arndt Date: Sat, 17 Nov 2018 15:10:32 +0000 (+0100) Subject: Lazily initialize VectorMemory's global variables X-Git-Tag: v9.1.0-rc1~549^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1c95b02697ad640f11ddc414dabed07bdc64e69b;p=dealii.git Lazily initialize VectorMemory's global variables --- diff --git a/include/deal.II/lac/vector_memory.h b/include/deal.II/lac/vector_memory.h index 669e15e80f..0195a30323 100644 --- a/include/deal.II/lac/vector_memory.h +++ b/include/deal.II/lac/vector_memory.h @@ -438,7 +438,8 @@ private: /** * Array of allocated vectors. */ - static Pool pool; + static Pool & + get_pool(); /** * Overall number of allocations. Only used for bookkeeping and to generate diff --git a/include/deal.II/lac/vector_memory.templates.h b/include/deal.II/lac/vector_memory.templates.h index 20412b7480..a8f44f8475 100644 --- a/include/deal.II/lac/vector_memory.templates.h +++ b/include/deal.II/lac/vector_memory.templates.h @@ -26,12 +26,20 @@ DEAL_II_NAMESPACE_OPEN template -typename GrowingVectorMemory::Pool - GrowingVectorMemory::pool; +typename GrowingVectorMemory::Pool & +GrowingVectorMemory::get_pool() +{ + static GrowingVectorMemory::Pool pool; + return pool; +} + + template Threads::Mutex GrowingVectorMemory::mutex; + + template inline GrowingVectorMemory::Pool::Pool() : data(nullptr) @@ -82,7 +90,7 @@ inline GrowingVectorMemory::GrowingVectorMemory( , log_statistics(log_statistics) { std::lock_guard lock(mutex); - pool.initialize(initial_size); + get_pool().initialize(initial_size); } @@ -96,7 +104,7 @@ inline GrowingVectorMemory::~GrowingVectorMemory() deallog << "GrowingVectorMemory:Overall allocated vectors: " << total_alloc << std::endl; deallog << "GrowingVectorMemory:Maximum allocated vectors: " - << pool.data->size() << std::endl; + << get_pool().data->size() << std::endl; } } @@ -112,8 +120,8 @@ GrowingVectorMemory::alloc() ++current_alloc; // see if there is a free vector // available in our list - for (typename std::vector::iterator i = pool.data->begin(); - i != pool.data->end(); + for (typename std::vector::iterator i = get_pool().data->begin(); + i != get_pool().data->end(); ++i) { if (i->first == false) @@ -124,10 +132,10 @@ GrowingVectorMemory::alloc() } // no free vector found, so let's just allocate a new one - pool.data->emplace_back( + get_pool().data->emplace_back( entry_type(true, std_cxx14::make_unique())); - return pool.data->back().second.get(); + return get_pool().data->back().second.get(); } @@ -138,8 +146,8 @@ GrowingVectorMemory::free(const VectorType *const v) { std::lock_guard lock(mutex); - for (typename std::vector::iterator i = pool.data->begin(); - i != pool.data->end(); + for (typename std::vector::iterator i = get_pool().data->begin(); + i != get_pool().data->end(); ++i) { if (v == i->second.get()) @@ -160,8 +168,8 @@ GrowingVectorMemory::release_unused_memory() { std::lock_guard lock(mutex); - if (pool.data != nullptr) - pool.data->clear(); + if (get_pool().data != nullptr) + get_pool().data->clear(); } @@ -173,8 +181,10 @@ GrowingVectorMemory::memory_consumption() const std::lock_guard lock(mutex); std::size_t result = sizeof(*this); - const typename std::vector::const_iterator end = pool.data->end(); - for (typename std::vector::const_iterator i = pool.data->begin(); + const typename std::vector::const_iterator end = + get_pool().data->end(); + for (typename std::vector::const_iterator i = + get_pool().data->begin(); i != end; ++i) result += sizeof(*i) + MemoryConsumption::memory_consumption(i->second);