]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/OptimizerLoop.hxx
Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / engine / OptimizerLoop.hxx
1 #ifndef __OPTIMIZERLOOP_HXX__
2 #define __OPTIMIZERLOOP_HXX__
3
4 #include "Pool.hxx"
5 #include "Thread.hxx"
6 #include "DynParaLoop.hxx"
7 #include "DynLibLoader.hxx"
8 #include "OptimizerAlg.hxx"
9 #include "ElementaryNode.hxx"
10 #include "DrivenCondition.hxx"
11
12 namespace YACS
13 {
14   namespace ENGINE
15   {
16     class OptimizerAlgStandardized : public OptimizerAlgSync
17     {
18     private:
19       ::YACS::BASES::Thread *_threadInCaseOfNotEvent;
20       ::YACS::BASES::DrivenCondition _condition;
21       OptimizerAlgBase *_algBehind;
22     public:
23       OptimizerAlgStandardized(Pool *pool, OptimizerAlgBase *alg);
24       ~OptimizerAlgStandardized();
25       TypeCode *getTCForIn() const;
26       TypeCode *getTCForOut() const;
27       void setAlgPointer(OptimizerAlgBaseFactory algFactory);
28       void parseFileToInit(const std::string& fileName);
29       void initialize(const Any *input) throw (Exception);
30       void takeDecision();
31       void finish();
32       void start();
33     private:
34       static void *threadFctForAsync(void* ownStack);
35     };
36
37     class OptimizerLoop;
38
39     class FakeNodeForOptimizerLoop : public ElementaryNode
40     {
41       friend class OptimizerLoop;
42     private:
43       OptimizerLoop *_loop;
44       unsigned _reason;
45       bool _normal;
46     private:
47       FakeNodeForOptimizerLoop(OptimizerLoop *loop, bool normal, unsigned reason);
48       FakeNodeForOptimizerLoop(const FakeNodeForOptimizerLoop& other);
49       Node *simpleClone(ComposedNode *father, bool editionOnly) const;
50       void exForwardFailed();
51       void exForwardFinished();
52       void execute();
53       void aborted();
54       void finished();
55     private:
56       static const char NAME[];
57       static const unsigned char ALG_WITHOUT_START_CASES = 52;
58       static const unsigned char NO_BRANCHES = 53;
59       static const unsigned char NO_ALG_INITIALIZATION = 54;
60     };
61
62     class OptimizerLoop : public DynParaLoop
63     {
64       friend class FakeNodeForOptimizerLoop;
65       
66     protected:
67       Pool _myPool;
68       bool _algInitOnFile;
69       std::string _symbol;
70       AnyInputPort _portForInitFile;
71       ::YACS::BASES::DynLibLoader _loader;
72       OptimizerAlgStandardized *_alg;
73       AnyInputPort _retPortForOutPool;
74       std::vector<bool> _initNodeUpdated;
75       bool _convergenceReachedWithOtherCalc;
76       FakeNodeForOptimizerLoop *_nodeForSpecialCases;
77       std::vector<AnyInputPort *> _interceptorsForOutPool;
78       ::YACS::BASES::DrivenCondition _condForCompletenessB4Relaunch;
79       //! outputports interceptors leaving current scope.
80       std::map<InputPort *, std::vector<InputPort *> > _interceptors;
81     public:
82       OptimizerLoop(const std::string& name, const std::string& algLibWthOutExt,
83                     const std::string& symbolNameToOptimizerAlgBaseInstanceFactory,
84                     bool algInitOnFile) throw(Exception);
85       OptimizerLoop(const OptimizerLoop& other, ComposedNode *father, bool editionOnly);
86       ~OptimizerLoop();
87       void init(bool start=true);
88       void exUpdateState();
89       int getNumberOfInputPorts() const;
90       InputPort *edGetPortForOutPool() { return &_retPortForOutPool; }
91       InputPort *edGetPortForInitFile() { return &_portForInitFile; }
92       InputPort *getInputPort(const std::string& name) const throw(Exception);
93       std::list<InputPort *> getSetOfInputPort() const;
94       std::list<InputPort *> getLocalInputPorts() const;
95       void selectRunnableTasks(std::vector<Task *>& tasks);
96       void getReadyTasks(std::vector<Task *>& tasks);
97       YACS::Event updateStateOnFinishedEventFrom(Node *node);
98       void checkNoCyclePassingThrough(Node *node) throw(Exception);
99     protected:
100       void buildDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView);
101       void buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView);
102       void checkControlDependancy(OutPort *start, InPort *end, bool cross,
103                                   std::map < ComposedNode *,  std::list < OutPort * >, SortHierarc >& fw,
104                                   std::vector<OutPort *>& fwCross,
105                                   std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
106                                   LinkInfo& info) const;
107       void checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const;
108     protected:
109       void cleanInterceptors();
110       void launchMaxOfSamples(bool first);
111       bool isFullyLazy() const;
112       bool isFullyBusy(unsigned& branchId) const;
113       void initInterceptors(unsigned nbOfBr);
114       void pushValueOutOfScopeForCase(unsigned branchId);
115       Node *simpleClone(ComposedNode *father, bool editionOnly=true) const;
116     protected:
117       static const int NOT_RUNNING_BRANCH_ID;
118       static const char NAME_OF_FILENAME_INPUT[];
119       static const char NAME_OF_OUT_POOL_INPUT[];
120     };
121   }
122 }
123
124 #endif