Salome HOME
79d30fe6587729443d8034aab193ff925f5de3ac
[modules/yacs.git] / src / engine / ForkBlocPoint.cxx
1 // Copyright (C) 2015-2016  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 "ForkBlocPoint.hxx"
21 #include "Exception.hxx"
22
23 #include <algorithm>
24
25 using namespace YACS::ENGINE;
26
27 ForkBlocPoint::ForkBlocPoint(const std::list<AbstractPoint *>& nodes, AbstractPoint *father):BlocPoint(nodes,father)
28 {
29 }
30
31 ForkBlocPoint::~ForkBlocPoint()
32 {
33 }
34
35 Node *ForkBlocPoint::getFirstNode()
36 {
37   if(_nodes.empty())
38     throw Exception("ForkBlocPoint::getFirstNode : error no branches !");
39   return _nodes.front()->getFirstNode();
40 }
41
42 Node *ForkBlocPoint::getLastNode()
43 {
44   if(_nodes.empty())
45     throw Exception("ForkBlocPoint::getLastNode : error no branches !");
46   return _nodes.front()->getLastNode();//not a bug - seen from the outside only first branch exists !
47 }
48
49 int ForkBlocPoint::getMaxLevelOfParallelism() const
50 {
51   int ret(0);
52   for(std::list<AbstractPoint *>::const_iterator it=_nodes.begin();it!=_nodes.end();it++)
53     ret+=(*it)->getMaxLevelOfParallelism();
54   return ret;
55 }
56
57 double ForkBlocPoint::getWeightRegardingDPL() const
58 {
59   double ret(0.);
60   for(std::list<AbstractPoint *>::const_iterator it=_nodes.begin();it!=_nodes.end();it++)
61     ret+=(*it)->getWeightRegardingDPL();
62   return ret;
63 }
64
65 void ForkBlocPoint::partitionRegardingDPL(const PartDefinition *pd, std::map<ComposedNode *, YACS::BASES::AutoRefCnt<PartDefinition> >& zeMap) const
66 {
67   std::vector< std::pair<const PartDefinition *,double> > parts,parts2;
68   std::vector<std::size_t> v,v2;
69   std::size_t ii(0);
70   for(std::list<AbstractPoint *>::const_iterator it=_nodes.begin();it!=_nodes.end();it++,ii++)
71     {
72       double w((*it)->getWeightRegardingDPL());
73       if(w!=0.)
74         {
75           parts.push_back(std::pair<const PartDefinition *,double >(pd,w));
76           v.push_back(ii);
77         }
78       else
79         {
80           parts2.push_back(std::pair<const PartDefinition *,double >(pd,1.));
81           v2.push_back(ii);
82         }
83     }
84   std::vector<AbstractPoint *> nodes2(_nodes.begin(),_nodes.end());
85   if(!parts.empty())
86     {
87       const PlayGround *pg(pd->getPlayGround());
88       std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > pds(pg->partition(parts));
89       ii=0;
90       for(std::vector<std::size_t>::const_iterator iter=v.begin();iter!=v.end();iter++,ii++)
91         nodes2[*iter]->partitionRegardingDPL(pds[ii],zeMap);
92     }
93   if(!parts2.empty())
94     {
95       const PlayGround *pg(pd->getPlayGround());
96       std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > pds(pg->partition(parts2));
97       ii=0;
98       for(std::vector<std::size_t>::const_iterator iter=v2.begin();iter!=v2.end();iter++,ii++)
99         nodes2[*iter]->partitionRegardingDPL(pds[ii],zeMap);
100     }
101 }
102
103 std::string ForkBlocPoint::getRepr() const
104 {
105   std::size_t sz(_nodes.size()),ii(0);
106   std::string ret("[");
107   std::vector<std::string> elts(sz);
108   for(std::list<AbstractPoint *>::const_iterator it=_nodes.begin();it!=_nodes.end();it++,ii++)
109     elts[ii]=(*it)->getRepr();
110   std::sort(elts.begin(),elts.end());
111   ii=0;
112   for(std::list<AbstractPoint *>::const_iterator it=_nodes.begin();it!=_nodes.end();it++,ii++)
113     {
114       ret+=elts[ii];
115       if(ii!=sz-1)
116         ret+="*";
117     }
118   ret+="]";
119   return ret;
120 }