1 // Copyright (C) 2006-2021 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
21 #include "ComplexWeight.hxx"
23 #include "Exception.hxx"
25 using namespace YACS::ENGINE;
31 ComplexWeight::ComplexWeight()
36 ComplexWeight::ComplexWeight(double elementaryWeight, double loopWeight, int nbCoresByIteration): _elementaryWeight(elementaryWeight), _bootWeight(false)
38 _loopWeight.push_back(std::pair<double,int>(loopWeight,nbCoresByIteration));
41 double ComplexWeight::getSimpleLoopWeight() const
43 if(_loopWeight.size()!=1)
44 throw Exception("ComplexWeight::getSimpleLoopWeight : can not get loop weight. Node contain multiple loop weights!");
45 return (*(_loopWeight.begin())).first;
48 double ComplexWeight::calculateTotalLength(int nbOfCoresAllocated) const
51 throw Exception("ComplexWeight::calculateTotalLength : can not calculate total weight with default value!");
52 double totalWeight(0);
53 if (hasValidElementaryWeight())
54 totalWeight+=_elementaryWeight;
55 for(std::vector< std::pair<double,int> >::const_iterator it=_loopWeight.begin();it!=_loopWeight.end();it++)
58 totalWeight+=(*it).first*((double)(*it).second)/((double)nbOfCoresAllocated);
63 void ComplexWeight::setLoopWeight(double loopWeight, int nbCoresByIteration)
66 int i(0), rankFound(0);
68 for(std::vector<std::pair<double,int> >::const_iterator it=_loopWeight.begin();it!=_loopWeight.end();it++,i++)
70 if ((*it).second==nbCoresByIteration)
78 _loopWeight.erase(_loopWeight.begin()+rankFound);
79 _loopWeight.push_back(std::pair<double,int>(loopWeight,nbCoresByIteration));
80 if ((_loopWeight.size()>1) && ((*(_loopWeight.begin())).second==-1))
81 _loopWeight.erase(_loopWeight.begin());
84 int ComplexWeight::getNbCoresConsoLoopMax() const
86 int nbCoresPerShotLoopMax(0);
87 for(std::vector< std::pair<double,int> >::const_iterator it=_loopWeight.begin();it!=_loopWeight.end();it++)
88 if ((*it).second>nbCoresPerShotLoopMax)nbCoresPerShotLoopMax=(*it).second;
89 return nbCoresPerShotLoopMax;
92 void ComplexWeight::max(ComplexWeight &other)
94 _elementaryWeight=std::max(_elementaryWeight,other._elementaryWeight);
96 double allLoopWeight1(0);
97 double allLoopWeight2(0);
98 for(std::vector< std::pair<double,int> >::const_iterator it=_loopWeight.begin();it!=_loopWeight.end();it++)
100 if (((*it).first>0) && ((*it).second>0))
101 allLoopWeight1+=(*it).first * (*it).second;
103 for(std::vector< std::pair<double,int> >::const_iterator it=other._loopWeight.begin();it!=other._loopWeight.end();it++)
105 if (((*it).first>0) && ((*it).second>0))
106 allLoopWeight2+=(*it).first * (*it).second;
108 if (allLoopWeight2>allLoopWeight1)
109 _loopWeight=other._loopWeight;
112 void ComplexWeight::addWeight(const ComplexWeight *other)
115 if ((!_bootWeight) && ((other->isUnsetLoopWeight() && this->isDefaultValueLoop()) || (this->isUnsetLoopWeight() && other->isDefaultValueLoop())))
119 for(std::vector< std::pair<double,int> >::const_iterator it=other->_loopWeight.begin();it!=other->_loopWeight.end();it++)
122 for(std::vector< std::pair<double,int> >::iterator it2=_loopWeight.begin();it2!=_loopWeight.end();it2++)
124 if ((*it).second == (*it2).second)
126 if (((*it2).first>=0) && ((*it).first>=0))
127 this->setLoopWeight((*it2).first+(*it).first, (*it2).second);
128 else if ((*it).first>=0)
129 this->setLoopWeight((*it).first, (*it2).second);
134 if ((!found) && ((*it).second!=-1))
135 this->setLoopWeight((*it).first, (*it).second);
137 if ((_loopWeight.size()>1) && ((*(_loopWeight.begin())).second==-1))
138 _loopWeight.erase(_loopWeight.begin());
141 if ((!_bootWeight) && ((other->isUnsetElementaryWeight() && this->isDefaultValueElementary()) || (this->isUnsetElementaryWeight() && other->isDefaultValueElementary())))
142 this->unsetElementary();
145 if (other->hasValidElementaryWeight())
147 if (hasValidElementaryWeight())
148 _elementaryWeight+=other->_elementaryWeight;
150 _elementaryWeight=other->_elementaryWeight;
153 if (!other->_bootWeight)