Salome HOME
Rename new ResourcesManager CORBA method
[modules/kernel.git] / src / ResourcesManager / SALOME_ResourcesManager.cxx
1 //  Copyright (C) 2007-2008  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 #include "SALOME_ResourcesManager.hxx" 
23 #include "Utils_ExceptHandlers.hxx"
24 #include "Utils_CorbaException.hxx"
25 #include "OpUtil.hxx"
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #ifndef WIN32
30 #include <unistd.h>
31 #else
32 #include <io.h>
33 #include <process.h>
34 #endif
35 #include <fstream>
36 #include <iostream>
37 #include <sstream>
38 #include <string.h>
39 #include <map>
40 #include <list>
41
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include "utilities.h"
45
46 #define MAX_SIZE_FOR_HOSTNAME 256;
47
48 using namespace std;
49
50 const char *SALOME_ResourcesManager::_ResourcesManagerNameInNS = "/ResourcesManager";
51
52 //=============================================================================
53 /*!
54  * just for test
55  */ 
56 //=============================================================================
57
58 SALOME_ResourcesManager::
59 SALOME_ResourcesManager(CORBA::ORB_ptr orb, 
60                         PortableServer::POA_var poa, 
61                         SALOME_NamingService *ns,
62                         const char *xmlFilePath) : _rm(xmlFilePath)
63 {
64   MESSAGE("SALOME_ResourcesManager constructor");
65   _NS = ns;
66   _orb = CORBA::ORB::_duplicate(orb) ;
67   _poa = PortableServer::POA::_duplicate(poa) ;
68   PortableServer::ObjectId_var id = _poa->activate_object(this);
69   CORBA::Object_var obj = _poa->id_to_reference(id);
70   Engines::ResourcesManager_var refContMan = Engines::ResourcesManager::_narrow(obj);
71   _NS->Register(refContMan,_ResourcesManagerNameInNS);
72   MESSAGE("SALOME_ResourcesManager constructor end");
73 }
74
75 //=============================================================================
76 /*!
77  *  Standard constructor, parse resource file.
78  *  - if ${APPLI} exists in environment,
79  *    look for ${HOME}/${APPLI}/CatalogResources.xml
80  *  - else look for default:
81  *    ${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml
82  *  - parse XML resource file.
83  */ 
84 //=============================================================================
85
86 SALOME_ResourcesManager::SALOME_ResourcesManager(CORBA::ORB_ptr orb, 
87                                                  PortableServer::POA_var poa, 
88                                                  SALOME_NamingService *ns) : _rm()
89 {
90   MESSAGE("SALOME_ResourcesManager constructor");
91   _NS = ns;
92   _orb = CORBA::ORB::_duplicate(orb) ;
93   _poa = PortableServer::POA::_duplicate(poa) ;
94   PortableServer::ObjectId_var id = _poa->activate_object(this);
95   CORBA::Object_var obj = _poa->id_to_reference(id);
96   Engines::ResourcesManager_var refContMan = Engines::ResourcesManager::_narrow(obj);
97   _NS->Register(refContMan,_ResourcesManagerNameInNS);
98
99   MESSAGE("SALOME_ResourcesManager constructor end");
100 }
101
102 //=============================================================================
103 /*!
104  *  Standard Destructor
105  */ 
106 //=============================================================================
107
108 SALOME_ResourcesManager::~SALOME_ResourcesManager()
109 {
110   MESSAGE("SALOME_ResourcesManager destructor");
111 }
112
113
114 //=============================================================================
115 /*! CORBA method:
116  *  shutdown all the containers, then the ContainerManager servant
117  */
118 //=============================================================================
119
120 void SALOME_ResourcesManager::Shutdown()
121 {
122   MESSAGE("Shutdown");
123   _NS->Destroy_Name(_ResourcesManagerNameInNS);
124   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
125   _poa->deactivate_object(oid);
126 }
127
128 //=============================================================================
129 //! get the name of resources fitting the specified constraints (params)
130 /*!
131  *  If hostname specified, check it is local or known in resources catalog.
132  *
133  *  Else
134  *  - select first machines with corresponding OS (all machines if
135  *    parameter OS empty),
136  *  - then select the sublist of machines on which the component is known
137  *    (if the result is empty, that probably means that the inventory of
138  *    components is probably not done, so give complete list from previous step)
139  */ 
140 //=============================================================================
141
142 Engines::ResourceList *
143 SALOME_ResourcesManager::GetFittingResources(const Engines::ResourceParameters& params)
144 {
145   MESSAGE("ResourcesManager::GetFittingResources");
146   Engines::ResourceList * ret = new Engines::ResourceList;
147
148   // CORBA -> C++
149   resourceParams p;
150   p.name = params.name;
151   p.hostname = params.hostname;
152   p.OS = params.OS;
153   p.nb_proc = params.nb_proc;
154   p.nb_node = params.nb_node;
155   p.nb_proc_per_node = params.nb_proc_per_node;
156   p.cpu_clock = params.cpu_clock;
157   p.mem_mb = params.mem_mb;
158   for(unsigned int i=0; i<params.componentList.length(); i++)
159     p.componentList.push_back(string(params.componentList[i]));
160   for(unsigned int i=0; i<params.resList.length(); i++)
161     p.resourceList.push_back(string(params.resList[i]));
162   
163   try
164   {
165     // Call C++ ResourceManager
166     vector <std::string> vec = _rm.GetFittingResources(p);
167
168     // C++ -> CORBA
169     ret->length(vec.size());
170     for(unsigned int i=0;i<vec.size();i++)
171       (*ret)[i] = (vec[i]).c_str();
172   }
173   catch(const ResourcesException &ex)
174   {
175     INFOS("Caught exception in GetFittingResources C++:  " << ex.msg);
176     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
177   }  
178
179   return ret;
180 }
181
182 //=============================================================================
183 /*!
184  *  dynamically obtains the first machines
185  */ 
186 //=============================================================================
187
188 char *
189 SALOME_ResourcesManager::FindFirst(const Engines::ResourceList& listOfResources)
190 {
191   // CORBA -> C++
192   vector<string> rl;
193   for(unsigned int i=0; i<listOfResources.length(); i++)
194     rl.push_back(string(listOfResources[i]));
195
196   return CORBA::string_dup(_rm.Find("first", rl).c_str());
197 }
198
199 char *
200 SALOME_ResourcesManager::Find(const char* policy, const Engines::ResourceList& listOfResources)
201 {
202   // CORBA -> C++
203   vector<string> rl;
204   for(unsigned int i=0; i<listOfResources.length(); i++)
205     rl.push_back(string(listOfResources[i]));
206
207   return CORBA::string_dup(_rm.Find(policy, rl).c_str());
208 }
209
210 Engines::ResourceDefinition* 
211 SALOME_ResourcesManager::GetResourceDefinition(const char * name)
212 {
213   ParserResourcesType resource = _rm.GetResourcesDescr(name);
214   Engines::ResourceDefinition *p_ptr = new Engines::ResourceDefinition;
215
216   p_ptr->name = CORBA::string_dup(resource.Name.c_str());
217   p_ptr->hostname = CORBA::string_dup(resource.HostName.c_str());
218   if( resource.Protocol == rsh )
219     p_ptr->protocol = "rsh";
220   else if( resource.Protocol == ssh )
221     p_ptr->protocol = "ssh";
222   if( resource.ClusterInternalProtocol == rsh )
223     p_ptr->iprotocol = "rsh";
224   else if( resource.ClusterInternalProtocol == ssh )
225     p_ptr->iprotocol = "ssh";
226   p_ptr->username = CORBA::string_dup(resource.UserName.c_str());
227   p_ptr->applipath = CORBA::string_dup(resource.AppliPath.c_str());
228   p_ptr->componentList.length(resource.ComponentsList.size());
229   for(unsigned int i=0;i<resource.ComponentsList.size();i++)
230     p_ptr->componentList[i] = CORBA::string_dup(resource.ComponentsList[i].c_str());
231   p_ptr->OS = CORBA::string_dup(resource.OS.c_str());
232   p_ptr->mem_mb = resource.DataForSort._memInMB;
233   p_ptr->cpu_clock = resource.DataForSort._CPUFreqMHz;
234   p_ptr->nb_proc_per_node = resource.DataForSort._nbOfProcPerNode;
235   p_ptr->nb_node = resource.DataForSort._nbOfNodes;
236
237   if( resource.mpi == lam )
238     p_ptr->mpiImpl = "lam";
239   else if( resource.mpi == mpich1 )
240     p_ptr->mpiImpl = "mpich1";
241   else if( resource.mpi == mpich2 )
242     p_ptr->mpiImpl = "mpich2";
243   else if( resource.mpi == openmpi )
244     p_ptr->mpiImpl = "openmpi";
245   else if( resource.mpi == slurm )
246     p_ptr->mpiImpl = "slurm";
247   else if( resource.mpi == prun )
248     p_ptr->mpiImpl = "prun";
249
250   if( resource.Batch == pbs )
251     p_ptr->batch = "pbs";
252   else if( resource.Batch == lsf )
253     p_ptr->batch = "lsf";
254   else if( resource.Batch == sge )
255     p_ptr->batch = "sge";
256   else if( resource.Batch == ssh_batch )
257     p_ptr->batch = "ssh";
258
259   return p_ptr;
260 }
261
262 void 
263 SALOME_ResourcesManager::AddResource(const Engines::ResourceDefinition& new_resource,
264                                      CORBA::Boolean write,
265                                      const char * xml_file)
266 {
267   ParserResourcesType resource;
268   resource.Name = new_resource.name.in();
269   resource.HostName = new_resource.hostname.in();
270   resource.OS = new_resource.OS.in();
271   resource.DataForSort._memInMB = new_resource.mem_mb;
272   resource.DataForSort._CPUFreqMHz = new_resource.cpu_clock;
273   resource.DataForSort._nbOfNodes = new_resource.nb_node;
274   resource.DataForSort._nbOfProcPerNode = new_resource.nb_proc_per_node;
275   resource.UserName = new_resource.username.in();
276
277   std::string aBatch = new_resource.batch.in();
278   if (aBatch == "pbs")
279     resource.Batch = pbs;
280   else if  (aBatch == "lsf")
281     resource.Batch = lsf;
282   else if  (aBatch == "sge")
283     resource.Batch = sge;
284   else if  (aBatch == "ssh_batch")
285     resource.Batch = ssh_batch;
286   else if (aBatch == "")
287     resource.Batch = none;
288   else {
289     INFOS("Bad Batch definition in AddResource: " << aBatch);
290     std::string message("Bad Batch definition in AddResource: ");
291     message += aBatch;
292     THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
293   }
294
295   std::string anMpi = new_resource.mpiImpl.in();
296   if (anMpi == "lam")
297     resource.mpi = lam;
298   else if (anMpi == "mpich1")
299     resource.mpi = mpich1;
300   else if (anMpi == "mpich2")
301     resource.mpi = mpich2;
302   else if (anMpi == "openmpi")
303     resource.mpi = openmpi;
304   else if  (anMpi == "slurm")
305     resource.mpi = slurm;
306   else if  (anMpi == "prun")
307     resource.mpi = prun;
308   else if (anMpi == "")
309     resource.mpi = nompi;
310   else {
311     INFOS("Bad MPI definition in AddResource: " << anMpi);
312     std::string message("Bad MPI definition in AddResource: ");
313     message += anMpi;
314     THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
315   }
316
317   std::string mode_str = new_resource.mode.in();
318   if (mode_str == "interactive")
319     resource.Mode = interactive;
320   else if (mode_str == "batch")
321     resource.Mode = batch;
322   else if (mode_str == "")
323     resource.Mode = interactive;
324   else {
325     INFOS("Bad mode definition in AddResource: " << mode_str);
326     std::string message("Bad mode definition in AddResource: ");
327     message += mode_str;
328     THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
329   }
330   
331   std::string protocol = new_resource.protocol.in();
332   if (protocol == "rsh")
333     resource.Protocol = rsh;
334   else if (protocol == "ssh")
335     resource.Protocol = ssh;
336   else if (protocol == "")
337     resource.Protocol = rsh;
338   else {
339     INFOS("Bad protocol definition in AddResource: " << protocol);
340     std::string message("Bad protocol definition in AddResource: ");
341     message += protocol;
342     THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
343   }
344
345   std::string iprotocol = new_resource.iprotocol.in();
346   if (iprotocol == "rsh")
347     resource.ClusterInternalProtocol = rsh;
348   else if (iprotocol == "ssh")
349     resource.ClusterInternalProtocol = ssh;
350   else if (iprotocol == "")
351     resource.ClusterInternalProtocol = rsh;
352   else {
353     INFOS("Bad iprotocol definition in AddResource: " << iprotocol);
354     std::string message("Bad iprotocol definition in AddResource: ");
355     message += iprotocol;
356     THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
357   }
358
359   for (CORBA::ULong i = 0; i < new_resource.componentList.length(); i++)
360     resource.ComponentsList.push_back(new_resource.componentList[i].in());
361
362   _rm.AddResourceInCatalog(resource);
363
364   if (write)
365   {
366     _rm.WriteInXmlFile(std::string(xml_file));
367     _rm.ParseXmlFiles();
368   }
369 }
370
371 std::string 
372 SALOME_ResourcesManager::getMachineFile(std::string hostname, 
373                                         CORBA::Long nb_procs, 
374                                         std::string parallelLib)
375 {
376   std::string machine_file_name("");
377
378   if (parallelLib == "Dummy")
379   {
380     MESSAGE("[getMachineFile] parallelLib is Dummy");
381     MapOfParserResourcesType resourcesList = _rm.GetList();
382     if (resourcesList.find(hostname) != resourcesList.end())
383     {
384       ParserResourcesType resource = resourcesList[hostname];
385
386       // Check if resource is cluster or not
387       if (resource.ClusterMembersList.empty())
388       {
389         //It is not a cluster so we create a cluster with one machine
390         ParserResourcesClusterMembersType fake_node;
391         fake_node.HostName = resource.HostName;
392         fake_node.Protocol = resource.Protocol;
393         fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
394         fake_node.UserName = resource.UserName;
395         fake_node.AppliPath = resource.AppliPath;
396         fake_node.DataForSort = resource.DataForSort;
397
398         resource.ClusterMembersList.push_front(fake_node);
399       }
400
401       // Creating list of machines for creating the machine file
402       std::list<std::string> list_of_machines;
403       std::list<ParserResourcesClusterMembersType>::iterator cluster_it = 
404         resource.ClusterMembersList.begin();
405       while (cluster_it != resource.ClusterMembersList.end())
406       {
407         // For each member of the cluster we add a nbOfNodes * nbOfProcPerNode in the list
408         unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes * 
409                                       (*cluster_it).DataForSort._nbOfProcPerNode;
410         for (unsigned int i = 0; i < number_of_proc; i++)
411           list_of_machines.push_back((*cluster_it).HostName);
412         cluster_it++;
413       }
414
415       // Creating machine file
416       machine_file_name = tmpnam(NULL);
417       std::ofstream machine_file(machine_file_name.c_str(), ios_base::out);
418
419       CORBA::Long machine_number = 0;
420       std::list<std::string>::iterator it = list_of_machines.begin();
421       while (machine_number != nb_procs)
422       {
423         // Adding a new node to the machine file
424         machine_file << *it << endl;
425
426         // counting...
427         it++;
428         if (it == list_of_machines.end())
429           it = list_of_machines.begin();
430         machine_number++;
431       }
432     }
433     else
434       INFOS("[getMachineFile] Error hostname not found in resourcesList -> " << hostname);
435   }
436   else if (parallelLib == "Mpi")
437   {
438     MESSAGE("[getMachineFile] parallelLib is Mpi");
439
440     MapOfParserResourcesType resourcesList = _rm.GetList();
441     if (resourcesList.find(hostname) != resourcesList.end())
442     {
443       ParserResourcesType resource = resourcesList[hostname];
444       // Check if resource is cluster or not
445       if (resource.ClusterMembersList.empty())
446       {
447         //It is not a cluster so we create a cluster with one machine
448         ParserResourcesClusterMembersType fake_node;
449         fake_node.HostName = resource.HostName;
450         fake_node.Protocol = resource.Protocol;
451         fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
452         fake_node.UserName = resource.UserName;
453         fake_node.AppliPath = resource.AppliPath;
454         fake_node.DataForSort = resource.DataForSort;
455
456         resource.ClusterMembersList.push_front(fake_node);
457       }
458
459       // Choose mpi implementation -> each MPI implementation has is own machinefile...
460       if (resource.mpi == lam)
461       {
462         // Creating machine file
463         machine_file_name = tmpnam(NULL);
464         std::ofstream machine_file(machine_file_name.c_str(), ios_base::out);
465
466         // We add all cluster machines to the file
467         std::list<ParserResourcesClusterMembersType>::iterator cluster_it = 
468           resource.ClusterMembersList.begin();
469         while (cluster_it != resource.ClusterMembersList.end())
470         {
471           unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes * 
472             (*cluster_it).DataForSort._nbOfProcPerNode;
473           machine_file << (*cluster_it).HostName << " cpu=" << number_of_proc << endl;
474           cluster_it++;
475         }
476       }
477       else if (resource.mpi == nompi)
478       {
479         INFOS("[getMachineFile] Error hostname MPI implementation was defined for " << hostname);
480       }
481       else
482         INFOS("[getMachineFile] Error hostname MPI implementation not currenly handled for " << hostname);
483     }
484     else
485       INFOS("[getMachineFile] Error hostname not found in resourcesList -> " << hostname);
486   }
487   else
488     INFOS("[getMachineFile] Error parallelLib is not handled -> " << parallelLib);
489
490   return machine_file_name;
491 }