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; }
46 std::size_t getNumberOfFreePlace(int nbCoresPerCont) const;
47 std::vector<std::size_t> allocateFor(std::size_t& nbOfPlacesToTake, int nbCoresPerCont) const;
51 mutable std::vector<bool> _occupied;
55 class ResourceIterator : public std::iterator<
56 std::input_iterator_tag, // iterator_category
57 Resource, // value_type
58 long, // difference_type
59 const Resource*, // pointer
60 std::pair<std::string,int> > // reference
62 const std::vector< Resource > *_vec;
65 explicit ResourceIterator(const std::vector< Resource > *vec, const std::size_t num) : _vec(vec),_num(num) { }
66 ResourceIterator& operator++() { _num++; return *this; }
67 bool operator==(ResourceIterator other) const { return _num == other._num; }
68 bool operator!=(ResourceIterator other) const { return !(*this == other); }
69 reference operator*() const { return (*_vec)[_num].toPair(); }
72 class YACSLIBENGINE_EXPORT PlayGround : public RefCounter
75 PlayGround(const std::vector< std::pair<std::string,int> >& defOfRes):_data(defOfRes.begin(),defOfRes.end()) { checkCoherentInfo(); }
77 std::string printSelf() const;
78 void loadFromKernelCatalog();
79 std::vector< std::pair<std::string,int> > getData() const { return std::vector< std::pair<std::string,int> >(ResourceIterator(&_data,0),ResourceIterator(&_data,_data.size())); }
80 void setData(const std::vector< std::pair<std::string,int> >& defOfRes);
81 int getNumberOfCoresAvailable() const;
82 int getMaxNumberOfContainersCanBeHostedWithoutOverlap(int nbCoresPerCont) const;
83 std::vector<int> computeOffsets() const;
84 std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > partition(const std::vector< std::pair<const PartDefinition *, const ComplexWeight *> >& parts, const std::vector<int> &nbCoresPerShot) const;
85 int fromWorkerIdToResId(int workerId, int nbProcPerNode) const;
86 std::string deduceMachineFrom(int workerId, int nbProcPerNode) const;
87 int getNumberOfWorkers(int nbCoresPerWorker) const;
88 void highlightOnIds(const std::vector<int>& coreIds, std::vector<bool>& v) const;
89 std::vector<bool> getFetchedCores(int nbCoresPerWorker) const;
90 std::vector<std::size_t> getWorkerIdsFullyFetchedBy(int nbCoresPerComp, const std::vector<bool>& coreFlags) const;
91 static std::vector<int> BuildVectOfIdsFromVecBool(const std::vector<bool>& v);
92 static std::vector<int> GetIdsMatching(const std::vector<bool>& bigArr, const std::vector<bool>& pat);
93 public:// critical section part
94 std::size_t getNumberOfFreePlace(int nbCoresPerCont) const;
95 std::vector<std::size_t> allocateFor(std::size_t nbOfPlacesToTake, int nbCoresPerCont) const;
97 std::vector< std::pair <const ComplexWeight *, int> > bigToTiny(const std::vector< std::pair <const ComplexWeight *, int> > &weights, std::map<int,int> &saveOrder) const;
98 std::vector< std::vector<int> > backToOriginalOrder(const std::vector< std::vector<int> > &disorderVec, const std::map<int,int> &saveOrder) const;
99 int getCriticalPath(const std::vector<std::pair <const ComplexWeight *, int > >& weights, const std::vector<int>& maxNbOfCores) const;
100 std::vector< std::vector<int> > splitIntoParts(const std::vector<int>& coreIds, const std::vector<std::pair <const ComplexWeight *, int> >& weights) const;
101 std::vector<int> takePlace(int maxNbOfCoresToAlloc, int nbCoresPerShot, std::vector<bool>& distributionOfCores, bool lastOne=false) const;
103 void checkCoherentInfo() const;
107 std::vector< Resource > _data;
110 class YACSLIBENGINE_EXPORT PartDefinition : public RefCounter
113 PartDefinition(const PlayGround *pg);
114 PartDefinition(const PartDefinition& other);
115 virtual ~PartDefinition();
117 //std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > partition(const std::vector< const ComplexWeight *>& wgs) const;
118 static YACS::BASES::AutoRefCnt<PartDefinition> BuildFrom(const PlayGround *pg, const std::vector<int>& coreIds);
119 const PlayGround *getPlayGround() const { return _pg; }
120 int getSpaceSize() const { return _pg->getNumberOfCoresAvailable(); }
121 void stashPart(int nbCoresStashed, double weightOfRemain, YACS::BASES::AutoRefCnt<PartDefinition>& pdStashed, YACS::BASES::AutoRefCnt<PartDefinition>& pdRemain) const;
122 std::vector<std::size_t> computeWorkerIdsCovered(int nbCoresPerComp) const;
123 virtual std::string printSelf() const = 0;
124 virtual std::vector<bool> getCoresOn() const = 0;
125 virtual PartDefinition *copy() const = 0;
126 virtual int getNumberOfCoresConsumed() const = 0;
128 YACS::BASES::AutoConstRefCnt<PlayGround> _pg;
131 class YACSLIBENGINE_EXPORT ContigPartDefinition : public PartDefinition
134 ContigPartDefinition(const PlayGround *pg, int zeStart, int zeStop);
135 ContigPartDefinition(const ContigPartDefinition& other);
136 std::string printSelf() const;
137 std::vector<bool> getCoresOn() const;
138 int getStart() const { return _start; }
139 int getStop() const { return _stop; }
140 ContigPartDefinition *copy() const;
141 int getNumberOfCoresConsumed() const;
143 ~ContigPartDefinition() { }
149 class YACSLIBENGINE_EXPORT NonContigPartDefinition : public PartDefinition
152 NonContigPartDefinition(const PlayGround *pg, const std::vector<int>& ids);
153 NonContigPartDefinition(const ContigPartDefinition& other);
154 std::string printSelf() const;
155 std::vector<bool> getCoresOn() const;
156 std::vector<int> getIDs() const { return _ids; }
157 NonContigPartDefinition *copy() const;
158 int getNumberOfCoresConsumed() const;
160 void checkOKIds() const;
161 ~NonContigPartDefinition() { }
163 std::vector<int> _ids;
166 class YACSLIBENGINE_EXPORT AllPartDefinition : public PartDefinition
169 AllPartDefinition(const PlayGround *pg):PartDefinition(pg) { }
170 AllPartDefinition(const AllPartDefinition& other);
171 std::string printSelf() const;
172 std::vector<bool> getCoresOn() const;
173 AllPartDefinition *copy() const;
174 int getNumberOfCoresConsumed() const;
176 ~AllPartDefinition() { }
179 class YACSLIBENGINE_EXPORT ForTestOmlyHPContCls
184 void setContainerType(const std::string& ct) { _container_type=ct; }
185 void setPD(YACS::BASES::AutoConstRefCnt<PartDefinition> pd) { _pd=pd; }
186 void setIDS(const std::vector<std::size_t>& ids) { _ids=ids; }
188 std::string getContainerType() const { return _container_type; }
189 const PartDefinition *getPD() const { return _pd; }
190 std::vector<int> getIDS() const;
192 std::string _container_type;
193 YACS::BASES::AutoConstRefCnt<PartDefinition> _pd;
194 std::vector<std::size_t> _ids;