Salome HOME
Copyright update 2021
[tools/libbatch.git] / src / CCC / BatchManager_CCC.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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, or (at your option) any later version.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  * BatchManager_CCC.cxx : emulation of CCC client for CCRT machines
24  *
25  * Auteur : Bernard SECHER - CEA DEN
26  * Mail   : mailto:bernard.secher@cea.fr
27  * Date   : Thu Apr 24 10:17:22 2010
28  * Projet : PAL Salome
29  *
30  */
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <iostream>
36 #include <fstream>
37 #include <sstream>
38 #include <string>
39
40 #include <stdlib.h>
41 #include <string.h>
42
43 #ifdef WIN32
44 #include <io.h>
45 #else
46 #include <libgen.h>
47 #endif
48
49 #include <Constants.hxx>
50 #include <NotYetImplementedException.hxx>
51 #include <Utils.hxx>
52
53 #include "BatchManager_CCC.hxx"
54 #include "JobInfo_CCC.hxx"
55 #include "Log.hxx"
56
57 using namespace std;
58
59 namespace Batch {
60
61   BatchManager_CCC::BatchManager_CCC(const FactBatchManager * parent, const char * host,
62                                        const char * username,
63                                        CommunicationProtocolType protocolType, const char * mpiImpl)
64   : BatchManager(parent, host, username, protocolType, mpiImpl)
65   {
66     // Nothing to do
67   }
68
69   // Destructeur
70   BatchManager_CCC::~BatchManager_CCC()
71   {
72     // Nothing to do
73   }
74
75   // Methode pour le controle des jobs : soumet un job au gestionnaire
76   const JobId BatchManager_CCC::runJob(const Job & job)
77   {
78     Parametre params = job.getParametre();
79     const std::string workDir = params[WORKDIR];
80     const string fileToExecute = params[EXECUTABLE];
81     string::size_type p1 = fileToExecute.find_last_of("/");
82     string::size_type p2 = fileToExecute.find_last_of(".");
83     std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
84
85     // build batch script for job
86     LOG("Construction du script de batch");
87     buildBatchScript(job);
88     LOG("Script envoye");
89
90     // define command to submit batch
91     string subCommand = string("bash -l -c \\\"cd ") + workDir + "; ccc_msub " + fileNameToExecute + "_Batch.sh\\\"";
92     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
93     command += " 2>&1";
94     LOG(command);
95
96     // submit job
97     string output;
98     int status = Utils::getCommandOutput(command, output);
99     LOG(output);
100     if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
101
102     // find id of submitted job in output
103     istringstream idfile(output);
104     string sidj;
105     idfile >> sidj;
106     idfile >> sidj;
107     idfile >> sidj;
108     idfile >> sidj;
109     if (sidj.size() == 0)
110       throw RunTimeException("Error in the submission of the job on the remote host");
111
112     JobId id(this, sidj);
113     return id;
114   }
115
116   // Methode pour le controle des jobs : retire un job du gestionnaire
117   void BatchManager_CCC::deleteJob(const JobId & jobid)
118   {
119     int status;
120     int ref;
121     istringstream iss(jobid.getReference());
122     iss >> ref;
123
124     // define command to delete batch
125     string subCommand = string("bash -l -c \\\"ccc_mdel ") + iss.str() + string("\\\"");
126     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
127     LOG(command);
128     status = system(command.c_str());
129     if (status)
130       throw RunTimeException("Error of connection on remote host");
131
132     LOG("jobId = " << ref << "killed");
133   }
134
135   // Methode pour le controle des jobs : renvoie l'etat du job
136   JobInfo BatchManager_CCC::queryJob(const JobId & jobid)
137   {
138     int id;
139     istringstream iss(jobid.getReference());
140     iss >> id;
141
142     // define command to query batch
143     string subCommand = string("bash -l -c \\\"bjobs ") + iss.str() + string("\\\"");
144     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
145     LOG(command);
146
147     string output;
148     int status = Utils::getCommandOutput(command, output);
149     if (status)
150       throw RunTimeException("Error of connection on remote host");
151
152     JobInfo_CCC ji = JobInfo_CCC(id, output);
153     return ji;
154   }
155
156
157
158   // Methode pour le controle des jobs : teste si un job est present en machine
159   bool BatchManager_CCC::isRunning(const JobId & /*jobid*/)
160   {
161     throw NotYetImplementedException("BatchManager_CCC::isRunning");
162   }
163
164   void BatchManager_CCC::buildBatchScript(const Job & job)
165   {
166 #ifndef WIN32 //TODO: need for porting on Windows
167     Parametre params = job.getParametre();
168
169     // Job Parameters
170     string workDir       = "";
171     string fileToExecute = "";
172     int nbproc           = 0;
173     int edt              = 0;
174     int mem              = 0;
175     string queue         = "";
176
177     // Mandatory parameters
178     if (params.find(WORKDIR) != params.end()) 
179       workDir = params[WORKDIR].str();
180     else 
181       throw RunTimeException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
182     if (params.find(EXECUTABLE) != params.end()) 
183       fileToExecute = params[EXECUTABLE].str();
184     else 
185       throw RunTimeException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
186
187     // Optional parameters
188     if (params.find(NBPROC) != params.end()) 
189       nbproc = params[NBPROC];
190     if (params.find(MAXWALLTIME) != params.end()) 
191       edt = (long)params[MAXWALLTIME] * 60;
192     if (params.find(MAXRAMSIZE) != params.end()) 
193       mem = params[MAXRAMSIZE];
194     if (params.find(QUEUE) != params.end()) 
195       queue = params[QUEUE].str();
196
197     string::size_type p1 = fileToExecute.find_last_of("/");
198     string::size_type p2 = fileToExecute.find_last_of(".");
199     string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
200     string fileNameToExecute = fileToExecute.substr(p1+1);
201  
202     // Create batch submit file
203     ofstream tempOutputFile;
204     std::string TmpFileName = Utils::createAndOpenTemporaryFile("LSF-script", tempOutputFile);
205
206     tempOutputFile << "#!/bin/bash" << endl ;
207     if (queue != "")
208       tempOutputFile << "#MSUB -q " << queue << endl;
209     if( edt > 0 )
210       tempOutputFile << "#MSUB -T " << edt << endl ;
211     if( mem > 0 )
212       tempOutputFile << "#MSUB -M " << mem << endl ;
213     tempOutputFile << "#MSUB -n " << nbproc << endl ;
214     size_t pos = workDir.find("$HOME");
215     string baseDir;
216     if( pos != string::npos )
217       baseDir = getHomeDir(workDir) + workDir.substr(pos+5,workDir.length()-5);
218     else{
219       pos = workDir.find("~");
220       if( pos != string::npos )
221         baseDir = getHomeDir(workDir) + workDir.substr(pos+1,workDir.length()-1);
222       else
223         baseDir = workDir;
224     }
225     tempOutputFile << "#MSUB -o " << baseDir << "/logs/output.log." << rootNameToExecute << endl ;
226     tempOutputFile << "#MSUB -e " << baseDir << "/logs/error.log." << rootNameToExecute << endl ;
227
228     if (params.find(EXTRAPARAMS) != params.end())
229       tempOutputFile << params[EXTRAPARAMS] << endl;
230
231     tempOutputFile << "cd " << workDir << endl ;
232
233     // generate nodes file
234     tempOutputFile << "bool=0" << endl;
235     tempOutputFile << "for i in $LSB_MCPU_HOSTS; do" << endl;
236     tempOutputFile << "  if test $bool = 0; then" << endl;
237     tempOutputFile << "    n=$i" << endl;
238     tempOutputFile << "    bool=1" << endl;
239     tempOutputFile << "  else" << endl;
240     tempOutputFile << "    for ((j=0;j<$i;j++)); do" << endl;
241     tempOutputFile << "      echo $n >> nodesFile." << rootNameToExecute << endl;
242     tempOutputFile << "    done" << endl;
243     tempOutputFile << "    bool=0" << endl;
244     tempOutputFile << "  fi" << endl;
245     tempOutputFile << "done" << endl;
246
247     // Abstraction of PBS_NODEFILE - TODO
248     tempOutputFile << "export LIBBATCH_NODEFILE=nodesFile." << rootNameToExecute << endl;
249
250     // Allow resource sharing in CCRT nodes
251     tempOutputFile << "export OMPI_MCA_orte_process_binding=none" << endl;
252
253     // Launch the executable
254     tempOutputFile << "./" + fileNameToExecute << endl;
255     tempOutputFile.flush();
256     tempOutputFile.close();
257
258     Utils::chmod(TmpFileName.c_str(), 0x1ED);
259     LOG("Batch script file generated is: " << TmpFileName.c_str());
260
261     int status = _protocol.copyFile(TmpFileName, "", "",
262                                     workDir + "/" + rootNameToExecute + "_Batch.sh",
263                                     _hostname, _username);
264     if (status)
265       throw RunTimeException("Error of connection on remote host");
266
267 #endif
268
269   }
270
271   std::string BatchManager_CCC::getHomeDir(std::string tmpdir)
272   {
273     std::string home;
274
275     string subCommand = string("echo ");
276     subCommand += tmpdir;
277     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
278     LOG(command);
279
280     string output;
281     int status = Utils::getCommandOutput(command, output);
282
283     if (status)
284       throw RunTimeException("Error of launching home command on remote host");
285
286     std::istringstream file_home(output);
287     std::getline(file_home, home);
288     return home;
289   }
290
291 }