Salome HOME
PR: default policy
[modules/kernel.git] / src / ResourcesManager / SALOME_ResourcesManager.cxx
1 //  Copyright (C) 2007-2008  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.
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 #include "SALOME_ResourcesManager.hxx" 
23 #include "Utils_ExceptHandlers.hxx"
24 #include "Utils_CorbaException.hxx"
25 #include "OpUtil.hxx"
26
27 #include <stdlib.h>
28 #ifndef WIN32
29 #include <unistd.h>
30 #else
31 #include <io.h>
32 #include <process.h>
33 #endif
34 #include <fstream>
35 #include <iostream>
36 #include <sstream>
37 #include <string.h>
38 #include <map>
39 #include <list>
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include "utilities.h"
44
45 #define MAX_SIZE_FOR_HOSTNAME 256;
46
47 using namespace std;
48
49 const char *SALOME_ResourcesManager::_ResourcesManagerNameInNS = "/ResourcesManager";
50
51 //=============================================================================
52 /*!
53  * just for test
54  */ 
55 //=============================================================================
56
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)
62 {
63   MESSAGE("SALOME_ResourcesManager constructor");
64   _NS = ns;
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");
72 }
73
74 //=============================================================================
75 /*!
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.
82  */ 
83 //=============================================================================
84
85 SALOME_ResourcesManager::SALOME_ResourcesManager(CORBA::ORB_ptr orb, 
86                                                  PortableServer::POA_var poa, 
87                                                  SALOME_NamingService *ns) : _rm()
88 {
89   MESSAGE("SALOME_ResourcesManager constructor");
90   _NS = ns;
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);
97
98   MESSAGE("SALOME_ResourcesManager constructor end");
99 }
100
101 //=============================================================================
102 /*!
103  *  Standard Destructor
104  */ 
105 //=============================================================================
106
107 SALOME_ResourcesManager::~SALOME_ResourcesManager()
108 {
109   MESSAGE("SALOME_ResourcesManager destructor");
110 }
111
112
113 //=============================================================================
114 /*! CORBA method:
115  *  shutdown all the containers, then the ContainerManager servant
116  */
117 //=============================================================================
118
119 void SALOME_ResourcesManager::Shutdown()
120 {
121   MESSAGE("Shutdown");
122   _NS->Destroy_Name(_ResourcesManagerNameInNS);
123   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
124   _poa->deactivate_object(oid);
125 }
126
127 //=============================================================================
128 /*!
129  *  get the list of name of ressources fitting for the specified component.
130  *  If hostname specified, check it is local or known in resources catalog.
131  *
132  *  Else
133  *  - select first machines with corresponding OS (all machines if
134  *    parameter OS empty),
135  *  - then select the sublist of machines on witch 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)
138  */ 
139 //=============================================================================
140
141 Engines::MachineList *
142 SALOME_ResourcesManager::GetFittingResources(const Engines::MachineParameters& params,
143                                              const Engines::CompoList& componentList)
144 {
145 //   MESSAGE("ResourcesManager::GetFittingResources");
146   machineParams p;
147   p.hostname = params.hostname;
148   p.OS = params.OS;
149   p.nb_node = params.nb_node;
150   p.nb_proc_per_node = params.nb_proc_per_node;
151   p.cpu_clock = params.cpu_clock;
152   p.mem_mb = params.mem_mb;
153
154   vector<string> cl;
155   for(unsigned int i=0;i<componentList.length();i++)
156     cl.push_back(string(componentList[i]));
157   
158   Engines::MachineList *ret=new Engines::MachineList;
159   try{
160       vector <std::string> vec = _rm.GetFittingResources(p,cl);
161       ret->length(vec.size());
162       for(unsigned int i=0;i<vec.size();i++)
163         (*ret)[i] = (vec[i]).c_str();
164   }
165   catch(const ResourcesException &ex){
166     INFOS("Caught exception.");
167     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
168   }  
169
170   return ret;
171 }
172
173 //=============================================================================
174 /*!
175  *  dynamically obtains the first machines
176  */ 
177 //=============================================================================
178
179 char *
180 SALOME_ResourcesManager::FindFirst(const Engines::MachineList& listOfMachines)
181 {
182   vector<string> ml;
183   for(unsigned int i=0;i<listOfMachines.length();i++)
184     ml.push_back(string(listOfMachines[i]));
185
186   return CORBA::string_dup(_rm.Find("first",ml).c_str());
187 }
188
189 char *
190 SALOME_ResourcesManager::Find(const char* policy, const Engines::MachineList& listOfMachines)
191 {
192   vector<string> ml;
193   for(unsigned int i=0;i<listOfMachines.length();i++)
194     ml.push_back(string(listOfMachines[i]));
195   return CORBA::string_dup(_rm.Find(policy,ml).c_str());
196 }
197
198 Engines::MachineDefinition* SALOME_ResourcesManager::GetMachineParameters(const char *hostname)
199 {
200   ParserResourcesType resource = _rm.GetResourcesList(string(hostname));
201   Engines::MachineDefinition *p_ptr = new Engines::MachineDefinition;
202   p_ptr->hostname = CORBA::string_dup(resource.HostName.c_str());
203   p_ptr->alias = CORBA::string_dup(resource.Alias.c_str());
204   if( resource.Protocol == rsh )
205     p_ptr->protocol = "rsh";
206   else if( resource.Protocol == ssh )
207     p_ptr->protocol = "ssh";
208   p_ptr->username = CORBA::string_dup(resource.UserName.c_str());
209   p_ptr->applipath = CORBA::string_dup(resource.AppliPath.c_str());
210   p_ptr->componentList.length(resource.ComponentsList.size());
211   for(unsigned int i=0;i<resource.ComponentsList.size();i++)
212     p_ptr->componentList[i] = CORBA::string_dup(resource.ComponentsList[i].c_str());
213   p_ptr->OS = CORBA::string_dup(resource.OS.c_str());
214   p_ptr->mem_mb = resource.DataForSort._memInMB;
215   p_ptr->cpu_clock = resource.DataForSort._CPUFreqMHz;
216   p_ptr->nb_proc_per_node = resource.DataForSort._nbOfProcPerNode;
217   p_ptr->nb_node = resource.DataForSort._nbOfNodes;
218
219   if( resource.mpi == lam )
220     p_ptr->mpiImpl = "lam";
221   else if( resource.mpi == mpich1 )
222     p_ptr->mpiImpl = "mpich1";
223   else if( resource.mpi == mpich2 )
224     p_ptr->mpiImpl = "mpich2";
225   else if( resource.mpi == openmpi )
226     p_ptr->mpiImpl = "openmpi";
227   else if( resource.mpi == slurm )
228     p_ptr->mpiImpl = "slurm";
229   else if( resource.mpi == prun )
230     p_ptr->mpiImpl = "prun";
231
232   if( resource.Batch == pbs )
233     p_ptr->batch = "pbs";
234   else if( resource.Batch == lsf )
235     p_ptr->batch = "lsf";
236   else if( resource.Batch == sge )
237     p_ptr->batch = "sge";
238
239   p_ptr->nb_component_nodes=1;
240
241   return p_ptr;
242 }
243