1 // Copyright (C) 2012-2015 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
19 // Author : Anthony Geay (EDF R&D)
21 #include "YACSEvalYFXPattern.hxx"
22 #include "YACSEvalResource.hxx"
23 #include "YACSEvalSeqAny.hxx"
24 #include "YACSEvalObserver.hxx"
25 #include "YACSEvalAutoPtr.hxx"
27 #include "ElementaryNode.hxx"
28 #include "RuntimeSALOME.hxx"
29 #include "Dispatcher.hxx"
30 #include "InputPort.hxx"
31 #include "LinkInfo.hxx"
32 #include "TypeCode.hxx"
35 #include "PythonPorts.hxx"
36 #include "ForEachLoop.hxx"
37 #include "PythonNode.hxx"
38 #include "InlineNode.hxx"
39 #include "ServiceNode.hxx"
41 #include "ResourcesManager.hxx"
48 const char YACSEvalYFXPattern::DFT_PROC_NAME[]="YFX";
50 const char YACSEvalYFXRunOnlyPattern::GATHER_NODE_NAME[]="__gather__";
52 std::vector< YACSEvalInputPort *> YACSEvalYFXPattern::getFreeInputPorts() const
54 std::size_t sz(_inputs.size());
55 std::vector< YACSEvalInputPort *> ret;
56 std::vector< YACSEvalInputPort >::const_iterator it(_inputs.begin());
57 for(std::size_t i=0;i<sz;i++,it++)
58 ret.push_back(const_cast<YACSEvalInputPort *>(&(*it)));
62 std::vector< YACSEvalOutputPort *> YACSEvalYFXPattern::getFreeOutputPorts() const
64 std::size_t sz(_outputs.size());
65 std::vector< YACSEvalOutputPort *> ret;
66 std::vector< YACSEvalOutputPort >::const_iterator it(_outputs.begin());
67 for(std::size_t i=0;i<sz;i++,it++)
68 ret.push_back(const_cast<YACSEvalOutputPort *>(&(*it)));
72 YACSEvalYFXPattern *YACSEvalYFXPattern::FindPatternFrom(YACSEvalYFX *boss, YACS::ENGINE::Proc *scheme, bool ownScheme)
75 throw YACS::Exception("YACSEvalYFXPattern::FindPatternFrom : input scheme must be not null !");
77 YACS::ENGINE::ComposedNode *zeRunNode(0);
78 bool isMatchingRunOnlyPattern(YACSEvalYFXRunOnlyPattern::IsMatching(scheme,zeRunNode));
79 if(isMatchingRunOnlyPattern)
80 return new YACSEvalYFXRunOnlyPattern(boss,scheme,ownScheme,zeRunNode);
82 throw YACS::Exception("YACSEvalYFXPattern::FindPatternFrom : no pattern found for the input scheme !");
85 bool YACSEvalYFXPattern::isAlreadyComputedResources() const
90 void YACSEvalYFXPattern::checkNonAlreadyComputedResources() const
92 if(isAlreadyComputedResources())
93 throw YACS::Exception("checkNonAlreadyComputedResources : instance of computed resources already computed !");
96 void YACSEvalYFXPattern::checkAlreadyComputedResources() const
98 if(!isAlreadyComputedResources())
99 throw YACS::Exception("checkAlreadyComputedResources : instance of computed resources not already computed !");
102 void YACSEvalYFXPattern::checkLocked() const
105 throw YACS::Exception("YACSEvalYFXPattern::checkLocked : Pattern is not locked !");
108 void YACSEvalYFXPattern::checkNonLocked() const
111 throw YACS::Exception("YACSEvalYFXPattern::checkNonLocked : Pattern is locked !");
114 void YACSEvalYFXPattern::CheckNodeIsOK(YACS::ENGINE::ComposedNode *node)
116 /*YACS::ENGINE::LinkInfo info(YACS::ENGINE::LinkInfo::WARN_ONLY_DONT_STOP);
119 node->checkConsistency(info);
121 catch(YACS::Exception& e)
124 if(info.getNumberOfErrLinks(YACS::ENGINE::E_ALL)!=0)
125 throw YACS::Exception("YACSEvalYFXPattern::CheckNodeIsOK : found node is not OK !");
126 std::list<YACS::ENGINE::ElementaryNode *> allNodes(node->getRecursiveConstituents());
127 for(std::list<YACS::ENGINE::ElementaryNode *>::const_iterator it=allNodes.begin();it!=allNodes.end();it++)
129 YACS::ENGINE::ServiceNode *node0(dynamic_cast<YACS::ENGINE::ServiceNode *>(*it));
130 YACS::ENGINE::InlineNode *node1(dynamic_cast<YACS::ENGINE::InlineNode *>(*it));
133 YACS::ENGINE::Container *cont(node0->getContainer());
134 YACS::ENGINE::ComponentInstance *comp(node0->getComponent());
137 std::ostringstream oss; oss << "YACSEvalYFXPattern::CheckNodeIsOK : ServiceNode called \"" << node0->getName() << "\" is not correctly defined !";
138 throw YACS::Exception(oss.str());
143 YACS::ENGINE::Container *cont(node1->getContainer());
144 if(!cont && node1->getExecutionMode()==YACS::ENGINE::InlineNode::REMOTE_STR)
146 std::ostringstream oss; oss << "YACSEvalYFXPattern::CheckNodeIsOK : InlineNode called \"" << node1->getName() << "\" is not correctly defined !";
147 throw YACS::Exception(oss.str());
153 void YACSEvalYFXPattern::registerObserver(YACSEvalObserver *observer)
155 if(_observer==observer)
158 _observer->decrRef();
161 _observer->incrRef();
164 YACSEvalYFXPattern::YACSEvalYFXPattern(YACSEvalYFX *boss, YACS::ENGINE::Proc *scheme, bool ownScheme):_boss(boss),_scheme(scheme),_ownScheme(ownScheme),_rm(new ResourcesManager_cpp),_res(0),_observer(0)
168 YACS::ENGINE::TypeCode *YACSEvalYFXPattern::createSeqTypeCodeFrom(YACS::ENGINE::Proc *scheme, const std::string& zeType)
170 std::ostringstream oss; oss << "list[" << zeType << "]";
171 YACS::ENGINE::TypeCode *tc(scheme->getTypeCode(zeType));
172 return scheme->createSequenceTc(oss.str(),oss.str(),tc);
175 void YACSEvalYFXPattern::setResources(YACSEvalListOfResources *res)
177 checkNonAlreadyComputedResources();
183 void YACSEvalYFXPattern::resetResources()
189 YACSEvalSeqAny *YACSEvalYFXPattern::BuildValueInPort(YACS::ENGINE::InputPyPort *port)
192 throw YACS::Exception("YACSEvalYFXPattern::GetValueInPort : null input port !");
193 PyObject *obj(port->getPyObj());
194 YACS::ENGINE::TypeCode *tc(port->edGetType());
195 YACS::ENGINE::TypeCodeSeq *tcc(dynamic_cast<YACS::ENGINE::TypeCodeSeq *>(tc));
198 std::ostringstream oss; oss << "YACSEvalYFXPattern::GetValueInPort : internal error for tc of input \"" << port->getName() << "\"";
199 throw YACS::Exception(oss.str());
201 const YACS::ENGINE::TypeCode *tcct(tcc->contentType());
202 if(!PyList_Check(obj))
203 throw YACS::Exception("YACSEvalYFXPattern::GetValueInPort : internal error 2 !");
204 std::size_t sz(PyList_Size(obj));
205 if(tcct->kind()==YACS::ENGINE::Double)
207 std::vector<double> eltCpp(sz);
208 for(std::size_t i=0;i<sz;i++)
210 PyObject *elt(PyList_GetItem(obj,i));
211 eltCpp[i]=PyFloat_AsDouble(elt);
213 YACS::AutoCppPtr<YACSEvalSeqAnyDouble> elt(new YACSEvalSeqAnyDouble(eltCpp));
214 return elt.dettach();
216 else if(tcct->kind()==YACS::ENGINE::Int)
218 std::vector<int> eltCpp(sz);
219 for(std::size_t i=0;i<sz;i++)
221 PyObject *elt(PyList_GetItem(obj,i));
222 eltCpp[i]=PyInt_AsLong(elt);
224 YACS::AutoCppPtr<YACSEvalSeqAnyInt> elt(new YACSEvalSeqAnyInt(eltCpp));
225 return elt.dettach();
228 throw YACS::Exception("YACSEvalYFXPattern::GetValueInPort : not implemented yet for other than Double and Int !");
231 void YACSEvalYFXPattern::cleanScheme()
238 YACSEvalYFXPattern::~YACSEvalYFXPattern()
241 _observer->decrRef();
246 /////////////////////
248 class YACSEvalYFXRunOnlyPatternInternalObserver : public YACS::ENGINE::Observer
251 YACSEvalYFXRunOnlyPatternInternalObserver(YACSEvalYFXRunOnlyPattern *boss):_boss(boss) { if(!_boss) throw YACS::Exception("YACSEvalYFXRunOnlyPatternInternalObserver constructor : null boss not supported :)"); }
252 void notifyObserver(YACS::ENGINE::Node *object, const std::string& event);
254 YACSEvalYFXRunOnlyPattern *_boss;
257 void YACSEvalYFXRunOnlyPatternInternalObserver::notifyObserver(YACS::ENGINE::Node *object, const std::string& event)
259 YACS::ENGINE::ForEachLoop *object2(dynamic_cast<YACS::ENGINE::ForEachLoop *>(object));
262 YACSEvalObserver *obs(_boss->getObserver());
265 if(event=="progress")
266 obs->notifyNewNumberOfPassedItems(_boss->getBoss(),object2->getCurrentIndex());
269 /////////////////////
271 YACSEvalYFXRunOnlyPattern::YACSEvalYFXRunOnlyPattern(YACSEvalYFX *boss, YACS::ENGINE::Proc *scheme, bool ownScheme, YACS::ENGINE::ComposedNode *runNode):YACSEvalYFXPattern(boss,scheme,ownScheme),_runNode(runNode),_generatedGraph(0),_FEInGeneratedGraph(0),_obs(new YACSEvalYFXRunOnlyPatternInternalObserver(this))
274 throw YACS::Exception("YACSEvalYFXRunOnlyPattern : internal run node must be not null !");
279 YACSEvalYFXRunOnlyPattern::~YACSEvalYFXRunOnlyPattern()
284 void YACSEvalYFXRunOnlyPattern::setOutPortsOfInterestForEvaluation(const std::vector<YACSEvalOutputPort *>& outputsOfInterest)
287 _outputsOfInterest=outputsOfInterest;
290 void YACSEvalYFXRunOnlyPattern::resetOutputsOfInterest()
293 _outputsOfInterest.clear();
296 void YACSEvalYFXRunOnlyPattern::generateGraph()
298 static const char LISTPYOBJ_STR[]="list[pyobj]";
299 if(_outputsOfInterest.empty())
301 YACS::ENGINE::RuntimeSALOME::setRuntime();
302 YACS::ENGINE::RuntimeSALOME *r(YACS::ENGINE::getSALOMERuntime());
303 _generatedGraph=r->createProc(DFT_PROC_NAME);
304 YACS::ENGINE::TypeCode *pyobjTC(_generatedGraph->createInterfaceTc("python:obj:1.0","pyobj",std::list<YACS::ENGINE::TypeCodeObjref *>()));
305 std::ostringstream oss; oss << "Loop_" << _runNode->getName();
306 _generatedGraph->createType(YACSEvalAnyDouble::TYPE_REPR,"double");
307 _generatedGraph->createType(YACSEvalAnyInt::TYPE_REPR,"int");
309 YACS::ENGINE::InlineNode *n0(r->createScriptNode(YACS::ENGINE::PythonNode::KIND,"__initializer__"));
310 _generatedGraph->edAddChild(n0);
311 YACS::ENGINE::TypeCode *listPyobjTC(_generatedGraph->createSequenceTc(LISTPYOBJ_STR,LISTPYOBJ_STR,pyobjTC));
312 YACS::ENGINE::OutputPort *sender(n0->edAddOutputPort("sender",listPyobjTC));
313 std::ostringstream var0;
314 for(std::vector< YACSEvalInputPort >::const_iterator it=_inputs.begin();it!=_inputs.end();it++)
316 if((*it).isRandomVar())
318 var0 << (*it).getName() << ",";
319 YACS::ENGINE::TypeCode *tc(createSeqTypeCodeFrom(_generatedGraph,(*it).getTypeOfData()));
320 YACS::ENGINE::InputPort *inp(n0->edAddInputPort((*it).getName(),tc));
321 YACS::ENGINE::InputPyPort *inpc(dynamic_cast<YACS::ENGINE::InputPyPort *>(inp));
323 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::generateGraph : internal error 1 !");
324 (*it).setUndergroundPortToBeSet(inpc);
327 std::ostringstream n0Script; n0Script << "sender=zip(" << var0.str() << ")\n";
328 n0->setScript(n0Script.str());
330 YACS::ENGINE::ForEachLoop *n1(r->createForEachLoop(oss.str(),pyobjTC));
331 _FEInGeneratedGraph=n1;
332 _generatedGraph->edAddChild(n1);
333 _generatedGraph->edAddCFLink(n0,n1);
334 _generatedGraph->edAddDFLink(sender,n1->edGetSeqOfSamplesPort());
335 YACS::ENGINE::InlineNode *n2(r->createScriptNode(YACS::ENGINE::PythonNode::KIND,GATHER_NODE_NAME));
336 _generatedGraph->edAddChild(n2);
337 _generatedGraph->edAddCFLink(n1,n2);
339 YACS::ENGINE::Bloc *n10(r->createBloc("Bloc"));
341 YACS::ENGINE::InlineNode *n100(r->createScriptNode(YACS::ENGINE::PythonNode::KIND,"__dispatch__"));
342 YACS::ENGINE::Node *n101(_runNode->cloneWithoutCompAndContDeepCpy(0,true));
343 n10->edAddChild(n100);
344 n10->edAddChild(n101);
345 YACS::ENGINE::InputPort *dispatchIn(n100->edAddInputPort("i0",pyobjTC));
346 n10->edAddCFLink(n100,n101);
347 n1->edAddDFLink(n1->edGetSamplePort(),dispatchIn);
348 std::ostringstream var1;
349 for(std::vector< YACSEvalInputPort >::const_iterator it=_inputs.begin();it!=_inputs.end();it++)
351 if((*it).isRandomVar())
353 var1 << (*it).getName() << ",";
354 YACS::ENGINE::OutputPort *myOut(n100->edAddOutputPort((*it).getName(),_generatedGraph->getTypeCode((*it).getTypeOfData())));
355 std::string tmpPortName(_runNode->getInPortName((*it).getUndergroundPtr()));
356 YACS::ENGINE::InputPort *myIn(n101->getInputPort(tmpPortName));
357 n10->edAddDFLink(myOut,myIn);
360 std::ostringstream n100Script; n100Script << var1.str() << "=i0\n";
361 n100->setScript(n100Script.str());
362 for(std::vector< YACSEvalOutputPort * >::const_iterator it=_outputsOfInterest.begin();it!=_outputsOfInterest.end();it++)
364 YACS::ENGINE::TypeCode *tc(createSeqTypeCodeFrom(_generatedGraph,(*it)->getTypeOfData()));
365 YACS::ENGINE::InputPort *myIn(n2->edAddInputPort((*it)->getName(),tc));
366 std::string tmpPortName(_runNode->getOutPortName((*it)->getUndergroundPtr()));
367 YACS::ENGINE::OutputPort *myOut(n101->getOutputPort(tmpPortName));
368 _generatedGraph->edAddDFLink(myOut,myIn);
370 _generatedGraph->updateContainersAndComponents();
373 void YACSEvalYFXRunOnlyPattern::resetGeneratedGraph()
375 delete _generatedGraph;
380 int YACSEvalYFXRunOnlyPattern::assignNbOfBranches()
382 checkAlreadyComputedResources();
384 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::assignNbOfBranches : the generated graph has not been created !");
385 std::list<YACS::ENGINE::Node *> nodes(_generatedGraph->getChildren());
386 YACS::ENGINE::ForEachLoop *zeMainNode(0);
387 for(std::list<YACS::ENGINE::Node *>::const_iterator it=nodes.begin();it!=nodes.end();it++)
389 YACS::ENGINE::ForEachLoop *isZeMainNode(dynamic_cast<YACS::ENGINE::ForEachLoop *>(*it));
393 zeMainNode=isZeMainNode;
395 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::assignNbOfBranches : internal error 1 !");
399 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::assignNbOfBranches : internal error 2 !");
400 unsigned int nbProcsDeclared(getResourcesInternal()->getNumberOfProcsDeclared());
401 nbProcsDeclared=std::max(nbProcsDeclared,4u);
402 int nbOfBranch(nbProcsDeclared/getResourcesInternal()->getMaxLevelOfParallelism());
403 nbOfBranch=std::max(nbOfBranch,1);
404 YACS::ENGINE::InputPort *zeInputToSet(zeMainNode->edGetNbOfBranchesPort());
405 YACS::ENGINE::AnyInputPort *zeInputToSetC(dynamic_cast<YACS::ENGINE::AnyInputPort *>(zeInputToSet));
407 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::assignNbOfBranches : internal error 3 !");
408 YACS::ENGINE::Any *a(YACS::ENGINE::AtomAny::New(nbOfBranch));
409 zeInputToSetC->put(a);
410 zeInputToSetC->exSaveInit();
415 void YACSEvalYFXRunOnlyPattern::assignRandomVarsInputs()
417 std::size_t sz(std::numeric_limits<std::size_t>::max());
418 for(std::vector< YACSEvalInputPort >::const_iterator it=_inputs.begin();it!=_inputs.end();it++)
419 if((*it).isRandomVar())
421 std::size_t locSize((*it).initializeUndergroundWithSeq());
422 if(sz==std::numeric_limits<std::size_t>::max())
426 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::assignRandomVarsInputs : length of sequences in random vars must be the same !");
430 bool YACSEvalYFXRunOnlyPattern::isLocked() const
432 return _generatedGraph!=0;
435 YACSEvalListOfResources *YACSEvalYFXRunOnlyPattern::giveResources()
438 if(!isAlreadyComputedResources())
440 YACS::ENGINE::DeploymentTree dt(_runNode->getDeploymentTree());
441 YACSEvalListOfResources *res(new YACSEvalListOfResources(_runNode->getMaxLevelOfParallelism(),getCatalogInAppli(),dt));
444 return getResourcesInternal();
447 YACS::ENGINE::Proc *YACSEvalYFXRunOnlyPattern::getUndergroundGeneratedGraph() const
449 return _generatedGraph;
452 std::vector<YACSEvalSeqAny *> YACSEvalYFXRunOnlyPattern::getResults() const
454 if(_generatedGraph->getState()!=YACS::DONE)
455 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::getResults : the execution did not finished correctly ! getResults should not be called !");
456 std::vector<YACSEvalSeqAny *> ret(_outputsOfInterest.size());
457 YACS::ENGINE::Node *node(_generatedGraph->getChildByName(GATHER_NODE_NAME));
458 YACS::ENGINE::PythonNode *nodeC(dynamic_cast<YACS::ENGINE::PythonNode *>(node));
460 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::getResults : internal error !");
462 for(std::vector< YACSEvalOutputPort * >::const_iterator it=_outputsOfInterest.begin();it!=_outputsOfInterest.end();it++,ii++)
464 YACS::ENGINE::InPort *input(nodeC->getInPort((*it)->getName()));
465 YACS::ENGINE::InputPyPort *inputC(dynamic_cast<YACS::ENGINE::InputPyPort *>(input));
468 std::ostringstream oss; oss << "YACSEvalYFXRunOnlyPattern::getResults : internal error for input \"" << (*it)->getName() << "\"";
469 throw YACS::Exception(oss.str());
471 ret[ii]=BuildValueInPort(inputC);
476 void YACSEvalYFXRunOnlyPattern::emitStart() const
478 YACSEvalObserver *obs(getObserver());
481 obs->notifyNumberOfSamplesToEval(getBoss(),_FEInGeneratedGraph->getNbOfElementsToBeProcessed());
484 bool YACSEvalYFXRunOnlyPattern::IsMatching(YACS::ENGINE::Proc *scheme, YACS::ENGINE::ComposedNode *& runNode)
486 std::list<YACS::ENGINE::Node *> nodes(scheme->getChildren());
489 bool areAllElementary(true);
490 for(std::list<YACS::ENGINE::Node *>::const_iterator it=nodes.begin();it!=nodes.end() && areAllElementary;it++)
491 if(!dynamic_cast<YACS::ENGINE::ElementaryNode *>(*it))
492 areAllElementary=false;
496 CheckNodeIsOK(scheme);
502 YACS::ENGINE::ComposedNode *candidate(dynamic_cast<YACS::ENGINE::ComposedNode *>(nodes.front()));
505 CheckNodeIsOK(candidate);
509 void YACSEvalYFXRunOnlyPattern::buildInputPorts()
512 std::list< YACS::ENGINE::InputPort *> allInputPorts(_runNode->getSetOfInputPort());
513 std::vector<std::string> allNames;
514 for(std::list< YACS::ENGINE::InputPort *>::const_iterator it=allInputPorts.begin();it!=allInputPorts.end();it++)
516 YACS::ENGINE::InputPort *elt(*it);
518 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::buildInputPorts : presence of null input !");
519 std::set<YACS::ENGINE::OutPort *> bls(elt->edSetOutPort());
522 std::string inpName(elt->getName());
524 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::buildInputPorts : an input has empty name ! Should not !");
525 _inputs.push_back(YACSEvalInputPort(elt));
526 if(std::find(allNames.begin(),allNames.end(),inpName)!=allNames.end())
528 std::ostringstream oss; oss << "YACSEvalYFXRunOnlyPattern::buildInputPorts : input name \"" << inpName << "\" appears more than once !";
529 throw YACS::Exception(oss.str());
531 allNames.push_back(inpName);
536 void YACSEvalYFXRunOnlyPattern::buildOutputPorts()
539 std::list< YACS::ENGINE::OutputPort *> allOutputPorts(_runNode->getSetOfOutputPort());
540 std::vector<std::string> allNames;
541 for(std::list< YACS::ENGINE::OutputPort *>::const_iterator it=allOutputPorts.begin();it!=allOutputPorts.end();it++)
543 YACS::ENGINE::OutputPort *elt(*it);
545 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::buildOutputPorts : presence of null output !");
546 std::string outpName(elt->getName());
548 throw YACS::Exception("YACSEvalYFXRunOnlyPattern::buildOutputPorts : an output has empty name ! Should not !");
549 if(std::find(allNames.begin(),allNames.end(),outpName)!=allNames.end())
551 std::ostringstream oss; oss << "YACSEvalYFXRunOnlyPattern::buildOutputPorts : output name \"" << outpName << "\" appears more than once !";
552 throw YACS::Exception(oss.str());
554 _outputs.push_back(YACSEvalOutputPort(*it));