Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge commit '045db1657e870c721be490b411868f4181a12ced' into surf++
[simgrid.git] / src / surf / solver.hpp
1 #include <xbt.h>
2 #include <vector>
3 #include "solver.h"
4 #include <boost/pool/object_pool.hpp>
5 #include <boost/bind.hpp>
6
7 #ifndef SURF_SOLVER_HPP_
8 #define SURF_SOLVER_HPP_
9 using namespace std;
10
11 /*static void double_update(double *variable, double value)
12 {
13   *variable -= value;
14   if (*variable < MAXMIN_PRECISION)
15     *variable = 0.0;
16 }*/
17
18 class Solver;
19 class Element;
20 class Constraint;
21 class ConstraintLight;
22 class Variable;
23
24 struct ElementOps
25 {
26   bool operator()( const Element & a, const Element & b )
27     { return true; } //a > b; }
28 };
29
30 class Element {
31 public:
32   Element(ConstraintPtr cnst, VariablePtr var, double value);
33   ConstraintPtr p_constraint;
34   VariablePtr p_variable;
35   double m_value;
36
37   void activate();
38   void inactivate();
39 };
40
41 class ConstraintLight {
42 public:
43   ConstraintLight(ConstraintPtr cnst, double remainingOverUsage):
44     m_remainingOverUsage(remainingOverUsage), p_cnst(cnst) {};
45   double m_remainingOverUsage;
46   ConstraintPtr p_cnst;
47 };
48
49 class Constraint {
50 public:
51   Constraint(void *id, double bound);
52   ~Constraint() {
53   std::cout << "Del Const:" << m_bound << std::endl;    
54   };
55   
56   void shared();
57   bool isShared();
58   void* id();
59   VariablePtr getVar(ElementPtr elem);
60   void addElement(ElementPtr elem);
61
62   vector<ElementPtr> m_elementSet;     /* a list of lmm_element_t */
63   int m_elementsZeroWeight;
64   vector<ElementPtr> m_activeElementSet;      /* a list of lmm_element_t */
65   double m_remaining;
66   double m_usage;
67   double m_bound;
68   int m_shared;
69   void *p_id;
70   int m_idInt;
71   double m_lambda;
72   double m_newLambda;
73   ConstraintLightPtr p_cnstLight;
74 };
75
76 class Variable {
77 public:
78   Variable(void *id, double weight, double bound, int visited);
79   ~Variable() {
80   std::cout << "Del Variable" << std::endl;
81   };
82
83   double value();
84   double bound();
85   void* id();
86   double getWeight();
87   int getNumberOfCnst();
88   ConstraintPtr getCnst(int num);
89   double getCnstWeight(int num);
90   double isLimitedByLatency();
91
92   /* \begin{For Lagrange only} */
93   double (*p_funcF) (VariablePtr var, double x);       /* (f)    */
94   double (*p_funcFP) (VariablePtr var, double x);      /* (f')    */
95   double (*p_funcFPI) (VariablePtr var, double x);     /* (f')^{-1}    */
96   /* \end{For Lagrange only} */
97   vector<ElementPtr> m_cnsts;
98
99   unsigned m_visited;             /* used by lmm_update_modified_set */
100   double m_weight;
101   double m_bound;
102   double m_value;
103   void *p_id;
104   int m_idInt;
105   double m_mu;
106   double m_newMu;
107
108 protected:
109   /* \begin{For Lagrange only} */
110   /* \end{For Lagrange only} */
111 };
112
113 class Solver {
114 public:
115   Solver(int selective_update);
116   ~Solver() {
117   std::cout << "Del Solver" << std::endl;    
118   }       
119
120   inline void disableVariable(VariablePtr var);
121   ConstraintPtr createConstraint(void *id, double bound_value);
122   VariablePtr createVariable(void *id, double weight, double bound);
123   void expand(ConstraintPtr cnst, VariablePtr var,  double value);
124   void expandAdd(ConstraintPtr cnst, VariablePtr var,  double value);
125   void elementSetValue(ConstraintPtr cnst, VariablePtr var, double value);
126   void saturatedConstraintSetUpdate(double usage, ConstraintLightPtr cnstLight,
127                                   vector<ConstraintLightPtr> saturatedConstraintSet,
128                                   double *minUsage);
129   void saturatedVariableSetUpdate(vector<ConstraintLightPtr> cnstLightList,
130     vector<ConstraintLightPtr> saturatedConstraintSet);
131   void solve();
132   void update(ConstraintPtr cnst, VariablePtr var, double value);
133   void updateVariableBound(VariablePtr var, double bound);
134   void updateVariableWeight(VariablePtr var, double weight);
135   void updateConstraintBound(ConstraintPtr cnst, double bound);
136   bool constraintUsed(ConstraintPtr cnst);
137   ConstraintPtr getFirstActiveConstraint();
138   ConstraintPtr getNextActiveConstraint(ConstraintPtr cnst);
139   void updateModifiedSetRec(ConstraintPtr cnst);
140   void updateModifiedSet(ConstraintPtr cnst);
141   void removeAllModifiedSet();
142   void activateConstraint(ConstraintPtr cnst);
143   void inactivateConstraint(ConstraintPtr cnst);
144   void print();
145
146   vector<VariablePtr>   m_variableSet;    /* a list of lmm_variable_t */
147   vector<VariablePtr> m_saturatedVariableSet;  /* a list of lmm_variable_t */
148   vector<ConstraintPtr> m_activeConstraintSet;   /* a list of lmm_constraint_t */
149   vector<ConstraintPtr> m_constraintSet;  /* a list of lmm_constraint_t */
150   vector<ConstraintPtr> m_modifiedConstraintSet; /* a list of modified lmm_constraint_t */
151   vector<ConstraintPtr> m_saturatedConstraintSet;        /* a list of lmm_constraint_t_t */
152
153   bool m_modified;
154 private:
155
156
157 protected:
158   bool m_selectiveUpdateActive;  /* flag to update partially the system only selecting changed portions */
159   unsigned m_visitedCounter;     /* used by lmm_update_modified_set */
160   
161
162   boost::object_pool<Constraint> m_constraintAllocator;
163   boost::object_pool<Variable> m_variableAllocator;
164   boost::object_pool<Element> m_elementAllocator;  
165   void destroyConstraint(Constraint* cnst);
166   void destroyVariable(Variable* var);
167   void destroyElement(Element* elem);
168
169   vector<void*> m_keepTrack;
170
171   //xbt_mallocator_t variable_mallocator;
172 };
173
174 #endif /* SURF_SOLVER_H_ */