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),_weight(), _loopWeight(0)
49 _weight.setDefaultLoop();
52 DynParaLoop::~DynParaLoop()
59 DynParaLoop::DynParaLoop(const DynParaLoop& other, ComposedNode *father, bool editionOnly)
60 : ComposedNode(other,father), _nbOfBranches(other._nbOfBranches,this),
61 _splittedPort(other._splittedPort,this), _node(0), _initNode(0), _finalizeNode(0),
62 _nbOfEltConsumed(0),_initializingCounter(0),_unfinishedCounter(0),_failedCounter(0),_weight(other._weight), _loopWeight(other._loopWeight)
65 _node=other._node->clone(this,editionOnly);
67 _initNode=other._initNode->clone(this,editionOnly);
68 if(other._finalizeNode)
69 _finalizeNode = other._finalizeNode->clone(this,editionOnly);
70 const AnyOutputPort& startOfLinksToReproduce=other._splittedPort;
71 set<InPort *> endsOfLinksToReproduce=startOfLinksToReproduce.edSetInPort();
72 for(set<InPort *>::iterator iter=endsOfLinksToReproduce.begin();iter!=endsOfLinksToReproduce.end();iter++)
73 edAddLink(&_splittedPort,getInPort(other.getPortName(*iter)));
76 Node *DynParaLoop::edSetNode(Node *node)
78 return checkConsistencyAndSetNode(_node, node);
81 //! This method is used to factorize methods edSetNode, edSetInitNode and edSetFinalizeNode
82 Node * DynParaLoop::checkConsistencyAndSetNode(Node* &nodeToReplace, Node* node)
84 if (node == NULL || nodeToReplace == node)
87 throw Exception(string("Can't set node: node ") + node->getName() + " is not orphan.");
88 if (_node && _node != nodeToReplace && _node->getName() == node->getName())
89 throw Exception(string("Can't set node: node ") + node->getName() +
90 " has the same name than exec node already in " + _name + ".");
91 if (_initNode && _initNode != nodeToReplace && _initNode->getName() == node->getName())
92 throw Exception(string("Can't set node: node ") + node->getName() +
93 " has the same name than init node already in " + _name + ".");
94 if (_finalizeNode && _finalizeNode != nodeToReplace && _finalizeNode->getName() == node->getName())
95 throw Exception(string("Can't set node: node ") + node->getName() +
96 " has the same name than finalize node already in " + _name + ".");
97 checkNoCrossHierachyWith(node);
98 ComposedNode::edRemoveChild(nodeToReplace);
99 Node * ret = nodeToReplace;
100 nodeToReplace = node;
101 nodeToReplace->_father = this;
102 // set _modified flag so that edUpdateState can refresh state
107 void DynParaLoop::init(bool start)
109 ComposedNode::init(start);
112 string what("DynParaLoop::init : no node specified for ForEachLoop with name "); what +=_name;
113 throw Exception(what);
116 if (_initNode) _initNode->init(start);
117 if (_finalizeNode) _finalizeNode->init(start);
118 _nbOfBranches.exInit(start);
119 _splittedPort.exInit();
124 Node *DynParaLoop::edSetInitNode(Node *node)
126 return checkConsistencyAndSetNode(_initNode, node);
129 Node * DynParaLoop::edSetFinalizeNode(Node * node)
131 return checkConsistencyAndSetNode(_finalizeNode, node);
134 //! Connect an OutPort to an InPort and add control link if necessary
136 * Connect the ports with a data link (edAddLink)
137 * In a Loop don't add control flow link : use this only to add data back links
139 * \param start : the OutPort to connect
140 * \param end : the InPort to connect
141 * \return true if a new link has been created, false otherwise.
143 bool DynParaLoop::edAddDFLink(OutPort *start, InPort *end) throw(YACS::Exception)
145 return edAddLink(start,end);
148 int DynParaLoop::getNumberOfInputPorts() const
150 return ComposedNode::getNumberOfInputPorts()+1;
153 int DynParaLoop::getNumberOfOutputPorts() const
155 return ComposedNode::getNumberOfOutputPorts()+1;
159 * DynParaLoop creates at runtime (exupdateState) clone of nodes. One clone per branch.
160 * This method returns the id of the branch given the node \a node.
161 * If \a node is not a dynamically created node in \a this -1 is returned.
163 int DynParaLoop::getBranchIDOfNode(Node *node) const
167 for(std::vector<Node *>::const_iterator it=_execNodes.begin();it!=_execNodes.end();it++)
169 return std::distance(_execNodes.begin(),it);
173 for(std::vector<Node *>::const_iterator it=_execFinalizeNodes.begin();it!=_execFinalizeNodes.end();it++)
175 return std::distance(_execFinalizeNodes.begin(),it);
179 for(std::vector<Node *>::const_iterator it=_execInitNodes.begin();it!=_execInitNodes.end();it++)
181 return std::distance(_execInitNodes.begin(),it);
186 std::list<OutputPort *> DynParaLoop::getSetOfOutputPort() const
188 list<OutputPort *> ret=ComposedNode::getSetOfOutputPort();
189 ret.push_back((OutputPort *)&_splittedPort);
193 std::list<OutputPort *> DynParaLoop::getLocalOutputPorts() const
195 list<OutputPort *> ret=ComposedNode::getLocalOutputPorts();
196 ret.push_back((OutputPort *)&_splittedPort);
200 OutPort *DynParaLoop::getOutPort(const std::string& name) const throw(YACS::Exception)
202 if (name == NAME_OF_SPLITTED_SEQ_OUT || name == OLD_NAME_OF_SPLITTED_SEQ_OUT)
203 return (OutPort *)&_splittedPort;
204 return ComposedNode::getOutPort(name);
208 OutputPort *DynParaLoop::getOutputPort(const std::string& name) const throw(YACS::Exception)
210 if (name == NAME_OF_SPLITTED_SEQ_OUT || name == OLD_NAME_OF_SPLITTED_SEQ_OUT)
211 return (OutputPort *)&_splittedPort;
212 return ComposedNode::getOutputPort(name);
215 bool DynParaLoop::isPlacementPredictableB4Run() const
220 Node *DynParaLoop::edRemoveNode()
222 return removeNode(_node);
225 //! This method is used to factorize methods edRemoveNode, edRemoveInitNode and edRemoveFinalizeNode
226 Node * DynParaLoop::removeNode(Node* &nodeToRemove)
230 ComposedNode::edRemoveChild(nodeToRemove);
231 Node * ret = nodeToRemove;
237 Node *DynParaLoop::edRemoveInitNode()
239 return removeNode(_initNode);
242 Node * DynParaLoop::edRemoveFinalizeNode()
244 return removeNode(_finalizeNode);
247 void DynParaLoop::edRemoveChild(Node *node) throw(YACS::Exception)
249 ComposedNode::edRemoveChild(node);
254 if(node==_finalizeNode)
259 bool DynParaLoop::edAddChild(Node *DISOWNnode) throw(YACS::Exception)
261 return edSetNode(DISOWNnode);
264 std::list<Node *> DynParaLoop::edGetDirectDescendants() const
268 ret.push_back(_node);
270 ret.push_back(_initNode);
272 ret.push_back(_finalizeNode);
276 std::list<InputPort *> DynParaLoop::getSetOfInputPort() const
278 list<InputPort *> ret=ComposedNode::getSetOfInputPort();
279 ret.push_back((InputPort *)&_nbOfBranches);
283 InputPort *DynParaLoop::getInputPort(const std::string& name) const throw(YACS::Exception)
285 if(name==NAME_OF_NUMBER_OF_BRANCHES)
286 return (InputPort *)&_nbOfBranches;
287 return ComposedNode::getInputPort(name);
290 std::list<InputPort *> DynParaLoop::getLocalInputPorts() const
292 list<InputPort *> ret=ComposedNode::getLocalInputPorts();
293 ret.push_back((InputPort *)&_nbOfBranches);
297 unsigned DynParaLoop::getNumberOfBranchesCreatedDyn() const throw(YACS::Exception)
299 if(_execNodes.empty())
300 throw Exception("ForEachLoop::getNumberOfBranches : No branches created dynamically ! - ForEachLoop needs to run or to be runned to call getNumberOfBranches");
302 return _execNodes.size();
305 Node *DynParaLoop::getChildByShortName(const std::string& name) const throw(YACS::Exception)
307 if (_node && name == _node->getName())
309 if (_initNode && name == _initNode->getName())
311 if (_finalizeNode && name == _finalizeNode->getName())
312 return _finalizeNode;
313 std::string what("node "); what+= name ; what+=" is not a child of DynParaLoop node "; what += getName();
314 throw Exception(what);
317 Node *DynParaLoop::getChildByNameExec(const std::string& name, unsigned id) const throw(YACS::Exception)
319 if(id>=getNumberOfBranchesCreatedDyn())
320 throw Exception("ForEachLoop::getChildByNameExec : invalid id - too large compared with dynamically created branches.");
321 if (_node && name == _node->getName())
322 return _execNodes[id];
323 if (_initNode && name == _initNode->getName())
324 return _execInitNodes[id];
325 if (_finalizeNode && name == _finalizeNode->getName())
326 return _execFinalizeNodes[id];
327 std::string what("node "); what+= name ; what+=" is not a child of DynParaLoop node "; what += getName();
328 throw Exception(what);
331 void DynParaLoop::cleanDynGraph()
333 vector<Node *>::iterator iter;
334 for(iter=_execNodes.begin() ; iter!=_execNodes.end() ; iter++)
337 for(iter=_execInitNodes.begin() ; iter!=_execInitNodes.end() ; iter++)
339 _execInitNodes.clear();
340 for(iter=_execFinalizeNodes.begin() ; iter!=_execFinalizeNodes.end() ; iter++)
342 _execFinalizeNodes.clear();
346 * This method applies on newly cloned on exec nodes (_execNodes/_execInitNodes)
347 * the setting of input ports coming from outside of 'this'
349 void DynParaLoop::prepareInputsFromOutOfScope(int branchNb)
351 set< InPort * > portsToSetVals=getAllInPortsComingFromOutsideOfCurrentScope();
353 // This tweak is to fix problems with nested dynamic loops where links are not cloned properly
354 list<InPort *> temp = getSetOfInPort();
355 for(list<InPort *>::iterator iter2=temp.begin();iter2!=temp.end();iter2++)
357 if ((*iter2)->edSetOutPort().size() == 1 && *(*iter2)->edSetOutPort().begin() == NULL)
359 portsToSetVals.insert(*iter2);
363 // local input ports are not candidates for dynamically duplicated inport.
364 list<InputPort *> localPorts = getLocalInputPorts();
365 for(list<InputPort *>::iterator iter = localPorts.begin() ; iter != localPorts.end() ; iter++)
366 portsToSetVals.erase(*iter);
367 for(set< InPort * >::iterator iter=portsToSetVals.begin();iter!=portsToSetVals.end();iter++)
369 InputPort *curPortCasted=(InputPort *) *iter;//Cast granted by ForEachLoop::buildDelegateOf(InPort)
370 void *val=curPortCasted->get();
371 InputPort *portToSet=getDynInputPortByAbsName(branchNb,getInPortName(*iter),true);
372 if(portToSet)//portToSet==0 in case of portToSet==_splitterNode._dataPortToDispatch of ForEach
374 portToSet->put((const void *)val);
375 portToSet->edNotifyReferencedBy(0);//This is to indicate that somewhere somebody deals with this inputport
376 //even if no direct physical link exists. This exclusively for _execNodes[branchNb]::init on the next turn of loop.
381 void DynParaLoop::putValueOnBranch(Any *val, unsigned branchId, bool first)
383 bool isDispatched = false;
384 set<InPort *> inPrtLkdWthSplttdPrt=_splittedPort.edSetInPort();
385 for(set<InPort *>::iterator iter=inPrtLkdWthSplttdPrt.begin();iter!=inPrtLkdWthSplttdPrt.end();iter++)
387 std::string portNameOnCurPrt=getPortName(*iter);
388 InputPort *portOnGivenBranch=getDynInputPortByAbsName(branchId,portNameOnCurPrt,first);//Cast granted because impossible to have cross protocol with _splittedPort
389 //see OptimizerLoop::buildDelegateOf
390 if(portOnGivenBranch)
393 portOnGivenBranch->edNotifyReferencedBy(0);
394 InputPort *traducer=getRuntime()->adapt(portOnGivenBranch,
395 Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME,_splittedPort.edGetType());
396 traducer->put((const void *)val);
398 if(traducer!=portOnGivenBranch)
404 Any *tmp=val->clone();
405 _splittedPort.setValue(tmp);
410 DynParaLoop::TypeOfNode DynParaLoop::getIdentityOfNotifyerNode(const Node *node, unsigned& id)
412 vector<Node *>::iterator iter;
414 for (iter=_execNodes.begin() ; iter!=_execNodes.end() ; iter++,id++)
418 for (iter=_execInitNodes.begin() ; iter!=_execInitNodes.end() ; iter++,id++)
422 for (iter=_execFinalizeNodes.begin() ; iter!=_execFinalizeNodes.end() ; iter++,id++)
424 return FINALIZE_NODE;
427 void DynParaLoop::setWeight(double loopWeight)
430 throw Exception("DynParaLoop::setWeight : invalid input value !");
431 _loopWeight=loopWeight;
434 ComplexWeight* DynParaLoop::getWeight()
437 _weight.setLoopWeight(_loopWeight, _node->getMaxLevelOfParallelism()); // not done in setWeight because _node can be null at that time
441 bool DynParaLoop::isMultiplicitySpecified(unsigned& value) const
443 if(_nbOfBranches.edIsManuallyInitialized())
444 if(_nbOfBranches.edGetNumberOfLinks()==0)
446 value=_nbOfBranches.getIntValue();
452 void DynParaLoop::forceMultiplicity(unsigned value)
454 _nbOfBranches.edRemoveAllLinksLinkedWithMe();
455 _nbOfBranches.edInit((int)value);
458 void DynParaLoop::buildDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView)
460 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
461 if(typeOfPortInstance!=InputPort::NAME)
462 throw Exception("DynParaLoop::buildDelegateOf : A link with datastream end inside DynParaLoop is not possible");
465 void DynParaLoop::buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView)
467 std::string linkName("(");
468 linkName += port.first->getName()+" to "+finalTarget->getName()+")";
470 if(isInMyDescendance(port.first->getNode())==_initNode)
471 throw Exception(std::string("Illegal link within a parallel loop: \
472 a link starting from the init node can't leave the scope of the loop.")
476 if(isInMyDescendance(port.first->getNode())==_finalizeNode)
477 throw Exception(std::string("Illegal link within a parallel loop: \
478 an output port of the finalize node can't be linked.")
481 if(port.first==&_splittedPort)
482 throw Exception(std::string("Illegal link within a parallel loop: \
483 the 'evalSamples' port must be linked within the scope of the loop.")
487 void DynParaLoop::checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const
489 const char what[]="DynParaLoop::checkCFLinks : internal error.";
490 //First dealing checkCFLinks forwarding...
491 if(isInMyDescendance(end->getNode())==0)//no chance that _splittedPort is in starts due to buildDelegate.
492 solveObviousOrDelegateCFLinks(starts,end,alreadyFed,direction,info);
494 {//no forwarding here.
496 throw Exception(what);
497 //ASSERT(direction) : see DynParaLoop::checkControlDependancy only 'fw' filled.
498 if(alreadyFed==FREE_ST)
500 else if(alreadyFed==FED_ST)
501 {//Shame ! splittedPort fills a port already fed...
502 info.pushInfoLink(*(starts.begin()),end,I_USELESS);
508 * \param start : start port
509 * \param end : end port
510 * \param cross indicates if start -> end link is a DS link behind.
511 * \param fw out parameter.
512 * \param fwCross out parameter storing links where a cross has been detected.
513 * \param bw out parameter where backward links are stored.
514 * \param info : collected information
516 void DynParaLoop::checkControlDependancy(OutPort *start, InPort *end, bool cross,
517 std::map < ComposedNode *, std::list < OutPort * >, SortHierarc >& fw,
518 std::vector<OutPort *>& fwCross,
519 std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
520 LinkInfo& info) const
522 fw[(ComposedNode *)this].push_back(start);
525 void DynParaLoop::checkLinkPossibility(OutPort *start, const std::list<ComposedNode *>& pointsOfViewStart,
526 InPort *end, const std::list<ComposedNode *>& pointsOfViewEnd) throw(YACS::Exception)
528 ComposedNode::checkLinkPossibility(start, pointsOfViewStart, end, pointsOfViewEnd);
529 Node * startNode = isInMyDescendance(start->getNode());
530 Node * endNode = isInMyDescendance(end->getNode());
531 std::string linkName("(");
532 linkName += start->getName()+" to "+end->getName()+")";
534 if(start == &_splittedPort && endNode != _node)
535 throw Exception(std::string("Illegal link within a parallel loop: \
536 the 'evalSamples' port can only be connected to the working node of the loop.")
539 if(_finalizeNode && _finalizeNode == startNode)
540 throw Exception(std::string("Illegal link within a parallel loop: \
541 the finalize node can't be the origin of a link.")
544 if(_initNode && _node == startNode && _initNode == endNode)
545 throw Exception(std::string("Illegal link within a parallel loop: \
546 can't make a link from the working node to the init node.")
549 if(_finalizeNode && _node == startNode && _finalizeNode == endNode)
550 throw Exception(std::string("Illegal link within a parallel loop: \
551 can't make a link from the working node to the finalize node.")
556 * \note : For a given name 'name' of port in absolute form from this, returns the corresponding InputPort
557 * instance of the port for the branch # 'branchNb'.
558 * The port can be part of _node or _initNode if it exists (if 'initNodeAdmitted' is true).
559 * \b WARNING : no check performed on 'branchNb' value to see if it is compatible with size of '_execNodes'.
560 * This method is called to dispatch value on each InputPort linked to this->._splitterNode._splittedPort
562 InputPort *DynParaLoop::getDynInputPortByAbsName(int branchNb, const std::string& name, bool initNodeAdmitted)
564 string portName, nodeName;
565 splitNamesBySep(name,Node::SEP_CHAR_IN_PORT,nodeName,portName,true);
566 Node *staticChild = getChildByName(nodeName);
567 Node *desc=isInMyDescendance(staticChild);
570 splitNamesBySep(name,Node::SEP_CHAR_IN_PORT,nodeName,portName,false);
571 return _execNodes[branchNb]->getInputPort(portName);
573 else if(desc==_initNode)
576 splitNamesBySep(name,Node::SEP_CHAR_IN_PORT,nodeName,portName,false);
577 return _execInitNodes[branchNb]->getInputPort(portName);
582 void DynParaLoop::checkBasicConsistency() const throw(YACS::Exception)
584 DEBTRACE("DynParaLoop::checkBasicConsistency");
585 ComposedNode::checkBasicConsistency();
587 throw Exception("For a dynamic loop, internal node is mandatory");
590 std::string DynParaLoop::getErrorReport()
592 DEBTRACE("DynParaLoop::getErrorReport: " << getName() << " " << _state);
593 YACS::StatesForNode effectiveState=getEffectiveState();
595 if(effectiveState != YACS::INVALID && effectiveState != YACS::ERROR && effectiveState != YACS::FAILED)
598 std::string report="<error node= " + getName();
599 switch(effectiveState)
602 report=report+" state= INVALID";
605 report=report+" state= ERROR";
608 report=report+" state= FAILED";
613 report=report + ">\n" ;
614 if(_errorDetails != "")
615 report=report+_errorDetails+"\n";
617 if(_execNodes.empty())
620 list<Node *> constituents=edGetDirectDescendants();
621 for(list<Node *>::iterator iter=constituents.begin(); iter!=constituents.end(); iter++)
623 std::string rep=(*iter)->getErrorReport();
626 report=report+rep+"\n";
633 for(vector<Node *>::iterator iter=_execInitNodes.begin();iter!=_execInitNodes.end();iter++)
635 std::string rep=(*iter)->getErrorReport();
638 report=report+rep+"\n";
641 for(vector<Node *>::iterator iter=_execNodes.begin();iter!=_execNodes.end();iter++)
643 std::string rep=(*iter)->getErrorReport();
646 report=report+rep+"\n";
649 for(vector<Node *>::iterator iter=_execFinalizeNodes.begin();iter!=_execFinalizeNodes.end();iter++)
651 std::string rep=(*iter)->getErrorReport();
654 report=report+rep+"\n";
659 report=report+"</error>";
663 void DynParaLoop::forwardExecStateToOriginalBody(Node *execNode)
666 Node * origNode = NULL;
667 switch (getIdentityOfNotifyerNode(execNode,id))
671 origNode = _initNode;
681 origNode = _finalizeNode;
688 YASSERT(origNode != NULL)
689 origNode->setState(execNode->getState());
690 origNode->setErrorDetails(execNode->getErrorDetails());
692 ComposedNode* compNode = dynamic_cast<ComposedNode*>(origNode);
693 ComposedNode* compNodeExe = dynamic_cast<ComposedNode*>(execNode);
694 if (compNode && compNodeExe)
696 list<Node *> aChldn = compNodeExe->getAllRecursiveConstituents();
697 list<Node *>::iterator iter=aChldn.begin();
698 for(;iter!=aChldn.end();iter++)
700 Node* node=compNode->getChildByName(compNodeExe->getChildName(*iter));
701 node->setState((*iter)->getState());
702 node->setErrorDetails((*iter)->getErrorDetails());
707 //! Method used to notify the node that a child node has failed
709 * Update the current state and return the change state
711 * \param node : the child node that has failed
712 * \return the state change
714 YACS::Event DynParaLoop::updateStateOnFailedEventFrom(Node *node, const Executor *execInst)
716 DEBTRACE("DynParaLoop::updateStateOnFailedEventFrom " << node->getName());
717 setState(YACS::FAILED);
718 forwardExecStateToOriginalBody(node);
720 switch (getIdentityOfNotifyerNode(node,id))
724 _node->setState(YACS::FAILED);
725 if (_finalizeNode != NULL) _finalizeNode->setState(YACS::FAILED);
730 if (_finalizeNode != NULL) _finalizeNode->setState(YACS::FAILED);
737 //! Clone nodes and make their placement consistent with the placement of the original ones.
739 * For instance, if two original nodes are placed on a component comp1 in a container cont1
740 * and a third one is placed on a component comp2 in the container cont1, the clones of the two
741 * first nodes will be placed on a component comp3 in a container cont2 and the third clone
742 * will be placed on a component comp4 in the container cont2.
744 vector<Node *> DynParaLoop::cloneAndPlaceNodesCoherently(const vector<Node *> & origNodes)
746 DEBTRACE("Begin cloneAndPlaceNodesCoherently")
747 vector<Node *> clones;
748 DeploymentTree treeToDup;
749 vector<list<ElementaryNode *> > origElemNodeList;
750 for (int i=0 ; i<origNodes.size() ; i++)
752 DEBTRACE("Cloning node " << i)
753 if (origNodes[i] == NULL)
755 DEBTRACE("Cloning node " << i << ", NULL" )
756 clones.push_back(NULL);
757 origElemNodeList.push_back(list<ElementaryNode *>());
761 DEBTRACE("Cloning node " << i << "," << origNodes[i]->getName())
762 clones.push_back(origNodes[i]->simpleClone(this, false));
763 list<ElementaryNode *> tasks = origNodes[i]->getRecursiveConstituents();
764 origElemNodeList.push_back(tasks);
765 for (list< ElementaryNode *>::iterator iter=tasks.begin() ; iter!=tasks.end() ; iter++)
766 treeToDup.appendTask(*iter, (*iter)->getDynClonerIfExists(this));
770 // Build the links between clones.
771 // Only the links starting from initNode are possible.
774 std::vector< std::pair<OutPort *, InPort *> > outLinks = _initNode->getSetOfLinksLeavingCurrentScope();
775 std::vector< std::pair<OutPort *, InPort *> >::const_iterator it;
776 for(it=outLinks.begin(); it!=outLinks.end(); it++)
778 OutPort *startPort = it->first;
779 InPort *endPort = it->second;
780 Node* destNode = isInMyDescendance(endPort->getNode());
781 if(destNode == _node)
782 edAddLink(clones[0]->getOutPort(startPort->getName()),
783 clones[1]->getInPort(endPort->getName()));
784 if(destNode == _finalizeNode)
785 edAddLink(clones[0]->getOutPort(startPort->getName()),
786 clones[2]->getInPort(endPort->getName()));
790 DEBTRACE("Placing nodes...")
791 vector<Container *> conts=treeToDup.getAllContainers();
793 //iterate on all containers
794 for(vector<Container *>::iterator iterCt=conts.begin();iterCt!=conts.end();iterCt++)
796 DEBTRACE("Container " << ((*iterCt)?(*iterCt)->getName():"NULL"))
797 vector<ComponentInstance *> comps=treeToDup.getComponentsLinkedToContainer(*iterCt);
798 Container *contCloned=0;
800 contCloned=(*iterCt)->clone();
802 //iterate on all component instances linked to the container
803 for(vector<ComponentInstance *>::iterator iterCp=comps.begin();iterCp!=comps.end();iterCp++)
805 DEBTRACE("Component " << (*iterCp)->getCompoName())
806 vector<Task *> tasks=treeToDup.getTasksLinkedToComponent(*iterCp);
807 ComponentInstance *curCloned=(*iterCp)->clone();
808 DEBTRACE("Assign container " << (*iterCp)->getCompoName())
809 curCloned->setContainer(contCloned);
810 for(vector<Task *>::iterator iterT=tasks.begin();iterT!=tasks.end();iterT++)
812 DEBTRACE("Task " << ((ElementaryNode *)(*iterT))->getName())
814 ElementaryNode * origElemNode = NULL;
815 for (i=0 ; i<origNodes.size() ; i++)
816 if (origNodes[i] != NULL)
818 DEBTRACE("Looking in original node " << i)
819 list<ElementaryNode *>::iterator res=find(origElemNodeList[i].begin(),
820 origElemNodeList[i].end(),
821 (ElementaryNode *)(*iterT));
822 if (res != origElemNodeList[i].end()) {
828 YASSERT(origElemNode != NULL)
829 DEBTRACE("Found task in node " << i)
830 ServiceNode * nodeC = NULL;
831 if (origNodes[i] == origElemNode)
832 nodeC = (ServiceNode *)clones[i];
835 string childName = ((ComposedNode *)origNodes[i])->getChildName(origElemNode);
836 nodeC = (ServiceNode *)clones[i]->getChildByName(childName);
838 DEBTRACE("Assign component: " << (*iterCp)->getCompoName() << "," << nodeC->getName())
839 nodeC->setComponent(curCloned);
841 curCloned->decrRef();
844 // iterate on all tasks linked to the container
845 vector<Task *> tasks=treeToDup.getTasksLinkedToContainer(*iterCt);
846 for(vector<Task *>::iterator iterT=tasks.begin();iterT!=tasks.end();iterT++)
848 DEBTRACE("Task " << ((ElementaryNode *)(*iterT))->getName())
850 ElementaryNode * origElemNode = NULL;
851 for (i=0 ; i<origNodes.size() ; i++)
852 if (origNodes[i] != NULL)
854 DEBTRACE("Looking in original node " << i)
855 list<ElementaryNode *>::iterator res=find(origElemNodeList[i].begin(),
856 origElemNodeList[i].end(),
857 (ElementaryNode *)(*iterT));
858 if (res != origElemNodeList[i].end())
864 YASSERT(origElemNode != NULL)
865 DEBTRACE("Found task in node " << i)
866 InlineFuncNode * nodeC = NULL;
867 if (origNodes[i] == origElemNode)
869 nodeC = (InlineFuncNode *)clones[i];
873 string childName = ((ComposedNode *)origNodes[i])->getChildName(origElemNode);
874 nodeC = (InlineFuncNode *)clones[i]->getChildByName(childName);
876 DEBTRACE("Assign container " << nodeC->getName() << "," << contCloned->getName())
877 nodeC->setContainer(contCloned);
880 // ended with current container
882 contCloned->decrRef();
885 DEBTRACE("End cloneAndPlaceNodesCoherently")
889 void DynParaLoop::accept(Visitor *visitor)
891 visitor->visitDynParaLoop(this);
894 Node * DynParaLoop::getInitNode()
899 Node * DynParaLoop::getExecNode()
904 Node * DynParaLoop::getFinalizeNode()
906 return _finalizeNode;
909 int DynParaLoop::getMaxLevelOfParallelism() const
911 return _nbOfBranches.getIntValue() * _node->getMaxLevelOfParallelism();
914 void DynParaLoop::partitionRegardingDPL(const PartDefinition *pd, std::map<ComposedNode *, YACS::BASES::AutoRefCnt<PartDefinition> >& zeMap)
916 YACS::BASES::AutoRefCnt<PartDefinition> pd2(pd->copy());
919 _node->partitionRegardingDPL(pd,zeMap);
922 void DynParaLoop::shutdown(int level)
927 std::vector<Node *>::iterator iter;
928 for (iter=_execNodes.begin() ; iter!=_execNodes.end() ; iter++)
929 (*iter)->shutdown(level);
930 for (iter=_execInitNodes.begin() ; iter!=_execInitNodes.end() ; iter++)
931 (*iter)->shutdown(level);
932 for (iter=_execFinalizeNodes.begin() ; iter!=_execFinalizeNodes.end() ; iter++)
933 (*iter)->shutdown(level);