Salome HOME
Copyright update 2021
[modules/yacs.git] / src / engine / Bloc.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 __BLOC_HXX__
21 #define __BLOC_HXX__
22
23 #include "YACSlibEngineExport.hxx"
24 #include "StaticDefinedComposedNode.hxx"
25 #include "AutoRefCnt.hxx"
26
27 namespace YACS
28 {
29   namespace ENGINE
30   {
31     class PartDefinition;
32     class DynParaLoop;
33     class AbstractPoint;
34     class PlayGround;
35     class YACSLIBENGINE_EXPORT Bloc : public StaticDefinedComposedNode
36     {
37     protected:
38       std::list<Node *> _setOfNode;//OWNERSHIP OF ALL NODES
39       //! For internal calculations
40       mutable std::map<Node *,std::set<Node *> > *_fwLinks;
41       //! For internal calculations
42       mutable std::map<Node *,std::set<Node *> > *_bwLinks;
43     protected:
44       Node *simpleClone(ComposedNode *father, bool editionOnly=true) const;
45     public:
46       Bloc(const Bloc& other, ComposedNode *father, bool editionOnly);
47       Bloc(const std::string& name);
48       virtual ~Bloc();
49       bool isFinished();
50       int getNumberOfCFLinks() const;
51       void init(bool start=true);
52       void getReadyTasks(std::vector<Task *>& tasks);
53       void exUpdateState();
54       //Node* DISOWNnode is a SWIG notation to indicate that the ownership of the node is transfered to C++
55       bool edAddChild(Node *DISOWNnode) ;
56       void edRemoveChild(Node *node) ;
57       std::list<Node *> getChildren() const { return _setOfNode; }
58       std::list<Node *> edGetDirectDescendants() const { return _setOfNode; }
59       std::vector< std::list<Node *> > splitIntoIndependantGraph() const;
60       Node *getChildByShortName(const std::string& name) const ;
61       virtual void writeDot(std::ostream &os) const;
62       void accept(Visitor *visitor);
63       template<bool direction>
64       void findAllPathsStartingFrom(Node *start, std::list< std::vector<Node *> >& vec, std::map<Node *, std::set<Node *> >& accelStr) const;
65       template<bool direction>
66       void findAllNodesStartingFrom(Node *start, std::set<Node *>& result, std::map<Node *, std::set<Node *> >& accelStr, LinkInfo& info) const;
67       virtual std::string typeName() { return "YACS__ENGINE__Bloc"; }
68       int getMaxLevelOfParallelism() const;
69       void getWeightRegardingDPL(ComplexWeight *weight);
70       void removeRecursivelyRedundantCL();
71       void partitionRegardingDPL(const PartDefinition *pd, std::map<ComposedNode *, YACS::BASES::AutoRefCnt<PartDefinition> >& zeMap);
72       void fitToPlayGround(const PlayGround *pg);
73       void propagePlayGround(const PlayGround *pg);
74     protected:
75       bool areAllSubNodesFinished() const;
76       bool areAllSubNodesDone() const;
77       bool isNameAlreadyUsed(const std::string& name) const;
78       void checkNoCyclePassingThrough(Node *node) ;
79       std::vector< std::pair<OutGate *, InGate *> > getSetOfInternalCFLinks() const;
80       YACS::Event updateStateOnFinishedEventFrom(Node *node);
81       YACS::Event updateStateOnFailedEventFrom(Node *node, const Executor *execInst);
82       void initComputation() const;
83       void performCFComputationsOnlyOneLevel(LinkInfo& info) const;
84       void performCFComputations(LinkInfo& info) const;
85       void destructCFComputations(LinkInfo& info) const;
86       void checkControlDependancy(OutPort *start, InPort *end, bool cross,
87                                   std::map < ComposedNode *,  std::list < OutPort * >, SortHierarc >& fw,
88                                   std::vector<OutPort *>& fwCross,
89                                   std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
90                                   LinkInfo& info) const;
91       bool areLinked(Node *start, Node *end, bool fw) const;
92       bool arePossiblyRunnableAtSameTime(Node *start, Node *end) const;
93       void checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const;
94       static void verdictForOkAndUseless1(const std::map<Node *,std::list <OutPort *> >& pool, InputPort *end, const std::vector<Node *>& candidates,
95                                           unsigned char& alreadyFed, bool direction, LinkInfo& info);
96       static void verdictForCollapses(const std::map<Node *,std::list <OutPort *> >& pool, InputPort *end, const std::set<Node *>& candidates,
97                                       unsigned char& alreadyFed, bool direction, LinkInfo& info);
98       void seekOkAndUseless1(std::vector<Node *>& okAndUseless1, std::set<Node *>& allNodes) const;
99       void seekUseless2(std::vector<Node *>& useless2, std::set<Node *>& allNodes) const;
100     private:
101       std::list< AbstractPoint * > analyzeParallelism() const;
102       static void findUselessLinksIn(const std::list< std::vector<Node *> >& res , LinkInfo& info);
103       template<bool direction>
104       static unsigned appendIfAlreadyFound(std::list< std::vector<Node *> >& res, const std::vector<Node *>& startRes, Node *node, std::map<Node *, std::set<Node *> >& fastFinder);
105       static void updateWithNewFind(const std::vector<Node *>& path, std::map<Node *, std::set<Node *> >& fastFinder);
106     };
107
108     template<bool direction>
109     struct CFDirectionVisTraits
110     {   
111     };
112     
113     template<>
114     struct CFDirectionVisTraits<true>
115     {
116       typedef std::list< std::pair<InGate *,bool> >::iterator Iterator;
117       typedef std::list< std::pair<InGate *,bool> >& Nexts;
118       static Nexts getNexts(Node *node) { return node->getOutGate()->edMapInGate(); }
119     };
120
121     
122     template<>
123     struct CFDirectionVisTraits<false>
124     {
125       typedef std::list< std::pair<OutGate *,bool> >::iterator Iterator;
126       typedef std::list< std::pair<OutGate *,bool> >& Nexts;
127       static Nexts getNexts(Node *node) { return node->getInGate()->edMapOutGate(); }
128     };
129
130     /*!
131      * Internal method for CF computation. Given 'fastFinder' it searched 'node' to see if an already found path in 'res' go through 'node'.
132      * If true all paths are deduced and append to res and 'fastFinder' is updated for next turn.
133      */
134     template<bool direction>
135     unsigned Bloc::appendIfAlreadyFound(std::list< std::vector<Node *> >& res, const std::vector<Node *>& startRes, Node *node, std::map<Node *, std::set<Node *> >& fastFinder)
136     {
137       std::map<Node *, std::set<Node *> >::iterator iter=fastFinder.find(node);
138       if(iter==fastFinder.end())
139         return 0;
140       unsigned ret=0;
141       std::vector<std::pair<std::set<Node *>::iterator, std::set<Node *>::iterator > > li;
142       std::pair<std::set<Node *>::iterator, std::set<Node *>::iterator> ipr(((*iter).second).begin(),((*iter).second).end());
143       li.push_back(ipr);
144       std::vector<Node *> work(startRes);
145       std::list< std::vector<Node *> >::iterator where=res.end(); where--;
146       std::list< std::vector<Node *> >::iterator updates=where;
147       while(!li.empty())
148         {
149           if(li.back().first!=li.back().second)
150             {
151               work.push_back(*(li.back().first));
152               if(CFDirectionVisTraits<direction>::getNexts(work.back()).empty())
153                 {
154                   where=res.insert(where,work);
155                   ret++;
156                   li.back().first++;
157                   work.pop_back();
158                 }
159               else
160                 {
161                   std::set<Node *>& s=fastFinder[*(li.back().first)];
162                   std::pair<std::set<Node *>::iterator, std::set<Node *>::iterator> pr(s.begin(),s.end());
163                   li.push_back(pr);
164                 }
165             }
166           else
167             {
168               work.pop_back();
169               li.pop_back();
170               if(!li.empty())
171                 li.back().first++;
172             }
173         }
174       updates--;
175       for(unsigned i=0;i<ret;i++,updates--)
176         updateWithNewFind(*updates,fastFinder);
177       return ret;
178     }
179
180     template<bool direction>
181     void Bloc::findAllNodesStartingFrom(Node *start, std::set<Node *>& result, 
182                                         std::map<Node *, std::set<Node *> >& accelStr, LinkInfo& info) const
183     {
184       std::list< std::vector<Node *> > li;
185       findAllPathsStartingFrom<direction>(start,li,accelStr);
186       for(std::list< std::vector<Node *> >::const_iterator iter=li.begin();iter!=li.end();iter++)
187         for(std::vector<Node *>::const_iterator iter2=(*iter).begin()+1;iter2!=(*iter).end();iter2++)
188           result.insert(*iter2);
189       if(direction)
190         findUselessLinksIn(li,info);
191     }
192
193     /*!
194      * Method for CF computation.DFS visitor is used.
195      * if direction is true forward visiting is performed, if false backward is performed.
196      * \param start \b must be a direct descendant of 'this'.
197      * \param vec :
198      * \param accelStr
199      */
200     template<bool direction>
201     void Bloc::findAllPathsStartingFrom(Node *start, std::list< std::vector<Node *> >& vec, 
202                                         std::map<Node *, std::set<Node *> >& accelStr) const
203     {
204       initComputation();
205       Node *current=start;
206       int caseId=0;
207       int idInCase=0;
208       vec.push_back(std::vector<Node *>());
209       typename CFDirectionVisTraits<direction>::Iterator iter;
210       std::list< std::vector<Node *> >::iterator curLine=vec.begin();
211       while(start->_colour!=YACS::Black)
212         {
213           (*curLine).push_back(current);
214           idInCase++;
215           //
216           if(CFDirectionVisTraits<direction>::getNexts(current).empty())
217             {
218               
219               vec.push_back(std::vector<Node *>((*curLine)));
220               updateWithNewFind(*curLine,accelStr);
221               current->_colour=YACS::Black;
222               curLine++;
223               if(idInCase>1)
224                 {
225                   idInCase-=2;
226                   current=(*curLine)[idInCase];
227                   (*curLine).pop_back();
228                   (*curLine).pop_back();
229                 }
230               continue;
231             }
232           if(current->_colour==YACS::Black)
233             {
234               appendIfAlreadyFound<direction>(vec,(*curLine),current,accelStr);
235               curLine=vec.end(); curLine--;
236               current->_colour=YACS::Black;
237               if(idInCase>1)
238                 {
239                   idInCase-=2;
240                   current=(*curLine)[idInCase];
241                   (*curLine).pop_back();
242                   (*curLine).pop_back();
243                 }
244               continue;
245             }
246           for(iter=CFDirectionVisTraits<direction>::getNexts(current).begin();iter!=CFDirectionVisTraits<direction>::getNexts(current).end();iter++)
247             if(!(*iter).second)
248               break;
249           if(iter==CFDirectionVisTraits<direction>::getNexts(current).end())
250             {//Fail this branch should be forgotten go rev
251               current->_colour=YACS::Black;
252               (*curLine).pop_back();
253               if(idInCase>1)
254                 {
255                   idInCase-=2;
256                   current=(*curLine)[idInCase];
257                   (*curLine).pop_back();
258                 }
259             }
260           else
261             {
262               //Nothing to signal continuing in this direction hoping to find
263               current=(*iter).first->getNode();
264               (*iter).second=true;
265             }
266         }
267       vec.pop_back();
268     }
269   }
270 }
271
272
273 #endif