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 "PlayGround.hxx"
21 #include "Runtime.hxx"
30 using namespace YACS::ENGINE;
32 std::string PlayGround::printSelf() const
34 std::ostringstream oss;
36 for(std::vector< std::pair<std::string,int> >::const_iterator it=_data.begin();it!=_data.end();it++)
37 sz=std::max(sz,(*it).first.length());
38 for(std::vector< std::pair<std::string,int> >::const_iterator it=_data.begin();it!=_data.end();it++)
40 oss << " - " << std::setw(10) << (*it).first << " : " << (*it).second << std::endl;
45 void PlayGround::loadFromKernelCatalog()
47 Runtime *r(getRuntime());
49 throw Exception("PlayGround::loadFromKernelCatalog : no runtime !");
50 std::vector< std::pair<std::string,int> > data(r->getCatalogOfComputeNodes());
54 void PlayGround::setData(const std::vector< std::pair<std::string,int> >& defOfRes)
60 int PlayGround::getNumberOfCoresAvailable() const
63 for(std::vector< std::pair<std::string,int> >::const_iterator it=_data.begin();it!=_data.end();it++)
68 int PlayGround::getMaxNumberOfContainersCanBeHostedWithoutOverlap(int nbCoresPerCont) const
71 throw Exception("PlayGround::getMaxNumberOfContainersCanBeHostedWithoutOverlap : invalid nbCoresPerCont. Must be >=1 !");
73 for(std::vector< std::pair<std::string,int> >::const_iterator it=_data.begin();it!=_data.end();it++)
74 ret+=(*it).second/nbCoresPerCont;
78 std::vector<int> PlayGround::computeOffsets() const
80 std::size_t sz(_data.size()),i(0);
81 std::vector<int> ret(sz+1); ret[0]=0;
82 for(std::vector< std::pair<std::string,int> >::const_iterator it=_data.begin();it!=_data.end();it++,i++)
83 ret[i+1]=ret[i]+(*it).second;
87 void PlayGround::checkCoherentInfo() const
89 std::set<std::string> s;
90 for(std::vector< std::pair<std::string,int> >::const_iterator it=_data.begin();it!=_data.end();it++)
92 s.insert((*it).first);
94 throw Exception("Presence of negative int value !");
96 if(s.size()!=_data.size())
97 throw Exception("host names entries must be different each other !");
100 std::vector<int> PlayGround::GetIdsMatching(const std::vector<bool>& bigArr, const std::vector<bool>& pat)
102 std::vector<int> ret;
103 std::size_t szp(pat.size());
104 std::size_t sz(bigArr.size()/szp);
105 for(std::size_t i=0;i<sz;i++)
107 std::vector<bool> t(bigArr.begin()+i*szp,bigArr.begin()+(i+1)*szp);
114 std::vector<int> PlayGround::BuildVectOfIdsFromVecBool(const std::vector<bool>& v)
116 std::size_t sz(std::count(v.begin(),v.end(),true)),i(0);
117 std::vector<int> ret(sz);
118 std::vector<bool>::const_iterator it(v.begin());
121 it=std::find(it,v.end(),true);
122 ret[i++]=std::distance(v.begin(),it);
128 void PlayGround::highlightOnIds(const std::vector<int>& coreIds, std::vector<bool>& v) const
130 if(v.size()!=getNumberOfCoresAvailable())
131 throw Exception("PlayGround::highlightOnIds : oops ! invalid size !");
132 for(std::vector<int>::const_iterator it=coreIds.begin();it!=coreIds.end();it++)
137 * you must garantee coherence between PlayGround::deduceMachineFrom, PlayGround::getNumberOfWorkers, and PartDefinition::computeWorkerIdsCovered
139 std::vector<bool> PlayGround::getFetchedCores(int nbCoresPerWorker) const
141 int nbCores(getNumberOfCoresAvailable());
142 std::vector<bool> ret(nbCores,false);
143 if(nbCoresPerWorker==1)
144 std::fill(ret.begin(),ret.end(),true);
147 std::size_t posBg(0);
148 for(std::vector< std::pair<std::string,int> >::const_iterator it=_data.begin();it!=_data.end();it++)
150 int nbElemsToPutOn(((*it).second/nbCoresPerWorker)*nbCoresPerWorker);
151 std::fill(ret.begin()+posBg,ret.begin()+posBg+nbElemsToPutOn,true);
158 * follow getMaxNumberOfContainersCanBeHostedWithoutOverlap method
160 std::vector<std::size_t> PlayGround::getWorkerIdsFullyFetchedBy(int nbCoresPerComp, const std::vector<bool>& coreFlags) const
162 std::size_t posBg(0),posWorker(0);
163 std::vector<std::size_t> ret;
164 for(std::vector< std::pair<std::string,int> >::const_iterator it=_data.begin();it!=_data.end();it++)
166 int nbWorker((*it).second/nbCoresPerComp);
167 for(int j=0;j<nbWorker;j++,posWorker++)
169 std::vector<bool>::const_iterator it2(std::find(coreFlags.begin()+posBg+j*nbCoresPerComp,coreFlags.begin()+posBg+(j+1)*nbCoresPerComp,false));
170 if(it2==coreFlags.begin()+posBg+(j+1)*nbCoresPerComp)
171 ret.push_back(posWorker);
178 std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > PlayGround::partition(const std::vector< std::pair<const PartDefinition *,double> >& parts) const
180 std::size_t sz(parts.size()),szs(getNumberOfCoresAvailable());
182 return std::vector< YACS::BASES::AutoRefCnt<PartDefinition> >();
185 const PartDefinition *pd(parts[0].first);
187 throw Exception("Presence of null pointer as part def 0 !");
188 YACS::BASES::AutoRefCnt<PartDefinition> ret(pd->copy());
189 std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > ret2(1,ret);
193 throw Exception("PlayGround::partition : not implemented yet for more than 31 ! You need to pay for it :)");
194 std::vector<bool> zeArr(szs*sz,false);
196 for(std::vector< std::pair<const PartDefinition *,double> >::const_iterator it=parts.begin();it!=parts.end();it++,i++)
198 const PartDefinition *pd((*it).first);
200 throw Exception("Presence of null pointer as part def !");
201 if(pd->getPlayGround()!=this)
202 throw Exception("Presence of non homogeneous playground !");
204 throw Exception("Invalid weight !");
205 std::vector<bool> bs(pd->getCoresOn());
206 for(std::size_t j=0;j<szs;j++)
209 std::vector< std::vector<int> > retIds(sz);
210 for(std::size_t i=0;i<szs;i++)
212 std::vector<bool> code(zeArr.begin()+i*sz,zeArr.begin()+(i+1)*sz);
213 std::vector<int> locIds(GetIdsMatching(zeArr,code));
214 std::vector<int> partsIds(BuildVectOfIdsFromVecBool(code));
217 std::vector<double> wg;
218 std::vector<int> nbCores2;
219 for(std::vector<int>::const_iterator it=partsIds.begin();it!=partsIds.end();it++)
221 wg.push_back(parts[*it].second);
222 nbCores2.push_back(parts[*it].first->getNbCoresPerCompo());
224 std::vector< std::vector<int> > ress(splitIntoParts(locIds,nbCores2,wg));
226 for(std::vector<int>::const_iterator it=partsIds.begin();it!=partsIds.end();it++,k++)
228 retIds[*it].insert(retIds[*it].end(),ress[k].begin(),ress[k].end());
232 std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > ret(sz);
233 for(std::size_t i=0;i<sz;i++)
235 std::set<int> s(retIds[i].begin(),retIds[i].end());
236 std::vector<int> v(s.begin(),s.end());
237 ret[i]=PartDefinition::BuildFrom(this,(parts[i].first)->getNbCoresPerCompo(),v);
242 std::vector<int> sortArr(const std::vector<int>& v)
244 std::multimap<int,int> m;
246 for(std::vector<int>::const_reverse_iterator it=v.rbegin();it!=v.rend();it++)
247 m.insert(std::pair<int,int>(*it,i--));
248 std::vector<int> ret(m.size());
250 for(std::multimap<int,int>::const_reverse_iterator it=m.rbegin();it!=m.rend();it++)// reverse -> sort from biggest to the finest
251 ret[i++]=(*it).second;
255 std::vector< std::vector<int> > PlayGround::splitIntoParts(const std::vector<int>& coreIds, const std::vector<int>& nbCoresConso, const std::vector<double>& weights) const
257 double wgs(std::accumulate(weights.begin(),weights.end(),0.));
258 std::size_t sz(nbCoresConso.size());
259 if(sz!=weights.size())
260 throw Exception("PlayGround::splitIntoParts : internal error !");
262 return std::vector< std::vector<int> >();
263 int totalSpace(coreIds.size());
264 std::vector< std::vector<int> > ret(sz);
265 std::vector<int> fromBigToTiny(sortArr(nbCoresConso));// start to treat the coarse grain to finish with fine grain
266 std::vector<bool> zeArr(getNumberOfCoresAvailable(),false);
267 highlightOnIds(coreIds,zeArr);
268 int nbOfCoresToSplit(coreIds.size());
269 std::size_t ii(fromBigToTiny.size());
270 for(std::vector<int>::const_iterator it=fromBigToTiny.begin();it!=fromBigToTiny.end();it++,ii--)
272 int maxNbOfCores((int)(totalSpace*weights[*it]/wgs));// now try to find in zeArr at most maxNbOfCores cores
273 ret[*it]=takePlace(maxNbOfCores,nbCoresConso[*it],zeArr,ii==1);
278 std::vector<int> PlayGround::takePlace(int maxNbOfCoresToAlloc, int nbCoresPerShot, std::vector<bool>& distributionOfCores, bool lastOne) const
280 if(maxNbOfCoresToAlloc<1)
281 throw Exception("PlayGround::takePlace : internal error ! no space to alloc !");
282 int tmpMaxNbOfCoresToAlloc(maxNbOfCoresToAlloc);
284 tmpMaxNbOfCoresToAlloc=std::max(tmpMaxNbOfCoresToAlloc,(int)std::count(distributionOfCores.begin(),distributionOfCores.end(),true));
285 std::vector<int> ret;
286 std::vector<int> offsets(computeOffsets());
288 std::size_t sz(offsets.size()-1);
289 for(std::size_t i=0;i<sz && tmpMaxNbOfCoresToAlloc>=nbCoresPerShot;i++)
291 int d(offsets[i+1]-offsets[i]);
294 std::vector<bool> target(nbCoresPerShot,true);
295 for(int j=0;j<=d-nbCoresPerShot && tmpMaxNbOfCoresToAlloc>=nbCoresPerShot;)
297 std::vector<bool> t(distributionOfCores.begin()+offsets[i]+j,distributionOfCores.begin()+offsets[i]+j+nbCoresPerShot);
301 tmpMaxNbOfCoresToAlloc-=nbCoresPerShot;
302 std::fill(distributionOfCores.begin()+offsets[i]+j,distributionOfCores.begin()+offsets[i]+j+nbCoresPerShot,false);
303 for(int k=offsets[i]+j;k<offsets[i]+j+nbCoresPerShot;k++)
313 if(nbCoresPerShot<=1)
314 throw Exception("PlayGround::takePlace : internal error !");
315 // not enough contiguous place. Find the first wider contiguous place
316 for(int kk=std::min(nbCoresPerShot-1,tmpMaxNbOfCoresToAlloc);kk>=1;kk--)
318 for(std::size_t i=0;i<sz && tmpMaxNbOfCoresToAlloc>=kk;i++)
320 int d(offsets[i+1]-offsets[i]);
323 std::vector<bool> target(kk,true);
324 for(int j=0;j<=d-kk && tmpMaxNbOfCoresToAlloc>=kk;)
326 std::vector<bool> t(distributionOfCores.begin()+offsets[i]+j,distributionOfCores.begin()+offsets[i]+j+kk);
330 tmpMaxNbOfCoresToAlloc-=kk;
331 std::fill(distributionOfCores.begin()+offsets[i]+j,distributionOfCores.begin()+offsets[i]+j+kk,false);
332 for(int k=offsets[i]+j;k<offsets[i]+j+kk;k++)
341 throw Exception("PlayGround::takePlace : internal error ! All cores are occupied !");
344 int PlayGround::fromWorkerIdToResId(int workerId, int nbProcPerNode) const
346 std::size_t sz2(_data.size());
347 std::vector<int> deltas(sz2+1); deltas[0]=0;
348 for(std::size_t i=0;i<sz2;i++)
349 deltas[i+1]=deltas[i]+(_data[i].second)/nbProcPerNode;
351 while(zePos<sz2 && (workerId<deltas[zePos] || workerId>=deltas[zePos+1]))
359 * you must garantee coherence between PlayGround::deduceMachineFrom, PlayGround::getNumberOfWorkers, and PartDefinition::computeWorkerIdsCovered
361 std::string PlayGround::deduceMachineFrom(int workerId, int nbProcPerNode) const
363 int zePos(fromWorkerIdToResId(workerId,nbProcPerNode));
364 return _data[zePos].first;
368 * you must garantee coherence between PlayGround::deduceMachineFrom, PlayGround::getNumberOfWorkers, PlayGround::getFetchedCores and PartDefinition::computeWorkerIdsCovered
370 int PlayGround::getNumberOfWorkers(int nbCoresPerWorker) const
372 return getMaxNumberOfContainersCanBeHostedWithoutOverlap(nbCoresPerWorker);
375 PlayGround::~PlayGround()
379 //////////////////////
381 PartDefinition::PartDefinition(const PlayGround *pg, int nbOfCoresPerComp):_nbOfCoresPerComp(nbOfCoresPerComp)
386 PartDefinition::PartDefinition(const PartDefinition& other):_pg(other._pg),_nbOfCoresPerComp(other._nbOfCoresPerComp)
390 PartDefinition::~PartDefinition()
394 std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > PartDefinition::partition(const std::vector< double >& wgs) const
396 std::size_t sz(wgs.size());
397 std::vector< std::pair<const PartDefinition *,double> > elts(sz);
398 for(std::size_t i=0;i<sz;i++)
399 elts[i]=std::pair<const PartDefinition *,double>(this,wgs[i]);
400 return getPlayGround()->partition(elts);
403 YACS::BASES::AutoRefCnt<PartDefinition> PartDefinition::BuildFrom(const PlayGround *pg, int nbOfCoresPerComp, const std::vector<int>& coreIds)
405 int spaceSz(pg->getNumberOfCoresAvailable()),sz(coreIds.size());
407 throw Exception("PartDefinition::BuildFrom : error 1 !");
409 throw Exception("PartDefinition::BuildFrom : error 2 !");
410 int zeStart(coreIds.front()),zeEnd(coreIds.back());
411 if(zeStart<0 || zeEnd<zeStart)
412 throw Exception("PartDefinition::BuildFrom : error ! The content of core Ids is not OK !");
413 for(std::size_t i=0;i<sz-1;i++)
414 if(coreIds[i+1]<coreIds[i])
415 throw Exception("PartDefinition::BuildFrom : error ! The content of core Ids is not OK 2 !");
416 if(zeEnd-zeStart+1!=sz)
418 YACS::BASES::AutoRefCnt<PartDefinition> pd(new NonContigPartDefinition(pg,nbOfCoresPerComp,coreIds));
423 YACS::BASES::AutoRefCnt<PartDefinition> pd(new AllPartDefinition(pg,nbOfCoresPerComp));
426 YACS::BASES::AutoRefCnt<PartDefinition> pd(new ContigPartDefinition(pg,nbOfCoresPerComp,zeStart,zeEnd+1));
430 void PartDefinition::stashPart(int nbCoresStashed, double weightOfRemain, YACS::BASES::AutoRefCnt<PartDefinition>& pdStashed, YACS::BASES::AutoRefCnt<PartDefinition>& pdRemain) const
432 if(nbCoresStashed<=0)
433 throw Exception("stashPart : Invalid nbCoresStashed value !");
434 if(weightOfRemain<=0.)
435 throw Exception("stashPart : Invalid weight !");
436 std::vector<bool> coresOn(getCoresOn());
437 int nbCoresAvailable(std::count(coresOn.begin(),coresOn.end(),true));
438 std::vector<int> ids(PlayGround::BuildVectOfIdsFromVecBool(coresOn));
439 if(nbCoresAvailable==0)
440 throw Exception("PartDefinition::stashPart : no available cores !");
441 if(nbCoresAvailable<=nbCoresStashed)
443 int n0((int)(1./(1.+weightOfRemain)*nbCoresAvailable)); n0=std::max(n0,1);
444 int n1(nbCoresAvailable-n0);
447 pdStashed=PartDefinition::BuildFrom(getPlayGround(),1,ids);
448 pdRemain=PartDefinition::BuildFrom(getPlayGround(),1,ids);
452 std::vector<int> ids0(ids.begin(),ids.begin()+n0),ids1(ids.begin()+n0,ids.end());
453 pdStashed=PartDefinition::BuildFrom(getPlayGround(),1,ids0);
454 pdRemain=PartDefinition::BuildFrom(getPlayGround(),1,ids1);
459 std::vector<int> ids0(ids.begin(),ids.begin()+nbCoresStashed),ids1(ids.begin()+nbCoresStashed,ids.end());
460 pdStashed=PartDefinition::BuildFrom(getPlayGround(),1,ids0);
461 pdRemain=PartDefinition::BuildFrom(getPlayGround(),1,ids1);
466 * you must garantee coherence between PlayGround::deduceMachineFrom, PlayGround::getNumberOfWorkers, and PartDefinition::computeWorkerIdsCovered
468 std::vector<std::size_t> PartDefinition::computeWorkerIdsCovered(int nbCoresPerComp) const
470 std::vector<bool> coresOn(getCoresOn());
471 return _pg->getWorkerIdsFullyFetchedBy(nbCoresPerComp,coresOn);
474 //////////////////////
476 ContigPartDefinition::ContigPartDefinition(const PlayGround *pg, int nbOfCoresPerComp, int zeStart, int zeStop):PartDefinition(pg,nbOfCoresPerComp),_start(zeStart),_stop(zeStop)
478 if(_start<0 || _stop<_start || _stop>getSpaceSize())
479 throw Exception("ContigPartDefinition constructor : Invalid input values");
482 ContigPartDefinition::ContigPartDefinition(const ContigPartDefinition& other):PartDefinition(other),_start(other._start),_stop(other._stop)
486 std::string ContigPartDefinition::printSelf() const
488 std::ostringstream oss;
489 oss << "Contiguous : start=" << _start << " stop=" << _stop;
493 std::vector<bool> ContigPartDefinition::getCoresOn() const
495 std::vector<bool> ret(getSpaceSize(),false);
496 for(int i=_start;i<_stop;i++)
501 ContigPartDefinition *ContigPartDefinition::copy() const
503 return new ContigPartDefinition(*this);
506 int ContigPartDefinition::getNumberOfCoresConsumed() const
511 //////////////////////
513 NonContigPartDefinition::NonContigPartDefinition(const PlayGround *pg, int nbOfCoresPerComp, const std::vector<int>& ids):PartDefinition(pg,nbOfCoresPerComp),_ids(ids)
518 NonContigPartDefinition::NonContigPartDefinition(const ContigPartDefinition& other):PartDefinition(other)
522 std::string NonContigPartDefinition::printSelf() const
524 std::ostringstream oss;
525 oss << "Non contiguous : ";
526 for(std::vector<int>::const_iterator it=_ids.begin();it!=_ids.end();it++)
531 std::vector<bool> NonContigPartDefinition::getCoresOn() const
533 std::vector<bool> ret(getSpaceSize(),false);
534 for(std::vector<int>::const_iterator it=_ids.begin();it!=_ids.end();it++)
539 NonContigPartDefinition *NonContigPartDefinition::copy() const
541 return new NonContigPartDefinition(*this);
544 int NonContigPartDefinition::getNumberOfCoresConsumed() const
549 void NonContigPartDefinition::checkOKIds() const
551 int maxVal(getSpaceSize());
554 int val(_ids.front());
555 if(val<0 || val>=maxVal)
556 throw Exception("checkOKIds : error 2 !");
557 std::size_t sz(_ids.size());
558 for(std::size_t i=0;i<sz-1;i++)
560 if(_ids[i+1]<=_ids[i])
561 throw Exception("checkOKIds : error 1 !");
562 if(_ids[i+1]>=maxVal)
563 throw Exception("checkOKIds : error 3 !");
567 //////////////////////
569 AllPartDefinition::AllPartDefinition(const AllPartDefinition& other):PartDefinition(other)
573 std::string AllPartDefinition::printSelf() const
575 std::ostringstream oss;
580 std::vector<bool> AllPartDefinition::getCoresOn() const
582 std::vector<bool> ret(getSpaceSize(),true);
586 AllPartDefinition *AllPartDefinition::copy() const
588 return new AllPartDefinition(*this);
591 int AllPartDefinition::getNumberOfCoresConsumed() const
593 return getSpaceSize();
596 //////////////////////
598 std::vector<int> ForTestOmlyHPContCls::getIDS() const
600 std::size_t sz(_ids.size());
601 std::vector<int> ret(sz);
602 for(std::size_t i=0;i<sz;i++)