]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/ComplexWeight.cxx
Salome HOME
files forgotten on previous comit
[modules/yacs.git] / src / engine / ComplexWeight.cxx
1 // Copyright (C) 2006-2017  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 "ComplexWeight.hxx"
21
22 #include "Exception.hxx"
23
24 using namespace YACS::ENGINE;
25
26 ComplexWeight::ComplexWeight()
27 {
28   setToZero();
29 }
30
31 ComplexWeight::ComplexWeight(double elementaryWeight, double loopWeight, int nbCoresByIteration): _elementaryWeight(elementaryWeight), _bootWeight(false)
32 {
33   _loopWeight.push_back(std::pair<double,int>(loopWeight,nbCoresByIteration));
34 }
35
36 double ComplexWeight::getSimpleLoopWeight() const
37 {
38   if(_loopWeight.size()!=1)
39     throw Exception("ComplexWeight::getSimpleLoopWeight : can not get loop weight. Node contain multiple loop weights!");
40   return (*(_loopWeight.begin())).first;
41 }
42
43 double ComplexWeight::calculateTotalLength(int nbOfCoresAllocated) const
44 {
45   if (isDefaultValue())
46     throw Exception("ComplexWeight::calculateTotalLength : can not calculate total weight with default value!");
47   double totalWeight(0);
48   if (hasValidElementaryWeight())
49     totalWeight+=_elementaryWeight;
50   for(std::vector< std::pair<double,int> >::const_iterator it=_loopWeight.begin();it!=_loopWeight.end();it++)
51     {
52       if ((*it).second>0)
53         totalWeight+=(*it).first*((double)(*it).second)/((double)nbOfCoresAllocated);
54         }
55   return totalWeight;
56 }
57
58 void ComplexWeight::setLoopWeight(double loopWeight, int nbCoresByIteration) 
59 {
60   bool found(false);
61   int i(0), rankFound(0);
62   _bootWeight=false;
63   for(std::vector<std::pair<double,int> >::const_iterator it=_loopWeight.begin();it!=_loopWeight.end();it++,i++)
64     {
65       if ((*it).second==nbCoresByIteration)
66         {
67           found=true;
68           rankFound=i;
69         }
70         
71     }
72   if (found)
73     _loopWeight.erase(_loopWeight.begin()+rankFound);
74   _loopWeight.push_back(std::pair<double,int>(loopWeight,nbCoresByIteration));
75   if ((_loopWeight.size()>1) && ((*(_loopWeight.begin())).second==-1))
76     _loopWeight.erase(_loopWeight.begin());  
77 }
78
79 int ComplexWeight::getNbCoresConsoLoopMax() const
80 {
81   int nbCoresPerShotLoopMax(0);
82   for(std::vector< std::pair<double,int> >::const_iterator it=_loopWeight.begin();it!=_loopWeight.end();it++)
83     if ((*it).second>nbCoresPerShotLoopMax)nbCoresPerShotLoopMax=(*it).second;
84   return nbCoresPerShotLoopMax;
85 }
86
87 void ComplexWeight::max(ComplexWeight &other)
88 {
89   _elementaryWeight=std::max(_elementaryWeight,other._elementaryWeight);
90   
91   double allLoopWeight1(0);
92   double allLoopWeight2(0);
93   for(std::vector< std::pair<double,int> >::const_iterator it=_loopWeight.begin();it!=_loopWeight.end();it++)
94     {
95       if (((*it).first>0) && ((*it).second>0))
96         allLoopWeight1+=(*it).first * (*it).second;
97     }
98   for(std::vector< std::pair<double,int> >::const_iterator it=other._loopWeight.begin();it!=other._loopWeight.end();it++)
99     {
100       if (((*it).first>0) && ((*it).second>0))
101         allLoopWeight2+=(*it).first * (*it).second;
102     }
103   if (allLoopWeight2>allLoopWeight1)
104     _loopWeight=other._loopWeight;  
105 }
106
107 ComplexWeight& ComplexWeight::addWeight(const ComplexWeight *other)
108 {
109   bool found;
110   if ((!_bootWeight) && ((other->isUnsetLoopWeight() && this->isDefaultValueLoop()) || (this->isUnsetLoopWeight() && other->isDefaultValueLoop())))
111     this->unsetLoop();
112   else
113     {
114           for(std::vector< std::pair<double,int> >::const_iterator it=other->_loopWeight.begin();it!=other->_loopWeight.end();it++)
115             {
116               found=false;
117               for(std::vector< std::pair<double,int> >::iterator it2=_loopWeight.begin();it2!=_loopWeight.end();it2++)  
118                 {
119                   if ((*it).second == (*it2).second)
120                     {
121                       if (((*it2).first>=0) && ((*it).first>=0))
122                         this->setLoopWeight((*it2).first+(*it).first, (*it2).second);
123                       else if ((*it).first>=0)
124                         this->setLoopWeight((*it).first, (*it2).second);
125                       found=true;
126                       continue;
127                     }            
128                 }
129               if ((!found) && ((*it).second!=-1))
130                 this->setLoopWeight((*it).first, (*it).second);
131             }
132           if ((_loopWeight.size()>1) && ((*(_loopWeight.begin())).second==-1))
133             _loopWeight.erase(_loopWeight.begin());
134         }
135
136   if ((!_bootWeight) && ((other->isUnsetElementaryWeight() && this->isDefaultValueElementary()) || (this->isUnsetElementaryWeight() && other->isDefaultValueElementary())))
137     this->unsetElementary();
138   else
139     {
140           if (other->hasValidElementaryWeight())
141             {
142               if (hasValidElementaryWeight())
143                 _elementaryWeight+=other->_elementaryWeight;
144               else
145                 _elementaryWeight=other->_elementaryWeight;
146             }
147     }
148   if (!other->_bootWeight)
149     _bootWeight=false;
150 }
151
152
153
154