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