]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/PlayGround.hxx
Salome HOME
WIP
[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     private:
47       std::string _name;
48       int _nbCores;
49       std::vector<bool> _occupied;
50     };
51     
52     
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
59     {
60       const std::vector< Resource > *_vec;
61       std::size_t _num;
62     public:
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(); }
68     };
69     
70     class YACSLIBENGINE_EXPORT PlayGround : public RefCounter
71     {
72     public:
73       PlayGround(const std::vector< std::pair<std::string,int> >& defOfRes):_data(defOfRes.begin(),defOfRes.end()) { checkCoherentInfo(); }
74       PlayGround() { }
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);
91     private:
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;
97     private:
98       void checkCoherentInfo() const;
99     private:
100       ~PlayGround();
101     private:
102       std::vector< Resource > _data;
103     };
104
105     class YACSLIBENGINE_EXPORT PartDefinition : public RefCounter
106     {
107     protected:
108       PartDefinition(const PlayGround *pg);
109       PartDefinition(const PartDefinition& other);
110       virtual ~PartDefinition();
111     public:
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;
122     private:
123       YACS::BASES::AutoConstRefCnt<PlayGround> _pg;
124     };
125
126     class YACSLIBENGINE_EXPORT ContigPartDefinition : public PartDefinition
127     {
128     public:
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;
137     private:
138       ~ContigPartDefinition() { }
139     private:
140       int _start;
141       int _stop;
142     };
143
144     class YACSLIBENGINE_EXPORT NonContigPartDefinition : public PartDefinition
145     {
146     public:
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;
154     private:
155       void checkOKIds() const;
156       ~NonContigPartDefinition() { }
157     private:
158       std::vector<int> _ids;
159     };
160       
161     class YACSLIBENGINE_EXPORT AllPartDefinition : public PartDefinition
162     {
163     public:
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;
170     private:
171       ~AllPartDefinition() { }
172     };
173
174     class YACSLIBENGINE_EXPORT ForTestOmlyHPContCls
175     {
176     public:
177
178 #ifndef SWIG
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; }
182 #endif
183       std::string getContainerType() const { return _container_type; }
184       const PartDefinition *getPD() const { return _pd; }
185       std::vector<int> getIDS() const;
186     private:
187       std::string _container_type;
188       YACS::BASES::AutoConstRefCnt<PartDefinition> _pd;
189       std::vector<std::size_t> _ids;
190     };
191   }
192 }
193
194 #endif