1 // Copyright (C) 2007-2016 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, PortableServer::POA_var poa, SALOME_NamingService *ns, const char *xmlFilePath) : _rm(xmlFilePath)
61 MESSAGE("SALOME_ResourcesManager constructor");
63 _orb = CORBA::ORB::_duplicate(orb) ;
65 PortableServer::POAManager_var pman = poa->the_POAManager();
66 CORBA::PolicyList policies;
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);
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");
80 //=============================================================================
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.
89 //=============================================================================
91 SALOME_ResourcesManager::SALOME_ResourcesManager(CORBA::ORB_ptr orb,
92 PortableServer::POA_var poa,
93 SALOME_NamingService *ns) : _rm()
95 MESSAGE("SALOME_ResourcesManager constructor");
97 _orb = CORBA::ORB::_duplicate(orb) ;
99 PortableServer::POAManager_var pman = poa->the_POAManager();
100 CORBA::PolicyList policies;
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();
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);
112 MESSAGE("SALOME_ResourcesManager constructor end");
115 //=============================================================================
117 * Standard Destructor
119 //=============================================================================
121 SALOME_ResourcesManager::~SALOME_ResourcesManager()
123 MESSAGE("SALOME_ResourcesManager destructor");
127 //=============================================================================
129 * shutdown all the containers, then the ContainerManager servant
131 //=============================================================================
133 void SALOME_ResourcesManager::Shutdown()
136 _NS->Destroy_Name(_ResourcesManagerNameInNS);
137 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
138 _poa->deactivate_object(oid);
142 * Return list of resources available (regarding containt of CatalogResources.xml). And for each resource the number of proc available of it.
144 void SALOME_ResourcesManager::ListAllAvailableResources(Engines::ResourceList_out machines, Engines::IntegerList_out nbProcsOfMachines)
146 const MapOfParserResourcesType& zeList(_rm.GetList());
147 std::size_t sz(zeList.size());
148 std::vector<std::string> ret0(sz);
149 std::vector<int> ret1(sz);
152 for(MapOfParserResourcesType::const_iterator it=zeList.begin();it!=zeList.end();it++,i++)
154 const ParserResourcesType& elt((*it).second);
155 ret0[i]=elt.HostName;
156 //ret1[i]=elt.nbOfProc;
157 ret1[i]=elt.DataForSort._nbOfNodes*elt.DataForSort._nbOfProcPerNode;
160 machines=new Engines::ResourceList;
161 nbProcsOfMachines=new Engines::IntegerList;
162 machines->length(sz); nbProcsOfMachines->length(sz);
163 for(std::size_t j=0;j<sz;j++)
165 (*machines)[j]=CORBA::string_dup(ret0[j].c_str());
166 (*nbProcsOfMachines)[j]=ret1[j];
170 //=============================================================================
171 //! get the name of resources fitting the specified constraints (params)
173 * If hostname specified, check it is local or known in resources catalog.
176 * - select first machines with corresponding OS (all machines if
177 * parameter OS empty),
178 * - then select the sublist of machines on which the component is known
179 * (if the result is empty, that probably means that the inventory of
180 * components is probably not done, so give complete list from previous step)
182 //=============================================================================
184 Engines::ResourceList *
185 SALOME_ResourcesManager::GetFittingResources(const Engines::ResourceParameters& params)
187 MESSAGE("ResourcesManager::GetFittingResources");
188 Engines::ResourceList_var ret;
191 resourceParams p = resourceParameters_CORBAtoCPP(params);
195 // Call C++ ResourceManager
196 std::vector <std::string> vec = _rm.GetFittingResources(p);
199 ret = resourceList_CPPtoCORBA(vec);
201 catch(const ResourcesException &ex)
203 INFOS("Caught exception in GetFittingResources C++: " << ex.msg);
204 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
210 //=============================================================================
212 * dynamically obtains the first machines
214 //=============================================================================
217 SALOME_ResourcesManager::FindFirst(const Engines::ResourceList& listOfResources)
220 std::vector<std::string> rl = resourceList_CORBAtoCPP(listOfResources);
222 return CORBA::string_dup(_rm.Find("first", rl).c_str());
226 SALOME_ResourcesManager::Find(const char* policy, const Engines::ResourceList& listOfResources)
229 std::vector<std::string> rl = resourceList_CORBAtoCPP(listOfResources);
231 return CORBA::string_dup(_rm.Find(policy, rl).c_str());
234 Engines::ResourceDefinition*
235 SALOME_ResourcesManager::GetResourceDefinition(const char * name)
237 Engines::ResourceDefinition_var resDef;
239 ParserResourcesType resource = _rm.GetResourcesDescr(name);
240 resDef = resourceDefinition_CPPtoCORBA(resource);
241 } catch (const exception & ex) {
242 INFOS("Caught exception in GetResourceDefinition: " << ex.what());
243 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::BAD_PARAM);
246 return resDef._retn();
250 SALOME_ResourcesManager::AddResource(const Engines::ResourceDefinition& new_resource,
251 CORBA::Boolean write,
252 const char * xml_file)
256 ParserResourcesType resource = resourceDefinition_CORBAtoCPP(new_resource);
257 _rm.AddResourceInCatalog(resource);
261 _rm.WriteInXmlFile(std::string(xml_file));
265 catch (const SALOME_Exception & e)
267 INFOS("Error in AddResourceInCatalog: " << e);
268 THROW_SALOME_CORBA_EXCEPTION(e.what(), SALOME::BAD_PARAM);
273 SALOME_ResourcesManager::RemoveResource(const char * resource_name,
274 CORBA::Boolean write,
275 const char * xml_file)
279 _rm.DeleteResourceInCatalog(resource_name);
281 catch (const SALOME_Exception & e)
283 INFOS("Error in DeleteResourceInCatalog: " << e);
284 THROW_SALOME_CORBA_EXCEPTION(e.what(), SALOME::BAD_PARAM);
289 _rm.WriteInXmlFile(std::string(xml_file));
295 SALOME_ResourcesManager::getMachineFile(const char * resource_name,
296 CORBA::Long nb_procs,
297 const char * parallelLib)
299 std::string machine_file_name("");
301 if (std::string(parallelLib) == "Dummy")
303 MESSAGE("[getMachineFile] parallelLib is Dummy");
304 MapOfParserResourcesType resourcesList = _rm.GetList();
305 if (resourcesList.find(std::string(resource_name)) != resourcesList.end())
307 ParserResourcesType resource = resourcesList[std::string(resource_name)];
309 // Check if resource is cluster or not
310 if (resource.ClusterMembersList.empty())
312 //It is not a cluster so we create a cluster with one machine
313 ParserResourcesType fake_node;
314 fake_node.HostName = resource.HostName;
315 fake_node.Protocol = resource.Protocol;
316 fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
317 fake_node.UserName = resource.UserName;
318 fake_node.AppliPath = resource.AppliPath;
319 fake_node.DataForSort = resource.DataForSort;
321 resource.ClusterMembersList.push_front(fake_node);
324 // Creating list of machines for creating the machine file
325 std::list<std::string> list_of_machines;
326 std::list<ParserResourcesType>::iterator cluster_it =
327 resource.ClusterMembersList.begin();
328 while (cluster_it != resource.ClusterMembersList.end())
330 // For each member of the cluster we add a nbOfNodes * nbOfProcPerNode in the list
331 unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes *
332 (*cluster_it).DataForSort._nbOfProcPerNode;
333 for (unsigned int i = 0; i < number_of_proc; i++)
334 list_of_machines.push_back((*cluster_it).HostName);
338 // Creating machine file
339 machine_file_name = tmpnam(NULL);
340 std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
342 CORBA::Long machine_number = 0;
343 std::list<std::string>::iterator it = list_of_machines.begin();
344 while (machine_number != nb_procs)
346 // Adding a new node to the machine file
347 machine_file << *it << std::endl;
351 if (it == list_of_machines.end())
352 it = list_of_machines.begin();
357 INFOS("[getMachineFile] Error resource_name not found in resourcesList -> " << resource_name);
359 else if (std::string(parallelLib) == "Mpi")
361 MESSAGE("[getMachineFile] parallelLib is Mpi");
363 MapOfParserResourcesType resourcesList = _rm.GetList();
364 if (resourcesList.find(std::string(resource_name)) != resourcesList.end())
366 ParserResourcesType resource = resourcesList[std::string(resource_name)];
367 // Check if resource is cluster or not
368 if (resource.ClusterMembersList.empty())
370 //It is not a cluster so we create a cluster with one machine
371 ParserResourcesType fake_node;
372 fake_node.HostName = resource.HostName;
373 fake_node.Protocol = resource.Protocol;
374 fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
375 fake_node.UserName = resource.UserName;
376 fake_node.AppliPath = resource.AppliPath;
377 fake_node.DataForSort = resource.DataForSort;
379 resource.ClusterMembersList.push_front(fake_node);
382 // Choose mpi implementation -> each MPI implementation has is own machinefile...
383 if (resource.mpi == lam)
385 // Creating machine file
386 machine_file_name = tmpnam(NULL);
387 std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
389 // We add all cluster machines to the file
390 std::list<ParserResourcesType>::iterator cluster_it =
391 resource.ClusterMembersList.begin();
392 while (cluster_it != resource.ClusterMembersList.end())
394 unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes *
395 (*cluster_it).DataForSort._nbOfProcPerNode;
396 machine_file << (*cluster_it).HostName << " cpu=" << number_of_proc << std::endl;
400 else if ((resource.mpi == openmpi) || (resource.mpi == ompi))
402 // Creating machine file
403 machine_file_name = tmpnam(NULL);
404 std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
406 // We add all cluster machines to the file
407 std::list<ParserResourcesType>::iterator cluster_it =
408 resource.ClusterMembersList.begin();
409 while (cluster_it != resource.ClusterMembersList.end())
411 unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes *
412 (*cluster_it).DataForSort._nbOfProcPerNode;
413 machine_file << (*cluster_it).HostName << " slots=" << number_of_proc << std::endl;
417 else if (resource.mpi == nompi)
419 INFOS("[getMachineFile] Error resource_name MPI implementation was defined for " << resource_name);
422 INFOS("[getMachineFile] Error resource_name MPI implementation not currenly handled for " << resource_name);
425 INFOS("[getMachineFile] Error resource_name not found in resourcesList -> " << resource_name);
428 INFOS("[getMachineFile] Error parallelLib is not handled -> " << parallelLib);
430 return CORBA::string_dup(machine_file_name.c_str());