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
23 * BatchManager_eLSF.cxx : emulation of LSF client
25 * Auteur : Bernard SECHER - CEA DEN
26 * Mail : mailto:bernard.secher@cea.fr
27 * Date : Thu Apr 24 10:17:22 2008
32 #include "Batch_BatchManager_eClient.hxx"
33 #include "Batch_RunTimeException.hxx"
34 #include "Batch_NotYetImplementedException.hxx"
48 BatchManager_eClient::BatchManager_eClient(const Batch::FactBatchManager * parent, const char* host, const char* protocol, const char* mpiImpl) : BatchManager(parent, host), _protocol(protocol), _username("")
50 // instanciation of mpi implementation needed to launch executable in batch script
51 _mpiImpl = FactoryMpiImpl(mpiImpl);
55 BatchManager_eClient::~BatchManager_eClient()
61 void BatchManager_eClient::exportInputFiles(const Job& job) throw(EmulationException)
64 Parametre params = job.getParametre();
65 Versatile V = params[INFILE];
66 Versatile::iterator Vit;
69 _username = string(params[USER]);
72 if( _protocol == "rsh" )
73 copy_command = "rcp ";
74 else if( _protocol == "ssh" )
75 copy_command = "scp ";
77 throw EmulationException("Unknown protocol : only rsh and ssh are known !");
79 // First step : creating batch tmp files directory
87 command += " \"mkdir -p ";
88 command += string(params[TMPDIR]);
90 cerr << command.c_str() << endl;
91 status = system(command.c_str());
93 std::ostringstream oss;
95 std::string ex_mess("Error of connection on remote host ! status = ");
97 throw EmulationException(ex_mess.c_str());
100 // Second step : copy fileToExecute into
101 // batch tmp files directory
102 string executeFile = params[EXECUTABLE];
103 if( executeFile.size() > 0 ){
104 command = copy_command;
105 command += string(params[EXECUTABLE]);
108 command += _username;
111 command += _hostname;
113 command += string(params[TMPDIR]);
114 cerr << command.c_str() << endl;
115 status = system(command.c_str());
117 std::ostringstream oss;
119 std::string ex_mess("Error of connection on remote host ! status = ");
120 ex_mess += oss.str();
121 throw EmulationException(ex_mess.c_str());
125 // Third step : copy filesToExportList into
126 // batch tmp files directory
127 for(Vit=V.begin(); Vit!=V.end(); Vit++) {
128 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
129 Couple inputFile = cpt;
130 command = copy_command;
131 command += inputFile.getLocal();
134 command += _username;
137 command += _hostname;
139 command += inputFile.getRemote();
140 cerr << command.c_str() << endl;
141 status = system(command.c_str());
143 std::ostringstream oss;
145 std::string ex_mess("Error of connection on remote host ! status = ");
146 ex_mess += oss.str();
147 throw EmulationException(ex_mess.c_str());
153 void BatchManager_eClient::importOutputFiles( const Job & job, const string directory ) throw(EmulationException)
158 Parametre params = job.getParametre();
159 Versatile V = params[OUTFILE];
160 Versatile::iterator Vit;
162 for(Vit=V.begin(); Vit!=V.end(); Vit++) {
163 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
164 Couple outputFile = cpt;
165 if( _protocol == "rsh" )
167 else if( _protocol == "ssh" )
170 throw EmulationException("Unknown protocol");
172 if (_username != ""){
173 command += _username;
176 command += _hostname;
178 command += outputFile.getRemote();
180 command += directory;
181 cerr << command.c_str() << endl;
182 status = system(command.c_str());
185 // Try to get what we can (logs files)
186 // throw BatchException("Error of connection on remote host");
187 std::string mess("Copy command failed ! status is :");
188 ostringstream status_str;
189 status_str << status;
190 mess += status_str.str();
191 cerr << mess << endl;
197 MpiImpl *BatchManager_eClient::FactoryMpiImpl(string mpiImpl) throw(EmulationException)
200 return new MpiImpl_LAM();
201 else if(mpiImpl == "mpich1")
202 return new MpiImpl_MPICH1();
203 else if(mpiImpl == "mpich2")
204 return new MpiImpl_MPICH2();
205 else if(mpiImpl == "openmpi")
206 return new MpiImpl_OPENMPI();
207 else if(mpiImpl == "slurm")
208 return new MpiImpl_SLURM();
209 else if(mpiImpl == "prun")
210 return new MpiImpl_PRUN();
211 else if(mpiImpl == "nompi")
212 throw EmulationException("you must specified an mpi implementation for batch manager");
215 oss << mpiImpl << " : not yet implemented";
216 throw EmulationException(oss.str().c_str());
221 * This method creates a temporary file and opens an output stream to write into this file.
222 * The file is created with the pattern "/tmp/batch_XXXXXX" where the X's are replaced by random
223 * characters. The caller is responsible for closing and deleting the file when it is no more used.
224 * \param outputStream an output stream that will be opened for writing in the temporary file. If
225 * the stream is already open, it will be closed first.
226 * \return the name of the created file.
228 string BatchManager_eClient::createAndOpenTemporaryFile(ofstream & outputStream) const
232 throw NotYetImplementedException("Temporary file creation in Batch library has not been ported to Windows yet");
234 char * tmpFileName = strdup("/tmp/batch_XXXXXX");
235 int fd = mkstemp(tmpFileName);
238 throw RunTimeException("Can't create temporary file");
241 if (outputStream.is_open())
242 outputStream.close();
243 outputStream.open(tmpFileName);
244 close(fd); // Close the file descriptor so that the file is not opened twice
246 fileName = tmpFileName;
247 delete[] tmpFileName;