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 "Basics_Utils.hxx"
33 #include "Basics_DirUtils.hxx"
34 #include "SALOME_Launcher_Handler.hxx"
35 #include "Launcher.hxx"
36 #include "Launcher_Job_Command.hxx"
44 //=============================================================================
48 * Define a CORBA single thread policy for the server, which avoid to deal
49 * with non thread-safe usage like Change_Directory in SALOME naming service
51 //=============================================================================
52 Launcher_cpp::Launcher_cpp()
54 LAUNCHER_MESSAGE("Launcher_cpp constructor");
56 _job_cpt_mutex = new pthread_mutex_t();
57 pthread_mutex_init(_job_cpt_mutex, NULL);
60 //=============================================================================
64 //=============================================================================
65 Launcher_cpp::~Launcher_cpp()
67 LAUNCHER_MESSAGE("Launcher_cpp destructor");
69 std::map < string, Batch::BatchManager_eClient * >::const_iterator it1;
70 for(it1=_batchmap.begin();it1!=_batchmap.end();it1++)
72 std::map<int, Launcher::Job *>::const_iterator it_job;
73 for(it_job = _launcher_job_map.begin(); it_job != _launcher_job_map.end(); it_job++)
74 delete it_job->second;
77 pthread_mutex_destroy(_job_cpt_mutex);
78 delete _job_cpt_mutex;
83 //=============================================================================
85 * Add a job into the launcher - check resource and choose one
87 //=============================================================================
89 Launcher_cpp::createJob(Launcher::Job * new_job)
91 LAUNCHER_MESSAGE("Creating a new job");
93 // First step take a resource
94 std::vector<std::string> ResourceList;
95 resourceParams params = new_job->getResourceRequiredParams();
97 ResourceList = _ResManager->GetFittingResources(params);
99 catch(const ResourcesException &ex){
100 throw LauncherException(ex.msg.c_str());
102 if (ResourceList.size() == 0)
104 LAUNCHER_INFOS("No adequate resource found for the job, number " << new_job->getNumber() << " - deleting it");
106 throw LauncherException("No resource found the job");
109 // Second step configure the job with the resource selected - the first of the list
110 ParserResourcesType resource_definition = _ResManager->GetResourcesDescr(ResourceList[0]);
112 // Set resource definition to the job
113 // The job will check if the definitions needed
116 new_job->setResourceDefinition(resource_definition);
118 catch(const LauncherException &ex)
120 LAUNCHER_INFOS("Error in the definition of the resource, mess: " << ex.msg);
125 // Third step search batch manager for the resource into the map -> instanciate one if does not exist
126 std::string resource_name = resource_definition.Name;
127 std::map<std::string, Batch::BatchManager_eClient *>::const_iterator it = _batchmap.find(resource_name);
128 if(it == _batchmap.end())
132 // Warning cannot write on one line like this, because map object is constructed before
133 // the method is called...
134 //_batchmap.[resource_name] = FactoryBatchManager(resource_definition);
135 Batch::BatchManager_eClient * batch_client = FactoryBatchManager(resource_definition);
136 _batchmap[resource_name] = batch_client;
138 catch(const LauncherException &ex)
140 LAUNCHER_INFOS("Error during creation of the batch manager of the resource, mess: " << ex.msg);
144 catch(const Batch::EmulationException &ex)
146 LAUNCHER_INFOS("Error during creation of the batch manager of the resource, mess: " << ex.message);
148 throw LauncherException(ex.message);
153 // Final step - add job to the jobs map
154 pthread_mutex_lock(_job_cpt_mutex);
155 new_job->setNumber(_job_cpt);
157 pthread_mutex_unlock(_job_cpt_mutex);
158 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
159 if (it_job == _launcher_job_map.end())
160 _launcher_job_map[new_job->getNumber()] = new_job;
163 LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
165 throw LauncherException("A job as already the same id - job is not created !");
167 LAUNCHER_MESSAGE("New Job created");
170 //=============================================================================
174 //=============================================================================
176 Launcher_cpp::launchJob(int job_id)
178 LAUNCHER_MESSAGE("Launch a job");
180 // Check if job exist
181 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
182 if (it_job == _launcher_job_map.end())
184 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
185 throw LauncherException("Cannot find the job, is it created ?");
188 Launcher::Job * job = it_job->second;
190 // Check job state (cannot launch a job already launched...)
191 if (job->getState() != "CREATED")
193 LAUNCHER_INFOS("Bad state of the job: " << job->getState());
194 throw LauncherException("Bad state of the job: " + job->getState());
197 std::string resource_name = job->getResourceDefinition().Name;
199 Batch::JobId batch_manager_job_id = _batchmap[resource_name]->submitJob(*(job->getBatchJob()));
200 job->setBatchManagerJobId(batch_manager_job_id);
201 job->setState("QUEUED");
203 catch(const Batch::EmulationException &ex)
205 LAUNCHER_INFOS("Job is not launched, exception in submitJob: " << ex.message);
206 throw LauncherException(ex.message.c_str());
208 LAUNCHER_MESSAGE("Job launched");
211 //=============================================================================
215 //=============================================================================
217 Launcher_cpp::getJobState(int job_id)
219 LAUNCHER_MESSAGE("Get job state");
221 // Check if job exist
222 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
223 if (it_job == _launcher_job_map.end())
225 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
226 throw LauncherException("Cannot find the job, is it created ?");
229 Launcher::Job * job = it_job->second;
230 std::string state = job->updateJobState();
232 return state.c_str();
235 //=============================================================================
237 * Get Job result - the result directory could be changed
239 //=============================================================================
241 Launcher_cpp::getJobResults(int job_id, std::string directory)
243 LAUNCHER_MESSAGE("Get Job results");
245 // Check if job exist
246 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
247 if (it_job == _launcher_job_map.end())
249 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
250 throw LauncherException("Cannot find the job, is it created ?");
253 Launcher::Job * job = it_job->second;
254 std::string resource_name = job->getResourceDefinition().Name;
258 _batchmap[resource_name]->importOutputFiles(*(job->getBatchJob()), directory);
260 _batchmap[resource_name]->importOutputFiles(*(job->getBatchJob()), job->getResultDirectory());
262 catch(const Batch::EmulationException &ex)
264 LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
265 throw LauncherException(ex.message.c_str());
267 LAUNCHER_MESSAGE("getJobResult ended");
270 //=============================================================================
272 * Remove the job - into the Launcher and its batch manager
274 //=============================================================================
276 Launcher_cpp::removeJob(int job_id)
278 LAUNCHER_MESSAGE("Remove Job");
280 // Check if job exist
281 std::map<int, Launcher::Job *>::iterator it_job = _launcher_job_map.find(job_id);
282 if (it_job == _launcher_job_map.end())
284 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
285 throw LauncherException("Cannot find the job, is it created ?");
288 delete it_job->second;
289 _launcher_job_map.erase(it_job);
292 //=============================================================================
294 * create a launcher job based on a file
295 * \param xmlExecuteFile : to define the execution on the batch cluster
297 //=============================================================================
299 Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
300 const std::string clusterName)
302 LAUNCHER_MESSAGE("Begin of Launcher_cpp::createJobWithFile");
305 ParserLauncherType job_params = ParseXmlFile(xmlExecuteFile);
307 // Creating a new job
308 Launcher::Job_Command * new_job = new Launcher::Job_Command();
310 string cmdFile = Kernel_Utils::GetTmpFileName();
317 os.open(cmdFile.c_str(), ofstream::out );
318 os << "#! /bin/sh" << endl;
319 os << job_params.Command;
322 new_job->setJobFile(cmdFile);
323 new_job->setLocalDirectory(job_params.RefDirectory);
324 new_job->setWorkDirectory(job_params.MachinesList[clusterName].WorkDirectory);
325 new_job->setEnvFile(job_params.MachinesList[clusterName].EnvFile);
327 for(int i=0; i < job_params.InputFile.size(); i++)
328 new_job->add_in_file(job_params.InputFile[i]);
329 for(int i=0; i < job_params.OutputFile.size();i++)
330 new_job->add_out_file(job_params.OutputFile[i]);
333 p.hostname = clusterName;
336 p.nb_proc = job_params.NbOfProcesses;
338 p.nb_proc_per_node = 0;
341 new_job->setResourceRequiredParams(p);
344 return new_job->getNumber();
347 //=============================================================================
349 * Factory to instanciate the good batch manager for choosen cluster.
351 //=============================================================================
352 Batch::BatchManager_eClient *
353 Launcher_cpp::FactoryBatchManager(ParserResourcesType& params)
356 Batch::CommunicationProtocolType protocol;
357 Batch::FactBatchManager_eClient* fact;
359 int nb_proc_per_node = params.DataForSort._nbOfProcPerNode;
360 std::string hostname = params.HostName;
362 switch(params.Protocol)
365 protocol = Batch::RSH;
368 protocol = Batch::SSH;
371 throw LauncherException("Unknown protocol for this resource");
399 std::string message = "Instanciation of batch manager of type: ";
400 switch( params.Batch )
404 fact = new Batch::FactBatchManager_ePBS;
408 fact = new Batch::FactBatchManager_eLSF;
412 fact = new Batch::FactBatchManager_eSGE;
416 fact = new Batch::FactBatchManager_eSSH;
419 LAUNCHER_MESSAGE("Bad batch description of the resource: Batch = " << params.Batch);
420 throw LauncherException("No batchmanager for that cluster - Bad batch description of the resource");
422 LAUNCHER_MESSAGE(message);
423 return (*fact)(hostname.c_str(), protocol, mpi.c_str(), nb_proc_per_node);
426 //----------------------------------------------------------
427 // Without LIBBATCH - Launcher_cpp do nothing...
428 //----------------------------------------------------------
432 Launcher_cpp::createJob(Launcher::Job * new_job)
434 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
436 throw LauncherException("Method Launcher_cpp::createJob is not available "
437 "(libBatch was not present at compilation time)");
441 Launcher_cpp::launchJob(int job_id)
443 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot launch a job !!!");
444 throw LauncherException("Method Launcher_cpp::launchJob is not available "
445 "(libBatch was not present at compilation time)");
449 Launcher_cpp::getJobState(int job_id)
451 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job state!!!");
452 throw LauncherException("Method Launcher_cpp::getJobState is not available "
453 "(libBatch was not present at compilation time)");
457 Launcher_cpp::getJobResults(int job_id, std::string directory)
459 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job results!!!");
460 throw LauncherException("Method Launcher_cpp::getJobResults is not available "
461 "(libBatch was not present at compilation time)");
465 Launcher_cpp::removeJob(int job_id)
467 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot remove job!!!");
468 throw LauncherException("Method Launcher_cpp::removeJob is not available "
469 "(libBatch was not present at compilation time)");
473 Launcher_cpp::createJobWithFile( const std::string xmlExecuteFile, std::string clusterName)
475 throw LauncherException("Method Launcher_cpp::createJobWithFile is not available "
476 "(libBatch was not present at compilation time)");
483 Launcher_cpp::ParseXmlFile(string xmlExecuteFile)
485 ParserLauncherType job_params;
486 SALOME_Launcher_Handler * handler = new SALOME_Launcher_Handler(job_params);
488 const char* aFilePath = xmlExecuteFile.c_str();
489 FILE* aFile = fopen(aFilePath, "r");
492 xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
494 handler->ProcessXmlDocument(aDoc);
497 std::string message = "ResourcesManager_cpp: could not parse file: " + xmlExecuteFile;
498 LAUNCHER_MESSAGE(message);
500 throw LauncherException(message);
508 std::string message = "ResourcesManager_cpp: file is not readable: " + xmlExecuteFile;
509 LAUNCHER_MESSAGE(message);
511 throw LauncherException(message);