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