]> SALOME platform Git repositories - modules/kernel.git/blob - src/ResourcesManager/SALOME_ResourcesManager.cxx
Salome HOME
bos #42937: [CEA 41954] Integration of UB24.04 patches
[modules/kernel.git] / src / ResourcesManager / SALOME_ResourcesManager.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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_Abstract *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_Abstract *ns) : _rm(new ResourcesManager_cpp())
98 {
99   _NS = ns;
100   _orb = CORBA::ORB::_duplicate(orb) ;
101   //
102   PortableServer::POAManager_var pman = poa->the_POAManager();
103   CORBA::PolicyList policies;
104   policies.length(1);
105   PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL));
106   policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
107   _poa = poa->create_POA("SingleThreadPOA",pman,policies);
108   threadPol->destroy();
109   //
110   PortableServer::ObjectId_var id = _poa->activate_object(this);
111   CORBA::Object_var obj = _poa->id_to_reference(id);
112   Engines::ResourcesManager_var refContMan = Engines::ResourcesManager::_narrow(obj);
113   if(_NS)
114     _NS->Register(refContMan,_ResourcesManagerNameInNS);
115 }
116
117 //=============================================================================
118 /*!
119  *  Standard Destructor
120  */ 
121 //=============================================================================
122
123 SALOME_ResourcesManager::~SALOME_ResourcesManager()
124 {
125   MESSAGE("SALOME_ResourcesManager destructor");
126 }
127
128
129 //=============================================================================
130 /*! CORBA method:
131  *  shutdown all the containers, then the ContainerManager servant
132  */
133 //=============================================================================
134
135 void SALOME_ResourcesManager::Shutdown()
136 {
137   MESSAGE("Shutdown");
138   if(!_NS)
139     return ;
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) but select only those with canRunContainers attribute set to true.
147  * And for each resource the number of proc available of it.
148  * 
149  * \sa SALOME_ResourcesManager::ListAllResourcesInCatalog, SALOME_ResourcesManager::ListAllResourceEntriesInCatalog
150  */
151 void SALOME_ResourcesManager::ListAllAvailableResources(Engines::ResourceList_out machines, Engines::IntegerList_out nbProcsOfMachines)
152 {
153   const MapOfParserResourcesType& zeList(_rm->GetList());
154   std::vector<std::string> ret0;
155   std::vector<int> ret1;
156   for(MapOfParserResourcesType::const_iterator it=zeList.begin();it!=zeList.end();it++)
157   {
158     const ParserResourcesType& elt((*it).second);
159     if(elt.can_run_containers)
160     {
161       ret0.push_back(elt.HostName);
162       ret1.push_back(elt.DataForSort._nbOfNodes*elt.DataForSort._nbOfProcPerNode);
163     }
164   }
165   machines=new Engines::ResourceList;
166   nbProcsOfMachines=new Engines::IntegerList;
167   std::size_t sz(ret0.size());
168   machines->length((CORBA::ULong)sz); nbProcsOfMachines->length((CORBA::ULong)sz);
169   for(std::size_t j=0;j<sz;j++)
170     {
171       (*machines)[(CORBA::ULong)j]=CORBA::string_dup(ret0[j].c_str());
172       (*nbProcsOfMachines)[(CORBA::ULong)j]=ret1[j];
173     }
174 }
175
176 /*!
177  * Return list of resources available (regarding content of CatalogResources.xml) whatever canRunContainers attribute value.
178  * 
179  * \sa SALOME_ResourcesManager::ListAllAvailableResources, SALOME_ResourcesManager::ListAllResourceEntriesInCatalog
180  */
181 Engines::ResourceList *SALOME_ResourcesManager::ListAllResourcesInCatalog()
182 {
183   const MapOfParserResourcesType& zeList(_rm->GetList());
184   auto sz = zeList.size();
185   Engines::ResourceList *ret(new Engines::ResourceList);
186   ret->length( sz );
187   CORBA::ULong i(0);
188   for(auto it : zeList)
189   {
190     (*ret)[i++] = CORBA::string_dup( it.second.HostName.c_str() );
191   }
192   return ret;
193 }
194
195 /*!
196  * Return list of resources entries available. Useful to scan remotely the content of the playground
197  */
198 Engines::ResourceList *SALOME_ResourcesManager::ListAllResourceEntriesInCatalog()
199 {
200   const MapOfParserResourcesType& zeList(_rm->GetList());
201   auto sz = zeList.size();
202   Engines::ResourceList *ret(new Engines::ResourceList);
203   ret->length( sz );
204   CORBA::ULong i(0);
205   for(auto it : zeList)
206   {
207     (*ret)[i++] = CORBA::string_dup( it.first.c_str() );
208   }
209   return ret;
210 }
211
212 //=============================================================================
213 //! get the name of resources fitting the specified constraints (params)
214 /*!
215  *  If hostname specified, check it is local or known in resources catalog.
216  *
217  *  Else
218  *  - select first machines with corresponding OS (all machines if
219  *    parameter OS empty),
220  *  - then select the sublist of machines on which the component is known
221  *    (if the result is empty, that probably means that the inventory of
222  *    components is probably not done, so give complete list from previous step)
223  */ 
224 //=============================================================================
225
226 Engines::ResourceList *
227 SALOME_ResourcesManager::GetFittingResources(const Engines::ResourceParameters& params)
228 {
229   //MESSAGE("ResourcesManager::GetFittingResources");
230   Engines::ResourceList_var ret;
231
232   // CORBA -> C++
233   resourceParams p = resourceParameters_CORBAtoCPP(params);
234
235   try
236   {
237     // Call C++ ResourceManager
238     std::vector <std::string> vec = _rm->GetFittingResources(p);
239
240     // C++ -> CORBA
241     ret = resourceList_CPPtoCORBA(vec);
242   }
243   catch(const ResourcesException &ex)
244   {
245     INFOS("Caught exception in GetFittingResources C++:  " << ex.msg);
246     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
247   }  
248
249   return ret._retn();
250 }
251
252 //=============================================================================
253 /*!
254  *  dynamically obtains the first machines
255  */ 
256 //=============================================================================
257
258 char *
259 SALOME_ResourcesManager::FindFirst(const Engines::ResourceList& listOfResources)
260 {
261   // CORBA -> C++
262   std::vector<std::string> rl = resourceList_CORBAtoCPP(listOfResources);
263
264   return CORBA::string_dup(_rm->Find("first", rl).c_str());
265 }
266
267 char *
268 SALOME_ResourcesManager::Find(const char* policy, const Engines::ResourceList& listOfResources)
269 {
270   // CORBA -> C++
271   std::vector<std::string> rl = resourceList_CORBAtoCPP(listOfResources);
272
273   return CORBA::string_dup(_rm->Find(policy, rl).c_str());
274 }
275
276 Engines::ResourceDefinition*
277 SALOME_ResourcesManager::GetResourceDefinition(const char * name)
278 {
279   Engines::ResourceDefinition_var resDef;
280   try {
281     ParserResourcesType resource = _rm->GetResourcesDescr(name);
282     resDef = resourceDefinition_CPPtoCORBA(resource);
283   } catch (const exception & ex) {
284     INFOS("Caught exception in GetResourceDefinition: " << ex.what());
285     THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::BAD_PARAM);
286   }
287
288   return resDef._retn();
289 }
290
291 void 
292 SALOME_ResourcesManager::AddResource(const Engines::ResourceDefinition& new_resource,
293                                      CORBA::Boolean write,
294                                      const char * xml_file)
295 {
296   try
297   {
298     ParserResourcesType resource = resourceDefinition_CORBAtoCPP(new_resource);
299     _rm->AddResourceInCatalog(resource);
300
301     if (write)
302     {
303       _rm->WriteInXmlFile(std::string(xml_file));
304       _rm->ParseXmlFiles();
305     }
306   }
307   catch (const SALOME_Exception & e)
308   {
309     INFOS("Error in AddResourceInCatalog: " << e);
310     THROW_SALOME_CORBA_EXCEPTION(e.what(), SALOME::BAD_PARAM);
311   }
312   catch (const ResourcesException & e)
313   {
314     INFOS("Error in AddResourceInCatalog: " << e.msg);
315     THROW_SALOME_CORBA_EXCEPTION(e.msg.c_str(), SALOME::BAD_PARAM);
316   }
317 }
318
319 void
320 SALOME_ResourcesManager::RemoveResource(const char * resource_name,
321                                         CORBA::Boolean write,
322                                         const char * xml_file)
323 {
324   try
325   {
326     _rm->DeleteResourceInCatalog(resource_name);
327   }
328   catch (const SALOME_Exception & e)
329   {
330     INFOS("Error in DeleteResourceInCatalog: " << e);
331     THROW_SALOME_CORBA_EXCEPTION(e.what(), SALOME::BAD_PARAM);
332   }
333
334   if (write)
335   {
336     _rm->WriteInXmlFile(std::string(xml_file));
337     _rm->ParseXmlFiles();
338   }
339 }
340
341 char *
342 SALOME_ResourcesManager::getMachineFile(const char * resource_name,
343                                         CORBA::Long nb_procs, 
344                                         const char * parallelLib)
345 {
346   std::string machine_file_name("");
347
348   if (std::string(parallelLib) == "Dummy")
349   {
350     MESSAGE("[getMachineFile] parallelLib is Dummy");
351     MapOfParserResourcesType resourcesList = _rm->GetList();
352     if (resourcesList.find(std::string(resource_name)) != resourcesList.end())
353     {
354       ParserResourcesType resource = resourcesList[std::string(resource_name)];
355
356       // Check if resource is cluster or not
357       if (resource.ClusterMembersList.empty())
358       {
359         //It is not a cluster so we create a cluster with one machine
360         ParserResourcesType fake_node;
361         fake_node.HostName = resource.HostName;
362         fake_node.Protocol = resource.Protocol;
363         fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
364         fake_node.UserName = resource.UserName;
365         fake_node.AppliPath = resource.AppliPath;
366         fake_node.DataForSort = resource.DataForSort;
367
368         resource.ClusterMembersList.push_front(fake_node);
369       }
370
371       // Creating list of machines for creating the machine file
372       std::list<std::string> list_of_machines;
373       std::list<ParserResourcesType>::iterator cluster_it =
374         resource.ClusterMembersList.begin();
375       while (cluster_it != resource.ClusterMembersList.end())
376       {
377         // For each member of the cluster we add a nbOfNodes * nbOfProcPerNode in the list
378         unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes * 
379                                       (*cluster_it).DataForSort._nbOfProcPerNode;
380         for (unsigned int i = 0; i < number_of_proc; i++)
381           list_of_machines.push_back((*cluster_it).HostName);
382         cluster_it++;
383       }
384
385       // Creating machine file
386       machine_file_name = tmpnam(NULL);
387       std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
388
389       CORBA::Long machine_number = 0;
390       std::list<std::string>::iterator it = list_of_machines.begin();
391       while (machine_number != nb_procs)
392       {
393         // Adding a new node to the machine file
394         machine_file << *it << std::endl;
395
396         // counting...
397         it++;
398         if (it == list_of_machines.end())
399           it = list_of_machines.begin();
400         machine_number++;
401       }
402     }
403     else
404       INFOS("[getMachineFile] Error resource_name not found in resourcesList -> " << resource_name);
405   }
406   else if (std::string(parallelLib) == "Mpi")
407   {
408     MESSAGE("[getMachineFile] parallelLib is Mpi");
409
410     MapOfParserResourcesType resourcesList = _rm->GetList();
411     if (resourcesList.find(std::string(resource_name)) != resourcesList.end())
412     {
413       ParserResourcesType resource = resourcesList[std::string(resource_name)];
414       // Check if resource is cluster or not
415       if (resource.ClusterMembersList.empty())
416       {
417         //It is not a cluster so we create a cluster with one machine
418         ParserResourcesType fake_node;
419         fake_node.HostName = resource.HostName;
420         fake_node.Protocol = resource.Protocol;
421         fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
422         fake_node.UserName = resource.UserName;
423         fake_node.AppliPath = resource.AppliPath;
424         fake_node.DataForSort = resource.DataForSort;
425
426         resource.ClusterMembersList.push_front(fake_node);
427       }
428
429       // Choose mpi implementation -> each MPI implementation has is own machinefile...
430       if (resource.mpi == lam)
431       {
432         // Creating machine file
433         machine_file_name = tmpnam(NULL);
434         std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
435
436         // We add all cluster machines to the file
437         std::list<ParserResourcesType>::iterator cluster_it =
438           resource.ClusterMembersList.begin();
439         while (cluster_it != resource.ClusterMembersList.end())
440         {
441           unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes * 
442             (*cluster_it).DataForSort._nbOfProcPerNode;
443           machine_file << (*cluster_it).HostName << " cpu=" << number_of_proc << std::endl;
444           cluster_it++;
445         }
446       }
447       else if ((resource.mpi == openmpi) || (resource.mpi == ompi))
448       {
449         // Creating machine file
450         machine_file_name = tmpnam(NULL);
451         std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
452
453         // We add all cluster machines to the file
454         std::list<ParserResourcesType>::iterator cluster_it =
455           resource.ClusterMembersList.begin();
456         while (cluster_it != resource.ClusterMembersList.end())
457         {
458           unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes *
459             (*cluster_it).DataForSort._nbOfProcPerNode;
460           machine_file << (*cluster_it).HostName << " slots=" << number_of_proc << std::endl;
461           cluster_it++;
462         }
463       }
464       else if (resource.mpi == nompi)
465       {
466         INFOS("[getMachineFile] Error resource_name MPI implementation was defined for " << resource_name);
467       }
468       else
469         INFOS("[getMachineFile] Error resource_name MPI implementation not currently handled for " << resource_name);
470     }
471     else
472       INFOS("[getMachineFile] Error resource_name not found in resourcesList -> " << resource_name);
473   }
474   else
475     INFOS("[getMachineFile] Error parallelLib is not handled -> " << parallelLib);
476
477   return CORBA::string_dup(machine_file_name.c_str());
478 }