1 // Copyright (C) 2006-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ComplexWeight.hxx"
22 #include "Exception.hxx"
24 using namespace YACS::ENGINE;
26 ComplexWeight::ComplexWeight()
31 ComplexWeight::ComplexWeight(double elementaryWeight, double loopWeight, int nbCoresByIteration): _elementaryWeight(elementaryWeight), _bootWeight(false)
33 _loopWeight.push_back(std::pair<double,int>(loopWeight,nbCoresByIteration));
36 double ComplexWeight::getSimpleLoopWeight() const
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;
43 double ComplexWeight::calculateTotalLength(int nbOfCoresAllocated) const
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++)
53 totalWeight+=(*it).first*((double)(*it).second)/((double)nbOfCoresAllocated);
58 void ComplexWeight::setLoopWeight(double loopWeight, int nbCoresByIteration)
61 int i(0), rankFound(0);
63 for(std::vector<std::pair<double,int> >::const_iterator it=_loopWeight.begin();it!=_loopWeight.end();it++,i++)
65 if ((*it).second==nbCoresByIteration)
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());
79 int ComplexWeight::getNbCoresConsoLoopMax() const
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;
87 void ComplexWeight::max(ComplexWeight &other)
89 _elementaryWeight=std::max(_elementaryWeight,other._elementaryWeight);
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++)
95 if (((*it).first>0) && ((*it).second>0))
96 allLoopWeight1+=(*it).first * (*it).second;
98 for(std::vector< std::pair<double,int> >::const_iterator it=other._loopWeight.begin();it!=other._loopWeight.end();it++)
100 if (((*it).first>0) && ((*it).second>0))
101 allLoopWeight2+=(*it).first * (*it).second;
103 if (allLoopWeight2>allLoopWeight1)
104 _loopWeight=other._loopWeight;
107 ComplexWeight& ComplexWeight::addWeight(const ComplexWeight *other)
110 if ((!_bootWeight) && ((other->isUnsetLoopWeight() && this->isDefaultValueLoop()) || (this->isUnsetLoopWeight() && other->isDefaultValueLoop())))
114 for(std::vector< std::pair<double,int> >::const_iterator it=other->_loopWeight.begin();it!=other->_loopWeight.end();it++)
117 for(std::vector< std::pair<double,int> >::iterator it2=_loopWeight.begin();it2!=_loopWeight.end();it2++)
119 if ((*it).second == (*it2).second)
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);
129 if ((!found) && ((*it).second!=-1))
130 this->setLoopWeight((*it).first, (*it).second);
132 if ((_loopWeight.size()>1) && ((*(_loopWeight.begin())).second==-1))
133 _loopWeight.erase(_loopWeight.begin());
136 if ((!_bootWeight) && ((other->isUnsetElementaryWeight() && this->isDefaultValueElementary()) || (this->isUnsetElementaryWeight() && other->isDefaultValueElementary())))
137 this->unsetElementary();
140 if (other->hasValidElementaryWeight())
142 if (hasValidElementaryWeight())
143 _elementaryWeight+=other->_elementaryWeight;
145 _elementaryWeight=other->_elementaryWeight;
148 if (!other->_bootWeight)