1 // Copyright (C) 2006-2021 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "YACSlibEngineExport.hxx"
24 #include "StaticDefinedComposedNode.hxx"
25 #include "AutoRefCnt.hxx"
35 class YACSLIBENGINE_EXPORT Bloc : public StaticDefinedComposedNode
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;
44 Node *simpleClone(ComposedNode *father, bool editionOnly=true) const;
46 Bloc(const Bloc& other, ComposedNode *father, bool editionOnly);
47 Bloc(const std::string& name);
50 int getNumberOfCFLinks() const;
51 void init(bool start=true);
52 void getReadyTasks(std::vector<Task *>& tasks);
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);
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;
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);
108 template<bool direction>
109 struct CFDirectionVisTraits
114 struct CFDirectionVisTraits<true>
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(); }
123 struct CFDirectionVisTraits<false>
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(); }
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.
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)
137 std::map<Node *, std::set<Node *> >::iterator iter=fastFinder.find(node);
138 if(iter==fastFinder.end())
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());
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;
149 if(li.back().first!=li.back().second)
151 work.push_back(*(li.back().first));
152 if(CFDirectionVisTraits<direction>::getNexts(work.back()).empty())
154 where=res.insert(where,work);
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());
175 for(unsigned i=0;i<ret;i++,updates--)
176 updateWithNewFind(*updates,fastFinder);
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
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);
190 findUselessLinksIn(li,info);
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'.
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
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)
213 (*curLine).push_back(current);
216 if(CFDirectionVisTraits<direction>::getNexts(current).empty())
219 vec.push_back(std::vector<Node *>((*curLine)));
220 updateWithNewFind(*curLine,accelStr);
221 current->_colour=YACS::Black;
226 current=(*curLine)[idInCase];
227 (*curLine).pop_back();
228 (*curLine).pop_back();
232 if(current->_colour==YACS::Black)
234 appendIfAlreadyFound<direction>(vec,(*curLine),current,accelStr);
235 curLine=vec.end(); curLine--;
236 current->_colour=YACS::Black;
240 current=(*curLine)[idInCase];
241 (*curLine).pop_back();
242 (*curLine).pop_back();
246 for(iter=CFDirectionVisTraits<direction>::getNexts(current).begin();iter!=CFDirectionVisTraits<direction>::getNexts(current).end();iter++)
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();
256 current=(*curLine)[idInCase];
257 (*curLine).pop_back();
262 //Nothing to signal continuing in this direction hoping to find
263 current=(*iter).first->getNode();