Salome HOME
537bb76082db4349f8b59de26f53974f08e857aa
[modules/yacs.git] / src / engine / OptimizerLoop.hxx
1 // Copyright (C) 2006-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef __OPTIMIZERLOOP_HXX__
21 #define __OPTIMIZERLOOP_HXX__
22
23 #include "YACSlibEngineExport.hxx"
24 #include "Pool.hxx"
25 #include "DynParaLoop.hxx"
26 #include "DynLibLoader.hxx"
27 #include "OptimizerAlg.hxx"
28 #include "ElementaryNode.hxx"
29
30 namespace YACS
31 {
32   namespace ENGINE
33   {
34     class OptimizerLoop;
35
36     class FakeNodeForOptimizerLoop : public ElementaryNode
37     {
38       friend class OptimizerLoop;
39     private:
40       OptimizerLoop *_loop;
41       std::string _message;
42       bool _normal;
43     private:
44       FakeNodeForOptimizerLoop(OptimizerLoop *loop, bool normal, std::string message);
45       FakeNodeForOptimizerLoop(const FakeNodeForOptimizerLoop& other);
46       Node *simpleClone(ComposedNode *father, bool editionOnly) const;
47       void exForwardFailed();
48       void exForwardFinished();
49       void execute();
50       void aborted();
51       void finished();
52     private:
53       static const char NAME[];
54     };
55
56     class YACSLIBENGINE_EXPORT OptimizerLoop : public DynParaLoop
57     {
58       friend class FakeNodeForOptimizerLoop;
59       
60     protected:
61       Pool _myPool;
62       bool _algInitOnFile;
63       std::string _symbol;
64       std::string _alglib;
65       AnyInputPort _algoInitPort;
66       ::YACS::BASES::DynLibLoader * _loader;
67       OptimizerAlgBase *_alg;
68       AnyInputPort _retPortForOutPool;
69       std::vector<bool> _initNodeUpdated;
70       bool _convergenceReachedWithOtherCalc;
71       FakeNodeForOptimizerLoop *_nodeForSpecialCases;
72       std::vector<AnyInputPort *> _interceptorsForOutPool;
73       //! outputports interceptors leaving current scope.
74       std::map<InputPort *, std::vector<InputPort *> > _interceptors;
75       AnyOutputPort _algoResultPort;
76     public:
77       OptimizerLoop(const std::string& name, const std::string& algLibWthOutExt,
78                     const std::string& symbolNameToOptimizerAlgBaseInstanceFactory,
79                     bool algInitOnFile,bool initAlgo=true, Proc * procForTypes = NULL);
80       OptimizerLoop(const OptimizerLoop& other, ComposedNode *father, bool editionOnly);
81       ~OptimizerLoop();
82       void init(bool start=true);
83       void exUpdateState();
84       int getNumberOfInputPorts() const;
85       InputPort *edGetPortForOutPool() { return &_retPortForOutPool; }
86       InputPort *edGetAlgoInitPort() { return &_algoInitPort; }
87       OutputPort *edGetAlgoResultPort() { return &_algoResultPort; }
88       InputPort *getInputPort(const std::string& name) const ;
89       std::list<InputPort *> getSetOfInputPort() const;
90       std::list<InputPort *> getLocalInputPorts() const;
91       void selectRunnableTasks(std::vector<Task *>& tasks);
92       void getReadyTasks(std::vector<Task *>& tasks);
93       YACS::Event updateStateOnFinishedEventFrom(Node *node);
94       void checkNoCyclePassingThrough(Node *node) ;
95       virtual void accept(Visitor *visitor);
96       virtual std::string getSymbol() const { return _symbol; }
97       virtual std::string getAlgLib() const ;
98       virtual void setAlgorithm(const std::string& alglib,const std::string& symbol,
99                                 bool checkLinks=true, Proc * procForTypes = NULL);
100       virtual void checkBasicConsistency() const ;
101       virtual std::string typeName() {return "YACS__ENGINE__OptimizerLoop";}
102       int getNumberOfOutputPorts() const;
103       std::list<OutputPort *> getSetOfOutputPort() const;
104       std::list<OutputPort *> getLocalOutputPorts() const;
105       OutPort *getOutPort(const std::string& name) const ;
106       OutputPort *getOutputPort(const std::string& name) const ;
107       YACS::Event finalize();
108     protected:
109       virtual YACS::Event updateStateOnFailedEventFrom(Node *node, const Executor *execInst);
110       void buildDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView);
111       void buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView);
112       void checkControlDependancy(OutPort *start, InPort *end, bool cross,
113                                   std::map < ComposedNode *,  std::list < OutPort * >, SortHierarc >& fw,
114                                   std::vector<OutPort *>& fwCross,
115                                   std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
116                                   LinkInfo& info) const;
117       void checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const;
118       void checkLinkPossibility(OutPort *start, const std::list<ComposedNode *>& pointsOfViewStart,
119                                 InPort *end, const std::list<ComposedNode *>& pointsOfViewEnd) ;
120     protected:
121       void cleanInterceptors();
122       void launchMaxOfSamples(bool first);
123       bool isFullyLazy() const;
124       bool isFullyBusy(unsigned& branchId) const;
125       void initInterceptors(unsigned nbOfBr);
126       void pushValueOutOfScopeForCase(unsigned branchId);
127       Node *simpleClone(ComposedNode *father, bool editionOnly=true) const;
128       virtual void loadAlgorithm();
129
130     private:
131       TypeCode * checkTypeCode(TypeCode * tc, const char * portName);
132
133     protected:
134       static const int NOT_RUNNING_BRANCH_ID;
135       static const int NOT_INITIALIZED_BRANCH_ID;
136       static const char NAME_OF_ALGO_INIT_PORT[];
137       static const char NAME_OF_OUT_POOL_INPUT[];
138       static const char NAME_OF_ALGO_RESULT_PORT[];
139     };
140   }
141 }
142
143 #endif