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