Salome HOME
Test EXPORTS definition with target name as suggested by cmake
[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::SalomeLauncher_var refContMan =
70     Engines::SalomeLauncher::_narrow(obj);
71
72   _NS->Register(refContMan,_ResourcesManagerNameInNS);
73   MESSAGE("SALOME_ResourcesManager constructor end");
74 }
75
76 //=============================================================================
77 /*!
78  *  Standard constructor, parse resource file.
79  *  - if ${APPLI} exists in environment,
80  *    look for ${HOME}/${APPLI}/CatalogResources.xml
81  *  - else look for default:
82  *    ${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml
83  *  - parse XML resource file.
84  */ 
85 //=============================================================================
86
87 SALOME_ResourcesManager::SALOME_ResourcesManager(CORBA::ORB_ptr orb, 
88                                                  PortableServer::POA_var poa, 
89                                                  SALOME_NamingService *ns) : _rm()
90 {
91   MESSAGE("SALOME_ResourcesManager constructor");
92   _NS = ns;
93   _orb = CORBA::ORB::_duplicate(orb) ;
94   _poa = PortableServer::POA::_duplicate(poa) ;
95   PortableServer::ObjectId_var id = _poa->activate_object(this);
96   CORBA::Object_var obj = _poa->id_to_reference(id);
97   Engines::ResourcesManager_var refContMan = Engines::ResourcesManager::_narrow(obj);
98   _NS->Register(refContMan,_ResourcesManagerNameInNS);
99
100   MESSAGE("SALOME_ResourcesManager constructor end");
101 }
102
103 //=============================================================================
104 /*!
105  *  Standard Destructor
106  */ 
107 //=============================================================================
108
109 SALOME_ResourcesManager::~SALOME_ResourcesManager()
110 {
111   MESSAGE("SALOME_ResourcesManager destructor");
112 }
113
114
115 //=============================================================================
116 /*! CORBA method:
117  *  shutdown all the containers, then the ContainerManager servant
118  */
119 //=============================================================================
120
121 void SALOME_ResourcesManager::Shutdown()
122 {
123   MESSAGE("Shutdown");
124   _NS->Destroy_Name(_ResourcesManagerNameInNS);
125   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
126   _poa->deactivate_object(oid);
127   //_remove_ref();
128 }
129
130 //=============================================================================
131 /*!
132  *  get the list of name of ressources fitting for the specified module.
133  *  If hostname specified, check it is local or known in resources catalog.
134  *
135  *  Else
136  *  - select first machines with corresponding OS (all machines if
137  *    parameter OS empty),
138  *  - then select the sublist of machines on witch the module is known
139  *    (if the result is empty, that probably means that the inventory of
140  *    modules is probably not done, so give complete list from previous step)
141  */ 
142 //=============================================================================
143
144 Engines::MachineList *
145 SALOME_ResourcesManager::GetFittingResources(const Engines::MachineParameters& params,
146                                              const Engines::CompoList& componentList)
147 {
148 //   MESSAGE("ResourcesManager::GetFittingResources");
149   machineParams p;
150   p.hostname = params.hostname;
151   p.OS = params.OS;
152   p.nb_node = params.nb_node;
153   p.nb_proc_per_node = params.nb_proc_per_node;
154   p.cpu_clock = params.cpu_clock;
155   p.mem_mb = params.mem_mb;
156
157   vector<string> cl;
158   for(unsigned int i=0;i<componentList.length();i++)
159     cl.push_back(string(componentList[i]));
160   
161   Engines::MachineList *ret=new Engines::MachineList;
162   try{
163       vector <std::string> vec = _rm.GetFittingResources(p,cl);
164       ret->length(vec.size());
165       for(unsigned int i=0;i<vec.size();i++)
166         (*ret)[i] = (vec[i]).c_str();
167   }
168   catch(const ResourcesException &ex){
169     INFOS("Caught exception.");
170     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
171   }  
172
173   return ret;
174 }
175
176 //=============================================================================
177 /*!
178  *  dynamically obtains the first machines
179  */ 
180 //=============================================================================
181
182 char *
183 SALOME_ResourcesManager::FindFirst(const Engines::MachineList& listOfMachines)
184 {
185   vector<string> ml;
186   for(unsigned int i=0;i<listOfMachines.length();i++)
187     ml.push_back(string(listOfMachines[i]));
188
189   return CORBA::string_dup(_rm.FindFirst(ml).c_str());
190 }
191
192 Engines::MachineParameters* SALOME_ResourcesManager::GetMachineParameters(const char *hostname)
193 {
194   ParserResourcesType resource = _rm.GetResourcesList(string(hostname));
195   Engines::MachineParameters *p_ptr = new Engines::MachineParameters;
196   p_ptr->container_name = CORBA::string_dup("");
197   p_ptr->hostname = CORBA::string_dup(resource.HostName.c_str());
198   p_ptr->alias = CORBA::string_dup(resource.Alias.c_str());
199   if( resource.Protocol == rsh )
200     p_ptr->protocol = "rsh";
201   else if( resource.Protocol == ssh )
202     p_ptr->protocol = "ssh";
203   p_ptr->username = CORBA::string_dup(resource.UserName.c_str());
204   p_ptr->applipath = CORBA::string_dup(resource.AppliPath.c_str());
205   p_ptr->modList.length(resource.ModulesList.size());
206   for(unsigned int i=0;i<resource.ModulesList.size();i++)
207     p_ptr->modList[i] = CORBA::string_dup(resource.ModulesList[i].c_str());
208   p_ptr->OS = CORBA::string_dup(resource.OS.c_str());
209   p_ptr->mem_mb = resource.DataForSort._memInMB;
210   p_ptr->cpu_clock = resource.DataForSort._CPUFreqMHz;
211   p_ptr->nb_proc_per_node = resource.DataForSort._nbOfProcPerNode;
212   p_ptr->nb_node = resource.DataForSort._nbOfNodes;
213
214   if( resource.mpi == lam )
215     p_ptr->mpiImpl = "lam";
216   else if( resource.mpi == mpich1 )
217     p_ptr->mpiImpl = "mpich1";
218   else if( resource.mpi == mpich2 )
219     p_ptr->mpiImpl = "mpich2";
220   else if( resource.mpi == openmpi )
221     p_ptr->mpiImpl = "openmpi";
222   else if( resource.mpi == slurm )
223     p_ptr->mpiImpl = "slurm";
224   else if( resource.mpi == prun )
225     p_ptr->mpiImpl = "prun";
226
227   if( resource.Batch == pbs )
228     p_ptr->batch = "pbs";
229   else if( resource.Batch == lsf )
230     p_ptr->batch = "lsf";
231   else if( resource.Batch == sge )
232     p_ptr->batch = "sge";
233
234   p_ptr->nb_component_nodes=1;
235
236   return p_ptr;
237 }
238