Salome HOME
Added LoadLeveler client (only submit is implemented for now)
[tools/libbatch.git] / src / LoadLeveler / Batch_BatchManager_eLL.cxx
1 //  Copyright (C) 2007-2010  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.
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  *  Batch_BatchManager_eLL.cxx :
24  *
25  *  Created on: 25 nov. 2010
26  *  Author : Renaud BARATE - EDF R&D
27  */
28
29 #include <iostream>
30 #include <fstream>
31
32 #include <Batch_NotYetImplementedException.hxx>
33
34 #include "Batch_BatchManager_eLL.hxx"
35
36 using namespace std;
37
38 namespace Batch {
39
40   BatchManager_eLL::BatchManager_eLL(const FactBatchManager * parent, const char * host,
41                                      const char * username,
42                                      CommunicationProtocolType protocolType, const char * mpiImpl,
43                                      int nb_proc_per_node)
44     : BatchManager(parent, host),
45       BatchManager_eClient(parent, host, username, protocolType, mpiImpl)
46   {
47     // Nothing to do
48   }
49
50   BatchManager_eLL::~BatchManager_eLL()
51   {
52     // Nothing to do
53   }
54
55   // Method to submit a job to the batch manager
56   const JobId BatchManager_eLL::submitJob(const Job & job)
57   {
58     int status;
59     Parametre params = job.getParametre();
60     const string workDir = params[WORKDIR];
61
62     // export input files on cluster
63     exportInputFiles(job);
64
65     // build command file to submit the job and copy it on the server
66     string cmdFile = buildCommandFile(job);
67
68     // define name of log file (local)
69     string logFile = generateTemporaryFileName("LL-submitlog");
70
71     // define command to submit batch
72     string subCommand = string("cd ") + workDir + "; llsubmit " + cmdFile;
73     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
74     command += " > ";
75     command += logFile;
76     cerr << command.c_str() << endl;
77     status = system(command.c_str());
78     if(status)
79     {
80       ifstream error_message(logFile.c_str());
81       string mess;
82       string temp;
83       while(std::getline(error_message, temp))
84           mess += temp;
85       error_message.close();
86       throw EmulationException("Error of connection on remote host, error was: " + mess);
87     }
88
89     // read id of submitted job in log file
90     string jobref;
91     ifstream idfile(logFile.c_str());
92     unsigned int linebufsize = 1024;
93     char linebuf[linebufsize];
94     idfile.getline(linebuf, linebufsize);
95     while (!idfile.eof() && strncmp(linebuf, "llsubmit:", 9) != 0)
96       idfile.getline(linebuf, linebufsize);
97     idfile.close();
98     if (strncmp(linebuf, "llsubmit:", 9) == 0)
99     {
100       string line(linebuf);
101       string::size_type p1 = line.find_first_of("\"");
102       string::size_type p2 = line.find_last_of("\"");
103       if (p1 != p2)
104         jobref = line.substr(p1 + 1, p2 - p1 - 1);
105     }
106     if (jobref.size() == 0)
107       throw EmulationException("Error in the submission of the job on the remote host");
108
109     JobId id(this, jobref);
110     return id;
111   }
112
113   /**
114    * Create LoadLeveler command file and copy it on the server.
115    * Return the name of the remote file.
116    */
117   string BatchManager_eLL::buildCommandFile(const Job & job)
118   {
119     Parametre params = job.getParametre();
120
121     // Job Parameters
122     string workDir = "";
123     string fileToExecute = "";
124     string queue = "";
125
126     // Mandatory parameters
127     if (params.find(WORKDIR) != params.end()) 
128       workDir = params[WORKDIR].str();
129     else 
130       throw EmulationException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
131     if (params.find(EXECUTABLE) != params.end()) 
132       fileToExecute = params[EXECUTABLE].str();
133     else 
134       throw EmulationException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
135
136     // Optional parameters
137     if (params.find(QUEUE) != params.end()) 
138       queue = params[QUEUE].str();
139
140     string::size_type p1 = fileToExecute.find_last_of("/");
141     string::size_type p2 = fileToExecute.find_last_of(".");
142     string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
143     string fileNameToExecute = fileToExecute.substr(p1+1);
144
145     // Create batch submit file
146     ofstream tempOutputFile;
147     std::string tmpFileName = createAndOpenTemporaryFile("LL-script", tempOutputFile);
148
149     tempOutputFile << "# @ executable = " << fileNameToExecute << endl;
150     tempOutputFile << "# @ output = " << workDir << "/logs/output.log." << rootNameToExecute << endl;
151     tempOutputFile << "# @ error = " << workDir << "/logs/error.log." << rootNameToExecute << endl;
152     if (queue != "")
153       tempOutputFile << "# @ class = " << queue << endl;
154     tempOutputFile << "# @ job_type = bluegene" << endl;
155     tempOutputFile << "# @ queue" << endl;
156
157     tempOutputFile.flush();
158     tempOutputFile.close();
159
160     cerr << "Batch script file generated is: " << tmpFileName.c_str() << endl;
161
162     string remoteFileName = rootNameToExecute + "_LL.cmd";
163     int status = _protocol.copyFile(tmpFileName, "", "",
164                                     workDir + "/" + remoteFileName,
165                                     _hostname, _username);
166     if (status)
167       throw EmulationException("Error of connection on remote host, cannot copy batch submission file");
168
169     return remoteFileName;
170   }
171
172   void BatchManager_eLL::deleteJob(const JobId & jobid)
173   {
174     throw NotYetImplementedException("BatchManager_eLL::deleteJob");
175   }
176
177   void BatchManager_eLL::holdJob(const JobId & jobid)
178   {
179     throw NotYetImplementedException("BatchManager_eLL::holdJob");
180   }
181
182   void BatchManager_eLL::releaseJob(const JobId & jobid)
183   {
184     throw NotYetImplementedException("BatchManager_eLL::releaseJob");
185   }
186
187   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
188   {
189     throw NotYetImplementedException("BatchManager_eLL::alterJob");
190   }
191
192   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param)
193   {
194     throw NotYetImplementedException("BatchManager_eLL::alterJob");
195   }
196
197   void BatchManager_eLL::alterJob(const JobId & jobid, const Environnement & env)
198   {
199     throw NotYetImplementedException("BatchManager_eLL::alterJob");
200   }
201
202   JobInfo BatchManager_eLL::queryJob(const JobId & jobid)
203   {
204     throw NotYetImplementedException("BatchManager_eLL::queryJob");
205   }
206
207   const JobId BatchManager_eLL::addJob(const Job & job, const string reference)
208   {
209     throw NotYetImplementedException("BatchManager_eLL::addJob");
210   }
211
212 }