1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SALOME_ResourcesManager.hxx"
24 #include "Utils_ExceptHandlers.hxx"
25 #include "Utils_CorbaException.hxx"
43 #include <sys/types.h>
45 #include "utilities.h"
47 #define MAX_SIZE_FOR_HOSTNAME 256;
51 const char *SALOME_ResourcesManager::_ResourcesManagerNameInNS = "/ResourcesManager";
53 //=============================================================================
57 //=============================================================================
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))
65 MESSAGE("SALOME_ResourcesManager constructor");
67 _orb = CORBA::ORB::_duplicate(orb) ;
69 PortableServer::POAManager_var pman = poa->the_POAManager();
70 CORBA::PolicyList policies;
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);
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");
84 //=============================================================================
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.
93 //=============================================================================
95 SALOME_ResourcesManager::SALOME_ResourcesManager(CORBA::ORB_ptr orb,
96 PortableServer::POA_var poa,
97 SALOME_NamingService *ns) : _rm(new ResourcesManager_cpp())
99 MESSAGE("SALOME_ResourcesManager constructor");
101 _orb = CORBA::ORB::_duplicate(orb) ;
103 PortableServer::POAManager_var pman = poa->the_POAManager();
104 CORBA::PolicyList policies;
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();
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);
116 MESSAGE("SALOME_ResourcesManager constructor end");
119 //=============================================================================
121 * Standard Destructor
123 //=============================================================================
125 SALOME_ResourcesManager::~SALOME_ResourcesManager()
127 MESSAGE("SALOME_ResourcesManager destructor");
131 //=============================================================================
133 * shutdown all the containers, then the ContainerManager servant
135 //=============================================================================
137 void SALOME_ResourcesManager::Shutdown()
140 _NS->Destroy_Name(_ResourcesManagerNameInNS);
141 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
142 _poa->deactivate_object(oid);
146 * Return list of resources available (regarding content of CatalogResources.xml). And for each resource the number of proc available of it.
148 void SALOME_ResourcesManager::ListAllAvailableResources(Engines::ResourceList_out machines, Engines::IntegerList_out nbProcsOfMachines)
150 const MapOfParserResourcesType& zeList(_rm->GetList());
151 std::size_t sz(zeList.size());
152 std::vector<std::string> ret0(sz);
153 std::vector<int> ret1(sz);
156 for(MapOfParserResourcesType::const_iterator it=zeList.begin();it!=zeList.end();it++,i++)
158 const ParserResourcesType& elt((*it).second);
159 ret0[i]=elt.HostName;
160 //ret1[i]=elt.nbOfProc;
161 ret1[i]=elt.DataForSort._nbOfNodes*elt.DataForSort._nbOfProcPerNode;
164 machines=new Engines::ResourceList;
165 nbProcsOfMachines=new Engines::IntegerList;
166 machines->length(sz); nbProcsOfMachines->length(sz);
167 for(std::size_t j=0;j<sz;j++)
169 (*machines)[j]=CORBA::string_dup(ret0[j].c_str());
170 (*nbProcsOfMachines)[j]=ret1[j];
174 //=============================================================================
175 //! get the name of resources fitting the specified constraints (params)
177 * If hostname specified, check it is local or known in resources catalog.
180 * - select first machines with corresponding OS (all machines if
181 * parameter OS empty),
182 * - then select the sublist of machines on which the component is known
183 * (if the result is empty, that probably means that the inventory of
184 * components is probably not done, so give complete list from previous step)
186 //=============================================================================
188 Engines::ResourceList *
189 SALOME_ResourcesManager::GetFittingResources(const Engines::ResourceParameters& params)
191 //MESSAGE("ResourcesManager::GetFittingResources");
192 Engines::ResourceList_var ret;
195 resourceParams p = resourceParameters_CORBAtoCPP(params);
199 // Call C++ ResourceManager
200 std::vector <std::string> vec = _rm->GetFittingResources(p);
203 ret = resourceList_CPPtoCORBA(vec);
205 catch(const ResourcesException &ex)
207 INFOS("Caught exception in GetFittingResources C++: " << ex.msg);
208 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
214 //=============================================================================
216 * dynamically obtains the first machines
218 //=============================================================================
221 SALOME_ResourcesManager::FindFirst(const Engines::ResourceList& listOfResources)
224 std::vector<std::string> rl = resourceList_CORBAtoCPP(listOfResources);
226 return CORBA::string_dup(_rm->Find("first", rl).c_str());
230 SALOME_ResourcesManager::Find(const char* policy, const Engines::ResourceList& listOfResources)
233 std::vector<std::string> rl = resourceList_CORBAtoCPP(listOfResources);
235 return CORBA::string_dup(_rm->Find(policy, rl).c_str());
238 Engines::ResourceDefinition*
239 SALOME_ResourcesManager::GetResourceDefinition(const char * name)
241 Engines::ResourceDefinition_var resDef;
243 ParserResourcesType resource = _rm->GetResourcesDescr(name);
244 resDef = resourceDefinition_CPPtoCORBA(resource);
245 } catch (const exception & ex) {
246 INFOS("Caught exception in GetResourceDefinition: " << ex.what());
247 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::BAD_PARAM);
250 return resDef._retn();
254 SALOME_ResourcesManager::AddResource(const Engines::ResourceDefinition& new_resource,
255 CORBA::Boolean write,
256 const char * xml_file)
260 ParserResourcesType resource = resourceDefinition_CORBAtoCPP(new_resource);
261 _rm->AddResourceInCatalog(resource);
265 _rm->WriteInXmlFile(std::string(xml_file));
266 _rm->ParseXmlFiles();
269 catch (const SALOME_Exception & e)
271 INFOS("Error in AddResourceInCatalog: " << e);
272 THROW_SALOME_CORBA_EXCEPTION(e.what(), SALOME::BAD_PARAM);
274 catch (const ResourcesException & e)
276 INFOS("Error in AddResourceInCatalog: " << e.msg);
277 THROW_SALOME_CORBA_EXCEPTION(e.msg.c_str(), SALOME::BAD_PARAM);
282 SALOME_ResourcesManager::RemoveResource(const char * resource_name,
283 CORBA::Boolean write,
284 const char * xml_file)
288 _rm->DeleteResourceInCatalog(resource_name);
290 catch (const SALOME_Exception & e)
292 INFOS("Error in DeleteResourceInCatalog: " << e);
293 THROW_SALOME_CORBA_EXCEPTION(e.what(), SALOME::BAD_PARAM);
298 _rm->WriteInXmlFile(std::string(xml_file));
299 _rm->ParseXmlFiles();
304 SALOME_ResourcesManager::getMachineFile(const char * resource_name,
305 CORBA::Long nb_procs,
306 const char * parallelLib)
308 std::string machine_file_name("");
310 if (std::string(parallelLib) == "Dummy")
312 MESSAGE("[getMachineFile] parallelLib is Dummy");
313 MapOfParserResourcesType resourcesList = _rm->GetList();
314 if (resourcesList.find(std::string(resource_name)) != resourcesList.end())
316 ParserResourcesType resource = resourcesList[std::string(resource_name)];
318 // Check if resource is cluster or not
319 if (resource.ClusterMembersList.empty())
321 //It is not a cluster so we create a cluster with one machine
322 ParserResourcesType fake_node;
323 fake_node.HostName = resource.HostName;
324 fake_node.Protocol = resource.Protocol;
325 fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
326 fake_node.UserName = resource.UserName;
327 fake_node.AppliPath = resource.AppliPath;
328 fake_node.DataForSort = resource.DataForSort;
330 resource.ClusterMembersList.push_front(fake_node);
333 // Creating list of machines for creating the machine file
334 std::list<std::string> list_of_machines;
335 std::list<ParserResourcesType>::iterator cluster_it =
336 resource.ClusterMembersList.begin();
337 while (cluster_it != resource.ClusterMembersList.end())
339 // For each member of the cluster we add a nbOfNodes * nbOfProcPerNode in the list
340 unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes *
341 (*cluster_it).DataForSort._nbOfProcPerNode;
342 for (unsigned int i = 0; i < number_of_proc; i++)
343 list_of_machines.push_back((*cluster_it).HostName);
347 // Creating machine file
348 machine_file_name = tmpnam(NULL);
349 std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
351 CORBA::Long machine_number = 0;
352 std::list<std::string>::iterator it = list_of_machines.begin();
353 while (machine_number != nb_procs)
355 // Adding a new node to the machine file
356 machine_file << *it << std::endl;
360 if (it == list_of_machines.end())
361 it = list_of_machines.begin();
366 INFOS("[getMachineFile] Error resource_name not found in resourcesList -> " << resource_name);
368 else if (std::string(parallelLib) == "Mpi")
370 MESSAGE("[getMachineFile] parallelLib is Mpi");
372 MapOfParserResourcesType resourcesList = _rm->GetList();
373 if (resourcesList.find(std::string(resource_name)) != resourcesList.end())
375 ParserResourcesType resource = resourcesList[std::string(resource_name)];
376 // Check if resource is cluster or not
377 if (resource.ClusterMembersList.empty())
379 //It is not a cluster so we create a cluster with one machine
380 ParserResourcesType fake_node;
381 fake_node.HostName = resource.HostName;
382 fake_node.Protocol = resource.Protocol;
383 fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
384 fake_node.UserName = resource.UserName;
385 fake_node.AppliPath = resource.AppliPath;
386 fake_node.DataForSort = resource.DataForSort;
388 resource.ClusterMembersList.push_front(fake_node);
391 // Choose mpi implementation -> each MPI implementation has is own machinefile...
392 if (resource.mpi == lam)
394 // Creating machine file
395 machine_file_name = tmpnam(NULL);
396 std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
398 // We add all cluster machines to the file
399 std::list<ParserResourcesType>::iterator cluster_it =
400 resource.ClusterMembersList.begin();
401 while (cluster_it != resource.ClusterMembersList.end())
403 unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes *
404 (*cluster_it).DataForSort._nbOfProcPerNode;
405 machine_file << (*cluster_it).HostName << " cpu=" << number_of_proc << std::endl;
409 else if ((resource.mpi == openmpi) || (resource.mpi == ompi))
411 // Creating machine file
412 machine_file_name = tmpnam(NULL);
413 std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
415 // We add all cluster machines to the file
416 std::list<ParserResourcesType>::iterator cluster_it =
417 resource.ClusterMembersList.begin();
418 while (cluster_it != resource.ClusterMembersList.end())
420 unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes *
421 (*cluster_it).DataForSort._nbOfProcPerNode;
422 machine_file << (*cluster_it).HostName << " slots=" << number_of_proc << std::endl;
426 else if (resource.mpi == nompi)
428 INFOS("[getMachineFile] Error resource_name MPI implementation was defined for " << resource_name);
431 INFOS("[getMachineFile] Error resource_name MPI implementation not currently handled for " << resource_name);
434 INFOS("[getMachineFile] Error resource_name not found in resourcesList -> " << resource_name);
437 INFOS("[getMachineFile] Error parallelLib is not handled -> " << parallelLib);
439 return CORBA::string_dup(machine_file_name.c_str());