1 // Copyright (C) 2007-2008 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
24 #include <Batch/Batch_Date.hxx>
25 #include <Batch/Batch_FactBatchManager_eLSF.hxx>
26 #include <Batch/Batch_FactBatchManager_ePBS.hxx>
27 #include <Batch/Batch_BatchManager_eClient.hxx>
28 #include <Batch/Batch_FactBatchManager_eSGE.hxx>
29 #include <Batch/Batch_FactBatchManager_eSSH.hxx>
32 #include "SALOME_Launcher_Handler.hxx"
33 #include "Launcher.hxx"
34 #include "Launcher_Job_Command.hxx"
42 //=============================================================================
46 * Define a CORBA single thread policy for the server, which avoid to deal
47 * with non thread-safe usage like Change_Directory in SALOME naming service
49 //=============================================================================
50 Launcher_cpp::Launcher_cpp()
52 LAUNCHER_MESSAGE("Launcher_cpp constructor");
54 _job_cpt_mutex = new pthread_mutex_t();
55 pthread_mutex_init(_job_cpt_mutex, NULL);
58 //=============================================================================
62 //=============================================================================
63 Launcher_cpp::~Launcher_cpp()
65 LAUNCHER_MESSAGE("Launcher_cpp destructor");
67 std::map < string, Batch::BatchManager_eClient * >::const_iterator it1;
68 for(it1=_batchmap.begin();it1!=_batchmap.end();it1++)
70 std::map<int, Launcher::Job *>::const_iterator it_job;
71 for(it_job = _launcher_job_map.begin(); it_job != _launcher_job_map.end(); it_job++)
72 delete it_job->second;
75 pthread_mutex_destroy(_job_cpt_mutex);
76 delete _job_cpt_mutex;
81 //=============================================================================
83 * Add a job into the launcher - check resource and choose one
85 //=============================================================================
87 Launcher_cpp::createJob(Launcher::Job * new_job)
89 LAUNCHER_MESSAGE("Creating a new job");
91 // First step take a resource
92 std::vector<std::string> ResourceList;
93 resourceParams params = new_job->getResourceRequiredParams();
95 ResourceList = _ResManager->GetFittingResources(params);
97 catch(const ResourcesException &ex){
98 throw LauncherException(ex.msg.c_str());
100 if (ResourceList.size() == 0)
102 LAUNCHER_INFOS("No adequate resource found for the job, number " << new_job->getNumber() << " - deleting it");
104 throw LauncherException("No resource found the job");
107 // Second step configure the job with the resource selected - the first of the list
108 ParserResourcesType resource_definition = _ResManager->GetResourcesDescr(ResourceList[0]);
110 // Set resource definition to the job
111 // The job will check if the definitions needed
114 new_job->setResourceDefinition(resource_definition);
116 catch(const LauncherException &ex)
118 LAUNCHER_INFOS("Error in the definition of the resource, mess: " << ex.msg);
123 // Third step search batch manager for the resource into the map -> instanciate one if does not exist
124 std::string resource_name = resource_definition.Name;
125 std::map<std::string, Batch::BatchManager_eClient *>::const_iterator it = _batchmap.find(resource_name);
126 if(it == _batchmap.end())
130 _batchmap[resource_name] = FactoryBatchManager(resource_definition);
132 catch(const LauncherException &ex)
134 LAUNCHER_INFOS("Error during creation of the batch manager of the resource, mess: " << ex.msg);
138 catch(const Batch::EmulationException &ex)
140 LAUNCHER_INFOS("Error during creation of the batch manager of the resource, mess: " << ex.message);
142 throw LauncherException(ex.message);
147 // Final step - add job to the jobs map
148 pthread_mutex_lock(_job_cpt_mutex);
149 new_job->setNumber(_job_cpt);
151 pthread_mutex_unlock(_job_cpt_mutex);
152 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
153 if (it_job == _launcher_job_map.end())
154 _launcher_job_map[new_job->getNumber()] = new_job;
157 LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
159 throw LauncherException("A job as already the same id - job is not created !");
161 LAUNCHER_MESSAGE("New Job created");
164 //=============================================================================
168 //=============================================================================
170 Launcher_cpp::launchJob(int job_id)
172 LAUNCHER_MESSAGE("Launch a job");
174 // Check if job exist
175 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
176 if (it_job == _launcher_job_map.end())
178 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
179 throw LauncherException("Cannot find the job, is it created ?");
182 Launcher::Job * job = it_job->second;
184 // Check job state (cannot launch a job already launched...)
185 if (job->getState() != "CREATED")
187 LAUNCHER_INFOS("Bad state of the job: " << job->getState());
188 throw LauncherException("Bad state of the job: " + job->getState());
191 std::string resource_name = job->getResourceDefinition().Name;
193 Batch::JobId batch_manager_job_id = _batchmap[resource_name]->submitJob(*(job->getBatchJob()));
194 job->setBatchManagerJobId(batch_manager_job_id);
195 job->setState("QUEUED");
197 catch(const Batch::EmulationException &ex)
199 LAUNCHER_INFOS("Job is not launched, exception in submitJob: " << ex.message);
200 throw LauncherException(ex.message.c_str());
202 LAUNCHER_MESSAGE("Job launched");
205 //=============================================================================
209 //=============================================================================
211 Launcher_cpp::getJobState(int job_id)
213 LAUNCHER_MESSAGE("Get job state");
215 // Check if job exist
216 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
217 if (it_job == _launcher_job_map.end())
219 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
220 throw LauncherException("Cannot find the job, is it created ?");
223 Launcher::Job * job = it_job->second;
224 std::string state = job->updateJobState();
226 return state.c_str();
229 //=============================================================================
231 * Get Job result - the result directory could be changed
233 //=============================================================================
235 Launcher_cpp::getJobResults(int job_id, std::string directory)
237 LAUNCHER_MESSAGE("Get Job results");
239 // Check if job exist
240 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
241 if (it_job == _launcher_job_map.end())
243 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
244 throw LauncherException("Cannot find the job, is it created ?");
247 Launcher::Job * job = it_job->second;
248 std::string resource_name = job->getResourceDefinition().Name;
252 _batchmap[resource_name]->importOutputFiles(*(job->getBatchJob()), directory);
254 _batchmap[resource_name]->importOutputFiles(*(job->getBatchJob()), job->getResultDirectory());
256 catch(const Batch::EmulationException &ex)
258 LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
259 throw LauncherException(ex.message.c_str());
261 LAUNCHER_MESSAGE("getJobResult ended");
264 //=============================================================================
266 * Remove the job - into the Launcher and its batch manager
268 //=============================================================================
270 Launcher_cpp::removeJob(int job_id)
272 LAUNCHER_MESSAGE("Remove Job");
274 // Check if job exist
275 std::map<int, Launcher::Job *>::iterator it_job = _launcher_job_map.find(job_id);
276 if (it_job == _launcher_job_map.end())
278 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
279 throw LauncherException("Cannot find the job, is it created ?");
282 delete it_job->second;
283 _launcher_job_map.erase(it_job);
286 //=============================================================================
288 * create a launcher job based on a file
289 * \param xmlExecuteFile : to define the execution on the batch cluster
291 //=============================================================================
293 Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
294 const std::string clusterName)
296 LAUNCHER_MESSAGE("Begin of Launcher_cpp::createJobWithFile");
299 ParserLauncherType job_params = ParseXmlFile(xmlExecuteFile);
301 // Creating a new job
302 Launcher::Job_Command * new_job = new Launcher::Job_Command();
303 new_job->setJobFile(job_params.Command);
304 new_job->setLocalDirectory(job_params.RefDirectory);
305 new_job->setWorkDirectory(job_params.MachinesList[clusterName].WorkDirectory);
306 new_job->setEnvFile(job_params.MachinesList[clusterName].EnvFile);
308 for(int i=0; i < job_params.InputFile.size(); i++)
309 new_job->add_in_file(job_params.InputFile[i]);
310 for(int i=0; i < job_params.OutputFile.size();i++)
311 new_job->add_out_file(job_params.OutputFile[i]);
314 p.hostname = clusterName;
315 p.nb_proc = job_params.NbOfProcesses;
316 new_job->setResourceRequiredParams(p);
319 return new_job->getNumber();
322 //=============================================================================
324 * Factory to instanciate the good batch manager for choosen cluster.
326 //=============================================================================
327 Batch::BatchManager_eClient *
328 Launcher_cpp::FactoryBatchManager(ParserResourcesType& params)
331 Batch::CommunicationProtocolType protocol;
332 Batch::FactBatchManager_eClient* fact;
334 int nb_proc_per_node = params.DataForSort._nbOfProcPerNode;
335 std::string hostname = params.HostName;
337 switch(params.Protocol)
340 protocol = Batch::RSH;
343 protocol = Batch::SSH;
346 throw LauncherException("Unknown protocol for this resource");
374 std::string message = "Instanciation of batch manager of type: ";
375 switch( params.Batch )
379 fact = new Batch::FactBatchManager_ePBS;
383 fact = new Batch::FactBatchManager_eLSF;
387 fact = new Batch::FactBatchManager_eSGE;
391 fact = new Batch::FactBatchManager_eSSH;
394 LAUNCHER_MESSAGE("Bad batch description of the resource: Batch = " << params.Batch);
395 throw LauncherException("No batchmanager for that cluster - Bad batch description of the resource");
397 LAUNCHER_MESSAGE(message);
398 return (*fact)(hostname.c_str(), protocol, mpi.c_str(), nb_proc_per_node);
401 //----------------------------------------------------------
402 // Without LIBBATCH - Launcher_cpp do nothing...
403 //----------------------------------------------------------
407 Launcher_cpp::createJob(Launcher::Job * new_job)
409 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
411 throw LauncherException("Method Launcher_cpp::createJob is not available "
412 "(libBatch was not present at compilation time)");
416 Launcher_cpp::launchJob(int job_id)
418 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot launch a job !!!");
419 throw LauncherException("Method Launcher_cpp::launchJob is not available "
420 "(libBatch was not present at compilation time)");
424 Launcher_cpp::getJobState(int job_id)
426 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job state!!!");
427 throw LauncherException("Method Launcher_cpp::getJobState is not available "
428 "(libBatch was not present at compilation time)");
432 Launcher_cpp::getJobResults(int job_id, std::string directory)
434 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job results!!!");
435 throw LauncherException("Method Launcher_cpp::getJobResults is not available "
436 "(libBatch was not present at compilation time)");
440 Launcher_cpp::removeJob(int job_id)
442 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot remove job!!!");
443 throw LauncherException("Method Launcher_cpp::removeJob is not available "
444 "(libBatch was not present at compilation time)");
448 Launcher_cpp::createJobWithFile( const std::string xmlExecuteFile, std::string clusterName)
450 throw LauncherException("Method Launcher_cpp::createJobWithFile is not available "
451 "(libBatch was not present at compilation time)");
458 Launcher_cpp::ParseXmlFile(string xmlExecuteFile)
460 ParserLauncherType job_params;
461 SALOME_Launcher_Handler * handler = new SALOME_Launcher_Handler(job_params);
463 const char* aFilePath = xmlExecuteFile.c_str();
464 FILE* aFile = fopen(aFilePath, "r");
467 xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
469 handler->ProcessXmlDocument(aDoc);
472 std::string message = "ResourcesManager_cpp: could not parse file: " + xmlExecuteFile;
473 LAUNCHER_MESSAGE(message);
475 throw LauncherException(message);
483 std::string message = "ResourcesManager_cpp: file is not readable: " + xmlExecuteFile;
484 LAUNCHER_MESSAGE(message);
486 throw LauncherException(message);