Salome HOME
Reduced dependencies on Launcher part. Removed SALOME_LAUNCHER_ONLY mode.
[modules/kernel.git] / src / ResourcesManager / SALOME_ResourcesCatalog_Parser.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SALOME_ResourcesCatalog_Parser.hxx"
24 #include <iostream>
25 #include <sstream>
26
27 using namespace std;
28
29 #define NULL_VALUE 0
30
31 unsigned int ResourceDataToSort::_nbOfProcWanted = NULL_VALUE;
32 unsigned int ResourceDataToSort::_nbOfNodesWanted = NULL_VALUE;
33 unsigned int ResourceDataToSort::_nbOfProcPerNodeWanted = NULL_VALUE;
34 unsigned int ResourceDataToSort::_CPUFreqMHzWanted = NULL_VALUE;
35 unsigned int ResourceDataToSort::_memInMBWanted = NULL_VALUE;
36
37 ResourceDataToSort::ResourceDataToSort()
38 {}
39
40 ResourceDataToSort::ResourceDataToSort(const std::string& name,
41                                        unsigned int nbOfNodes,
42                                        unsigned int nbOfProcPerNode,
43                                        unsigned int CPUFreqMHz,
44                                        unsigned int memInMB):
45     _Name(name),
46     _nbOfNodes(nbOfNodes),
47     _nbOfProcPerNode(nbOfProcPerNode),
48     _CPUFreqMHz(CPUFreqMHz),
49     _memInMB(memInMB)
50 {}
51
52 //! Method used by list::sort to sort the resources used in SALOME_ResourcesManager::GetResourcesFitting
53 bool ResourceDataToSort::operator< (const ResourceDataToSort& other) const
54   {
55     unsigned int nbPts = GetNumberOfPoints();
56     return nbPts < other.GetNumberOfPoints();
57   }
58
59 unsigned int ResourceDataToSort::GetNumberOfPoints() const
60   {
61     unsigned int ret = 0;
62     //priority 0 : Nb of proc
63
64     if (_nbOfProcWanted != NULL_VALUE)
65       {
66         unsigned int nb_proc = _nbOfNodes * _nbOfProcPerNode;
67         if (nb_proc == _nbOfProcWanted)
68           ret += 30000;
69         else if (nb_proc > _nbOfProcWanted)
70           ret += 20000;
71         else
72           ret += 10000;
73       }
74
75     //priority 1 : Nb of nodes
76
77     if (_nbOfNodesWanted != NULL_VALUE)
78       {
79         if (_nbOfNodes == _nbOfNodesWanted)
80           ret += 3000;
81         else if (_nbOfNodes > _nbOfNodesWanted)
82           ret += 2000;
83         else
84           ret += 1000;
85       }
86
87     //priority 2 : Nb of proc by node
88     if (_nbOfProcPerNodeWanted != NULL_VALUE)
89       {
90         if (_nbOfProcPerNode == _nbOfProcPerNodeWanted)
91           ret += 300;
92         else if (_nbOfProcPerNode > _nbOfProcPerNodeWanted)
93           ret += 200;
94         else
95           ret += 100;
96       }
97
98     //priority 3 : Cpu freq
99     if (_CPUFreqMHzWanted != NULL_VALUE)
100       {
101         if (_CPUFreqMHz == _CPUFreqMHzWanted)
102           ret += 30;
103         else if (_CPUFreqMHz > _CPUFreqMHzWanted)
104           ret += 20;
105         else
106           ret += 10;
107       }
108
109     //priority 4 : memory
110     if (_memInMBWanted != NULL_VALUE)
111       {
112         if (_memInMB == _memInMBWanted)
113           ret += 3;
114         else if (_memInMB > _memInMBWanted)
115           ret += 2;
116         else
117           ret += 1;
118       }
119
120     //RES_MESSAGE("[GetNumberOfPoints] points number for resource: " << _Name << " " << ret);
121     return ret;
122   }
123
124 //! Method used for debug
125 void ResourceDataToSort::Print() const
126   {
127     std::cout << _nbOfNodes << std::endl;
128     std::cout << _nbOfProcPerNode << std::endl;
129     std::cout << _CPUFreqMHz << std::endl;
130     std::cout << _memInMB << std::endl;
131   }
132
133
134 ParserResourcesType::ParserResourcesType()
135 : type(single_machine),
136   Protocol(ssh),
137   ClusterInternalProtocol(ssh),
138   Batch(none),
139   mpi(nompi),
140   nbOfProc(1),
141   can_launch_batch_jobs(false),
142   can_run_containers(false)
143 {
144   DataForSort._Name = "";
145   DataForSort._nbOfNodes = 1;
146   DataForSort._nbOfProcPerNode = 1;
147   DataForSort._CPUFreqMHz = 0;
148   DataForSort._memInMB = 0;
149 }
150
151 ParserResourcesType::~ParserResourcesType()
152 {
153 }
154
155 std::string ParserResourcesType::protocolToString(AccessProtocolType protocol)
156 {
157   switch (protocol)
158   {
159   case sh:
160     return "sh";
161   case rsh:
162     return "rsh";
163   case ssh:
164     return "ssh";
165   case srun:
166     return "srun";
167   case pbsdsh:
168     return "pbsdsh";
169   case blaunch:
170     return "blaunch";
171   default:
172     throw ResourcesException("Unknown protocol");
173   }
174 }
175
176 AccessProtocolType ParserResourcesType::stringToProtocol(const std::string & protocolStr)
177 {
178   if (protocolStr == "sh")
179     return sh;
180   else if (protocolStr == "rsh")
181     return rsh;
182   else if (protocolStr == "ssh")
183     return ssh;
184   else if (protocolStr == "srun")
185     return srun;
186   else if (protocolStr == "pbsdsh")
187     return pbsdsh;
188   else if (protocolStr == "blaunch")
189     return blaunch;
190   else
191     throw ResourcesException((string("Unknown protocol ") + protocolStr).c_str());
192 }
193
194 ostream & operator<<(ostream &os, const ParserResourcesType &prt)
195 {
196   os << "Name: " << prt.Name << endl <<
197         "HostName: " << prt.HostName << endl <<
198         "Type: " << prt.getResourceTypeStr() << endl <<
199         "NbOfNodes: " << prt.DataForSort._nbOfNodes << endl <<
200         "NbOfProcPerNode: " << prt.DataForSort._nbOfProcPerNode << endl <<
201         "CPUFreqMHz: " << prt.DataForSort._CPUFreqMHz << endl <<
202         "MemInMB: " << prt.DataForSort._memInMB << endl <<
203         "Protocol: " << prt.getAccessProtocolTypeStr() << endl <<
204         "ClusterInternalProtocol: " << prt.getClusterInternalProtocolStr() << endl <<
205         "Batch: " << prt.getBatchTypeStr() << endl <<
206         "mpi: " << prt.getMpiImplTypeStr() << endl <<
207         "UserName: " << prt.UserName << endl <<
208         "AppliPath: " << prt.AppliPath << endl <<
209         "OS: " << prt.OS << endl <<
210         "batchQueue: " << prt.batchQueue << endl <<
211         "userCommands: " << prt.userCommands << endl <<
212         "use: " << prt.use << endl <<
213         "NbOfProc: " << prt.nbOfProc << endl <<
214         "Can Launch Batch Jobs: " << prt.can_launch_batch_jobs << endl <<
215         "Can Run Containers: " << prt.can_run_containers << endl <<
216         "Working Directory: " << prt.working_directory << endl;
217
218   for(unsigned int i=0 ; i<prt.ComponentsList.size() ; i++)
219     os << "Component " << i+1 << " called: " << prt.ComponentsList[i] << endl;
220
221   list<ParserResourcesType>::const_iterator it;
222   for(it = prt.ClusterMembersList.begin() ; it != prt.ClusterMembersList.end() ; it++)
223   {
224     os << "Cluster member called: " << (*it).HostName << endl;
225   }
226   return os;
227 }
228
229 std::string
230 ParserResourcesType::getAccessProtocolTypeStr() const
231 {
232   return protocolToString(Protocol);
233 }
234
235 std::string
236 ParserResourcesType::getClusterInternalProtocolStr() const
237 {
238   return protocolToString(ClusterInternalProtocol);
239 }
240
241 std::string 
242 ParserResourcesType::getResourceTypeStr() const
243 {
244   switch (type)
245   {
246   case cluster:
247     return "cluster";
248   case single_machine:
249     return "single_machine";
250   default:
251     throw ResourcesException("Unknown resource type");
252   }
253 }
254
255 std::string 
256 ParserResourcesType::getBatchTypeStr() const
257 {
258   switch (Batch)
259   {
260   case none:
261     return "none";
262   case pbs:
263     return "pbs";
264   case lsf:
265     return "lsf";
266   case sge:
267     return "sge";
268   case ccc:
269     return "ccc";
270   case slurm:
271     return "slurm";
272   case ll:
273     return "ll";
274   case vishnu:
275     return "vishnu";
276   case oar:
277     return "oar";
278   case coorm:
279     return "coorm";
280   default:
281     throw ResourcesException("Unknown batch type");
282   }
283 }
284
285 std::string 
286 ParserResourcesType::getMpiImplTypeStr() const
287 {
288   switch (mpi)
289   {
290   case nompi:
291     return "no mpi";
292   case lam:
293     return "lam";
294   case mpich1:
295     return "mpich1";
296   case mpich2:
297     return "mpich2";
298   case openmpi:
299     return "openmpi";
300   case ompi:
301     return "ompi";
302   case slurmmpi:
303     return "slurmmpi";
304   case prun:
305     return "prun";
306   default:
307     throw ResourcesException("Unknown MPI implementation type");
308   }
309 }
310
311 string ParserResourcesType::getCanLaunchBatchJobsStr() const
312 {
313   return can_launch_batch_jobs ? "true" : "false";
314 }
315
316 string ParserResourcesType::getCanRunContainersStr() const
317 {
318   return can_run_containers ? "true" : "false";
319 }
320
321 void ParserResourcesType::setAccessProtocolTypeStr(const string & protocolTypeStr)
322 {
323   Protocol = stringToProtocol(protocolTypeStr);
324 }
325
326 void ParserResourcesType::setResourceTypeStr(const string & resourceTypeStr)
327 {
328   if (resourceTypeStr == "cluster")
329     type = cluster;
330   else if (resourceTypeStr == "single_machine")
331     type = single_machine;
332   else
333     throw ResourcesException((string("Unknown resource type ") + resourceTypeStr).c_str());
334 }
335
336 void ParserResourcesType::setBatchTypeStr(const string & batchTypeStr)
337 {
338   if (batchTypeStr == "pbs")
339     Batch = pbs;
340   else if (batchTypeStr == "lsf")
341     Batch = lsf;
342   else if (batchTypeStr == "sge")
343     Batch = sge;
344   else if (batchTypeStr == "slurm")
345     Batch = slurm;
346   else if (batchTypeStr == "ccc")
347     Batch = ccc;
348   else if (batchTypeStr == "ll")
349     Batch = ll;
350   else if (batchTypeStr == "vishnu")
351     Batch = vishnu;
352   else if (batchTypeStr == "oar")
353     Batch = oar;
354   else if (batchTypeStr == "coorm")
355     Batch = coorm;
356   else if (batchTypeStr == "" || batchTypeStr == "none" || batchTypeStr == "ssh_batch")
357     Batch = none;
358   else
359     throw ResourcesException((string("Unknown batch type ") + batchTypeStr).c_str());
360 }
361
362 void ParserResourcesType::setMpiImplTypeStr(const string & mpiImplTypeStr)
363 {
364   if (mpiImplTypeStr == "lam")
365     mpi = lam;
366   else if (mpiImplTypeStr == "mpich1")
367     mpi = mpich1;
368   else if (mpiImplTypeStr == "mpich2")
369     mpi = mpich2;
370   else if (mpiImplTypeStr == "openmpi")
371     mpi = openmpi;
372   else if (mpiImplTypeStr == "ompi")
373     mpi = ompi;
374   else if (mpiImplTypeStr == "slurmmpi")
375     mpi = slurmmpi;
376   else if (mpiImplTypeStr == "prun")
377     mpi = prun;
378   else if (mpiImplTypeStr == "" || mpiImplTypeStr == "no mpi")
379     mpi = nompi;
380   else
381     throw ResourcesException((string("Unknown MPI implementation type ") + mpiImplTypeStr).c_str());
382 }
383
384 void ParserResourcesType::setClusterInternalProtocolStr(const string & internalProtocolTypeStr)
385 {
386   ClusterInternalProtocol = stringToProtocol(internalProtocolTypeStr);
387 }
388
389 void ParserResourcesType::setCanLaunchBatchJobsStr(const string & canLaunchBatchJobsStr)
390 {
391   if (canLaunchBatchJobsStr == "true")
392     can_launch_batch_jobs = true;
393   else if (canLaunchBatchJobsStr == "false")
394     can_launch_batch_jobs = false;
395   else
396     throw ResourcesException((string("Invalid boolean value for can_launch_batch_jobs: ") +
397                             canLaunchBatchJobsStr).c_str());
398 }
399
400 void ParserResourcesType::setCanRunContainersStr(const string & canRunContainersStr)
401 {
402   if (canRunContainersStr == "true")
403     can_run_containers = true;
404   else if (canRunContainersStr == "false")
405     can_run_containers = false;
406   else
407     throw ResourcesException((string("Invalid boolean value for can_run_containers: ") +
408                             canRunContainersStr).c_str());
409 }