Salome HOME
Copyright update 2021
[tools/libbatch.git] / src / LoadLeveler / BatchManager_LL.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_LL.cxx :
24  *
25  *  Created on: 25 nov. 2010
26  *  Author : Renaud BARATE - EDF R&D
27  */
28
29 #include <cstdlib>
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33
34 #include <Constants.hxx>
35 #include <Utils.hxx>
36
37 #include "FactBatchManager_LL.hxx"
38 #include "BatchManager_LL.hxx"
39 #include "JobInfo_LL.hxx"
40 #include "Log.hxx"
41
42 using namespace std;
43
44 namespace Batch {
45
46   BatchManager_LL::BatchManager_LL(const FactBatchManager * parent, const char * host,
47                                      const char * username,
48                                      CommunicationProtocolType protocolType, const char * mpiImpl)
49     : BatchManager(parent, host, username, protocolType, mpiImpl)
50   {
51     // Nothing to do
52   }
53
54   BatchManager_LL::~BatchManager_LL()
55   {
56     // Nothing to do
57   }
58
59   // Method to submit a job to the batch manager
60   const JobId BatchManager_LL::runJob(const Job & job)
61   {
62     Parametre params = job.getParametre();
63     const string workDir = params[WORKDIR];
64
65     // build command file to submit the job and copy it on the server
66     string cmdFile = buildCommandFile(job);
67
68     // define command to submit batch
69     string subCommand = string("cd ") + workDir + "; llsubmit " + cmdFile;
70     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
71     LOG(command);
72
73     // submit job
74     string output;
75     int status = Utils::getCommandOutput(command, output);
76     LOG(output);
77     if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
78
79     // find id of submitted job in output
80     string jobref;
81     istringstream idfile(output);
82     string line;
83     while (idfile && line.compare(0, 9, "llsubmit:") != 0)
84       getline(idfile, line);
85     if (line.compare(0, 9, "llsubmit:") == 0)
86     {
87       string::size_type p1 = line.find_first_of("\"");
88       string::size_type p2 = line.find_last_of("\"");
89       if (p1 != p2)
90         jobref = line.substr(p1 + 1, p2 - p1 - 1);
91     }
92     if (jobref.size() == 0)
93       throw RunTimeException("Error in the submission of the job on the remote host");
94
95     JobId id(this, jobref);
96     return id;
97   }
98
99   /**
100    * Create LoadLeveler command file and copy it on the server.
101    * Return the name of the remote file.
102    */
103   string BatchManager_LL::buildCommandFile(const Job & job)
104   {
105     Parametre params = job.getParametre();
106
107     // Job Parameters
108     string workDir = "";
109     string fileToExecute = "";
110     string queue = "";
111
112     // Mandatory parameters
113     if (params.find(WORKDIR) != params.end()) 
114       workDir = params[WORKDIR].str();
115     else 
116       throw RunTimeException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
117     if (params.find(EXECUTABLE) != params.end()) 
118       fileToExecute = params[EXECUTABLE].str();
119     else 
120       throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
121
122     string::size_type p1 = fileToExecute.find_last_of("/");
123     string::size_type p2 = fileToExecute.find_last_of(".");
124     string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
125     string fileNameToExecute = fileToExecute.substr(p1+1);
126
127     // Create batch submit file
128     ofstream tempOutputFile;
129     string tmpFileName = Utils::createAndOpenTemporaryFile("LL-script", tempOutputFile);
130
131     tempOutputFile << "#!/bin/bash" << endl;
132     tempOutputFile << "# @ output = " << workDir << "/logs/output.log." << rootNameToExecute << endl;
133     tempOutputFile << "# @ error = " << workDir << "/logs/error.log." << rootNameToExecute << endl;
134
135     if (params.find(NAME) != params.end())
136       tempOutputFile << "# @ job_name = " << params[NAME] << endl;
137
138     // Optional parameters
139     int nbproc = 1;
140     if (params.find(NBPROC) != params.end())
141       nbproc = params[NBPROC];
142     int nbprocpernode = 1;
143     if (params.find(NBPROCPERNODE) != params.end())
144       nbprocpernode = params[NBPROCPERNODE];
145
146     if (params.find(EXCLUSIVE) != params.end()) {
147       if (params[EXCLUSIVE])
148         tempOutputFile << "# @ node_usage = not_shared" << endl;
149       else
150         tempOutputFile << "# @ node_usage = shared" << endl;
151     }
152
153     // If job type is not specified, try to guess it from number of procs
154     string job_type;
155     if (params.find(LL_JOBTYPE) != params.end())
156       job_type = params[LL_JOBTYPE].str();
157     else if (nbproc == 1)
158       job_type = "serial";
159     else
160       job_type = "mpich";
161
162     tempOutputFile << "# @ job_type = " << job_type << endl;
163
164     if (job_type == "mpich") {
165       int nodes_requested = (nbproc + nbprocpernode -1) / nbprocpernode;
166       tempOutputFile << "# @ node = " << nodes_requested << endl;
167       tempOutputFile << "# @ total_tasks = " << nbproc << endl;
168     }
169
170     if (params.find(MAXWALLTIME) != params.end())
171       tempOutputFile << "# @ wall_clock_limit = " << params[MAXWALLTIME] << ":00" << endl;
172     if (params.find(MAXRAMSIZE) != params.end())
173       tempOutputFile << "# @ as_limit = " << params[MAXRAMSIZE] << "mb" << endl;
174     if (params.find(QUEUE) != params.end())
175       tempOutputFile << "# @ class = " << params[QUEUE] << endl;
176
177     // Define environment for the job
178     Environnement env = job.getEnvironnement();
179     if (!env.empty()) {
180       tempOutputFile << "# @ environment = ";
181       Environnement::const_iterator iter;
182       for (iter = env.begin() ; iter != env.end() ; ++iter) {
183         tempOutputFile << iter->first << "=" << iter->second << "; ";
184       }
185       tempOutputFile << endl;
186     }
187
188     tempOutputFile << "# @ queue" << endl;
189
190     // generate nodes file
191     tempOutputFile << "export LIBBATCH_NODEFILE=$LOADL_HOSTFILE" << endl;
192
193     // Launch the executable
194     tempOutputFile << "cd " << workDir << endl;
195     tempOutputFile << "./" + fileNameToExecute << endl;
196
197     tempOutputFile.flush();
198     tempOutputFile.close();
199
200     LOG("Batch script file generated is: " << tmpFileName);
201
202     string remoteFileName = rootNameToExecute + "_LL.cmd";
203     int status = _protocol.copyFile(tmpFileName, "", "",
204                                     workDir + "/" + remoteFileName,
205                                     _hostname, _username);
206     if (status)
207       throw RunTimeException("Cannot copy command file on host " + _hostname);
208
209     return remoteFileName;
210   }
211
212   void BatchManager_LL::deleteJob(const JobId & jobid)
213   {
214     // define command to delete job
215     string subCommand = "llcancel " + jobid.getReference();
216     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
217     LOG(command);
218
219     int status = system(command.c_str());
220     if (status)
221       throw RunTimeException("Can't delete job " + jobid.getReference());
222
223     LOG("job " << jobid.getReference() << " killed");
224   }
225
226   JobInfo BatchManager_LL::queryJob(const JobId & jobid)
227   {
228     // define command to query batch
229     string subCommand = "llq -f %st " + jobid.getReference();
230     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
231     LOG(command);
232     string output;
233     int status = Utils::getCommandOutput(command, output);
234     if (status != 0)
235       throw RunTimeException("Can't query job " + jobid.getReference());
236
237     JobInfo_LL jobinfo = JobInfo_LL(jobid.getReference(), output);
238     return jobinfo;
239   }
240
241 }