Salome HOME
Ready to unplug nbOfBranches from foreach
authorAnthony Geay <anthony.geay@edf.fr>
Tue, 17 Mar 2020 09:28:30 +0000 (10:28 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Tue, 17 Mar 2020 09:28:30 +0000 (10:28 +0100)
src/engine/DynParaLoop.cxx
src/engine/DynParaLoop.hxx
src/engine/ForEachLoop.cxx
src/engine/NbBranches.cxx
src/engine/NbBranches.hxx
src/engine/OptimizerLoop.cxx

index ea2e40072d03b8220d95637a85a27fca37fd0d7f..fd1b2b420b84d6586b7dafe9148dc7e427c15238 100644 (file)
@@ -39,9 +39,9 @@ using namespace YACS::ENGINE;
 const char DynParaLoop::NAME_OF_SPLITTED_SEQ_OUT[] = "evalSamples";
 const char DynParaLoop::OLD_NAME_OF_SPLITTED_SEQ_OUT[] = "SmplPrt"; // For backward compatibility with 5.1.4
 
-DynParaLoop::DynParaLoop(const std::string& name, TypeCode *typeOfDataSplitted)
+DynParaLoop::DynParaLoop(const std::string& name, TypeCode *typeOfDataSplitted, std::unique_ptr<NbBranchesAbstract>&& branchManager)
   : ComposedNode(name),_node(0),_initNode(0),_finalizeNode(0),_nbOfEltConsumed(0),
-    _nbOfBranches(this),
+    _nbOfBranches(std::move(branchManager)),
     _splittedPort(NAME_OF_SPLITTED_SEQ_OUT,this,typeOfDataSplitted),_initializingCounter(0),_unfinishedCounter(0),_failedCounter(0),_weight(), _loopWeight(0)
 {
   _weight.setDefaultLoop();
@@ -55,7 +55,7 @@ DynParaLoop::~DynParaLoop()
 }
 
 DynParaLoop::DynParaLoop(const DynParaLoop& other, ComposedNode *father, bool editionOnly)
-  : ComposedNode(other,father), _nbOfBranches(other._nbOfBranches,this),
+  : ComposedNode(other,father),_nbOfBranches(other._nbOfBranches->copy(this)),
     _splittedPort(other._splittedPort,this), _node(0), _initNode(0), _finalizeNode(0),
     _nbOfEltConsumed(0),_initializingCounter(0),_unfinishedCounter(0),_failedCounter(0),_weight(other._weight), _loopWeight(other._loopWeight)
 {
@@ -113,7 +113,7 @@ void DynParaLoop::init(bool start)
   _node->init(start);
   if (_initNode) _initNode->init(start);
   if (_finalizeNode) _finalizeNode->init(start);
-  _nbOfBranches.exInit(start);
+  _nbOfBranches->exInit(start);
   _splittedPort.exInit();
   _nbOfEltConsumed=0;
   _failedCounter=0;
@@ -274,21 +274,25 @@ std::list<Node *> DynParaLoop::edGetDirectDescendants() const
 std::list<InputPort *> DynParaLoop::getSetOfInputPort() const
 {
   list<InputPort *> ret=ComposedNode::getSetOfInputPort();
-  ret.push_back(_nbOfBranches.getPort());
+  InputPort *port(_nbOfBranches->getPort());
+  if(port)
+    ret.push_back(port);
   return ret;
 }
 
 InputPort *DynParaLoop::getInputPort(const std::string& name) const throw(YACS::Exception)
 {
-  if(NbBranches::IsBranchPortName(name))
-    return _nbOfBranches.getPort();
+  if(_nbOfBranches->isMyName(name))
+    return _nbOfBranches->getPort();
   return ComposedNode::getInputPort(name);
 }
 
 std::list<InputPort *> DynParaLoop::getLocalInputPorts() const
 {
   list<InputPort *> ret=ComposedNode::getLocalInputPorts();
-  ret.push_back(_nbOfBranches.getPort());
+  InputPort *port(_nbOfBranches->getPort());
+  if(port)
+    ret.push_back(port);
   return ret;
 }
 
@@ -439,12 +443,12 @@ ComplexWeight* DynParaLoop::getWeight()
 
 bool DynParaLoop::isMultiplicitySpecified(unsigned& value) const
 {
-  return _nbOfBranches.isMultiplicitySpecified(value);
+  return _nbOfBranches->isMultiplicitySpecified(value);
 }
 
 void DynParaLoop::forceMultiplicity(unsigned value)
 {
-  _nbOfBranches.forceMultiplicity(value);
+  _nbOfBranches->forceMultiplicity(value);
 }
 
 void DynParaLoop::buildDelegateOf(InPort * & port, OutPort *initialStart, const std::list<ComposedNode *>& pointsOfView)
@@ -903,7 +907,7 @@ Node * DynParaLoop::getFinalizeNode()
 
 int DynParaLoop::getMaxLevelOfParallelism() const
 {
-  return _nbOfBranches.getIntValue() * _node->getMaxLevelOfParallelism();
+  return _nbOfBranches->getIntValue() * _node->getMaxLevelOfParallelism();
 }
 
 void DynParaLoop::partitionRegardingDPL(const PartDefinition *pd, std::map<ComposedNode *, YACS::BASES::AutoRefCnt<PartDefinition> >& zeMap)
index 1ff196e95676b7ea3337702ff185fd82172751a3..8e58bd4991c774052c0dcee229341b05fd4780b7 100644 (file)
@@ -27,6 +27,8 @@
 #include "OutputPort.hxx"
 #include "NbBranches.hxx"
 
+#include <memory>
+
 namespace YACS
 {
   namespace ENGINE
@@ -50,7 +52,8 @@ namespace YACS
       Node *_finalizeNode;
       unsigned _nbOfEltConsumed;
       std::vector<int> _execIds;
-      NbBranches _nbOfBranches;
+      //NbBranches _nbOfBranches2;
+      std::unique_ptr<NbBranchesAbstract> _nbOfBranches;
       AnyOutputPort _splittedPort;
       std::vector<Node *> _execNodes;
       std::vector<Node *> _execInitNodes;
@@ -64,7 +67,7 @@ namespace YACS
       static const char NAME_OF_SPLITTED_SEQ_OUT[];
       static const char OLD_NAME_OF_SPLITTED_SEQ_OUT[];
     protected:
-      DynParaLoop(const std::string& name, TypeCode *typeOfDataSplitted);
+      DynParaLoop(const std::string& name, TypeCode *typeOfDataSplitted, std::unique_ptr<NbBranchesAbstract>&& branchManager);
       virtual ~DynParaLoop();
       DynParaLoop(const DynParaLoop& other, ComposedNode *father, bool editionOnly);
     public:
@@ -77,7 +80,7 @@ namespace YACS
       Node *edSetFinalizeNode(Node *DISOWNnode);
       virtual bool edAddDFLink(OutPort *start, InPort *end) throw(Exception);
       void init(bool start=true);
-      InputPort *edGetNbOfBranchesPort() { return _nbOfBranches.getPort(); }
+      InputPort *edGetNbOfBranchesPort() { return _nbOfBranches->getPort(); }
       int getNumberOfInputPorts() const;
       int getNumberOfOutputPorts() const;
       unsigned getNumberOfEltsConsumed() const { return _nbOfEltConsumed; }
index 34f231b9b5f5595ac0b4adcd0cac7f9c3a1d1d87..76eebb5a7cb21c1c31591441469a042c970fdc46 100644 (file)
@@ -465,7 +465,7 @@ void ForEachLoopPassedData::assignAlreadyDone(const std::vector<SequenceAny *>&
     }
 }
 
-ForEachLoop::ForEachLoop(const std::string& name, TypeCode *typeOfDataSplitted):DynParaLoop(name,typeOfDataSplitted),
+ForEachLoop::ForEachLoop(const std::string& name, TypeCode *typeOfDataSplitted):DynParaLoop(name,typeOfDataSplitted,std::unique_ptr<NbBranchesAbstract>(new NbBranches(this))),
                                                                                 _splitterNode(NAME_OF_SPLITTERNODE,typeOfDataSplitted,this),
                                                                                 _execCurrentId(0),_nodeForSpecialCases(0),_currentIndex(0),_passedData(0)
 {
@@ -526,7 +526,7 @@ void ForEachLoop::exUpdateState()
     {
       //internal graph update
       int i;
-      int nbOfBr(_nbOfBranches.getIntValue()),nbOfElts(_splitterNode.getNumberOfElements()),nbOfEltsDone(0);
+      int nbOfBr(_nbOfBranches->getIntValue()),nbOfElts(_splitterNode.getNumberOfElements()),nbOfEltsDone(0);
       if(_passedData)
         {
           _passedData->checkCompatibilyWithNb(nbOfElts);
@@ -1084,7 +1084,7 @@ void ForEachLoop::checkLinkPossibility(OutPort *start, const std::list<ComposedN
   if(end->getNode() == &_splitterNode)
     throw Exception("Illegal link within a foreach loop: \
 the 'SmplsCollection' port cannot be linked within the scope of the loop.");
-  if(end == _nbOfBranches.getPort())
+  if(end == _nbOfBranches->getPort())
     throw Exception("Illegal link within a foreach loop: \
 the 'nbBranches' port cannot be linked within the scope of the loop.");
 }
@@ -1168,7 +1168,7 @@ list<ProgressWeight> ForEachLoop::getProgressWeight() const
 
 int ForEachLoop::getNbOfElementsToBeProcessed() const
 {
-  int nbBranches = _nbOfBranches.getIntValue();
+  int nbBranches = _nbOfBranches->getIntValue();
   return _splitterNode.getNumberOfElements()
          + (_initNode ? nbBranches:0)
          + (_finalizeNode ? nbBranches:0) ;
index b39e4ce62119acb7469d21f3db6a60aa30ce8776..bf7a8e1193b8b5d23e5eec2e558117a8933b2762 100644 (file)
 
 using namespace YACS::ENGINE;
 
-const char NbBranches::NAME_OF_NUMBER_OF_BRANCHES[]="nbBranches";
+const char NbBranchesAbstract::NAME_OF_NUMBER_OF_BRANCHES[]="nbBranches";
+
+bool NbBranchesAbstract::IsBranchPortName(const std::string& name)
+{
+  return name == NAME_OF_NUMBER_OF_BRANCHES;
+}
+
+std::unique_ptr<NbBranchesAbstract> NbBranches::copy(Node *node) const
+{
+  return std::unique_ptr<NbBranchesAbstract>(new NbBranches(*this,node));
+}
+
+bool NbBranches::isMyName(const std::string& name) const
+{
+  return NbBranchesAbstract::IsBranchPortName(name);
+}
 
 void NbBranches::exInit(bool start)
 {
@@ -50,7 +65,31 @@ void NbBranches::forceMultiplicity(unsigned value)
   _nbOfBranches.edInit((int)value);
 }
 
-bool NbBranches::IsBranchPortName(const std::string& name)
+std::unique_ptr<NbBranchesAbstract> NoNbBranches::copy(Node *) const
 {
-  return name == NAME_OF_NUMBER_OF_BRANCHES;
-}
\ No newline at end of file
+  return std::unique_ptr<NbBranchesAbstract>(new NoNbBranches);
+}
+
+void NoNbBranches::exInit(bool start)
+{
+}
+
+InputPort *NoNbBranches::getPort() const
+{
+  return nullptr;
+}
+
+bool NoNbBranches::isMultiplicitySpecified(unsigned& value) const
+{
+  return false;
+}
+
+void NoNbBranches::forceMultiplicity(unsigned value)
+{
+  throw Exception("NoNbBranches::forceMultiplicity : impossible to be forced !");
+}
+
+int NoNbBranches::getIntValue() const
+{
+  throw Exception("NoNbBranches::getIntValue : no value stored !");
+}
index 5960da73f3542e112a6dcab3e4889c0b54053f63..a9cca28f3709c4dd1e0bfaecf6decf49b0a19c48 100644 (file)
 #include "YACSlibEngineExport.hxx"
 #include "AnyInputPort.hxx"
 
+#include <memory>
+
 namespace YACS
 {
   namespace ENGINE
   {
     class Node;
-    class YACSLIBENGINE_EXPORT NbBranches
+    class YACSLIBENGINE_EXPORT NbBranchesAbstract
+    {
+      public:
+        virtual std::unique_ptr<NbBranchesAbstract> copy(Node *node) const = 0;
+        virtual bool isMyName(const std::string& name) const = 0;
+        virtual void exInit(bool start) = 0;
+        virtual InputPort *getPort() const = 0;
+        virtual bool isMultiplicitySpecified(unsigned& value) const = 0;
+        virtual void forceMultiplicity(unsigned value) = 0;
+        virtual int getIntValue() const = 0;
+        static bool IsBranchPortName(const std::string& name);
+      protected:
+        static const char NAME_OF_NUMBER_OF_BRANCHES[];
+    };
+
+    class YACSLIBENGINE_EXPORT NbBranches : public NbBranchesAbstract
     {
       public:
         NbBranches(Node *node):_nbOfBranches(NAME_OF_NUMBER_OF_BRANCHES,node,Runtime::_tc_int) { }
         NbBranches(const NbBranches& other, Node *node):_nbOfBranches(other._nbOfBranches,node) { }
-        void exInit(bool start);
-        InputPort *getPort() const;
-        bool isMultiplicitySpecified(unsigned& value) const;
-        void forceMultiplicity(unsigned value);
-        int getIntValue() const { return _nbOfBranches.getIntValue(); }
-        static bool IsBranchPortName(const std::string& name);
+        bool isMyName(const std::string& name) const override;
+        std::unique_ptr<NbBranchesAbstract> copy(Node *node) const override;
+        void exInit(bool start) override;
+        InputPort *getPort() const override;
+        bool isMultiplicitySpecified(unsigned& value) const override;
+        void forceMultiplicity(unsigned value) override;
+        int getIntValue() const override { return _nbOfBranches.getIntValue(); }
       private:
         AnyInputPort _nbOfBranches;
-        static const char NAME_OF_NUMBER_OF_BRANCHES[];
+    };
+
+    class YACSLIBENGINE_EXPORT NoNbBranches : public NbBranchesAbstract
+    {
+      public:
+        NoNbBranches() = default;
+        std::unique_ptr<NbBranchesAbstract> copy(Node *node) const override;
+        bool isMyName(const std::string& name) const override { return false; }
+        void exInit(bool start) override;
+        InputPort *getPort() const override;
+        bool isMultiplicitySpecified(unsigned& value) const override;
+        void forceMultiplicity(unsigned value) override;
+        int getIntValue() const override;
     };
   }
 }
index 39c45ffd0a5a0c1e635fe9fa4257052624d45d43..48fbf9abdc978081b130e494461704dd9d60a254 100644 (file)
@@ -98,7 +98,7 @@ void FakeNodeForOptimizerLoop::finished()
 OptimizerLoop::OptimizerLoop(const std::string& name, const std::string& algLibWthOutExt,
                              const std::string& symbolNameToOptimizerAlgBaseInstanceFactory,
                              bool algInitOnFile,bool initAlgo, Proc * procForTypes):
-        DynParaLoop(name,Runtime::_tc_string),_algInitOnFile(algInitOnFile),_alglib(algLibWthOutExt),
+        DynParaLoop(name,Runtime::_tc_string,std::unique_ptr<NbBranchesAbstract>(new NbBranches(this))),_algInitOnFile(algInitOnFile),_alglib(algLibWthOutExt),
         _algoInitPort(NAME_OF_ALGO_INIT_PORT, this, Runtime::_tc_string, true),
         _loader(NULL),_alg(0),_convergenceReachedWithOtherCalc(false),
         _retPortForOutPool(NAME_OF_OUT_POOL_INPUT,this,Runtime::_tc_string),
@@ -183,7 +183,7 @@ void OptimizerLoop::exUpdateState()
 
           //internal graph update
           int i;
-          int nbOfBr=_nbOfBranches.getIntValue();
+          int nbOfBr=_nbOfBranches->getIntValue();
           _alg->setNbOfBranches(nbOfBr);
 
           _alg->startProxy();
@@ -435,7 +435,7 @@ YACS::Event OptimizerLoop::finalize()
     {
       // Run the finalize nodes, the OptimizerLoop will be done only when they all finish
       _unfinishedCounter = 0;  // This counter indicates how many branches are not finished
-      for (int i=0 ; i<_nbOfBranches.getIntValue() ; i++)
+      for (int i=0 ; i<_nbOfBranches->getIntValue() ; i++)
         if (_execIds[i] == NOT_RUNNING_BRANCH_ID)
           {
             DEBTRACE("Launching finalize node for branch " << i)
@@ -530,7 +530,7 @@ void OptimizerLoop::checkLinkPossibility(OutPort *start, const std::list<Compose
   linkName += start->getName()+" to "+end->getName()+")";
 
   // Yes, it should be possible to link back the result port to any input port of the loop.
-  if(end == _nbOfBranches.getPort() || end == &_algoInitPort)
+  if(end == _nbOfBranches->getPort() || end == &_algoInitPort)
     if(start != &_algoResultPort)
       throw Exception(std::string("Illegal OptimizerLoop link.") + linkName);
     else