]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/PlayGround.hxx
Salome HOME
Before virtualization of SalomeContainerTools
[modules/yacs.git] / src / engine / PlayGround.hxx
1 // Copyright (C) 2006-2019  CEA/DEN, EDF R&D
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 #ifndef __PLAYGROUND_HXX__
21 #define __PLAYGROUND_HXX__
22
23 #include "YACSlibEngineExport.hxx"
24 #include "RefCounter.hxx"
25 #include "AutoRefCnt.hxx"
26 #include "ComplexWeight.hxx"
27
28 #include <vector>
29 #include <string>
30 #include <mutex>
31 #include <map>
32
33 namespace YACS
34 {
35   namespace ENGINE
36   {
37     class PartDefinition;
38
39     class YACSLIBENGINE_EXPORT Resource
40     {
41     public:
42       Resource(const std::string& name, int nbCores):_name(name),_nbCores(nbCores),_occupied(_nbCores,false) { }
43       Resource(const std::pair<std::string,int>& p):_name(p.first),_nbCores(p.second),_occupied(_nbCores,false) { }
44       std::pair<std::string,int> toPair() const { return {_name,_nbCores}; }
45       int nbCores() const { return _nbCores; }
46       std::string name() const { return _name; }
47       std::size_t getNumberOfFreePlace(int nbCoresPerCont) const;
48       std::vector<std::size_t> allocateFor(std::size_t& nbOfPlacesToTake, int nbCoresPerCont) const;
49       void release(std::size_t workerId, int nbCoresPerCont) const;
50       std::size_t getNumberOfWorkers(int nbCoresPerCont) const;
51     private:
52       std::string _name;
53       int _nbCores;
54       mutable std::vector<bool> _occupied;
55     };
56     
57     
58     class ResourceIterator : public std::iterator<
59                         std::input_iterator_tag,     // iterator_category
60                         Resource,                    // value_type
61                         long,                        // difference_type
62                         const Resource*,             // pointer
63                         std::pair<std::string,int> > // reference
64     {
65       const std::vector< Resource > *_vec;
66       std::size_t _num;
67     public:
68       explicit ResourceIterator(const std::vector< Resource > *vec, const std::size_t num) : _vec(vec),_num(num) { }
69       ResourceIterator& operator++() { _num++; return *this; }
70       bool operator==(ResourceIterator other) const { return _num == other._num; }
71       bool operator!=(ResourceIterator other) const { return !(*this == other); }
72       reference operator*() const { return (*_vec)[_num].toPair(); }
73     };
74     
75     class YACSLIBENGINE_EXPORT PlayGround : public RefCounter
76     {
77     public:
78       PlayGround(const std::vector< std::pair<std::string,int> >& defOfRes):_data(defOfRes.begin(),defOfRes.end()) { checkCoherentInfo(); }
79       PlayGround() { }
80       std::string printSelf() const;
81       void loadFromKernelCatalog();
82       std::vector< std::pair<std::string,int> > getData() const { return std::vector< std::pair<std::string,int> >(ResourceIterator(&_data,0),ResourceIterator(&_data,_data.size())); }
83       void setData(const std::vector< std::pair<std::string,int> >& defOfRes);
84       int getNumberOfCoresAvailable() const;
85       int getMaxNumberOfContainersCanBeHostedWithoutOverlap(int nbCoresPerCont) const;
86       std::vector<int> computeOffsets() const;
87       std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > partition(const std::vector< std::pair<const PartDefinition *, const ComplexWeight *> >& parts, const std::vector<int> &nbCoresPerShot) const;
88       int fromWorkerIdToResId(int workerId, int nbProcPerNode) const;
89       std::string deduceMachineFrom(int workerId, int nbProcPerNode) const;
90       int getNumberOfWorkers(int nbCoresPerWorker) const;
91       void highlightOnIds(const std::vector<int>& coreIds, std::vector<bool>& v) const;
92       std::vector<bool> getFetchedCores(int nbCoresPerWorker) const;
93       std::vector<std::size_t> getWorkerIdsFullyFetchedBy(int nbCoresPerComp, const std::vector<bool>& coreFlags) const;
94       static std::vector<int> BuildVectOfIdsFromVecBool(const std::vector<bool>& v);
95       static std::vector<int> GetIdsMatching(const std::vector<bool>& bigArr, const std::vector<bool>& pat);
96     public:// critical section part
97       std::size_t getNumberOfFreePlace(int nbCoresPerCont) const;
98       std::vector<std::size_t> allocateFor(std::size_t nbOfPlacesToTake, int nbCoresPerCont) const;
99       void release(std::size_t workerId, int nbCoresPerCont) const;
100       std::mutex& getLocker() const { return _locker; }
101     private:
102       std::vector< std::pair <const ComplexWeight *, int> > bigToTiny(const std::vector< std::pair <const ComplexWeight *, int> > &weights, std::map<int,int> &saveOrder) const;
103           std::vector< std::vector<int> > backToOriginalOrder(const std::vector< std::vector<int> > &disorderVec, const std::map<int,int> &saveOrder) const;
104           int getCriticalPath(const std::vector<std::pair <const ComplexWeight *, int > >& weights, const std::vector<int>& maxNbOfCores) const;    
105       std::vector< std::vector<int> > splitIntoParts(const std::vector<int>& coreIds, const std::vector<std::pair <const ComplexWeight *, int> >& weights) const;
106       std::vector<int> takePlace(int maxNbOfCoresToAlloc, int nbCoresPerShot, std::vector<bool>& distributionOfCores, bool lastOne=false) const;
107     private:
108       void checkCoherentInfo() const;
109     private:
110       ~PlayGround();
111     private:
112       mutable std::mutex _locker;
113       std::vector< Resource > _data;
114     };
115
116     class YACSLIBENGINE_EXPORT PartDefinition : public RefCounter
117     {
118     protected:
119       PartDefinition(const PlayGround *pg);
120       PartDefinition(const PartDefinition& other);
121       virtual ~PartDefinition();
122     public:
123       //std::vector< YACS::BASES::AutoRefCnt<PartDefinition> > partition(const std::vector< const ComplexWeight *>& wgs) const;
124       static YACS::BASES::AutoRefCnt<PartDefinition> BuildFrom(const PlayGround *pg, const std::vector<int>& coreIds);
125       const PlayGround *getPlayGround() const { return _pg; }
126       int getSpaceSize() const { return _pg->getNumberOfCoresAvailable(); }
127       void stashPart(int nbCoresStashed, double weightOfRemain, YACS::BASES::AutoRefCnt<PartDefinition>& pdStashed, YACS::BASES::AutoRefCnt<PartDefinition>& pdRemain) const;
128       std::vector<std::size_t> computeWorkerIdsCovered(int nbCoresPerComp) const;
129       virtual std::string printSelf() const = 0;
130       virtual std::vector<bool> getCoresOn() const = 0;
131       virtual PartDefinition *copy() const = 0;
132       virtual int getNumberOfCoresConsumed() const = 0;
133     private:
134       YACS::BASES::AutoConstRefCnt<PlayGround> _pg;
135     };
136
137     class YACSLIBENGINE_EXPORT ContigPartDefinition : public PartDefinition
138     {
139     public:
140       ContigPartDefinition(const PlayGround *pg, int zeStart, int zeStop);
141       ContigPartDefinition(const ContigPartDefinition& other);
142       std::string printSelf() const;
143       std::vector<bool> getCoresOn() const;
144       int getStart() const { return _start; }
145       int getStop() const { return _stop; }
146       ContigPartDefinition *copy() const;
147       int getNumberOfCoresConsumed() const;
148     private:
149       ~ContigPartDefinition() { }
150     private:
151       int _start;
152       int _stop;
153     };
154
155     class YACSLIBENGINE_EXPORT NonContigPartDefinition : public PartDefinition
156     {
157     public:
158       NonContigPartDefinition(const PlayGround *pg, const std::vector<int>& ids);
159       NonContigPartDefinition(const ContigPartDefinition& other);
160       std::string printSelf() const;
161       std::vector<bool> getCoresOn() const;
162       std::vector<int> getIDs() const { return _ids; }
163       NonContigPartDefinition *copy() const;
164       int getNumberOfCoresConsumed() const;
165     private:
166       void checkOKIds() const;
167       ~NonContigPartDefinition() { }
168     private:
169       std::vector<int> _ids;
170     };
171       
172     class YACSLIBENGINE_EXPORT AllPartDefinition : public PartDefinition
173     {
174     public:
175       AllPartDefinition(const PlayGround *pg):PartDefinition(pg) { }
176       AllPartDefinition(const AllPartDefinition& other);
177       std::string printSelf() const;
178       std::vector<bool> getCoresOn() const;
179       AllPartDefinition *copy() const;
180       int getNumberOfCoresConsumed() const;
181     private:
182       ~AllPartDefinition() { }
183     };
184
185     class YACSLIBENGINE_EXPORT ForTestOmlyHPContCls
186     {
187     public:
188
189 #ifndef SWIG
190       void setContainerType(const std::string& ct) { _container_type=ct; }
191       void setPD(YACS::BASES::AutoConstRefCnt<PartDefinition> pd) { _pd=pd; }
192       void setIDS(const std::vector<std::size_t>& ids) { _ids=ids; }
193 #endif
194       std::string getContainerType() const { return _container_type; }
195       const PartDefinition *getPD() const { return _pd; }
196       std::vector<int> getIDS() const;
197     private:
198       std::string _container_type;
199       YACS::BASES::AutoConstRefCnt<PartDefinition> _pd;
200       std::vector<std::size_t> _ids;
201     };
202   }
203 }
204
205 #endif