1 // Copyright (C) 2006-2016 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
20 #include "DynParaLoop.hxx"
21 #include "LinkInfo.hxx"
22 #include "OutPort.hxx"
23 #include "Container.hxx"
24 #include "ComponentInstance.hxx"
25 #include "ServiceNode.hxx"
26 #include "InlineNode.hxx"
27 #include "ElementaryNode.hxx"
28 #include "Visitor.hxx"
34 #include "YacsTrace.hxx"
37 using namespace YACS::ENGINE;
39 const char DynParaLoop::NAME_OF_SPLITTED_SEQ_OUT[] = "evalSamples";
40 const char DynParaLoop::OLD_NAME_OF_SPLITTED_SEQ_OUT[] = "SmplPrt"; // For backward compatibility with 5.1.4
42 const char DynParaLoop::NAME_OF_NUMBER_OF_BRANCHES[]="nbBranches";
44 DynParaLoop::DynParaLoop(const std::string& name, TypeCode *typeOfDataSplitted)
45 : ComposedNode(name),_node(0),_initNode(0),_finalizeNode(0),_nbOfEltConsumed(0),
46 _nbOfBranches(NAME_OF_NUMBER_OF_BRANCHES,this,Runtime::_tc_int),
47 _splittedPort(NAME_OF_SPLITTED_SEQ_OUT,this,typeOfDataSplitted),_initializingCounter(0),_unfinishedCounter(0),_failedCounter(0)
51 DynParaLoop::~DynParaLoop()
58 DynParaLoop::DynParaLoop(const DynParaLoop& other, ComposedNode *father, bool editionOnly)
59 : ComposedNode(other,father), _nbOfBranches(other._nbOfBranches,this),
60 _splittedPort(other._splittedPort,this), _node(0), _initNode(0), _finalizeNode(0),
61 _nbOfEltConsumed(0),_initializingCounter(0),_unfinishedCounter(0),_failedCounter(0)
64 _node=other._node->clone(this,editionOnly);
66 _initNode=other._initNode->clone(this,editionOnly);
67 if(other._finalizeNode)
68 _finalizeNode = other._finalizeNode->clone(this,editionOnly);
69 const AnyOutputPort& startOfLinksToReproduce=other._splittedPort;
70 set<InPort *> endsOfLinksToReproduce=startOfLinksToReproduce.edSetInPort();
71 for(set<InPort *>::iterator iter=endsOfLinksToReproduce.begin();iter!=endsOfLinksToReproduce.end();iter++)
72 edAddLink(&_splittedPort,getInPort(other.getPortName(*iter)));
75 Node *DynParaLoop::edSetNode(Node *node)
77 return checkConsistencyAndSetNode(_node, node);
80 //! This method is used to factorize methods edSetNode, edSetInitNode and edSetFinalizeNode
81 Node * DynParaLoop::checkConsistencyAndSetNode(Node* &nodeToReplace, Node* node)
83 if (node == NULL || nodeToReplace == node)
86 throw Exception(string("Can't set node: node ") + node->getName() + " is not orphan.");
87 if (_node && _node != nodeToReplace && _node->getName() == node->getName())
88 throw Exception(string("Can't set node: node ") + node->getName() +
89 " has the same name than exec node already in " + _name + ".");
90 if (_initNode && _initNode != nodeToReplace && _initNode->getName() == node->getName())
91 throw Exception(string("Can't set node: node ") + node->getName() +
92 " has the same name than init node already in " + _name + ".");
93 if (_finalizeNode && _finalizeNode != nodeToReplace && _finalizeNode->getName() == node->getName())
94 throw Exception(string("Can't set node: node ") + node->getName() +
95 " has the same name than finalize node already in " + _name + ".");
96 checkNoCrossHierachyWith(node);
97 ComposedNode::edRemoveChild(nodeToReplace);
98 Node * ret = nodeToReplace;
100 nodeToReplace->_father = this;
101 // set _modified flag so that edUpdateState can refresh state
106 void DynParaLoop::init(bool start)
108 ComposedNode::init(start);
111 string what("DynParaLoop::init : no node specified for ForEachLoop with name "); what +=_name;
112 throw Exception(what);
115 if (_initNode) _initNode->init(start);
116 if (_finalizeNode) _finalizeNode->init(start);
117 _nbOfBranches.exInit(start);
118 _splittedPort.exInit();
123 Node *DynParaLoop::edSetInitNode(Node *node)
125 return checkConsistencyAndSetNode(_initNode, node);
128 Node * DynParaLoop::edSetFinalizeNode(Node * node)
130 return checkConsistencyAndSetNode(_finalizeNode, node);
133 //! Connect an OutPort to an InPort and add control link if necessary
135 * Connect the ports with a data link (edAddLink)
136 * In a Loop don't add control flow link : use this only to add data back links
138 * \param start : the OutPort to connect
139 * \param end : the InPort to connect
140 * \return true if a new link has been created, false otherwise.
142 bool DynParaLoop::edAddDFLink(OutPort *start, InPort *end) throw(YACS::Exception)
144 return edAddLink(start,end);
147 int DynParaLoop::getNumberOfInputPorts() const
149 return ComposedNode::getNumberOfInputPorts()+1;
152 int DynParaLoop::getNumberOfOutputPorts() const
154 return ComposedNode::getNumberOfOutputPorts()+1;
158 * DynParaLoop creates at runtime (exupdateState) clone of nodes. One clone per branch.
159 * This method returns the id of the branch given the node \a node.
160 * If \a node is not a dynamically created node in \a this -1 is returned.
162 int DynParaLoop::getBranchIDOfNode(Node *node) const
166 for(std::vector<Node *>::const_iterator it=_execNodes.begin();it!=_execNodes.end();it++)
168 return std::distance(_execNodes.begin(),it);
172 for(std::vector<Node *>::const_iterator it=_execFinalizeNodes.begin();it!=_execFinalizeNodes.end();it++)
174 return std::distance(_execFinalizeNodes.begin(),it);
178 for(std::vector<Node *>::const_iterator it=_execInitNodes.begin();it!=_execInitNodes.end();it++)
180 return std::distance(_execInitNodes.begin(),it);
185 std::list<OutputPort *> DynParaLoop::getSetOfOutputPort() const
187 list<OutputPort *> ret=ComposedNode::getSetOfOutputPort();
188 ret.push_back((OutputPort *)&_splittedPort);
192 std::list<OutputPort *> DynParaLoop::getLocalOutputPorts() const
194 list<OutputPort *> ret=ComposedNode::getLocalOutputPorts();
195 ret.push_back((OutputPort *)&_splittedPort);
199 OutPort *DynParaLoop::getOutPort(const std::string& name) const throw(YACS::Exception)
201 if (name == NAME_OF_SPLITTED_SEQ_OUT || name == OLD_NAME_OF_SPLITTED_SEQ_OUT)
202 return (OutPort *)&_splittedPort;
203 return ComposedNode::getOutPort(name);
207 OutputPort *DynParaLoop::getOutputPort(const std::string& name) const throw(YACS::Exception)
209 if (name == NAME_OF_SPLITTED_SEQ_OUT || name == OLD_NAME_OF_SPLITTED_SEQ_OUT)
210 return (OutputPort *)&_splittedPort;
211 return ComposedNode::getOutputPort(name);
214 bool DynParaLoop::isPlacementPredictableB4Run() const
219 Node *DynParaLoop::edRemoveNode()
221 return removeNode(_node);
224 //! This method is used to factorize methods edRemoveNode, edRemoveInitNode and edRemoveFinalizeNode
225 Node * DynParaLoop::removeNode(Node* &nodeToRemove)
229 ComposedNode::edRemoveChild(nodeToRemove);
230 Node * ret = nodeToRemove;
236 Node *DynParaLoop::edRemoveInitNode()
238 return removeNode(_initNode);
241 Node * DynParaLoop::edRemoveFinalizeNode()
243 return removeNode(_finalizeNode);
246 void DynParaLoop::edRemoveChild(Node *node) throw(YACS::Exception)
248 ComposedNode::edRemoveChild(node);
253 if(node==_finalizeNode)
258 bool DynParaLoop::edAddChild(Node *DISOWNnode) throw(YACS::Exception)
260 return edSetNode(DISOWNnode);
263 std::list<Node *> DynParaLoop::edGetDirectDescendants() const
267 ret.push_back(_node);
269 ret.push_back(_initNode);
271 ret.push_back(_finalizeNode);
275 std::list<InputPort *> DynParaLoop::getSetOfInputPort() const
277 list<InputPort *> ret=ComposedNode::getSetOfInputPort();
278 ret.push_back((InputPort *)&_nbOfBranches);
282 InputPort *DynParaLoop::getInputPort(const std::string& name) const throw(YACS::Exception)
284 if(name==NAME_OF_NUMBER_OF_BRANCHES)
285 return (InputPort *)&_nbOfBranches;
286 return ComposedNode::getInputPort(name);
289 std::list<InputPort *> DynParaLoop::getLocalInputPorts() const
291 list<InputPort *> ret=ComposedNode::getLocalInputPorts();
292 ret.push_back((InputPort *)&_nbOfBranches);
296 unsigned DynParaLoop::getNumberOfBranchesCreatedDyn() const throw(YACS::Exception)
298 if(_execNodes.empty())
299 throw Exception("ForEachLoop::getNumberOfBranches : No branches created dynamically ! - ForEachLoop needs to run or to be runned to call getNumberOfBranches");
301 return _execNodes.size();
304 Node *DynParaLoop::getChildByShortName(const std::string& name) const throw(YACS::Exception)
306 if (_node && name == _node->getName())
308 if (_initNode && name == _initNode->getName())
310 if (_finalizeNode && name == _finalizeNode->getName())
311 return _finalizeNode;
312 std::string what("node "); what+= name ; what+=" is not a child of DynParaLoop node "; what += getName();
313 throw Exception(what);
316 Node *DynParaLoop::getChildByNameExec(const std::string& name, unsigned id) const throw(YACS::Exception)
318 if(id>=getNumberOfBranchesCreatedDyn())
319 throw Exception("ForEachLoop::getChildByNameExec : invalid id - too large compared with dynamically created branches.");
320 if (_node && name == _node->getName())
321 return _execNodes[id];
322 if (_initNode && name == _initNode->getName())
323 return _execInitNodes[id];
324 if (_finalizeNode && name == _finalizeNode->getName())
325 return _execFinalizeNodes[id];
326 std::string what("node "); what+= name ; what+=" is not a child of DynParaLoop node "; what += getName();
327 throw Exception(what);
330 void DynParaLoop::cleanDynGraph()
332 vector<Node *>::iterator iter;
333 for(iter=_execNodes.begin() ; iter!=_execNodes.end() ; iter++)
336 for(iter=_execInitNodes.begin() ; iter!=_execInitNodes.end() ; iter++)
338 _execInitNodes.clear();
339 for(iter=_execFinalizeNodes.begin() ; iter!=_execFinalizeNodes.end() ; iter++)
341 _execFinalizeNodes.clear();
345 * This method applies on newly cloned on exec nodes (_execNodes/_execInitNodes)
346 * the setting of input ports coming from outside of 'this'
348 void DynParaLoop::prepareInputsFromOutOfScope(int branchNb)
350 set< InPort * > portsToSetVals=getAllInPortsComingFromOutsideOfCurrentScope();
352 // This tweak is to fix problems with nested dynamic loops where links are not cloned properly
353 list<InPort *> temp = getSetOfInPort();
354 for(list<InPort *>::iterator iter2=temp.begin();iter2!=temp.end();iter2++)
356 if ((*iter2)->edSetOutPort().size() == 1 && *(*iter2)->edSetOutPort().begin() == NULL)
358 portsToSetVals.insert(*iter2);
362 // local input ports are not candidates for dynamically duplicated inport.
363 list<InputPort *> localPorts = getLocalInputPorts();
364 for(list<InputPort *>::iterator iter = localPorts.begin() ; iter != localPorts.end() ; iter++)
365 portsToSetVals.erase(*iter);
366 for(set< InPort * >::iterator iter=portsToSetVals.begin();iter!=portsToSetVals.end();iter++)
368 InputPort *curPortCasted=(InputPort *) *iter;//Cast granted by ForEachLoop::buildDelegateOf(InPort)
369 void *val=curPortCasted->get();
370 InputPort *portToSet=getDynInputPortByAbsName(branchNb,getInPortName(*iter),true);
371 if(portToSet)//portToSet==0 in case of portToSet==_splitterNode._dataPortToDispatch of ForEach
373 portToSet->put((const void *)val);
374 portToSet->edNotifyReferencedBy(0);//This is to indicate that somewhere somebody deals with this inputport
375 //even if no direct physical link exists. This exclusively for _execNodes[branchNb]::init on the next turn of loop.
380 void DynParaLoop::putValueOnBranch(Any *val, unsigned branchId, bool first)
382 bool isDispatched = false;
383 set<InPort *> inPrtLkdWthSplttdPrt=_splittedPort.edSetInPort();
384 for(set<InPort *>::iterator iter=inPrtLkdWthSplttdPrt.begin();iter!=inPrtLkdWthSplttdPrt.end();iter++)
386 std::string portNameOnCurPrt=getPortName(*iter);
387 InputPort *portOnGivenBranch=getDynInputPortByAbsName(branchId,portNameOnCurPrt,first);//Cast granted because impossible to have cross protocol with _splittedPort
388 //see OptimizerLoop::buildDelegateOf
389 if(portOnGivenBranch)
392 portOnGivenBranch->edNotifyReferencedBy(0);
393 InputPort *traducer=getRuntime()->adapt(portOnGivenBranch,
394 Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME,_splittedPort.edGetType());
395 traducer->put((const void *)val);
397 if(traducer!=portOnGivenBranch)
403 Any *tmp=val->clone();
404 _splittedPort.setValue(tmp);
409 DynParaLoop::TypeOfNode DynParaLoop::getIdentityOfNotifyerNode(const Node *node, unsigned& id)
411 vector<Node *>::iterator iter;
413 for (iter=_execNodes.begin() ; iter!=_execNodes.end() ; iter++,id++)
417 for (iter=_execInitNodes.begin() ; iter!=_execInitNodes.end() ; iter++,id++)
421 for (iter=_execFinalizeNodes.begin() ; iter!=_execFinalizeNodes.end() ; iter++,id++)
423 return FINALIZE_NODE;
426 bool DynParaLoop::isMultiplicitySpecified(unsigned& value) const
428 if(_nbOfBranches.edIsManuallyInitialized())
429 if(_nbOfBranches.edGetNumberOfLinks()==0)
431 value=_nbOfBranches.getIntValue();
437 void DynParaLoop::forceMultiplicity(unsigned value)
439 _nbOfBranches.edRemoveAllLinksLinkedWithMe();
440 _nbOfBranches.edInit((int)value);
443 void DynParaLoop::buildDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView)
445 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
446 if(typeOfPortInstance!=InputPort::NAME)
447 throw Exception("DynParaLoop::buildDelegateOf : A link with datastream end inside DynParaLoop is not possible");
450 void DynParaLoop::buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView)
452 std::string linkName("(");
453 linkName += port.first->getName()+" to "+finalTarget->getName()+")";
455 if(isInMyDescendance(port.first->getNode())==_initNode)
456 throw Exception(std::string("Illegal link within a parallel loop: \
457 a link starting from the init node can't leave the scope of the loop.")
461 if(isInMyDescendance(port.first->getNode())==_finalizeNode)
462 throw Exception(std::string("Illegal link within a parallel loop: \
463 an output port of the finalize node can't be linked.")
466 if(port.first==&_splittedPort)
467 throw Exception(std::string("Illegal link within a parallel loop: \
468 the 'evalSamples' port must be linked within the scope of the loop.")
472 void DynParaLoop::checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const
474 const char what[]="DynParaLoop::checkCFLinks : internal error.";
475 //First dealing checkCFLinks forwarding...
476 if(isInMyDescendance(end->getNode())==0)//no chance that _splittedPort is in starts due to buildDelegate.
477 solveObviousOrDelegateCFLinks(starts,end,alreadyFed,direction,info);
479 {//no forwarding here.
481 throw Exception(what);
482 //ASSERT(direction) : see DynParaLoop::checkControlDependancy only 'fw' filled.
483 if(alreadyFed==FREE_ST)
485 else if(alreadyFed==FED_ST)
486 {//Shame ! splittedPort fills a port already fed...
487 info.pushInfoLink(*(starts.begin()),end,I_USELESS);
493 * \param start : start port
494 * \param end : end port
495 * \param cross indicates if start -> end link is a DS link behind.
496 * \param fw out parameter.
497 * \param fwCross out parameter storing links where a cross has been detected.
498 * \param bw out parameter where backward links are stored.
499 * \param info : collected information
501 void DynParaLoop::checkControlDependancy(OutPort *start, InPort *end, bool cross,
502 std::map < ComposedNode *, std::list < OutPort * >, SortHierarc >& fw,
503 std::vector<OutPort *>& fwCross,
504 std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
505 LinkInfo& info) const
507 fw[(ComposedNode *)this].push_back(start);
510 void DynParaLoop::checkLinkPossibility(OutPort *start, const std::list<ComposedNode *>& pointsOfViewStart,
511 InPort *end, const std::list<ComposedNode *>& pointsOfViewEnd) throw(YACS::Exception)
513 ComposedNode::checkLinkPossibility(start, pointsOfViewStart, end, pointsOfViewEnd);
514 Node * startNode = isInMyDescendance(start->getNode());
515 Node * endNode = isInMyDescendance(end->getNode());
516 std::string linkName("(");
517 linkName += start->getName()+" to "+end->getName()+")";
519 if(start == &_splittedPort && endNode != _node)
520 throw Exception(std::string("Illegal link within a parallel loop: \
521 the 'evalSamples' port can only be connected to the working node of the loop.")
524 if(_finalizeNode && _finalizeNode == startNode)
525 throw Exception(std::string("Illegal link within a parallel loop: \
526 the finalize node can't be the origin of a link.")
529 if(_initNode && _node == startNode && _initNode == endNode)
530 throw Exception(std::string("Illegal link within a parallel loop: \
531 can't make a link from the working node to the init node.")
534 if(_finalizeNode && _node == startNode && _finalizeNode == endNode)
535 throw Exception(std::string("Illegal link within a parallel loop: \
536 can't make a link from the working node to the finalize node.")
541 * \note : For a given name 'name' of port in absolute form from this, returns the corresponding InputPort
542 * instance of the port for the branch # 'branchNb'.
543 * The port can be part of _node or _initNode if it exists (if 'initNodeAdmitted' is true).
544 * \b WARNING : no check performed on 'branchNb' value to see if it is compatible with size of '_execNodes'.
545 * This method is called to dispatch value on each InputPort linked to this->._splitterNode._splittedPort
547 InputPort *DynParaLoop::getDynInputPortByAbsName(int branchNb, const std::string& name, bool initNodeAdmitted)
549 string portName, nodeName;
550 splitNamesBySep(name,Node::SEP_CHAR_IN_PORT,nodeName,portName,true);
551 Node *staticChild = getChildByName(nodeName);
552 Node *desc=isInMyDescendance(staticChild);
555 splitNamesBySep(name,Node::SEP_CHAR_IN_PORT,nodeName,portName,false);
556 return _execNodes[branchNb]->getInputPort(portName);
558 else if(desc==_initNode)
561 splitNamesBySep(name,Node::SEP_CHAR_IN_PORT,nodeName,portName,false);
562 return _execInitNodes[branchNb]->getInputPort(portName);
567 void DynParaLoop::checkBasicConsistency() const throw(YACS::Exception)
569 DEBTRACE("DynParaLoop::checkBasicConsistency");
570 ComposedNode::checkBasicConsistency();
572 throw Exception("For a dynamic loop, internal node is mandatory");
575 std::string DynParaLoop::getErrorReport()
577 DEBTRACE("DynParaLoop::getErrorReport: " << getName() << " " << _state);
578 YACS::StatesForNode effectiveState=getEffectiveState();
580 if(effectiveState != YACS::INVALID && effectiveState != YACS::ERROR && effectiveState != YACS::FAILED)
583 std::string report="<error node= " + getName();
584 switch(effectiveState)
587 report=report+" state= INVALID";
590 report=report+" state= ERROR";
593 report=report+" state= FAILED";
598 report=report + ">\n" ;
599 if(_errorDetails != "")
600 report=report+_errorDetails+"\n";
602 if(_execNodes.empty())
605 list<Node *> constituents=edGetDirectDescendants();
606 for(list<Node *>::iterator iter=constituents.begin(); iter!=constituents.end(); iter++)
608 std::string rep=(*iter)->getErrorReport();
611 report=report+rep+"\n";
618 for(vector<Node *>::iterator iter=_execInitNodes.begin();iter!=_execInitNodes.end();iter++)
620 std::string rep=(*iter)->getErrorReport();
623 report=report+rep+"\n";
626 for(vector<Node *>::iterator iter=_execNodes.begin();iter!=_execNodes.end();iter++)
628 std::string rep=(*iter)->getErrorReport();
631 report=report+rep+"\n";
634 for(vector<Node *>::iterator iter=_execFinalizeNodes.begin();iter!=_execFinalizeNodes.end();iter++)
636 std::string rep=(*iter)->getErrorReport();
639 report=report+rep+"\n";
644 report=report+"</error>";
648 void DynParaLoop::forwardExecStateToOriginalBody(Node *execNode)
651 Node * origNode = NULL;
652 switch (getIdentityOfNotifyerNode(execNode,id))
656 origNode = _initNode;
666 origNode = _finalizeNode;
673 YASSERT(origNode != NULL)
674 origNode->setState(execNode->getState());
675 origNode->setErrorDetails(execNode->getErrorDetails());
677 ComposedNode* compNode = dynamic_cast<ComposedNode*>(origNode);
678 ComposedNode* compNodeExe = dynamic_cast<ComposedNode*>(execNode);
679 if (compNode && compNodeExe)
681 list<Node *> aChldn = compNodeExe->getAllRecursiveConstituents();
682 list<Node *>::iterator iter=aChldn.begin();
683 for(;iter!=aChldn.end();iter++)
685 Node* node=compNode->getChildByName(compNodeExe->getChildName(*iter));
686 node->setState((*iter)->getState());
687 node->setErrorDetails((*iter)->getErrorDetails());
692 //! Method used to notify the node that a child node has failed
694 * Update the current state and return the change state
696 * \param node : the child node that has failed
697 * \return the state change
699 YACS::Event DynParaLoop::updateStateOnFailedEventFrom(Node *node, const Executor *execInst)
701 DEBTRACE("DynParaLoop::updateStateOnFailedEventFrom " << node->getName());
702 setState(YACS::FAILED);
703 forwardExecStateToOriginalBody(node);
705 switch (getIdentityOfNotifyerNode(node,id))
709 _node->setState(YACS::FAILED);
710 if (_finalizeNode != NULL) _finalizeNode->setState(YACS::FAILED);
715 if (_finalizeNode != NULL) _finalizeNode->setState(YACS::FAILED);
722 //! Clone nodes and make their placement consistent with the placement of the original ones.
724 * For instance, if two original nodes are placed on a component comp1 in a container cont1
725 * and a third one is placed on a component comp2 in the container cont1, the clones of the two
726 * first nodes will be placed on a component comp3 in a container cont2 and the third clone
727 * will be placed on a component comp4 in the container cont2.
729 vector<Node *> DynParaLoop::cloneAndPlaceNodesCoherently(const vector<Node *> & origNodes)
731 DEBTRACE("Begin cloneAndPlaceNodesCoherently")
732 vector<Node *> clones;
733 DeploymentTree treeToDup;
734 vector<list<ElementaryNode *> > origElemNodeList;
735 for (int i=0 ; i<origNodes.size() ; i++)
737 DEBTRACE("Cloning node " << i)
738 if (origNodes[i] == NULL)
740 DEBTRACE("Cloning node " << i << ", NULL" )
741 clones.push_back(NULL);
742 origElemNodeList.push_back(list<ElementaryNode *>());
746 DEBTRACE("Cloning node " << i << "," << origNodes[i]->getName())
747 clones.push_back(origNodes[i]->simpleClone(this, false));
748 list<ElementaryNode *> tasks = origNodes[i]->getRecursiveConstituents();
749 origElemNodeList.push_back(tasks);
750 for (list< ElementaryNode *>::iterator iter=tasks.begin() ; iter!=tasks.end() ; iter++)
751 treeToDup.appendTask(*iter, (*iter)->getDynClonerIfExists(this));
755 // Build the links between clones.
756 // Only the links starting from initNode are possible.
759 std::vector< std::pair<OutPort *, InPort *> > outLinks = _initNode->getSetOfLinksLeavingCurrentScope();
760 std::vector< std::pair<OutPort *, InPort *> >::const_iterator it;
761 for(it=outLinks.begin(); it!=outLinks.end(); it++)
763 OutPort *startPort = it->first;
764 InPort *endPort = it->second;
765 Node* destNode = isInMyDescendance(endPort->getNode());
766 if(destNode == _node)
767 edAddLink(clones[0]->getOutPort(startPort->getName()),
768 clones[1]->getInPort(endPort->getName()));
769 if(destNode == _finalizeNode)
770 edAddLink(clones[0]->getOutPort(startPort->getName()),
771 clones[2]->getInPort(endPort->getName()));
775 DEBTRACE("Placing nodes...")
776 vector<Container *> conts=treeToDup.getAllContainers();
778 //iterate on all containers
779 for(vector<Container *>::iterator iterCt=conts.begin();iterCt!=conts.end();iterCt++)
781 DEBTRACE("Container " << ((*iterCt)?(*iterCt)->getName():"NULL"))
782 vector<ComponentInstance *> comps=treeToDup.getComponentsLinkedToContainer(*iterCt);
783 Container *contCloned=0;
785 contCloned=(*iterCt)->clone();
787 //iterate on all component instances linked to the container
788 for(vector<ComponentInstance *>::iterator iterCp=comps.begin();iterCp!=comps.end();iterCp++)
790 DEBTRACE("Component " << (*iterCp)->getCompoName())
791 vector<Task *> tasks=treeToDup.getTasksLinkedToComponent(*iterCp);
792 ComponentInstance *curCloned=(*iterCp)->clone();
793 DEBTRACE("Assign container " << (*iterCp)->getCompoName())
794 curCloned->setContainer(contCloned);
795 for(vector<Task *>::iterator iterT=tasks.begin();iterT!=tasks.end();iterT++)
797 DEBTRACE("Task " << ((ElementaryNode *)(*iterT))->getName())
799 ElementaryNode * origElemNode = NULL;
800 for (i=0 ; i<origNodes.size() ; i++)
801 if (origNodes[i] != NULL)
803 DEBTRACE("Looking in original node " << i)
804 list<ElementaryNode *>::iterator res=find(origElemNodeList[i].begin(),
805 origElemNodeList[i].end(),
806 (ElementaryNode *)(*iterT));
807 if (res != origElemNodeList[i].end()) {
813 YASSERT(origElemNode != NULL)
814 DEBTRACE("Found task in node " << i)
815 ServiceNode * nodeC = NULL;
816 if (origNodes[i] == origElemNode)
817 nodeC = (ServiceNode *)clones[i];
820 string childName = ((ComposedNode *)origNodes[i])->getChildName(origElemNode);
821 nodeC = (ServiceNode *)clones[i]->getChildByName(childName);
823 DEBTRACE("Assign component: " << (*iterCp)->getCompoName() << "," << nodeC->getName())
824 nodeC->setComponent(curCloned);
826 curCloned->decrRef();
829 // iterate on all tasks linked to the container
830 vector<Task *> tasks=treeToDup.getTasksLinkedToContainer(*iterCt);
831 for(vector<Task *>::iterator iterT=tasks.begin();iterT!=tasks.end();iterT++)
833 DEBTRACE("Task " << ((ElementaryNode *)(*iterT))->getName())
835 ElementaryNode * origElemNode = NULL;
836 for (i=0 ; i<origNodes.size() ; i++)
837 if (origNodes[i] != NULL)
839 DEBTRACE("Looking in original node " << i)
840 list<ElementaryNode *>::iterator res=find(origElemNodeList[i].begin(),
841 origElemNodeList[i].end(),
842 (ElementaryNode *)(*iterT));
843 if (res != origElemNodeList[i].end())
849 YASSERT(origElemNode != NULL)
850 DEBTRACE("Found task in node " << i)
851 InlineFuncNode * nodeC = NULL;
852 if (origNodes[i] == origElemNode)
854 nodeC = (InlineFuncNode *)clones[i];
858 string childName = ((ComposedNode *)origNodes[i])->getChildName(origElemNode);
859 nodeC = (InlineFuncNode *)clones[i]->getChildByName(childName);
861 DEBTRACE("Assign container " << nodeC->getName() << "," << contCloned->getName())
862 nodeC->setContainer(contCloned);
865 // ended with current container
867 contCloned->decrRef();
870 DEBTRACE("End cloneAndPlaceNodesCoherently")
874 void DynParaLoop::accept(Visitor *visitor)
876 visitor->visitDynParaLoop(this);
879 Node * DynParaLoop::getInitNode()
884 Node * DynParaLoop::getExecNode()
889 Node * DynParaLoop::getFinalizeNode()
891 return _finalizeNode;
894 int DynParaLoop::getMaxLevelOfParallelism() const
896 return _nbOfBranches.getIntValue() * _node->getMaxLevelOfParallelism();
899 void DynParaLoop::shutdown(int level)
904 std::vector<Node *>::iterator iter;
905 for (iter=_execNodes.begin() ; iter!=_execNodes.end() ; iter++)
906 (*iter)->shutdown(level);
907 for (iter=_execInitNodes.begin() ; iter!=_execInitNodes.end() ; iter++)
908 (*iter)->shutdown(level);
909 for (iter=_execFinalizeNodes.begin() ; iter!=_execFinalizeNodes.end() ; iter++)
910 (*iter)->shutdown(level);