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