Schnek
dependencies.hpp
1 /*
2  * dependencies.hpp
3  *
4  * Created on: 10 Jul 2012
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef SCHNEK_DEPENDENCIES_HPP_
28 #define SCHNEK_DEPENDENCIES_HPP_
29 
30 #include "variables.hpp"
31 #include "blockparameters.hpp"
32 
33 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
35 
36 #include <boost/variant.hpp>
37 
38 #pragma GCC diagnostic pop
39 
40 #include <memory>
41 #include <map>
42 #include <set>
43 #include <list>
44 
45 namespace schnek {
46 
47 class DependencyUpdater;
48 
54 {
55  private:
56  typedef std::set<long> DependencySet;
57  struct VarInfo
58  {
59  pVariable v;
60  DependencySet dependsOn;
61  DependencySet modifies;
62  int counter;
63  VarInfo() {}
64  VarInfo(pVariable v_, DependencySet dependsOn_, DependencySet modifies_)
65  : v(v_), dependsOn(dependsOn_), modifies(modifies_), counter(0) {
66  assert(v);
67  }
68  };
69 
70  typedef std::map<long, VarInfo> DepMap;
71 
73  typedef std::map<long, VarInfo*> RefDepMap;
74  typedef std::shared_ptr<RefDepMap> pRefDepMap;
75 
76  typedef std::set<pVariable> VariableSet;
77  typedef std::list<pVariable> VariableList;
78 
79  DepMap dependencies;
80  pBlockVariables blockVars;
81 
82  pVariable dummyVar;
83 
84  friend class DependencyUpdater;
85 
86  void constructMapRecursive(const pBlockVariables vars);
87  void constructMap(const pBlockVariables vars);
88  void resetCounters();
89 
90  void makeUpdateList(const VariableSet &independentVars, const VariableSet &dependentVars, VariableList &updateList);
91  pRefDepMap makeUpdatePredecessors(const VariableSet &independentVars, const VariableSet &dependentVars);
92  pRefDepMap makeUpdateFollowers(const VariableSet &independentVars, pRefDepMap reverseDeps);
93  void makeUpdateOrder(pRefDepMap deps, VariableList &updateList);
94 
95  public:
96  DependencyMap(const pBlockVariables vars);
97  void recreate() { constructMap(blockVars); }
98  pBlockVariables getBlockVariables();
99  void updateAll();
100 
101 // bool hasRoots(pVariable v, pParametersGroup roots);
102 };
103 
104 typedef std::shared_ptr<DependencyMap> pDependencyMap;
105 
107 {
108  private:
109  typedef std::set<pParameter> ParameterSet;
110  typedef std::set<pVariable> VariableSet;
111  typedef std::list<pVariable> VariableList;
112 
113  VariableList updateList;
114  VariableSet independentVars;
115  VariableSet dependentVars;
116  ParameterSet dependentParameters;
117 
118  pDependencyMap dependencies;
119  bool isValid;
120  public:
121  DependencyUpdater(pDependencyMap dependencies_);
122  void addIndependent(pParameter v);
123  void addDependent(pParameter v);
124  void clearDependent();
125 
126  template<size_t rank, template<size_t> class CheckingPolicy>
127  void addIndependentArray(Array<pParameter, rank, CheckingPolicy> v)
128  { for (size_t i=0; i<rank; ++i) addIndependent(v[i]); }
129 
130  template<size_t rank, template<size_t> class CheckingPolicy>
131  void addDependentArray(Array<pParameter, rank, CheckingPolicy> v)
132  { for (size_t i=0; i<rank; ++i) addDependent(v[i]); }
133 
138  void update()
139  {
140  if (!isValid) {
141  dependencies->makeUpdateList(independentVars, dependentVars, updateList);
142  isValid = true;
143  }
144  for(pVariable v: updateList) v->evaluateExpression();
145  for(pParameter p: dependentParameters) p->update();
146  }
147 };
148 
149 typedef std::shared_ptr<DependencyUpdater> pDependencyUpdater;
150 
151 } // namespace
152 
153 
154 #endif // SCHNEK_DEPENDENCIES_HPP_
Definition: dependencies.hpp:53
Definition: array.hpp:55
Definition: algo.hpp:30
void update()
Definition: dependencies.hpp:138
Definition: dependencies.hpp:106