Salome HOME
Work in progress : workload manager engine test ok
[modules/yacs.git] / src / workloadmanager / DefaultAlgorithm.hxx
1 // Copyright (C) 2020  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 #ifndef ALGORITHMIMPLEMENT_H
20 #define ALGORITHMIMPLEMENT_H
21
22 #include "WorkloadAlgorithm.hxx"
23 #include <set>
24 #include <map>
25 #include <list>
26
27 namespace WorkloadManager
28 {
29 /**
30  * @todo write docs
31  */
32 class DefaultAlgorithm : public WorkloadAlgorithm
33 {
34 public:
35   void addTask(Task* t)override;
36   void addResource(const Resource& r)override;
37   LaunchInfo chooseTask()override;
38   void liberate(const LaunchInfo& info)override;
39   bool empty()const override;
40
41 // ----------------------------- PRIVATE ----------------------------- //
42 private:
43   class ResourceInfoForContainer
44   {
45   public:
46     ResourceInfoForContainer(const Resource& r, const ContainerType& ctype);
47     unsigned int maxContainers()const;
48     unsigned int  alloc();
49     void free(unsigned int index);
50     unsigned int nbRunningContainers()const;
51     bool isContainerRunning(unsigned int index)const;
52     bool operator<(const ResourceInfoForContainer& other)const
53     { return _ctype < other._ctype;}
54     bool operator==(const ContainerType& other)const
55     { return _ctype == other;}
56     const ContainerType& type()const { return _ctype;}
57   private:
58     ContainerType _ctype;
59     const Resource& _resource; // same ref as ResourceLoadInfo
60     std::set<unsigned int> _runningContainers; // 0 to max possible containers on this resource
61     unsigned int _firstFreeContainer;
62   };
63   
64   class ResourceLoadInfo
65   {
66   public:
67     ResourceLoadInfo(const Resource& r);
68     bool isSupported(const ContainerType& ctype)const;
69     bool isAllocPossible(const ContainerType& ctype)const;
70     float cost(const ContainerType& ctype)const;
71     unsigned int alloc(const ContainerType& ctype);
72     void free(const ContainerType& ctype, int index);
73     bool operator<(const ResourceLoadInfo& other)const
74     { return _resource < other._resource;}
75     bool operator==(const Resource& other)const
76     { return _resource == other;}
77     const Resource& resource()const { return _resource;}
78   private:
79     Resource _resource;
80     float _load;
81     std::list<ResourceInfoForContainer> _ctypes;
82   };
83   
84 private:
85   std::list<ResourceLoadInfo> _resources;
86   std::list<Task*> _waitingTasks;
87 };
88 }
89 #endif // ALGORITHMIMPLEMENT_H