From 73910a404750f0f079b188a46f37ca55b24a38ec Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 19 Mar 2020 15:51:24 +0100 Subject: [PATCH 1/1] WIP --- src/engine/VisitorSaveSchema.cxx | 8 +- src/yacsloader/loopParsers.hxx | 105 ++++++++++++++++++++++++++ src/yacsloader_swig/Test/testFEDyn.py | 48 ++++++++++-- 3 files changed, 149 insertions(+), 12 deletions(-) diff --git a/src/engine/VisitorSaveSchema.cxx b/src/engine/VisitorSaveSchema.cxx index a2fa7eadb..85a30463c 100644 --- a/src/engine/VisitorSaveSchema.cxx +++ b/src/engine/VisitorSaveSchema.cxx @@ -166,7 +166,7 @@ void VisitorSaveSchema::visitForEachLoopDyn(ForEachLoopDyn *node) node->DynParaLoop::accept(this); writeSimpleDataLinks(node); writeSimpleStreamLinks(node); - _out << indent(depth) << "" << endl; + _out << indent(depth) << "" << endl; endCase(node); DEBTRACE("END visitForEachLoopDyn " << _root->getChildName(node)); } @@ -871,7 +871,7 @@ void VisitorSaveSchema::writeSimpleDataLinks(ComposedNode *node) for (list::iterator ic = setOfChildren.begin(); ic != setOfChildren.end(); ++ic) // add "splitter" node of ForEachLoop nodes to the set of children - if ( dynamic_cast( *ic ) ) + if ( dynamic_cast( *ic ) ) { Node *nodeToInsert=(*ic)->getChildByName(ForEachLoop::NAME_OF_SPLITTERNODE); if(find(setOfChildrenPlusSplitters.begin(),setOfChildrenPlusSplitters.end(),nodeToInsert)==setOfChildrenPlusSplitters.end()) @@ -901,13 +901,13 @@ void VisitorSaveSchema::writeSimpleDataLinks(ComposedNode *node) DEBTRACE( "BINGO!" ); string fromName; - if ( dynamic_cast(from) && dynamic_cast(from->getFather()) ) + if ( dynamic_cast(from) && dynamic_cast(from->getFather()) ) fromName = from->getFather()->getName(); else fromName = node->getChildName(from); string childName; - if ( dynamic_cast(to) && dynamic_cast(to->getFather()) ) + if ( dynamic_cast(to) && dynamic_cast(to->getFather()) ) childName = node->getChildName(to->getFather()); else childName = node->getChildName(to); diff --git a/src/yacsloader/loopParsers.hxx b/src/yacsloader/loopParsers.hxx index 313666a06..ee42b9ab3 100644 --- a/src/yacsloader/loopParsers.hxx +++ b/src/yacsloader/loopParsers.hxx @@ -62,6 +62,7 @@ struct looptypeParser:parser virtual void node (ENGINE::InlineNode* const& n); virtual void forloop (ENGINE::ForLoop* const& b); virtual void foreach (ENGINE::ForEachLoop* const& b); + virtual void foreachdyn (ENGINE::ForEachLoopDyn* const& b); virtual void optimizer (ENGINE::OptimizerLoop* const& b); virtual void while_ (ENGINE::WhileLoop* const& b); virtual void switch_ (ENGINE::Switch* const& b); @@ -191,6 +192,16 @@ void looptypeParser::foreach (ENGINE::ForEachLoop* const& b) currentProc->nodeMap[fullname]=b->getChildByShortName("splitter"); } template +void looptypeParser::foreachdyn (ENGINE::ForEachLoopDyn* const& b) +{ + DEBTRACE("loop_foreachdyn" << b->getName()) + _cnode->edSetNode(b); + std::string fullname=currentProc->names.back()+ b->getName(); + currentProc->nodeMap[fullname]=b; + fullname += ".splitter"; + currentProc->nodeMap[fullname]=b->getChildByShortName("splitter"); +} +template void looptypeParser::optimizer (ENGINE::OptimizerLoop* const& b) { DEBTRACE( "loop_optimizer: " << b->getName() ); @@ -581,6 +592,97 @@ struct foreachlooptypeParser:dynparalooptypeParser template foreachlooptypeParser foreachlooptypeParser::foreachloopParser; +//FIXME +template +struct foreachloopdyntypeParser:dynparalooptypeParser +{ + static foreachloopdyntypeParser foreachloopdynParser; + + virtual void buildAttr(const XML_Char** attr) + { + if (!attr) + return; + this->required("name",attr); + this->required("type",attr); + for (int i = 0; attr[i]; i += 2) + { + if(std::string(attr[i]) == "name")name(attr[i+1]); + if(std::string(attr[i]) == "state")this->state(attr[i+1]); + if(std::string(attr[i]) == "nbranch")nbranch(atoi(attr[i+1])); + if(std::string(attr[i]) == "loopWeight")weight(atof(attr[i+1])); + if(std::string(attr[i]) == "type")datatype(attr[i+1]); + } + postAttr(); + } + virtual void pre () + { + _nbranch=0; + _weight=-1.; + this->looptypeParser::pre(); + } + virtual void name (const std::string& name) + { + DEBTRACE("foreach_name: " << name) + _name=name; + _fullname=currentProc->names.back()+name; + } + virtual void nbranch (const int& n) + { + DEBTRACE("foreach_nbranch: " << n ) + _nbranch=n; + } + virtual void weight (const double& x) + { + DEBTRACE("foreach_weight: " << x ) + _weight=x; + } + virtual void datatype (const std::string& type) + { + DEBTRACE("foreach_datatype: "<< type) + _datatype=type; + } + virtual void postAttr() + { + if(currentProc->typeMap.count(_datatype)==0) + { + //Check if the typecode is defined in the runtime + YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(_datatype); + if(t==0) + { + std::stringstream msg; + msg << "Type "<< _datatype <<" does not exist"<<" ("<<__FILE__<<":"<<__LINE__<< ")"; + throw Exception(msg.str()); + } + else + { + currentProc->typeMap[_datatype]=t; + t->incrRef(); + } + } + this->_cnode=theRuntime->createForEachLoopDyn(_name,currentProc->typeMap[_datatype]); + //set number of branches + if(_nbranch > 0)this->_cnode->edGetNbOfBranchesPort()->edInit(_nbranch); + if(_weight > 0)this->_cnode->setWeight(_weight); + this->_cnodes.push_back(this->_cnode); + currentProc->names.push_back(_fullname + '.'); + } + virtual T post() + { + DEBTRACE("foreach_post" << this->_cnode->getName()) + T b=this->_cnode; + this->_cnodes.pop_back(); + currentProc->names.pop_back(); + this->_cnode=this->_cnodes.empty() ? 0 : this->_cnodes.back(); + return b; + } + int _nbranch; + double _weight; + std::string _fullname; + std::string _name; + std::string _datatype; +}; + +template foreachloopdyntypeParser foreachloopdyntypeParser::foreachloopdynParser; } namespace YACS @@ -697,6 +799,7 @@ void looptypeParser::onStart(const XML_Char* el, const XML_Char** attr) this->maxcount("node",1,element); this->maxcount("forloop",1,element); this->maxcount("foreach",1,element); + this->maxcount("foreachdyn",1,element); this->maxcount("optimizer",1,element); this->maxcount("while",1,element); this->maxcount("switch",1,element); @@ -713,6 +816,7 @@ void looptypeParser::onStart(const XML_Char* el, const XML_Char** attr) else if(element == "forloop")pp=&forlooptypeParser<>::forloopParser; else if(element == "foreach")pp=&foreachlooptypeParser<>::foreachloopParser; + else if(element == "foreachdyn")pp=&foreachloopdyntypeParser<>::foreachloopdynParser; else if(element == "optimizer")pp=&optimizerlooptypeParser<>::optimizerloopParser; else if(element == "while")pp=&whilelooptypeParser<>::whileloopParser; else if(element == "switch")pp=&switchtypeParser::switchParser; @@ -739,6 +843,7 @@ void looptypeParser::onEnd(const char *el,parser* child) else if(element == "forloop")forloop(((forlooptypeParser<>*)child)->post()); else if(element == "foreach")foreach(((foreachlooptypeParser<>*)child)->post()); + else if(element == "foreachdyn")foreachdyn(((foreachloopdyntypeParser<>*)child)->post()); else if(element == "optimizer")optimizer(((optimizerlooptypeParser<>*)child)->post()); else if(element == "while")while_(((whilelooptypeParser<>*)child)->post()); else if(element == "switch")switch_(((switchtypeParser*)child)->post()); diff --git a/src/yacsloader_swig/Test/testFEDyn.py b/src/yacsloader_swig/Test/testFEDyn.py index c75430438..86027a12d 100644 --- a/src/yacsloader_swig/Test/testFEDyn.py +++ b/src/yacsloader_swig/Test/testFEDyn.py @@ -1,8 +1,12 @@ import pilot import SALOMERuntime import loader +import datetime +fname="testFEDyn.xml" +fname2="REtestFEDyn.xml" SALOMERuntime.RuntimeSALOME.setRuntime() +l=loader.YACSLoader() r=SALOMERuntime.getSALOMERuntime() p=r.createProc("prTest1") td=p.createType("double","double") @@ -18,17 +22,17 @@ n0=r.createScriptNode("","n0") o0=n0.edAddOutputPort("o0",tsi) n0.setScript("o0=[ elt for elt in range(8) ]") p.edAddChild(n0) -n1=r.createForEachLoopDyn("n1",ti) +n1=r.createForEachLoop("n1",ti)#Dyn +n1.getInputPort("nbBranches").edInitPy(2) n10=r.createScriptNode("","n10") n10.setExecutionMode("remote") n10.setContainer(cont) n1.edAddChild(n10) n10.setScript(""" import time -time.sleep(2) +time.sleep(1) o2=2*i1 """) -n10.setWeight(4.) i1=n10.edAddInputPort("i1",ti) o2=n10.edAddOutputPort("o2",ti) p.edAddChild(n1) @@ -38,15 +42,43 @@ p.edAddCFLink(n0,n1) n2=r.createScriptNode("","n2") n2.setScript("o4=i3") i3=n2.edAddInputPort("i3",tsi) +i4=n2.edAddInputPort("i4",tsi) o4=n2.edAddOutputPort("o4",tsi) -n2.setScript("o4=i3") +n2.setScript("o4=[a+b for a,b in zip(i3,i4)]") p.edAddChild(n2) p.edAddCFLink(n1,n2) p.edAddLink(o2,i3) -#p.saveSchema(fname) -#p=l.load(fname) -ex=pilot.ExecutorSwig() +# Second parallel foreach +n11=r.createForEachLoop("n11",ti)#Dyn +n11.getInputPort("nbBranches").edInitPy(2) +n110=r.createScriptNode("","n110") +n110.setExecutionMode("remote") +n110.setContainer(cont) +n11.edAddChild(n110) +n110.setScript(""" +import time +time.sleep(10) +o2=3*i1 +""") +i1_1=n110.edAddInputPort("i1",ti) +o2_1=n110.edAddOutputPort("o2",ti) +p.edAddChild(n11) +p.edAddLink(o0,n11.edGetSeqOfSamplesPort()) +p.edAddLink(n11.edGetSamplePort(),i1_1) +p.edAddCFLink(n0,n11) +p.edAddCFLink(n11,n2) +p.edAddLink(o2_1,i4) +# +p.saveSchema(fname) +p=l.load(fname) +p.saveSchema(fname2) +p.init() +"""ex=pilot.ExecutorSwig() assert(p.getState()==pilot.READY) +stt = datetime.datetime.now() ex.RunW(p,0) +print(str(datetime.datetime.now()-stt)) assert(p.getState()==pilot.DONE) -assert(o4.getPyObj()==[0, 2, 4, 6, 8, 10, 12, 14]) +assert(o4.getPyObj()==[0, 5, 10, 15, 20, 25, 30, 35])""" +# Ideal ForEachDyn time = 22 s +# ForEach time = 40 s""" \ No newline at end of file -- 2.39.2