1 // Copyright (C) 2006-2019 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 #ifndef __PLAYGROUND_HXX__
21 #define __PLAYGROUND_HXX__
23 #include "YACSlibEngineExport.hxx"
24 #include "RefCounter.hxx"
25 #include "AutoRefCnt.hxx"
26 #include "ComplexWeight.hxx"
38 class YACSLIBENGINE_EXPORT Resource
41 Resource(const std::string& name, int nbCores):_name(name),_nbCores(nbCores),_occupied(_nbCores,false) { }
42 Resource(const std::pair<std::string,int>& p):_name(p.first),_nbCores(p.second),_occupied(_nbCores,false) { }
43 std::pair<std::string,int> toPair() const { return {_name,_nbCores}; }
44 int nbCores() const { return _nbCores; }
45 std::string name() const { return _name; }
49 std::vector<bool> _occupied;
53 class ResourceIterator : public std::iterator<
54 std::input_iterator_tag, // iterator_category
55 Resource, // value_type
56 long, // difference_type
57 const Resource*, // pointer
58 std::pair<std::string,int> > // reference
60 const std::vector< Resource > *_vec;
63 explicit ResourceIterator(const std::vector< Resource > *vec, const std::size_t num) : _vec(vec),_num(num) { }
64 ResourceIterator& operator++() { _num++; return *this; }
65 bool operator==(ResourceIterator other) const { return _num == other._num; }
66 bool operator!=(ResourceIterator other) const { return !(*this == other); }
67 reference operator*() const { return (*_vec)[_num].toPair(); }
70 class YACSLIBENGINE_EXPORT PlayGround : public RefCounter
73 PlayGround(const std::vector< std::pair<std::string,int> >& defOfRes):_data(defOfRes.begin(),defOfRes.end()) { checkCoherentInfo(); }
75 std::string printSelf() const;
76 void loadFromKernelCatalog();
77 std::vector< std::pair<std::string,int> > getData() const { return std::vector< std::pair<std::string,int> >(ResourceIterator(&_data,0),ResourceIterator(&_data,_data.size())); }
78 void setData(const std::vector< std::pair<std::string,int> >& defOfRes);
79 int getNumberOfCoresAvailable() const;
80 int getMaxNumberOfContainersCanBeHostedWithoutOverlap(int nbCoresPerCont) const;
81 std::vector<int> computeOffsets() const;
82 std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > partition(const std::vector< std::pair<const PartDefinition *, const ComplexWeight *> >& parts, const std::vector<int> &nbCoresPerShot) const;
83 int fromWorkerIdToResId(int workerId, int nbProcPerNode) const;
84 std::string deduceMachineFrom(int workerId, int nbProcPerNode) const;
85 int getNumberOfWorkers(int nbCoresPerWorker) const;
86 void highlightOnIds(const std::vector<int>& coreIds, std::vector<bool>& v) const;
87 std::vector<bool> getFetchedCores(int nbCoresPerWorker) const;
88 std::vector<std::size_t> getWorkerIdsFullyFetchedBy(int nbCoresPerComp, const std::vector<bool>& coreFlags) const;
89 static std::vector<int> BuildVectOfIdsFromVecBool(const std::vector<bool>& v);
90 static std::vector<int> GetIdsMatching(const std::vector<bool>& bigArr, const std::vector<bool>& pat);
92 std::vector< std::pair <const ComplexWeight *, int> > bigToTiny(const std::vector< std::pair <const ComplexWeight *, int> > &weights, std::map<int,int> &saveOrder) const;
93 std::vector< std::vector<int> > backToOriginalOrder(const std::vector< std::vector<int> > &disorderVec, const std::map<int,int> &saveOrder) const;
94 int getCriticalPath(const std::vector<std::pair <const ComplexWeight *, int > >& weights, const std::vector<int>& maxNbOfCores) const;
95 std::vector< std::vector<int> > splitIntoParts(const std::vector<int>& coreIds, const std::vector<std::pair <const ComplexWeight *, int> >& weights) const;
96 std::vector<int> takePlace(int maxNbOfCoresToAlloc, int nbCoresPerShot, std::vector<bool>& distributionOfCores, bool lastOne=false) const;
98 void checkCoherentInfo() const;
102 std::vector< Resource > _data;
105 class YACSLIBENGINE_EXPORT PartDefinition : public RefCounter
108 PartDefinition(const PlayGround *pg);
109 PartDefinition(const PartDefinition& other);
110 virtual ~PartDefinition();
112 //std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > partition(const std::vector< const ComplexWeight *>& wgs) const;
113 static YACS::BASES::AutoRefCnt<PartDefinition> BuildFrom(const PlayGround *pg, const std::vector<int>& coreIds);
114 const PlayGround *getPlayGround() const { return _pg; }
115 int getSpaceSize() const { return _pg->getNumberOfCoresAvailable(); }
116 void stashPart(int nbCoresStashed, double weightOfRemain, YACS::BASES::AutoRefCnt<PartDefinition>& pdStashed, YACS::BASES::AutoRefCnt<PartDefinition>& pdRemain) const;
117 std::vector<std::size_t> computeWorkerIdsCovered(int nbCoresPerComp) const;
118 virtual std::string printSelf() const = 0;
119 virtual std::vector<bool> getCoresOn() const = 0;
120 virtual PartDefinition *copy() const = 0;
121 virtual int getNumberOfCoresConsumed() const = 0;
123 YACS::BASES::AutoConstRefCnt<PlayGround> _pg;
126 class YACSLIBENGINE_EXPORT ContigPartDefinition : public PartDefinition
129 ContigPartDefinition(const PlayGround *pg, int zeStart, int zeStop);
130 ContigPartDefinition(const ContigPartDefinition& other);
131 std::string printSelf() const;
132 std::vector<bool> getCoresOn() const;
133 int getStart() const { return _start; }
134 int getStop() const { return _stop; }
135 ContigPartDefinition *copy() const;
136 int getNumberOfCoresConsumed() const;
138 ~ContigPartDefinition() { }
144 class YACSLIBENGINE_EXPORT NonContigPartDefinition : public PartDefinition
147 NonContigPartDefinition(const PlayGround *pg, const std::vector<int>& ids);
148 NonContigPartDefinition(const ContigPartDefinition& other);
149 std::string printSelf() const;
150 std::vector<bool> getCoresOn() const;
151 std::vector<int> getIDs() const { return _ids; }
152 NonContigPartDefinition *copy() const;
153 int getNumberOfCoresConsumed() const;
155 void checkOKIds() const;
156 ~NonContigPartDefinition() { }
158 std::vector<int> _ids;
161 class YACSLIBENGINE_EXPORT AllPartDefinition : public PartDefinition
164 AllPartDefinition(const PlayGround *pg):PartDefinition(pg) { }
165 AllPartDefinition(const AllPartDefinition& other);
166 std::string printSelf() const;
167 std::vector<bool> getCoresOn() const;
168 AllPartDefinition *copy() const;
169 int getNumberOfCoresConsumed() const;
171 ~AllPartDefinition() { }
174 class YACSLIBENGINE_EXPORT ForTestOmlyHPContCls
179 void setContainerType(const std::string& ct) { _container_type=ct; }
180 void setPD(YACS::BASES::AutoConstRefCnt<PartDefinition> pd) { _pd=pd; }
181 void setIDS(const std::vector<std::size_t>& ids) { _ids=ids; }
183 std::string getContainerType() const { return _container_type; }
184 const PartDefinition *getPD() const { return _pd; }
185 std::vector<int> getIDS() const;
187 std::string _container_type;
188 YACS::BASES::AutoConstRefCnt<PartDefinition> _pd;
189 std::vector<std::size_t> _ids;