1 // Copyright (C) 2007-2010 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.
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;
49 const char *SALOME_ResourcesManager::_ResourcesManagerNameInNS = "/ResourcesManager";
51 //=============================================================================
55 //=============================================================================
57 SALOME_ResourcesManager::
58 SALOME_ResourcesManager(CORBA::ORB_ptr orb,
59 PortableServer::POA_var poa,
60 SALOME_NamingService *ns,
61 const char *xmlFilePath) : _rm(xmlFilePath)
63 MESSAGE("SALOME_ResourcesManager constructor");
65 _orb = CORBA::ORB::_duplicate(orb) ;
66 _poa = PortableServer::POA::_duplicate(poa) ;
67 PortableServer::ObjectId_var id = _poa->activate_object(this);
68 CORBA::Object_var obj = _poa->id_to_reference(id);
69 Engines::ResourcesManager_var refContMan = Engines::ResourcesManager::_narrow(obj);
70 _NS->Register(refContMan,_ResourcesManagerNameInNS);
71 MESSAGE("SALOME_ResourcesManager constructor end");
74 //=============================================================================
76 * Standard constructor, parse resource file.
77 * - if ${APPLI} exists in environment,
78 * look for ${HOME}/${APPLI}/CatalogResources.xml
79 * - else look for default:
80 * ${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml
81 * - parse XML resource file.
83 //=============================================================================
85 SALOME_ResourcesManager::SALOME_ResourcesManager(CORBA::ORB_ptr orb,
86 PortableServer::POA_var poa,
87 SALOME_NamingService *ns) : _rm()
89 MESSAGE("SALOME_ResourcesManager constructor");
91 _orb = CORBA::ORB::_duplicate(orb) ;
92 _poa = PortableServer::POA::_duplicate(poa) ;
93 PortableServer::ObjectId_var id = _poa->activate_object(this);
94 CORBA::Object_var obj = _poa->id_to_reference(id);
95 Engines::ResourcesManager_var refContMan = Engines::ResourcesManager::_narrow(obj);
96 _NS->Register(refContMan,_ResourcesManagerNameInNS);
98 MESSAGE("SALOME_ResourcesManager constructor end");
101 //=============================================================================
103 * Standard Destructor
105 //=============================================================================
107 SALOME_ResourcesManager::~SALOME_ResourcesManager()
109 MESSAGE("SALOME_ResourcesManager destructor");
113 //=============================================================================
115 * shutdown all the containers, then the ContainerManager servant
117 //=============================================================================
119 void SALOME_ResourcesManager::Shutdown()
122 _NS->Destroy_Name(_ResourcesManagerNameInNS);
123 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
124 _poa->deactivate_object(oid);
127 //=============================================================================
128 //! get the name of resources fitting the specified constraints (params)
130 * If hostname specified, check it is local or known in resources catalog.
133 * - select first machines with corresponding OS (all machines if
134 * parameter OS empty),
135 * - then select the sublist of machines on which the component is known
136 * (if the result is empty, that probably means that the inventory of
137 * components is probably not done, so give complete list from previous step)
139 //=============================================================================
141 Engines::ResourceList *
142 SALOME_ResourcesManager::GetFittingResources(const Engines::ResourceParameters& params)
144 MESSAGE("ResourcesManager::GetFittingResources");
145 Engines::ResourceList * ret = new Engines::ResourceList;
149 p.name = params.name;
150 p.hostname = params.hostname;
152 p.nb_proc = params.nb_proc;
153 p.nb_node = params.nb_node;
154 p.nb_proc_per_node = params.nb_proc_per_node;
155 p.cpu_clock = params.cpu_clock;
156 p.mem_mb = params.mem_mb;
157 for(unsigned int i=0; i<params.componentList.length(); i++)
158 p.componentList.push_back(std::string(params.componentList[i]));
159 for(unsigned int i=0; i<params.resList.length(); i++)
160 p.resourceList.push_back(std::string(params.resList[i]));
164 // Call C++ ResourceManager
165 std::vector <std::string> vec = _rm.GetFittingResources(p);
168 ret->length(vec.size());
169 for(unsigned int i=0;i<vec.size();i++)
170 (*ret)[i] = (vec[i]).c_str();
172 catch(const ResourcesException &ex)
174 INFOS("Caught exception in GetFittingResources C++: " << ex.msg);
175 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
181 //=============================================================================
183 * dynamically obtains the first machines
185 //=============================================================================
188 SALOME_ResourcesManager::FindFirst(const Engines::ResourceList& listOfResources)
191 std::vector<std::string> rl;
192 for(unsigned int i=0; i<listOfResources.length(); i++)
193 rl.push_back(std::string(listOfResources[i]));
195 return CORBA::string_dup(_rm.Find("first", rl).c_str());
199 SALOME_ResourcesManager::Find(const char* policy, const Engines::ResourceList& listOfResources)
202 std::vector<std::string> rl;
203 for(unsigned int i=0; i<listOfResources.length(); i++)
204 rl.push_back(std::string(listOfResources[i]));
206 return CORBA::string_dup(_rm.Find(policy, rl).c_str());
209 Engines::ResourceDefinition*
210 SALOME_ResourcesManager::GetResourceDefinition(const char * name)
212 ParserResourcesType resource = _rm.GetResourcesDescr(name);
213 Engines::ResourceDefinition *p_ptr = new Engines::ResourceDefinition;
215 p_ptr->name = CORBA::string_dup(resource.Name.c_str());
216 p_ptr->hostname = CORBA::string_dup(resource.HostName.c_str());
217 if( resource.Protocol == rsh )
218 p_ptr->protocol = "rsh";
219 else if( resource.Protocol == ssh )
220 p_ptr->protocol = "ssh";
221 if( resource.ClusterInternalProtocol == rsh )
222 p_ptr->iprotocol = "rsh";
223 else if( resource.ClusterInternalProtocol == ssh )
224 p_ptr->iprotocol = "ssh";
225 p_ptr->username = CORBA::string_dup(resource.UserName.c_str());
226 p_ptr->applipath = CORBA::string_dup(resource.AppliPath.c_str());
227 p_ptr->componentList.length(resource.ComponentsList.size());
228 for(unsigned int i=0;i<resource.ComponentsList.size();i++)
229 p_ptr->componentList[i] = CORBA::string_dup(resource.ComponentsList[i].c_str());
230 p_ptr->OS = CORBA::string_dup(resource.OS.c_str());
231 p_ptr->mem_mb = resource.DataForSort._memInMB;
232 p_ptr->cpu_clock = resource.DataForSort._CPUFreqMHz;
233 p_ptr->nb_proc_per_node = resource.DataForSort._nbOfProcPerNode;
234 p_ptr->nb_node = resource.DataForSort._nbOfNodes;
236 if( resource.mpi == lam )
237 p_ptr->mpiImpl = "lam";
238 else if( resource.mpi == mpich1 )
239 p_ptr->mpiImpl = "mpich1";
240 else if( resource.mpi == mpich2 )
241 p_ptr->mpiImpl = "mpich2";
242 else if( resource.mpi == openmpi )
243 p_ptr->mpiImpl = "openmpi";
244 else if( resource.mpi == slurm )
245 p_ptr->mpiImpl = "slurm";
246 else if( resource.mpi == prun )
247 p_ptr->mpiImpl = "prun";
249 if( resource.Batch == pbs )
250 p_ptr->batch = "pbs";
251 else if( resource.Batch == lsf )
252 p_ptr->batch = "lsf";
253 else if( resource.Batch == sge )
254 p_ptr->batch = "sge";
255 else if( resource.Batch == ccc )
256 p_ptr->batch = "ccc";
257 else if( resource.Batch == ssh_batch )
258 p_ptr->batch = "ssh";
259 else if( resource.Batch == ll )
266 SALOME_ResourcesManager::AddResource(const Engines::ResourceDefinition& new_resource,
267 CORBA::Boolean write,
268 const char * xml_file)
270 ParserResourcesType resource;
271 resource.Name = new_resource.name.in();
272 resource.HostName = new_resource.hostname.in();
273 resource.OS = new_resource.OS.in();
274 resource.AppliPath = new_resource.applipath.in();
275 resource.DataForSort._memInMB = new_resource.mem_mb;
276 resource.DataForSort._CPUFreqMHz = new_resource.cpu_clock;
277 resource.DataForSort._nbOfNodes = new_resource.nb_node;
278 resource.DataForSort._nbOfProcPerNode = new_resource.nb_proc_per_node;
279 resource.UserName = new_resource.username.in();
281 std::string aBatch = new_resource.batch.in();
283 resource.Batch = pbs;
284 else if (aBatch == "lsf")
285 resource.Batch = lsf;
286 else if (aBatch == "sge")
287 resource.Batch = sge;
288 else if (aBatch == "ccc")
289 resource.Batch = ccc;
290 else if (aBatch == "ssh_batch")
291 resource.Batch = ssh_batch;
292 else if (aBatch == "ll")
294 else if (aBatch == "")
295 resource.Batch = none;
297 INFOS("Bad Batch definition in AddResource: " << aBatch);
298 std::string message("Bad Batch definition in AddResource: ");
300 THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
303 std::string anMpi = new_resource.mpiImpl.in();
306 else if (anMpi == "mpich1")
307 resource.mpi = mpich1;
308 else if (anMpi == "mpich2")
309 resource.mpi = mpich2;
310 else if (anMpi == "openmpi")
311 resource.mpi = openmpi;
312 else if (anMpi == "slurm")
313 resource.mpi = slurm;
314 else if (anMpi == "prun")
316 else if (anMpi == "")
317 resource.mpi = nompi;
319 INFOS("Bad MPI definition in AddResource: " << anMpi);
320 std::string message("Bad MPI definition in AddResource: ");
322 THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
325 std::string mode_str = new_resource.mode.in();
326 if (mode_str == "interactive")
327 resource.Mode = interactive;
328 else if (mode_str == "batch")
329 resource.Mode = batch;
330 else if (mode_str == "")
331 resource.Mode = interactive;
333 INFOS("Bad mode definition in AddResource: " << mode_str);
334 std::string message("Bad mode definition in AddResource: ");
336 THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
339 std::string protocol = new_resource.protocol.in();
340 if (protocol == "rsh")
341 resource.Protocol = rsh;
342 else if (protocol == "ssh")
343 resource.Protocol = ssh;
344 else if (protocol == "")
345 resource.Protocol = rsh;
347 INFOS("Bad protocol definition in AddResource: " << protocol);
348 std::string message("Bad protocol definition in AddResource: ");
350 THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
353 std::string iprotocol = new_resource.iprotocol.in();
354 if (iprotocol == "rsh")
355 resource.ClusterInternalProtocol = rsh;
356 else if (iprotocol == "ssh")
357 resource.ClusterInternalProtocol = ssh;
358 else if (iprotocol == "")
359 resource.ClusterInternalProtocol = rsh;
361 INFOS("Bad iprotocol definition in AddResource: " << iprotocol);
362 std::string message("Bad iprotocol definition in AddResource: ");
363 message += iprotocol;
364 THROW_SALOME_CORBA_EXCEPTION(message.c_str(),SALOME::BAD_PARAM);
367 for (CORBA::ULong i = 0; i < new_resource.componentList.length(); i++)
368 resource.ComponentsList.push_back(new_resource.componentList[i].in());
370 _rm.AddResourceInCatalog(resource);
374 _rm.WriteInXmlFile(std::string(xml_file));
380 SALOME_ResourcesManager::RemoveResource(const char * resource_name,
381 CORBA::Boolean write,
382 const char * xml_file)
384 _rm.DeleteResourceInCatalog(resource_name);
387 _rm.WriteInXmlFile(std::string(xml_file));
393 SALOME_ResourcesManager::getMachineFile(std::string resource_name,
394 CORBA::Long nb_procs,
395 std::string parallelLib)
397 std::string machine_file_name("");
399 if (parallelLib == "Dummy")
401 MESSAGE("[getMachineFile] parallelLib is Dummy");
402 MapOfParserResourcesType resourcesList = _rm.GetList();
403 if (resourcesList.find(resource_name) != resourcesList.end())
405 ParserResourcesType resource = resourcesList[resource_name];
407 // Check if resource is cluster or not
408 if (resource.ClusterMembersList.empty())
410 //It is not a cluster so we create a cluster with one machine
411 ParserResourcesClusterMembersType fake_node;
412 fake_node.HostName = resource.HostName;
413 fake_node.Protocol = resource.Protocol;
414 fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
415 fake_node.UserName = resource.UserName;
416 fake_node.AppliPath = resource.AppliPath;
417 fake_node.DataForSort = resource.DataForSort;
419 resource.ClusterMembersList.push_front(fake_node);
422 // Creating list of machines for creating the machine file
423 std::list<std::string> list_of_machines;
424 std::list<ParserResourcesClusterMembersType>::iterator cluster_it =
425 resource.ClusterMembersList.begin();
426 while (cluster_it != resource.ClusterMembersList.end())
428 // For each member of the cluster we add a nbOfNodes * nbOfProcPerNode in the list
429 unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes *
430 (*cluster_it).DataForSort._nbOfProcPerNode;
431 for (unsigned int i = 0; i < number_of_proc; i++)
432 list_of_machines.push_back((*cluster_it).HostName);
436 // Creating machine file
437 machine_file_name = tmpnam(NULL);
438 std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
440 CORBA::Long machine_number = 0;
441 std::list<std::string>::iterator it = list_of_machines.begin();
442 while (machine_number != nb_procs)
444 // Adding a new node to the machine file
445 machine_file << *it << std::endl;
449 if (it == list_of_machines.end())
450 it = list_of_machines.begin();
455 INFOS("[getMachineFile] Error resource_name not found in resourcesList -> " << resource_name);
457 else if (parallelLib == "Mpi")
459 MESSAGE("[getMachineFile] parallelLib is Mpi");
461 MapOfParserResourcesType resourcesList = _rm.GetList();
462 if (resourcesList.find(resource_name) != resourcesList.end())
464 ParserResourcesType resource = resourcesList[resource_name];
465 // Check if resource is cluster or not
466 if (resource.ClusterMembersList.empty())
468 //It is not a cluster so we create a cluster with one machine
469 ParserResourcesClusterMembersType fake_node;
470 fake_node.HostName = resource.HostName;
471 fake_node.Protocol = resource.Protocol;
472 fake_node.ClusterInternalProtocol = resource.ClusterInternalProtocol;
473 fake_node.UserName = resource.UserName;
474 fake_node.AppliPath = resource.AppliPath;
475 fake_node.DataForSort = resource.DataForSort;
477 resource.ClusterMembersList.push_front(fake_node);
480 // Choose mpi implementation -> each MPI implementation has is own machinefile...
481 if (resource.mpi == lam)
483 // Creating machine file
484 machine_file_name = tmpnam(NULL);
485 std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
487 // We add all cluster machines to the file
488 std::list<ParserResourcesClusterMembersType>::iterator cluster_it =
489 resource.ClusterMembersList.begin();
490 while (cluster_it != resource.ClusterMembersList.end())
492 unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes *
493 (*cluster_it).DataForSort._nbOfProcPerNode;
494 machine_file << (*cluster_it).HostName << " cpu=" << number_of_proc << std::endl;
498 else if (resource.mpi == openmpi)
500 // Creating machine file
501 machine_file_name = tmpnam(NULL);
502 std::ofstream machine_file(machine_file_name.c_str(), std::ios_base::out);
504 // We add all cluster machines to the file
505 std::list<ParserResourcesClusterMembersType>::iterator cluster_it =
506 resource.ClusterMembersList.begin();
507 while (cluster_it != resource.ClusterMembersList.end())
509 unsigned int number_of_proc = (*cluster_it).DataForSort._nbOfNodes *
510 (*cluster_it).DataForSort._nbOfProcPerNode;
511 machine_file << (*cluster_it).HostName << " slots=" << number_of_proc << std::endl;
515 else if (resource.mpi == nompi)
517 INFOS("[getMachineFile] Error resource_name MPI implementation was defined for " << resource_name);
520 INFOS("[getMachineFile] Error resource_name MPI implementation not currenly handled for " << resource_name);
523 INFOS("[getMachineFile] Error resource_name not found in resourcesList -> " << resource_name);
526 INFOS("[getMachineFile] Error parallelLib is not handled -> " << parallelLib);
528 return machine_file_name;