]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Lazily initialize VectorMemory's global variables
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Sat, 17 Nov 2018 15:10:32 +0000 (16:10 +0100)
committerDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Sat, 17 Nov 2018 15:10:32 +0000 (16:10 +0100)
include/deal.II/lac/vector_memory.h
include/deal.II/lac/vector_memory.templates.h

index 669e15e80fa6641d37cf8899725986c30964e31f..0195a3032344827600b4650be13f6461a15b3a3b 100644 (file)
@@ -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
index 20412b74805905a560dec84bc99ac1ebf0e37607..a8f44f8475906cab1199e83ed272f9f1922fbd52 100644 (file)
@@ -26,12 +26,20 @@ DEAL_II_NAMESPACE_OPEN
 
 
 template <typename VectorType>
-typename GrowingVectorMemory<VectorType>::Pool
-  GrowingVectorMemory<VectorType>::pool;
+typename GrowingVectorMemory<VectorType>::Pool &
+GrowingVectorMemory<VectorType>::get_pool()
+{
+  static GrowingVectorMemory<VectorType>::Pool pool;
+  return pool;
+}
+
+
 
 template <typename VectorType>
 Threads::Mutex GrowingVectorMemory<VectorType>::mutex;
 
+
+
 template <typename VectorType>
 inline GrowingVectorMemory<VectorType>::Pool::Pool()
   : data(nullptr)
@@ -82,7 +90,7 @@ inline GrowingVectorMemory<VectorType>::GrowingVectorMemory(
   , log_statistics(log_statistics)
 {
   std::lock_guard<std::mutex> lock(mutex);
-  pool.initialize(initial_size);
+  get_pool().initialize(initial_size);
 }
 
 
@@ -96,7 +104,7 @@ inline GrowingVectorMemory<VectorType>::~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<VectorType>::alloc()
   ++current_alloc;
   // see if there is a free vector
   // available in our list
-  for (typename std::vector<entry_type>::iterator i = pool.data->begin();
-       i != pool.data->end();
+  for (typename std::vector<entry_type>::iterator i = get_pool().data->begin();
+       i != get_pool().data->end();
        ++i)
     {
       if (i->first == false)
@@ -124,10 +132,10 @@ GrowingVectorMemory<VectorType>::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<VectorType>()));
 
-  return pool.data->back().second.get();
+  return get_pool().data->back().second.get();
 }
 
 
@@ -138,8 +146,8 @@ GrowingVectorMemory<VectorType>::free(const VectorType *const v)
 {
   std::lock_guard<std::mutex> lock(mutex);
 
-  for (typename std::vector<entry_type>::iterator i = pool.data->begin();
-       i != pool.data->end();
+  for (typename std::vector<entry_type>::iterator i = get_pool().data->begin();
+       i != get_pool().data->end();
        ++i)
     {
       if (v == i->second.get())
@@ -160,8 +168,8 @@ GrowingVectorMemory<VectorType>::release_unused_memory()
 {
   std::lock_guard<std::mutex> lock(mutex);
 
-  if (pool.data != nullptr)
-    pool.data->clear();
+  if (get_pool().data != nullptr)
+    get_pool().data->clear();
 }
 
 
@@ -173,8 +181,10 @@ GrowingVectorMemory<VectorType>::memory_consumption() const
   std::lock_guard<std::mutex> lock(mutex);
 
   std::size_t                                            result = sizeof(*this);
-  const typename std::vector<entry_type>::const_iterator end = pool.data->end();
-  for (typename std::vector<entry_type>::const_iterator i = pool.data->begin();
+  const typename std::vector<entry_type>::const_iterator end =
+    get_pool().data->end();
+  for (typename std::vector<entry_type>::const_iterator i =
+         get_pool().data->begin();
        i != end;
        ++i)
     result += sizeof(*i) + MemoryConsumption::memory_consumption(i->second);

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.