Salome HOME
Updated copyright comment
[modules/yacs.git] / src / engine / ComplexWeight.cxx
1 // Copyright (C) 2006-2024  CEA, EDF
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
21 #include "ComplexWeight.hxx"
22
23 #include "Exception.hxx"
24
25 using namespace YACS::ENGINE;
26
27 #ifdef WIN32
28 #include <algorithm>
29 #endif
30
31 ComplexWeight::ComplexWeight()
32 {
33   setToZero();
34 }
35
36 ComplexWeight::ComplexWeight(double elementaryWeight, double loopWeight, int nbCoresByIteration): _elementaryWeight(elementaryWeight), _bootWeight(false)
37 {
38   _loopWeight.push_back(std::pair<double,int>(loopWeight,nbCoresByIteration));
39 }
40
41 double ComplexWeight::getSimpleLoopWeight() const
42 {
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;
46 }
47
48 double ComplexWeight::calculateTotalLength(int nbOfCoresAllocated) const
49 {
50   if (isDefaultValue())
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++)
56     {
57       if ((*it).second>0)
58         totalWeight+=(*it).first*((double)(*it).second)/((double)nbOfCoresAllocated);
59         }
60   return totalWeight;
61 }
62
63 void ComplexWeight::setLoopWeight(double loopWeight, int nbCoresByIteration) 
64 {
65   bool found(false);
66   int i(0), rankFound(0);
67   _bootWeight=false;
68   for(std::vector<std::pair<double,int> >::const_iterator it=_loopWeight.begin();it!=_loopWeight.end();it++,i++)
69     {
70       if ((*it).second==nbCoresByIteration)
71         {
72           found=true;
73           rankFound=i;
74         }
75         
76     }
77   if (found)
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());  
82 }
83
84 int ComplexWeight::getNbCoresConsoLoopMax() const
85 {
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;
90 }
91
92 void ComplexWeight::max(ComplexWeight &other)
93 {
94   _elementaryWeight=std::max(_elementaryWeight,other._elementaryWeight);
95   
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++)
99     {
100       if (((*it).first>0) && ((*it).second>0))
101         allLoopWeight1+=(*it).first * (*it).second;
102     }
103   for(std::vector< std::pair<double,int> >::const_iterator it=other._loopWeight.begin();it!=other._loopWeight.end();it++)
104     {
105       if (((*it).first>0) && ((*it).second>0))
106         allLoopWeight2+=(*it).first * (*it).second;
107     }
108   if (allLoopWeight2>allLoopWeight1)
109     _loopWeight=other._loopWeight;  
110 }
111
112 void ComplexWeight::addWeight(const ComplexWeight *other)
113 {
114   bool found;
115   if ((!_bootWeight) && ((other->isUnsetLoopWeight() && this->isDefaultValueLoop()) || (this->isUnsetLoopWeight() && other->isDefaultValueLoop())))
116     this->unsetLoop();
117   else
118     {
119           for(std::vector< std::pair<double,int> >::const_iterator it=other->_loopWeight.begin();it!=other->_loopWeight.end();it++)
120             {
121               found=false;
122               for(std::vector< std::pair<double,int> >::iterator it2=_loopWeight.begin();it2!=_loopWeight.end();it2++)  
123                 {
124                   if ((*it).second == (*it2).second)
125                     {
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);
130                       found=true;
131                       continue;
132                     }            
133                 }
134               if ((!found) && ((*it).second!=-1))
135                 this->setLoopWeight((*it).first, (*it).second);
136             }
137           if ((_loopWeight.size()>1) && ((*(_loopWeight.begin())).second==-1))
138             _loopWeight.erase(_loopWeight.begin());
139         }
140
141   if ((!_bootWeight) && ((other->isUnsetElementaryWeight() && this->isDefaultValueElementary()) || (this->isUnsetElementaryWeight() && other->isDefaultValueElementary())))
142     this->unsetElementary();
143   else
144     {
145           if (other->hasValidElementaryWeight())
146             {
147               if (hasValidElementaryWeight())
148                 _elementaryWeight+=other->_elementaryWeight;
149               else
150                 _elementaryWeight=other->_elementaryWeight;
151             }
152     }
153   if (!other->_bootWeight)
154     _bootWeight=false;
155 }
156
157
158
159