Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/kernel.git] / src / ResourcesManager / SALOME_ResourcesCatalog_Parser.cxx
1 // Copyright (C) 2007-2012  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.
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 "Utils_SALOME_Exception.hxx"
25 #include <iostream>
26 #include <sstream>
27
28 #define NULL_VALUE 0
29
30 unsigned int ResourceDataToSort::_nbOfProcWanted = NULL_VALUE;
31 unsigned int ResourceDataToSort::_nbOfNodesWanted = NULL_VALUE;
32 unsigned int ResourceDataToSort::_nbOfProcPerNodeWanted = NULL_VALUE;
33 unsigned int ResourceDataToSort::_CPUFreqMHzWanted = NULL_VALUE;
34 unsigned int ResourceDataToSort::_memInMBWanted = NULL_VALUE;
35
36 ResourceDataToSort::ResourceDataToSort()
37 {}
38
39 ResourceDataToSort::ResourceDataToSort(const std::string& name,
40                                        unsigned int nbOfNodes,
41                                        unsigned int nbOfProcPerNode,
42                                        unsigned int CPUFreqMHz,
43                                        unsigned int memInMB):
44     _Name(name),
45     _nbOfNodes(nbOfNodes),
46     _nbOfProcPerNode(nbOfProcPerNode),
47     _CPUFreqMHz(CPUFreqMHz),
48     _memInMB(memInMB)
49 {}
50
51 //! Method used by list::sort to sort the resources used in SALOME_ResourcesManager::GetResourcesFitting
52 bool ResourceDataToSort::operator< (const ResourceDataToSort& other) const
53   {
54     unsigned int nbPts = GetNumberOfPoints();
55     return nbPts < other.GetNumberOfPoints();
56   }
57
58 unsigned int ResourceDataToSort::GetNumberOfPoints() const
59   {
60     unsigned int ret = 0;
61     //priority 0 : Nb of proc
62
63     if (_nbOfProcWanted != NULL_VALUE)
64       {
65         unsigned int nb_proc = _nbOfNodes * _nbOfProcPerNode;
66         if (nb_proc == _nbOfProcWanted)
67           ret += 30000;
68         else if (nb_proc > _nbOfProcWanted)
69           ret += 20000;
70         else
71           ret += 10000;
72       }
73
74     //priority 1 : Nb of nodes
75
76     if (_nbOfNodesWanted != NULL_VALUE)
77       {
78         if (_nbOfNodes == _nbOfNodesWanted)
79           ret += 3000;
80         else if (_nbOfNodes > _nbOfNodesWanted)
81           ret += 2000;
82         else
83           ret += 1000;
84       }
85
86     //priority 2 : Nb of proc by node
87     if (_nbOfProcPerNodeWanted != NULL_VALUE)
88       {
89         if (_nbOfProcPerNode == _nbOfProcPerNodeWanted)
90           ret += 300;
91         else if (_nbOfProcPerNode > _nbOfProcPerNodeWanted)
92           ret += 200;
93         else
94           ret += 100;
95       }
96
97     //priority 3 : Cpu freq
98     if (_CPUFreqMHzWanted != NULL_VALUE)
99       {
100         if (_CPUFreqMHz == _CPUFreqMHzWanted)
101           ret += 30;
102         else if (_CPUFreqMHz > _CPUFreqMHzWanted)
103           ret += 20;
104         else
105           ret += 10;
106       }
107
108     //priority 4 : memory
109     if (_memInMBWanted != NULL_VALUE)
110       {
111         if (_memInMB == _memInMBWanted)
112           ret += 3;
113         else if (_memInMB > _memInMBWanted)
114           ret += 2;
115         else
116           ret += 1;
117       }
118
119     //RES_MESSAGE("[GetNumberOfPoints] points number for resource: " << _Name << " " << ret);
120     return ret;
121   }
122
123 //! Method used for debug
124 void ResourceDataToSort::Print() const
125   {
126     std::cout << _nbOfNodes << std::endl;
127     std::cout << _nbOfProcPerNode << std::endl;
128     std::cout << _CPUFreqMHz << std::endl;
129     std::cout << _memInMB << std::endl;
130   }
131
132
133 std::string ParserResourcesType::protocolToString(AccessProtocolType protocol)
134 {
135   switch (protocol)
136   {
137   case rsh:
138     return "rsh";
139   case ssh:
140     return "ssh";
141   case srun:
142     return "srun";
143   case pbsdsh:
144     return "pbsdsh";
145   case blaunch:
146     return "blaunch";
147   default:
148     throw SALOME_Exception("Unknown protocol");
149   }
150 }
151
152 AccessProtocolType ParserResourcesType::stringToProtocol(const std::string & protocolStr)
153 {
154   if (protocolStr == "rsh")
155     return rsh;
156   else if (protocolStr == "ssh")
157     return ssh;
158   else if (protocolStr == "srun")
159     return srun;
160   else if (protocolStr == "pbsdsh")
161     return pbsdsh;
162   else if (protocolStr == "blaunch")
163     return blaunch;
164   else
165     throw SALOME_Exception("Unknown protocol");
166 }
167
168 void ParserResourcesType::Print()
169 {
170   std::ostringstream oss;
171   oss << std::endl <<
172     "Name : " << Name << std::endl <<
173     "HostName : " << HostName << std::endl << 
174     "NbOfNodes : " << DataForSort._nbOfNodes << std::endl <<
175     "NbOfProcPerNode : " << DataForSort._nbOfProcPerNode << std::endl <<
176     "CPUFreqMHz : " << DataForSort._CPUFreqMHz << std::endl <<
177     "MemInMB : " << DataForSort._memInMB << std::endl <<
178     "Protocol : " << protocolToString(Protocol) << std::endl <<
179     "ClusterInternalProtocol : " << protocolToString(ClusterInternalProtocol) << std::endl <<
180     "Mode : " << Mode << std::endl <<
181     "Batch : " << Batch << std::endl <<
182     "mpi : " << mpi << std::endl <<
183     "UserName : " << UserName << std::endl <<
184     "AppliPath : " << AppliPath << std::endl <<
185     "OS : " << OS << std::endl <<
186     "batchQueue : " << batchQueue << std::endl <<
187     "userCommands : " << userCommands << std::endl <<
188     "use : " << use << std::endl <<
189     "NbOfProc : " << nbOfProc << std::endl <<
190     "Modules : " << std::endl <<
191     "Components : " << std::endl <<
192     "Is Cluster Head: " << is_cluster_head << std::endl <<
193     "Working Directory: " << working_directory << std::endl;
194
195   for(unsigned int i=0;i<ComponentsList.size();i++)
196     oss << "Component " << i+1 << " called : " << ComponentsList[i] << std::endl;
197
198   
199   std::list<ParserResourcesClusterMembersType>::iterator it;
200   for(it = ClusterMembersList.begin(); 
201       it != ClusterMembersList.end();
202       it++)
203   {
204     oss << "Cluster member  called : " << (*it).HostName << std::endl;
205   }
206   std::cout << oss.str() << std::endl;
207 }
208
209 std::string
210 ParserResourcesType::PrintAccessProtocolType() const
211 {
212   return protocolToString(Protocol);
213 }
214
215 std::string
216 ParserResourcesType::PrintClusterInternalProtocol() const
217 {
218   return protocolToString(ClusterInternalProtocol);
219 }
220
221 std::string 
222 ParserResourcesType::PrintAccessModeType() const
223 {
224   if (Mode == interactive)
225     return "interactive";
226   else
227     return "batch";
228 }
229
230 std::string 
231 ParserResourcesType::PrintBatchType() const
232 {
233   if (Batch == none)
234     return "none";
235   else if (Batch == pbs)
236     return "pbs";
237   else if (Batch == lsf)
238     return "lsf";
239   else if (Batch == sge)
240     return "sge";
241   else if (Batch == ccc)
242     return "ccc";
243   else if (Batch == slurm)
244     return "slurm";
245   else if (Batch == ll)
246     return "ll";
247   else if (Batch == vishnu)
248     return "vishnu";
249   else 
250     return "ssh";
251 }
252
253 std::string 
254 ParserResourcesType::PrintMpiImplType() const
255 {
256   if (mpi == nompi)
257     return "no mpi";
258   else if (mpi == lam)
259     return "lam";
260   else if (mpi == mpich1)
261     return "mpich1";
262   else if (mpi == mpich2)
263     return "mpich2";
264   else if (mpi == openmpi)
265     return "openmpi";
266   else if (mpi == slurmmpi)
267     return "slurmmpi";
268   else
269     return "prun";
270 }
271
272 void ParserResourcesType::Clear()
273 {
274   Name = "";
275   HostName = "";
276   Protocol = rsh;
277   ClusterInternalProtocol = rsh;
278   Mode = interactive;
279   Batch = none;
280   mpi = nompi;
281   UserName = "";
282   AppliPath = "";
283   batchQueue = "";
284   userCommands = "";
285   ComponentsList.clear();
286   OS = "";
287   use = "";
288   ClusterMembersList.clear();
289   nbOfProc = 1;
290   is_cluster_head = false;
291   working_directory = "";
292
293   DataForSort._Name = "";
294   DataForSort._nbOfNodes = 1;
295   DataForSort._nbOfProcPerNode = 1;
296   DataForSort._CPUFreqMHz = 0;
297   DataForSort._memInMB = 0;
298 }