]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/PlayGround.hxx
Salome HOME
SALOMEHPContainer getNumberOfFreePlace and allocateFor
[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 <map>
31
32 namespace YACS
33 {
34   namespace ENGINE
35   {
36     class PartDefinition;
37
38     class YACSLIBENGINE_EXPORT Resource
39     {
40     public:
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;
48     private:
49       std::string _name;
50       int _nbCores;
51       mutable std::vector<bool> _occupied;
52     };
53     
54     
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
61     {
62       const std::vector< Resource > *_vec;
63       std::size_t _num;
64     public:
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(); }
70     };
71     
72     class YACSLIBENGINE_EXPORT PlayGround : public RefCounter
73     {
74     public:
75       PlayGround(const std::vector< std::pair<std::string,int> >& defOfRes):_data(defOfRes.begin(),defOfRes.end()) { checkCoherentInfo(); }
76       PlayGround() { }
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;
96     private:
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;
102     private:
103       void checkCoherentInfo() const;
104     private:
105       ~PlayGround();
106     private:
107       std::vector< Resource > _data;
108     };
109
110     class YACSLIBENGINE_EXPORT PartDefinition : public RefCounter
111     {
112     protected:
113       PartDefinition(const PlayGround *pg);
114       PartDefinition(const PartDefinition& other);
115       virtual ~PartDefinition();
116     public:
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;
127     private:
128       YACS::BASES::AutoConstRefCnt<PlayGround> _pg;
129     };
130
131     class YACSLIBENGINE_EXPORT ContigPartDefinition : public PartDefinition
132     {
133     public:
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;
142     private:
143       ~ContigPartDefinition() { }
144     private:
145       int _start;
146       int _stop;
147     };
148
149     class YACSLIBENGINE_EXPORT NonContigPartDefinition : public PartDefinition
150     {
151     public:
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;
159     private:
160       void checkOKIds() const;
161       ~NonContigPartDefinition() { }
162     private:
163       std::vector<int> _ids;
164     };
165       
166     class YACSLIBENGINE_EXPORT AllPartDefinition : public PartDefinition
167     {
168     public:
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;
175     private:
176       ~AllPartDefinition() { }
177     };
178
179     class YACSLIBENGINE_EXPORT ForTestOmlyHPContCls
180     {
181     public:
182
183 #ifndef SWIG
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; }
187 #endif
188       std::string getContainerType() const { return _container_type; }
189       const PartDefinition *getPD() const { return _pd; }
190       std::vector<int> getIDS() const;
191     private:
192       std::string _container_type;
193       YACS::BASES::AutoConstRefCnt<PartDefinition> _pd;
194       std::vector<std::size_t> _ids;
195     };
196   }
197 }
198
199 #endif