Salome HOME
Copyright update 2020
[modules/yacs.git] / src / engine / Bloc_impl.cxx
1 // Copyright (C) 2006-2020  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Bloc.hxx"
21 #include "Proc.hxx"
22 #include "Visitor.hxx"
23 #include "ForEachLoop.hxx"
24 #include "InlineNode.hxx"
25 #include "HomogeneousPoolContainer.hxx"
26
27 using namespace YACS::ENGINE;
28
29 void Bloc::fitToPlayGround(const PlayGround *pg)
30 {
31   static const char MSG[]="Bloc::fitToPlayGround : Not implemented yet for this type of node !";
32   class MyVisitor : public Visitor
33     {
34     public:
35       MyVisitor(ComposedNode *root):Visitor(root),_lev(0),_max_lev(0) { }
36       void visitBloc(Bloc *node) { node->ComposedNode::accept(this); }
37       void visitElementaryNode(ElementaryNode *node) { }
38       void visitForEachLoop(ForEachLoop *node)
39       {
40         _max_lev=std::max(_max_lev,_lev);
41         {
42           _lev++;
43           node->ComposedNode::accept(this);
44           _lev--;
45         }
46         node->edGetNbOfBranchesPort()->edInit(1);
47         if(_lev==_max_lev)
48           {
49           _fes.push_back(node);// locate all leaves ForEach
50           }
51         if(_lev==0)
52           _max_lev=0;
53         }
54       void visitOptimizerLoop(OptimizerLoop *node) { throw YACS::Exception(MSG); }
55       void visitDynParaLoop(DynParaLoop *node) { throw YACS::Exception(MSG); }
56       void visitForLoop(ForLoop *node) { throw YACS::Exception(MSG); }
57       void visitInlineNode(InlineNode *node)
58       {
59           Container *cont(node->getContainer());
60           HomogeneousPoolContainer *cont2(dynamic_cast<HomogeneousPoolContainer *>(cont));
61           if(!cont2)
62             return ;
63           _cont.push_back(cont2);
64           HomogeneousPoolContainer *cont3(cont2->getDirectFather());
65           if(cont3)
66             _cont2.insert(cont3);
67       }
68       void visitInlineFuncNode(InlineFuncNode *node) { throw YACS::Exception(MSG); }
69       void visitLoop(Loop *node) { throw YACS::Exception(MSG); }
70       void visitProc(Proc *node) { node->ComposedNode::accept(this); }
71       void visitServiceNode(ServiceNode *node) { throw YACS::Exception(MSG); }
72       void visitServerNode(ServerNode *node) { throw YACS::Exception(MSG); }
73       void visitServiceInlineNode(ServiceInlineNode *node) { throw YACS::Exception(MSG); }
74       void visitSwitch(Switch *node) { throw YACS::Exception(MSG); }
75       void visitWhileLoop(WhileLoop *node) { throw YACS::Exception(MSG); }
76       void visitPresetNode(DataNode *node) { throw YACS::Exception(MSG); }
77       void visitOutNode(DataNode *node) { throw YACS::Exception(MSG); }
78       void visitStudyInNode(DataNode *node) { throw YACS::Exception(MSG); }
79       void visitStudyOutNode(DataNode *node) { throw YACS::Exception(MSG); }
80     public:
81       std::list<ForEachLoop *> _fes;
82       std::list< HomogeneousPoolContainer *> _cont;
83       std::set< HomogeneousPoolContainer * > _cont2;
84       int _lev;
85       int _max_lev;
86   };
87   YACS::BASES::AutoRefCnt<PartDefinition> pd(new AllPartDefinition(pg));
88   std::map<ComposedNode *,YACS::BASES::AutoRefCnt<PartDefinition> > zeMap;
89   MyVisitor vis(this);
90   this->accept(&vis);
91   for(std::list<ForEachLoop *>::const_iterator it=vis._fes.begin();it!=vis._fes.end();it++)
92     (*it)->edGetNbOfBranchesPort()->edInit(1);
93   this->removeRecursivelyRedundantCL();
94   if (this->getMaxLevelOfParallelism() > pg->getNumberOfCoresAvailable())
95     throw YACS::Exception("Bloc::fitToPlayGround : Not enough cores available to run the calculation !");
96   this->partitionRegardingDPL(pd,zeMap);
97   this->accept(&vis);
98   for(std::list<ForEachLoop *>::const_iterator it=vis._fes.begin();it!=vis._fes.end();it++)
99     {
100       std::map<ComposedNode *,YACS::BASES::AutoRefCnt<PartDefinition> >::iterator it2(zeMap.find(*it));
101       if(it2==zeMap.end())
102         throw YACS::Exception("Bloc::fitToPlayGround : internal error !");
103       int maxLev((*it)->getExecNode()->getMaxLevelOfParallelism());
104       int a((*it2).second->getNumberOfCoresConsumed());
105       int res(a/maxLev);
106       (*it)->edGetNbOfBranchesPort()->edInit(res);
107     }
108   for(std::set< HomogeneousPoolContainer * >::const_iterator it=vis._cont2.begin();it!=vis._cont2.end();it++)
109     (*it)->setSizeOfPool(pg->getNumberOfWorkers((*it)->getNumberOfCoresPerWorker()));
110   for(std::list< HomogeneousPoolContainer *>::const_iterator it=vis._cont.begin();it!=vis._cont.end();it++)
111     (*it)->prepareMaskForExecution();
112 }