1 // Copyright (C) 2006-2019 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 DynParaLoop::DynParaLoop(const std::string& name, TypeCode *typeOfDataSplitted)
43 : ComposedNode(name),_node(0),_initNode(0),_finalizeNode(0),_nbOfEltConsumed(0),
45 _splittedPort(NAME_OF_SPLITTED_SEQ_OUT,this,typeOfDataSplitted),_initializingCounter(0),_unfinishedCounter(0),_failedCounter(0),_weight(), _loopWeight(0)
47 _weight.setDefaultLoop();
50 DynParaLoop::~DynParaLoop()
57 DynParaLoop::DynParaLoop(const DynParaLoop& other, ComposedNode *father, bool editionOnly)
58 : ComposedNode(other,father), _nbOfBranches(other._nbOfBranches,this),
59 _splittedPort(other._splittedPort,this), _node(0), _initNode(0), _finalizeNode(0),
60 _nbOfEltConsumed(0),_initializingCounter(0),_unfinishedCounter(0),_failedCounter(0),_weight(other._weight), _loopWeight(other._loopWeight)
63 _node=other._node->clone(this,editionOnly);
65 _initNode=other._initNode->clone(this,editionOnly);
66 if(other._finalizeNode)
67 _finalizeNode = other._finalizeNode->clone(this,editionOnly);
68 const AnyOutputPort& startOfLinksToReproduce=other._splittedPort;
69 set<InPort *> endsOfLinksToReproduce=startOfLinksToReproduce.edSetInPort();
70 for(set<InPort *>::iterator iter=endsOfLinksToReproduce.begin();iter!=endsOfLinksToReproduce.end();iter++)
71 edAddLink(&_splittedPort,getInPort(other.getPortName(*iter)));
74 Node *DynParaLoop::edSetNode(Node *node)
76 return checkConsistencyAndSetNode(_node, node);
79 //! This method is used to factorize methods edSetNode, edSetInitNode and edSetFinalizeNode
80 Node * DynParaLoop::checkConsistencyAndSetNode(Node* &nodeToReplace, Node* node)
82 if (node == NULL || nodeToReplace == node)
85 throw Exception(string("Can't set node: node ") + node->getName() + " is not orphan.");
86 if (_node && _node != nodeToReplace && _node->getName() == node->getName())
87 throw Exception(string("Can't set node: node ") + node->getName() +
88 " has the same name than exec node already in " + _name + ".");
89 if (_initNode && _initNode != nodeToReplace && _initNode->getName() == node->getName())
90 throw Exception(string("Can't set node: node ") + node->getName() +
91 " has the same name than init node already in " + _name + ".");
92 if (_finalizeNode && _finalizeNode != nodeToReplace && _finalizeNode->getName() == node->getName())
93 throw Exception(string("Can't set node: node ") + node->getName() +
94 " has the same name than finalize node already in " + _name + ".");
95 checkNoCrossHierachyWith(node);
96 ComposedNode::edRemoveChild(nodeToReplace);
97 Node * ret = nodeToReplace;
99 nodeToReplace->_father = this;
100 // set _modified flag so that edUpdateState can refresh state
105 void DynParaLoop::init(bool start)
107 ComposedNode::init(start);
110 string what("DynParaLoop::init : no node specified for ForEachLoop with name "); what +=_name;
111 throw Exception(what);
114 if (_initNode) _initNode->init(start);
115 if (_finalizeNode) _finalizeNode->init(start);
116 _nbOfBranches.exInit(start);
117 _splittedPort.exInit();
122 Node *DynParaLoop::edSetInitNode(Node *node)
124 return checkConsistencyAndSetNode(_initNode, node);
127 Node * DynParaLoop::edSetFinalizeNode(Node * node)
129 return checkConsistencyAndSetNode(_finalizeNode, node);
132 //! Connect an OutPort to an InPort and add control link if necessary
134 * Connect the ports with a data link (edAddLink)
135 * In a Loop don't add control flow link : use this only to add data back links
137 * \param start : the OutPort to connect
138 * \param end : the InPort to connect
139 * \return true if a new link has been created, false otherwise.
141 bool DynParaLoop::edAddDFLink(OutPort *start, InPort *end) throw(YACS::Exception)
143 return edAddLink(start,end);
146 int DynParaLoop::getNumberOfInputPorts() const
148 return ComposedNode::getNumberOfInputPorts()+1;
151 int DynParaLoop::getNumberOfOutputPorts() const
153 return ComposedNode::getNumberOfOutputPorts()+1;
157 * DynParaLoop creates at runtime (exupdateState) clone of nodes. One clone per branch.
158 * This method returns the id of the branch given the node \a node.
159 * If \a node is not a dynamically created node in \a this -1 is returned.
161 int DynParaLoop::getBranchIDOfNode(Node *node) const
165 for(std::vector<Node *>::const_iterator it=_execNodes.begin();it!=_execNodes.end();it++)
167 return std::distance(_execNodes.begin(),it);
171 for(std::vector<Node *>::const_iterator it=_execFinalizeNodes.begin();it!=_execFinalizeNodes.end();it++)
173 return std::distance(_execFinalizeNodes.begin(),it);
177 for(std::vector<Node *>::const_iterator it=_execInitNodes.begin();it!=_execInitNodes.end();it++)
179 return std::distance(_execInitNodes.begin(),it);
184 std::list<OutputPort *> DynParaLoop::getSetOfOutputPort() const
186 list<OutputPort *> ret=ComposedNode::getSetOfOutputPort();
187 ret.push_back((OutputPort *)&_splittedPort);
191 std::list<OutputPort *> DynParaLoop::getLocalOutputPorts() const
193 list<OutputPort *> ret=ComposedNode::getLocalOutputPorts();
194 ret.push_back((OutputPort *)&_splittedPort);
198 OutPort *DynParaLoop::getOutPort(const std::string& name) const throw(YACS::Exception)
200 if (name == NAME_OF_SPLITTED_SEQ_OUT || name == OLD_NAME_OF_SPLITTED_SEQ_OUT)
201 return (OutPort *)&_splittedPort;
202 return ComposedNode::getOutPort(name);
206 OutputPort *DynParaLoop::getOutputPort(const std::string& name) const throw(YACS::Exception)
208 if (name == NAME_OF_SPLITTED_SEQ_OUT || name == OLD_NAME_OF_SPLITTED_SEQ_OUT)
209 return (OutputPort *)&_splittedPort;
210 return ComposedNode::getOutputPort(name);
213 bool DynParaLoop::isPlacementPredictableB4Run() const
218 Node *DynParaLoop::edRemoveNode()
220 return removeNode(_node);
223 //! This method is used to factorize methods edRemoveNode, edRemoveInitNode and edRemoveFinalizeNode
224 Node * DynParaLoop::removeNode(Node* &nodeToRemove)
228 ComposedNode::edRemoveChild(nodeToRemove);
229 Node * ret = nodeToRemove;
235 Node *DynParaLoop::edRemoveInitNode()
237 return removeNode(_initNode);
240 Node * DynParaLoop::edRemoveFinalizeNode()
242 return removeNode(_finalizeNode);
245 void DynParaLoop::edRemoveChild(Node *node) throw(YACS::Exception)
247 ComposedNode::edRemoveChild(node);
252 if(node==_finalizeNode)
257 bool DynParaLoop::edAddChild(Node *DISOWNnode) throw(YACS::Exception)
259 return edSetNode(DISOWNnode);
262 std::list<Node *> DynParaLoop::edGetDirectDescendants() const
266 ret.push_back(_node);
268 ret.push_back(_initNode);
270 ret.push_back(_finalizeNode);
274 std::list<InputPort *> DynParaLoop::getSetOfInputPort() const
276 list<InputPort *> ret=ComposedNode::getSetOfInputPort();
277 ret.push_back(_nbOfBranches.getPort());
281 InputPort *DynParaLoop::getInputPort(const std::string& name) const throw(YACS::Exception)
283 if(NbBranches::IsBranchPortName(name))
284 return _nbOfBranches.getPort();
285 return ComposedNode::getInputPort(name);
288 std::list<InputPort *> DynParaLoop::getLocalInputPorts() const
290 list<InputPort *> ret=ComposedNode::getLocalInputPorts();
291 ret.push_back(_nbOfBranches.getPort());
295 unsigned DynParaLoop::getNumberOfBranchesCreatedDyn() const throw(YACS::Exception)
297 if(_execNodes.empty())
298 throw Exception("ForEachLoop::getNumberOfBranches : No branches created dynamically ! - ForEachLoop needs to run or to be runned to call getNumberOfBranches");
300 return _execNodes.size();
303 Node *DynParaLoop::getChildByShortName(const std::string& name) const throw(YACS::Exception)
305 if (_node && name == _node->getName())
307 if (_initNode && name == _initNode->getName())
309 if (_finalizeNode && name == _finalizeNode->getName())
310 return _finalizeNode;
311 std::string what("node "); what+= name ; what+=" is not a child of DynParaLoop node "; what += getName();
312 throw Exception(what);
315 Node *DynParaLoop::getChildByNameExec(const std::string& name, unsigned id) const throw(YACS::Exception)
317 if(id>=getNumberOfBranchesCreatedDyn())
318 throw Exception("ForEachLoop::getChildByNameExec : invalid id - too large compared with dynamically created branches.");
319 if (_node && name == _node->getName())
320 return _execNodes[id];
321 if (_initNode && name == _initNode->getName())
322 return _execInitNodes[id];
323 if (_finalizeNode && name == _finalizeNode->getName())
324 return _execFinalizeNodes[id];
325 std::string what("node "); what+= name ; what+=" is not a child of DynParaLoop node "; what += getName();
326 throw Exception(what);
329 void DynParaLoop::cleanDynGraph()
331 vector<Node *>::iterator iter;
332 for(iter=_execNodes.begin() ; iter!=_execNodes.end() ; iter++)
335 for(iter=_execInitNodes.begin() ; iter!=_execInitNodes.end() ; iter++)
337 _execInitNodes.clear();
338 for(iter=_execFinalizeNodes.begin() ; iter!=_execFinalizeNodes.end() ; iter++)
340 _execFinalizeNodes.clear();
344 * This method applies on newly cloned on exec nodes (_execNodes/_execInitNodes)
345 * the setting of input ports coming from outside of 'this'
347 void DynParaLoop::prepareInputsFromOutOfScope(int branchNb)
349 set< InPort * > portsToSetVals=getAllInPortsComingFromOutsideOfCurrentScope();
351 // This tweak is to fix problems with nested dynamic loops where links are not cloned properly
352 list<InPort *> temp = getSetOfInPort();
353 for(list<InPort *>::iterator iter2=temp.begin();iter2!=temp.end();iter2++)
355 if ((*iter2)->edSetOutPort().size() == 1 && *(*iter2)->edSetOutPort().begin() == NULL)
357 portsToSetVals.insert(*iter2);
361 // local input ports are not candidates for dynamically duplicated inport.
362 list<InputPort *> localPorts = getLocalInputPorts();
363 for(list<InputPort *>::iterator iter = localPorts.begin() ; iter != localPorts.end() ; iter++)
364 portsToSetVals.erase(*iter);
365 for(set< InPort * >::iterator iter=portsToSetVals.begin();iter!=portsToSetVals.end();iter++)
367 InputPort *curPortCasted=(InputPort *) *iter;//Cast granted by ForEachLoop::buildDelegateOf(InPort)
368 void *val=curPortCasted->get();
369 InputPort *portToSet=getDynInputPortByAbsName(branchNb,getInPortName(*iter),true);
370 if(portToSet)//portToSet==0 in case of portToSet==_splitterNode._dataPortToDispatch of ForEach
372 portToSet->put((const void *)val);
373 portToSet->edNotifyReferencedBy(nullptr,false);//This is to indicate that somewhere somebody deals with this inputport
374 //even if no direct physical link exists. This exclusively for _execNodes[branchNb]::init on the next turn of loop.
375 //false is put as 2nd parameter to tell to portToSet, do not touch to the data in case of squeezeMemory.
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 void DynParaLoop::setWeight(double loopWeight)
429 throw Exception("DynParaLoop::setWeight : invalid input value !");
430 _loopWeight=loopWeight;
433 ComplexWeight* DynParaLoop::getWeight()
436 _weight.setLoopWeight(_loopWeight, _node->getMaxLevelOfParallelism()); // not done in setWeight because _node can be null at that time
440 bool DynParaLoop::isMultiplicitySpecified(unsigned& value) const
442 return _nbOfBranches.isMultiplicitySpecified(value);
445 void DynParaLoop::forceMultiplicity(unsigned value)
447 _nbOfBranches.forceMultiplicity(value);
450 void DynParaLoop::buildDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView)
452 string typeOfPortInstance=port->getNameOfTypeOfCurrentInstance();
453 if(typeOfPortInstance!=InputPort::NAME)
454 throw Exception("DynParaLoop::buildDelegateOf : A link with datastream end inside DynParaLoop is not possible");
457 void DynParaLoop::buildDelegateOf(std::pair<OutPort *, OutPort *>& port, InPort *finalTarget, const std::list<ComposedNode *>& pointsOfView)
459 std::string linkName("(");
460 linkName += port.first->getName()+" to "+finalTarget->getName()+")";
462 if(isInMyDescendance(port.first->getNode())==_initNode)
463 throw Exception(std::string("Illegal link within a parallel loop: \
464 a link starting from the init node can't leave the scope of the loop.")
468 if(isInMyDescendance(port.first->getNode())==_finalizeNode)
469 throw Exception(std::string("Illegal link within a parallel loop: \
470 an output port of the finalize node can't be linked.")
473 if(port.first==&_splittedPort)
474 throw Exception(std::string("Illegal link within a parallel loop: \
475 the 'evalSamples' port must be linked within the scope of the loop.")
479 void DynParaLoop::checkCFLinks(const std::list<OutPort *>& starts, InputPort *end, unsigned char& alreadyFed, bool direction, LinkInfo& info) const
481 const char what[]="DynParaLoop::checkCFLinks : internal error.";
482 //First dealing checkCFLinks forwarding...
483 if(isInMyDescendance(end->getNode())==0)//no chance that _splittedPort is in starts due to buildDelegate.
484 solveObviousOrDelegateCFLinks(starts,end,alreadyFed,direction,info);
486 {//no forwarding here.
488 throw Exception(what);
489 //ASSERT(direction) : see DynParaLoop::checkControlDependancy only 'fw' filled.
490 if(alreadyFed==FREE_ST)
492 else if(alreadyFed==FED_ST)
493 {//Shame ! splittedPort fills a port already fed...
494 info.pushInfoLink(*(starts.begin()),end,I_USELESS);
500 * \param start : start port
501 * \param end : end port
502 * \param cross indicates if start -> end link is a DS link behind.
503 * \param fw out parameter.
504 * \param fwCross out parameter storing links where a cross has been detected.
505 * \param bw out parameter where backward links are stored.
506 * \param info : collected information
508 void DynParaLoop::checkControlDependancy(OutPort *start, InPort *end, bool cross,
509 std::map < ComposedNode *, std::list < OutPort * >, SortHierarc >& fw,
510 std::vector<OutPort *>& fwCross,
511 std::map< ComposedNode *, std::list < OutPort *>, SortHierarc >& bw,
512 LinkInfo& info) const
514 fw[(ComposedNode *)this].push_back(start);
517 void DynParaLoop::checkLinkPossibility(OutPort *start, const std::list<ComposedNode *>& pointsOfViewStart,
518 InPort *end, const std::list<ComposedNode *>& pointsOfViewEnd) throw(YACS::Exception)
520 ComposedNode::checkLinkPossibility(start, pointsOfViewStart, end, pointsOfViewEnd);
521 Node * startNode = isInMyDescendance(start->getNode());
522 Node * endNode = isInMyDescendance(end->getNode());
523 std::string linkName("(");
524 linkName += start->getName()+" to "+end->getName()+")";
526 if(start == &_splittedPort && endNode != _node)
527 throw Exception(std::string("Illegal link within a parallel loop: \
528 the 'evalSamples' port can only be connected to the working node of the loop.")
531 if(_finalizeNode && _finalizeNode == startNode)
532 throw Exception(std::string("Illegal link within a parallel loop: \
533 the finalize node can't be the origin of a link.")
536 if(_initNode && _node == startNode && _initNode == endNode)
537 throw Exception(std::string("Illegal link within a parallel loop: \
538 can't make a link from the working node to the init node.")
541 if(_finalizeNode && _node == startNode && _finalizeNode == endNode)
542 throw Exception(std::string("Illegal link within a parallel loop: \
543 can't make a link from the working node to the finalize node.")
548 * \note : For a given name 'name' of port in absolute form from this, returns the corresponding InputPort
549 * instance of the port for the branch # 'branchNb'.
550 * The port can be part of _node or _initNode if it exists (if 'initNodeAdmitted' is true).
551 * \b WARNING : no check performed on 'branchNb' value to see if it is compatible with size of '_execNodes'.
552 * This method is called to dispatch value on each InputPort linked to this->._splitterNode._splittedPort
554 InputPort *DynParaLoop::getDynInputPortByAbsName(int branchNb, const std::string& name, bool initNodeAdmitted)
556 string portName, nodeName;
557 splitNamesBySep(name,Node::SEP_CHAR_IN_PORT,nodeName,portName,true);
558 Node *staticChild = getChildByName(nodeName);
559 Node *desc=isInMyDescendance(staticChild);
562 splitNamesBySep(name,Node::SEP_CHAR_IN_PORT,nodeName,portName,false);
563 return _execNodes[branchNb]->getInputPort(portName);
565 else if(desc==_initNode)
568 splitNamesBySep(name,Node::SEP_CHAR_IN_PORT,nodeName,portName,false);
569 return _execInitNodes[branchNb]->getInputPort(portName);
574 void DynParaLoop::checkBasicConsistency() const throw(YACS::Exception)
576 DEBTRACE("DynParaLoop::checkBasicConsistency");
577 ComposedNode::checkBasicConsistency();
579 throw Exception("For a dynamic loop, internal node is mandatory");
582 std::string DynParaLoop::getErrorReport()
584 DEBTRACE("DynParaLoop::getErrorReport: " << getName() << " " << _state);
585 YACS::StatesForNode effectiveState=getEffectiveState();
587 if(effectiveState != YACS::INVALID && effectiveState != YACS::ERROR && effectiveState != YACS::FAILED)
590 std::string report="<error node= " + getName();
591 switch(effectiveState)
594 report=report+" state= INVALID";
597 report=report+" state= ERROR";
600 report=report+" state= FAILED";
605 report=report + ">\n" ;
606 if(_errorDetails != "")
607 report=report+_errorDetails+"\n";
609 if(_execNodes.empty())
612 list<Node *> constituents=edGetDirectDescendants();
613 for(list<Node *>::iterator iter=constituents.begin(); iter!=constituents.end(); iter++)
615 std::string rep=(*iter)->getErrorReport();
618 report=report+rep+"\n";
625 for(vector<Node *>::iterator iter=_execInitNodes.begin();iter!=_execInitNodes.end();iter++)
627 std::string rep=(*iter)->getErrorReport();
630 report=report+rep+"\n";
633 for(vector<Node *>::iterator iter=_execNodes.begin();iter!=_execNodes.end();iter++)
637 std::string rep=(*iter)->getErrorReport();
640 report=report+rep+"\n";
644 for(vector<Node *>::iterator iter=_execFinalizeNodes.begin();iter!=_execFinalizeNodes.end();iter++)
646 std::string rep=(*iter)->getErrorReport();
649 report=report+rep+"\n";
654 report=report+"</error>";
658 void DynParaLoop::forwardExecStateToOriginalBody(Node *execNode)
661 Node * origNode = NULL;
662 switch (getIdentityOfNotifyerNode(execNode,id))
666 origNode = _initNode;
676 origNode = _finalizeNode;
683 YASSERT(origNode != NULL)
684 origNode->setState(execNode->getState());
685 origNode->setErrorDetails(execNode->getErrorDetails());
687 ComposedNode* compNode = dynamic_cast<ComposedNode*>(origNode);
688 ComposedNode* compNodeExe = dynamic_cast<ComposedNode*>(execNode);
689 if (compNode && compNodeExe)
691 list<Node *> aChldn = compNodeExe->getAllRecursiveConstituents();
692 list<Node *>::iterator iter=aChldn.begin();
693 for(;iter!=aChldn.end();iter++)
695 Node* node=compNode->getChildByName(compNodeExe->getChildName(*iter));
696 node->setState((*iter)->getState());
697 node->setErrorDetails((*iter)->getErrorDetails());
702 //! Method used to notify the node that a child node has failed
704 * Update the current state and return the change state
706 * \param node : the child node that has failed
707 * \return the state change
709 YACS::Event DynParaLoop::updateStateOnFailedEventFrom(Node *node, const Executor *execInst)
711 DEBTRACE("DynParaLoop::updateStateOnFailedEventFrom " << node->getName());
712 setState(YACS::FAILED);
713 forwardExecStateToOriginalBody(node);
715 switch (getIdentityOfNotifyerNode(node,id))
719 _node->setState(YACS::FAILED);
720 if (_finalizeNode != NULL) _finalizeNode->setState(YACS::FAILED);
725 if (_finalizeNode != NULL) _finalizeNode->setState(YACS::FAILED);
732 //! Clone nodes and make their placement consistent with the placement of the original ones.
734 * For instance, if two original nodes are placed on a component comp1 in a container cont1
735 * and a third one is placed on a component comp2 in the container cont1, the clones of the two
736 * first nodes will be placed on a component comp3 in a container cont2 and the third clone
737 * will be placed on a component comp4 in the container cont2.
739 vector<Node *> DynParaLoop::cloneAndPlaceNodesCoherently(const vector<Node *> & origNodes)
741 DEBTRACE("Begin cloneAndPlaceNodesCoherently")
742 vector<Node *> clones;
743 DeploymentTree treeToDup;
744 vector<list<ElementaryNode *> > origElemNodeList;
745 for (int i=0 ; i<origNodes.size() ; i++)
747 DEBTRACE("Cloning node " << i)
748 if (origNodes[i] == NULL)
750 DEBTRACE("Cloning node " << i << ", NULL" )
751 clones.push_back(NULL);
752 origElemNodeList.push_back(list<ElementaryNode *>());
756 DEBTRACE("Cloning node " << i << "," << origNodes[i]->getName())
757 clones.push_back(origNodes[i]->simpleClone(this, false));
758 list<ElementaryNode *> tasks = origNodes[i]->getRecursiveConstituents();
759 origElemNodeList.push_back(tasks);
760 for (list< ElementaryNode *>::iterator iter=tasks.begin() ; iter!=tasks.end() ; iter++)
761 treeToDup.appendTask(*iter, (*iter)->getDynClonerIfExists(this));
765 // Build the links between clones.
766 // Only the links starting from initNode are possible.
769 std::vector< std::pair<OutPort *, InPort *> > outLinks = _initNode->getSetOfLinksLeavingCurrentScope();
770 std::vector< std::pair<OutPort *, InPort *> >::const_iterator it;
771 for(it=outLinks.begin(); it!=outLinks.end(); it++)
773 OutPort *startPort = it->first;
774 InPort *endPort = it->second;
775 Node* destNode = isInMyDescendance(endPort->getNode());
776 if(destNode == _node)
777 edAddLink(clones[0]->getOutPort(startPort->getName()),
778 clones[1]->getInPort(endPort->getName()));
779 if(destNode == _finalizeNode)
780 edAddLink(clones[0]->getOutPort(startPort->getName()),
781 clones[2]->getInPort(endPort->getName()));
785 DEBTRACE("Placing nodes...")
786 vector<Container *> conts=treeToDup.getAllContainers();
788 //iterate on all containers
789 for(vector<Container *>::iterator iterCt=conts.begin();iterCt!=conts.end();iterCt++)
791 DEBTRACE("Container " << ((*iterCt)?(*iterCt)->getName():"NULL"))
792 vector<ComponentInstance *> comps=treeToDup.getComponentsLinkedToContainer(*iterCt);
793 Container *contCloned=0;
795 contCloned=(*iterCt)->clone();
797 //iterate on all component instances linked to the container
798 for(vector<ComponentInstance *>::iterator iterCp=comps.begin();iterCp!=comps.end();iterCp++)
800 DEBTRACE("Component " << (*iterCp)->getCompoName())
801 vector<Task *> tasks=treeToDup.getTasksLinkedToComponent(*iterCp);
802 ComponentInstance *curCloned=(*iterCp)->clone();
803 DEBTRACE("Assign container " << (*iterCp)->getCompoName())
804 curCloned->setContainer(contCloned);
805 for(vector<Task *>::iterator iterT=tasks.begin();iterT!=tasks.end();iterT++)
807 DEBTRACE("Task " << ((ElementaryNode *)(*iterT))->getName())
809 ElementaryNode * origElemNode = NULL;
810 for (i=0 ; i<origNodes.size() ; i++)
811 if (origNodes[i] != NULL)
813 DEBTRACE("Looking in original node " << i)
814 list<ElementaryNode *>::iterator res=find(origElemNodeList[i].begin(),
815 origElemNodeList[i].end(),
816 (ElementaryNode *)(*iterT));
817 if (res != origElemNodeList[i].end()) {
823 YASSERT(origElemNode != NULL)
824 DEBTRACE("Found task in node " << i)
825 ServiceNode * nodeC = NULL;
826 if (origNodes[i] == origElemNode)
827 nodeC = (ServiceNode *)clones[i];
830 string childName = ((ComposedNode *)origNodes[i])->getChildName(origElemNode);
831 nodeC = (ServiceNode *)clones[i]->getChildByName(childName);
833 DEBTRACE("Assign component: " << (*iterCp)->getCompoName() << "," << nodeC->getName())
834 nodeC->setComponent(curCloned);
836 curCloned->decrRef();
839 // iterate on all tasks linked to the container
840 vector<Task *> tasks=treeToDup.getTasksLinkedToContainer(*iterCt);
841 for(vector<Task *>::iterator iterT=tasks.begin();iterT!=tasks.end();iterT++)
843 DEBTRACE("Task " << ((ElementaryNode *)(*iterT))->getName())
845 ElementaryNode * origElemNode = NULL;
846 for (i=0 ; i<origNodes.size() ; i++)
847 if (origNodes[i] != NULL)
849 DEBTRACE("Looking in original node " << i)
850 list<ElementaryNode *>::iterator res=find(origElemNodeList[i].begin(),
851 origElemNodeList[i].end(),
852 (ElementaryNode *)(*iterT));
853 if (res != origElemNodeList[i].end())
859 YASSERT(origElemNode != NULL)
860 DEBTRACE("Found task in node " << i)
861 InlineFuncNode * nodeC = NULL;
862 if (origNodes[i] == origElemNode)
864 nodeC = (InlineFuncNode *)clones[i];
868 string childName = ((ComposedNode *)origNodes[i])->getChildName(origElemNode);
869 nodeC = (InlineFuncNode *)clones[i]->getChildByName(childName);
871 DEBTRACE("Assign container " << nodeC->getName() << "," << contCloned->getName())
872 nodeC->setContainer(contCloned);
875 // ended with current container
877 contCloned->decrRef();
880 DEBTRACE("End cloneAndPlaceNodesCoherently")
884 void DynParaLoop::accept(Visitor *visitor)
886 visitor->visitDynParaLoop(this);
889 Node * DynParaLoop::getInitNode()
894 Node * DynParaLoop::getExecNode()
899 Node * DynParaLoop::getFinalizeNode()
901 return _finalizeNode;
904 int DynParaLoop::getMaxLevelOfParallelism() const
906 return _nbOfBranches.getIntValue() * _node->getMaxLevelOfParallelism();
909 void DynParaLoop::partitionRegardingDPL(const PartDefinition *pd, std::map<ComposedNode *, YACS::BASES::AutoRefCnt<PartDefinition> >& zeMap)
911 YACS::BASES::AutoRefCnt<PartDefinition> pd2(pd->copy());
914 _node->partitionRegardingDPL(pd,zeMap);
917 void DynParaLoop::shutdown(int level)
922 std::vector<Node *>::iterator iter;
923 for (iter=_execNodes.begin() ; iter!=_execNodes.end() ; iter++)
924 (*iter)->shutdown(level);
925 for (iter=_execInitNodes.begin() ; iter!=_execInitNodes.end() ; iter++)
926 (*iter)->shutdown(level);
927 for (iter=_execFinalizeNodes.begin() ; iter!=_execFinalizeNodes.end() ; iter++)
928 (*iter)->shutdown(level);