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