1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 * Auteur : Bernard SECHER - CEA/DEN
33 #include "BatchLight_Job.hxx"
34 #include "BatchLight_BatchManager.hxx"
35 #include "Batch_Date.hxx"
38 namespace BatchLight {
41 BatchManager::BatchManager(const batchParams& p) throw(SALOME_Exception) : _params(p)
43 SCRUTE(_params.hostname);
44 SCRUTE(_params.protocol);
45 SCRUTE(_params.username);
46 // On verifie que le hostname est correct
47 if (!gethostbyname(_params.hostname.c_str())) { // hostname unknown from network
48 string msg = "hostname \"";
49 msg += _params.hostname;
50 msg += "\" unknown from the network";
51 throw SALOME_Exception(msg.c_str());
57 BatchManager::~BatchManager()
59 MESSAGE("BatchManager destructor "<<_params.hostname);
60 std::map < int, const BatchLight::Job * >::const_iterator it;
61 for(it=_jobmap.begin();it!=_jobmap.end();it++)
63 if(_mpiImpl) delete _mpiImpl;
66 // Methode pour le controle des jobs : soumet un job au gestionnaire
67 const int BatchManager::submitJob(Job* job)
69 BEGIN_OF("BatchManager::submitJob");
72 // temporary directory on cluster to put input files for job
74 SCRUTE(_dirForTmpFiles);
76 // export input files on cluster
77 exportInputFiles(job->getFileToExecute(),job->getFilesToExportList());
79 // build salome coupling script for job
80 buildSalomeCouplingScript(job->getFileToExecute());
82 // build batch script for job
83 buildSalomeBatchScript(job->getNbProc());
85 // submit job on cluster
88 // register job on map
90 END_OF("BatchManager::submitJob");
94 void BatchManager::setDirForTmpFiles()
98 _dirForTmpFiles = string("Batch/");
99 Batch::Date date = Batch::Date(time(0)) ;
100 std::string thedate = date.str() ;
101 int lend = thedate.size() ;
104 if ( thedate[i] == '/' || thedate[i] == '-' || thedate[i] == ':' ) {
109 _dirForTmpFiles += thedate ;
112 void BatchManager::exportInputFiles(const char *fileToExecute, const Engines::FilesList filesToExportList) throw(SALOME_Exception)
114 BEGIN_OF("BatchManager::exportInFiles");
115 string command = _params.protocol;
120 if (_params.username != ""){
121 command += _params.username;
125 command += _params.hostname;
126 command += " \"mkdir -p ";
127 command += _dirForTmpFiles ;
129 SCRUTE(command.c_str());
130 status = system(command.c_str());
132 throw SALOME_Exception("Error of connection on remote host");
134 if( _params.protocol == "rsh" )
136 else if( _params.protocol == "ssh" )
139 throw SALOME_Exception("Unknown protocol");
141 command += fileToExecute;
144 if (_params.username != ""){
145 command += _params.username;
149 command += _params.hostname;
151 command += _dirForTmpFiles ;
152 SCRUTE(command.c_str());
153 status = system(command.c_str());
155 throw SALOME_Exception("Error of connection on remote host");
158 for ( i = 0 ; i < filesToExportList.length() ; i++ ) {
159 if( _params.protocol == "rsh" )
161 else if( _params.protocol == "ssh" )
164 throw SALOME_Exception("Unknown protocol");
165 command += filesToExportList[i] ;
167 if (_params.username != ""){
168 command += _params.username;
171 command += _params.hostname;
173 command += _dirForTmpFiles ;
174 SCRUTE(command.c_str());
175 status = system(command.c_str());
177 throw SALOME_Exception("Error of connection on remote host");
180 END_OF("BatchManager::exportInFiles");
183 void BatchManager::importOutputFiles( const char *directory, const CORBA::Long jobId ) throw(SALOME_Exception)
185 BEGIN_OF("BatchManager::importOutputFiles");
189 const BatchLight::Job* myJob = _jobmap[jobId];
190 Engines::FilesList filesToImportList = myJob->getFilesToImportList();
192 for ( int i = 0 ; i < filesToImportList.length() ; i++ ) {
193 if( _params.protocol == "rsh" )
195 else if( _params.protocol == "ssh" )
198 throw SALOME_Exception("Unknown protocol");
199 if (_params.username != ""){
200 command += _params.username;
203 command += _params.hostname;
205 command += filesToImportList[i] ;
207 command += directory;
208 SCRUTE(command.c_str());
209 status = system(command.c_str());
211 throw SALOME_Exception("Error of connection on remote host");
214 END_OF("BatchManager::importOutputFiles");
217 string BatchManager::BuildTemporaryFileName() const
219 //build more complex file name to support multiple salome session
220 char *temp = new char[19];
221 strcpy(temp, "/tmp/command");
222 strcat(temp, "XXXXXX");
229 itoa(getpid(), aPID, 10);
233 string command(temp);
239 void BatchManager::RmTmpFile()
241 if (_TmpFileName != ""){
242 string command = "rm ";
243 command += _TmpFileName;
244 char *temp = strdup(command.c_str());
245 int lgthTemp = strlen(temp);
246 temp[lgthTemp - 3] = '*';
247 temp[lgthTemp - 2] = '\0';
253 MpiImpl *BatchManager::FactoryMpiImpl(string mpiImpl) throw(SALOME_Exception)
256 return new MpiImpl_LAM();
257 else if(mpiImpl == "mpich1")
258 return new MpiImpl_MPICH1();
259 else if(mpiImpl == "mpich2")
260 return new MpiImpl_MPICH2();
261 else if(mpiImpl == "openmpi")
262 return new MpiImpl_OPENMPI();
263 else if(mpiImpl == "indif")
264 throw SALOME_Exception("you must specify a mpi implementation in CatalogResources.xml file");
267 oss << mpiImpl << " : not yet implemented";
268 throw SALOME_Exception(oss.str().c_str());