GetFEM  5.4.2
dal_singleton.cc
1 /*===========================================================================
2 
3  Copyright (C) 2004-2020 Julien Pommier
4 
5  This file is a part of GetFEM
6 
7  GetFEM is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Lesser General Public License as published
9  by the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version along with the GCC Runtime Library
11  Exception either version 3.1 or (at your option) any later version.
12  This program is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15  License and GCC Runtime Library Exception for more details.
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program; if not, write to the Free Software Foundation,
18  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20 ===========================================================================*/
21 
22 #include "getfem/dal_singleton.h"
23 #include <algorithm>
24 #include "gmm/gmm.h"
25 #include "getfem/getfem_omp.h"
26 
27 
28 namespace dal {
29 
30  singletons_manager::singletons_manager()
31  : lst{}, nb_partitions{lst.num_threads()}
32  {}
33 
34  singletons_manager& singletons_manager::manager(){
35  static singletons_manager x;
36  return x;
37  }
38 
39  void singletons_manager::register_new_singleton(singleton_instance_base *p){
40  register_new_singleton(p, manager().lst.this_thread());
41  }
42 
43  void singletons_manager::register_new_singleton(singleton_instance_base *p, size_t ithread){
44  if (p) manager().lst(ithread).push_back(p);
45  }
46 
47  void singletons_manager::on_partitions_change(){
48  auto new_nb_partitions = manager().lst.num_threads();
49  auto &nb_partitions = manager().nb_partitions;
50  if (new_nb_partitions > nb_partitions){ //not allowing reducing nb. of partitions,
51  manager().lst.on_thread_update(); //as it will invalidate global storage
52  nb_partitions = new_nb_partitions;
53  }
54  }
55 
56  static int level_compare(singleton_instance_base *a, singleton_instance_base *b) {
57  return a->level() < b->level();
58  }
59 
60  singletons_manager::~singletons_manager() {
61  for(size_type i = 0; i != nb_partitions; ++i){
62  std::sort(lst(i).begin(), lst(i).end(), level_compare);
63  for (auto &&p : lst(i)) delete p;
64  }
65  }
66 
67 }/* end of namespace dal */
bgeot::size_type
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49
dal_singleton.h
A simple singleton implementation.
dal
Dynamic Array Library.
Definition: dal_backtrace.cc:29
getfem_omp.h
Tools for multithreaded, OpenMP and Boost based parallelization.
gmm.h
Include common gmm files.