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,
49 const char* protocol, const char* mpiImpl)
50 : BatchManager(parent, host), _protocol(protocol), _username("")
52 // instanciation of mpi implementation needed to launch executable in batch script
53 _mpiImpl = FactoryMpiImpl(mpiImpl);
57 BatchManager_eClient::~BatchManager_eClient()
63 void BatchManager_eClient::exportInputFiles(const Job& job)
66 Parametre params = job.getParametre();
67 Versatile V = params[INFILE];
68 Versatile::iterator Vit;
71 _username = string(params[USER]);
74 if( _protocol == "rsh" )
75 copy_command = "rcp ";
76 else if( _protocol == "ssh" )
77 copy_command = "scp ";
79 throw EmulationException("Unknown protocol : only rsh and ssh are known !");
81 // First step : creating batch tmp files directory
89 command += " \"mkdir -p ";
90 command += string(params[TMPDIR]);
92 cerr << command.c_str() << endl;
93 status = system(command.c_str());
95 std::ostringstream oss;
97 std::string ex_mess("Error of connection on remote host ! status = ");
99 throw EmulationException(ex_mess.c_str());
102 // Second step : copy fileToExecute into
103 // batch tmp files directory
104 string executeFile = params[EXECUTABLE];
105 if( executeFile.size() > 0 ){
106 command = copy_command;
107 command += string(params[EXECUTABLE]);
110 command += _username;
113 command += _hostname;
115 command += string(params[TMPDIR]);
116 cerr << command.c_str() << endl;
117 status = system(command.c_str());
119 std::ostringstream oss;
121 std::string ex_mess("Error of connection on remote host ! status = ");
122 ex_mess += oss.str();
123 throw EmulationException(ex_mess.c_str());
127 // Third step : copy filesToExportList into
128 // batch tmp files directory
129 for(Vit=V.begin(); Vit!=V.end(); Vit++) {
130 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
131 Couple inputFile = cpt;
132 command = copy_command;
133 command += inputFile.getLocal();
136 command += _username;
139 command += _hostname;
141 command += inputFile.getRemote();
142 cerr << command.c_str() << endl;
143 status = system(command.c_str());
145 std::ostringstream oss;
147 std::string ex_mess("Error of connection on remote host ! status = ");
148 ex_mess += oss.str();
149 throw EmulationException(ex_mess.c_str());
155 void BatchManager_eClient::importOutputFiles( const Job & job, const string directory ) throw(EmulationException)
160 Parametre params = job.getParametre();
161 Versatile V = params[OUTFILE];
162 Versatile::iterator Vit;
164 for(Vit=V.begin(); Vit!=V.end(); Vit++) {
165 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
166 Couple outputFile = cpt;
167 if( _protocol == "rsh" )
169 else if( _protocol == "ssh" )
172 throw EmulationException("Unknown protocol");
174 if (_username != ""){
175 command += _username;
178 command += _hostname;
180 command += outputFile.getRemote();
182 command += directory;
183 cerr << command.c_str() << endl;
184 status = system(command.c_str());
187 // Try to get what we can (logs files)
188 // throw BatchException("Error of connection on remote host");
189 std::string mess("Copy command failed ! status is :");
190 ostringstream status_str;
191 status_str << status;
192 mess += status_str.str();
193 cerr << mess << endl;
199 MpiImpl *BatchManager_eClient::FactoryMpiImpl(string mpiImpl) throw(EmulationException)
202 return new MpiImpl_LAM();
203 else if(mpiImpl == "mpich1")
204 return new MpiImpl_MPICH1();
205 else if(mpiImpl == "mpich2")
206 return new MpiImpl_MPICH2();
207 else if(mpiImpl == "openmpi")
208 return new MpiImpl_OPENMPI();
209 else if(mpiImpl == "slurm")
210 return new MpiImpl_SLURM();
211 else if(mpiImpl == "prun")
212 return new MpiImpl_PRUN();
213 else if(mpiImpl == "nompi")
214 throw EmulationException("you must specify an mpi implementation for batch manager");
217 oss << mpiImpl << " : not yet implemented";
218 throw EmulationException(oss.str().c_str());
223 * This method creates a temporary file and opens an output stream to write into this file.
224 * The file is created with the pattern "/tmp/batch_XXXXXX" where the X's are replaced by random
225 * characters. The caller is responsible for closing and deleting the file when it is no more used.
226 * \param outputStream an output stream that will be opened for writing in the temporary file. If
227 * the stream is already open, it will be closed first.
228 * \return the name of the created file.
230 string BatchManager_eClient::createAndOpenTemporaryFile(ofstream & outputStream) const
234 throw NotYetImplementedException("Temporary file creation in Batch library has not been ported to Windows yet");
236 char * tmpFileName = strdup("/tmp/batch_XXXXXX");
237 int fd = mkstemp(tmpFileName);
240 throw RunTimeException("Can't create temporary file");
243 if (outputStream.is_open())
244 outputStream.close();
245 outputStream.open(tmpFileName);
246 close(fd); // Close the file descriptor so that the file is not opened twice
248 fileName = tmpFileName;
249 delete[] tmpFileName;