GetFEM  5.5
getfem_context.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2004-2026 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 /**@file getfem_context.h
32  @author Yves Renard <Yves.Renard@insa-lyon.fr>
33  @date June 17, 2004.
34  @brief Deal with interdependencies of objects (getfem::context_dependencies).
35 */
36 #ifndef GETFEM_CONTEXT_H__
37 #define GETFEM_CONTEXT_H__
38 
39 #include "getfem_config.h"
40 #include "getfem_omp.h"
41 #include <list>
42 
43 #include <atomic>
44 
45 namespace getfem {
46  /**Deal with interdependencies of objects.
47 
48  An object can be in three different states :
49  NORMAL : no change is necessary
50  CHANGED : something in the context has changed and the update function
51  of the object has to be called.
52  INVALID : one of the dependencies desappears, the object is invalid
53  and should no longer be used.
54 
55  add_dependency(ct) : add a dependency to the dependency list.
56 
57  touch() : significate to the dependent objects that something
58  has change in the object. This make the dependent
59  objects to be in the CHANGED state
60 
61  context_check() : check if the object has to be updated. if it is the
62  case it makes first a check to the dependency list
63  and call the update function of the object.
64  (the update function of the dependencies are called
65  before the update function of the current object).
66 
67  context_valid() : says if the object has still a valid context
68 
69  Remarks :
70 
71  - A protection against round dependencies exists. In this case, the
72  order of call of the update functions can be arbitrary
73 
74  - Detection of context changes is very fast (control the
75  state). the touch operation can cover the whole tree of dependent
76  object. But this is the case only for the first touch operation
77  because once a dependent object is in the CHANGED state it will not
78  be considered by next touch operations.
79  */
80  class APIDECL context_dependencies {
81 
82  protected :
83  enum context_state { CONTEXT_NORMAL, CONTEXT_CHANGED, CONTEXT_INVALID };
84  mutable context_state state;
85  mutable std::atomic_bool touched;
86  mutable std::vector<const context_dependencies *> dependencies;
87  mutable std::vector<const context_dependencies *> dependent;
88  typedef std::vector<const context_dependencies *>::iterator iterator_list;
89  getfem::lock_factory locks_;
90 
91  void sup_dependent_(const context_dependencies &cd) const;
92  void sup_dependency_(const context_dependencies &cd) const;
93  void invalid_context() const;
94  bool go_check() const;
95 
96  public :
97 
98  /** this function has to be defined and should update the object when
99  the context is modified. */
100  virtual void update_from_context() const = 0;
101 
102  void change_context() const
103  {
104  if (state == CONTEXT_NORMAL)
105  {
106  touch();
107  getfem::local_guard lock = locks_.get_lock();
108  state = CONTEXT_CHANGED;
109  }
110  }
111  void add_dependency(const context_dependencies &cd);
112 
113  void sup_dependency(const context_dependencies &cd)
114  {
115  cd.sup_dependent_(*this);
116  sup_dependency_(cd);
117  }
118 
119  void clear_dependencies();
120 
121 
122  bool is_context_valid() const { return (state != CONTEXT_INVALID); }
123  bool is_context_changed() const { return (state == CONTEXT_CHANGED); }
124  /** return true if update_from_context was called */
125  bool context_check() const
126  { if (state == CONTEXT_NORMAL) return false; return go_check(); }
127  void touch() const;
128  virtual ~context_dependencies();
129  context_dependencies() : state(CONTEXT_NORMAL), touched( ) {touched = false;}
130  context_dependencies(const context_dependencies& cd);
131  context_dependencies& operator=(const context_dependencies& cd);
132  };
133 
134 } /* end of namespace getfem. */
135 
136 
137 #endif /* GETFEM_CONTEXT_H__ */
Deal with interdependencies of objects.
bool context_check() const
return true if update_from_context was called
virtual void update_from_context() const =0
this function has to be defined and should update the object when the context is modified.
defines and typedefs for namespace getfem
Tools for multithreaded, OpenMP and Boost based parallelization.
GEneric Tool for Finite Element Methods.