votca 2024-dev
Loading...
Searching...
No Matches
job.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
21
22// Third party includes
23#include <boost/algorithm/string.hpp>
24#include <boost/format.hpp>
25
26// VOTCA includes
28
29// Local VOTCA includes
30#include "votca/xtp/job.h"
31
32namespace votca {
33namespace xtp {
34using boost::format;
36
37 // DEFINED BY USER
38 id_ = prop.get("id").as<Index>();
39 tag_ = prop.get("tag").as<std::string>();
40 input_ = prop.get("input");
41 if (prop.exists("status")) {
42 status_ = ConvertStatus(prop.get("status").as<std::string>());
43 } else {
45 }
46
47 // GENERATED DURING RUNTIME
48 if (prop.exists("host")) {
49 host_ = prop.get("host").as<std::string>();
50 has_host_ = true;
51 }
52 if (prop.exists("time")) {
53 time_ = prop.get("time").as<std::string>();
54 has_time_ = true;
55 }
56 if (prop.exists("output")) {
57 output_ = prop.get("output");
58 has_output_ = true;
59 }
60 if (prop.exists("error")) {
61 error_ = prop.get("error").as<std::string>();
62 has_error_ = true;
63 }
64}
65
66Job::Job(Index id, const std::string &tag, const tools::Property &input,
67 JobStatus status) {
68
69 id_ = id;
70 tag_ = tag;
71 input_ = input.get("input");
72 status_ = status;
73}
74
75std::string Job::ConvertStatus(JobStatus status) const {
76
77 std::string converted;
78 switch (status) {
79 case AVAILABLE:
80 converted = "AVAILABLE";
81 break;
82 case ASSIGNED:
83 converted = "ASSIGNED";
84 break;
85 case FAILED:
86 converted = "FAILED";
87 break;
88 case COMPLETE:
89 converted = "COMPLETE";
90 break;
91 default:
92 throw std::runtime_error("Incomprehensible status (enum)");
93 }
94 return converted;
95}
96
97Job::JobStatus Job::ConvertStatus(std::string status) const {
98 JobStatus converted;
99 if (status == "AVAILABLE") {
100 converted = AVAILABLE;
101 } else if (status == "ASSIGNED") {
102 converted = ASSIGNED;
103 } else if (status == "FAILED") {
104 converted = FAILED;
105 } else if (status == "COMPLETE") {
106 converted = COMPLETE;
107 } else {
108 throw std::runtime_error("Incomprehensible status: " + status);
109 }
110 return converted;
111}
112
115 has_output_ = false;
116 error_ = "";
117 has_error_ = false;
118 return;
119}
120
121void Job::ToStream(std::ofstream &ofs) const {
122
124 "\t\t");
125 std::string tab = "\t";
126 ofs << tab << "<job>\n";
127 ofs << tab << tab << (format("<id>%1$d</id>\n") % id_).str();
128 ofs << tab << tab << (format("<tag>%1$s</tag>\n") % tag_).str();
129 ofs << iomXML << input_;
130 ofs << tab << tab
131 << (format("<status>%1$s</status>\n") % ConvertStatus(status_)).str();
132
133 if (has_host_) {
134 ofs << tab << tab << (format("<host>%1$s</host>\n") % host_).str();
135 }
136 if (has_time_) {
137 ofs << tab << tab << (format("<time>%1$s</time>\n") % time_).str();
138 }
139 if (has_output_) {
140 ofs << iomXML << output_;
141 }
142 if (has_error_) {
143 ofs << tab << tab << (format("<error>%1$s</error>\n") % error_).str();
144 }
145 ofs << tab << "</job>\n";
146 return;
147}
148
149void Job::UpdateFrom(const Job &ext) {
150 status_ = ext.getStatus();
151 if (ext.hasHost()) {
152 has_host_ = true;
153 host_ = ext.getHost();
154 }
155 if (ext.hasTime()) {
156 has_time_ = true;
157 time_ = ext.getTime();
158 }
159 if (ext.hasOutput()) {
160 has_output_ = true;
161 output_ = ext.getOutput();
162 }
163 if (ext.hasError()) {
164 has_error_ = true;
165 error_ = ext.getError();
166 }
167 return;
168}
169
171 status_ = res.getStatus();
172 if (res.hasOutput()) {
173 output_ = res.getOutput();
174 has_output_ = true;
175 }
176 if (res.hasError()) {
177 error_ = res.getError();
178 has_error_ = true;
179 }
181 return;
182}
183
184std::vector<Job> LOAD_JOBS(const std::string &job_file) {
185
186 tools::Property xml;
187 xml.LoadFromXML(job_file);
188
189 std::vector<tools::Property *> jobProps = xml.Select("jobs.job");
190 std::vector<Job> jobs;
191 jobs.reserve(jobProps.size());
192 for (tools::Property *prop : jobProps) {
193 jobs.push_back(Job(*prop));
194 }
195
196 return jobs;
197}
198
199void WRITE_JOBS(const std::vector<Job> &jobs, const std::string &job_file) {
200 std::ofstream ofs;
201 ofs.open(job_file, std::ofstream::out);
202 if (!ofs.is_open()) {
203 throw std::runtime_error("Bad file handle: " + job_file);
204 }
205 ofs << "<jobs>" << std::endl;
206 for (auto &job : jobs) {
207 job.ToStream(ofs);
208 }
209 ofs << "</jobs>" << std::endl;
210
211 ofs.close();
212 return;
213}
214
215void UPDATE_JOBS(const std::vector<Job> &from, std::vector<Job> &to,
216 const std::string &thisHost) {
217 std::vector<Job>::iterator it_int;
218 std::vector<Job>::const_iterator it_ext;
219
220 if (to.size() != from.size()) {
221 throw std::runtime_error("Progress file out of sync (::size), abort.");
222 }
223
224 for (it_int = to.begin(), it_ext = from.begin(); it_int != to.end();
225 ++it_int, ++it_ext) {
226 Job &job_int = *it_int;
227 const Job &job_ext = *it_ext;
228 if (job_int.getId() != job_ext.getId()) {
229 throw std::runtime_error("Progress file out of sync (::id), abort.");
230 }
231 if (job_ext.hasHost() && job_ext.getHost() != thisHost) {
232 job_int.UpdateFrom(job_ext);
233 }
234 }
235
236 return;
237}
238
239} // namespace xtp
240} // namespace votca
Manipulates the format state of the output stream.
class to manage program options with xml serialization functionality
Definition property.h:55
Property & get(const std::string &key)
get existing property
Definition property.cc:79
bool exists(const std::string &key) const
check whether property exists
Definition property.cc:122
T as() const
return value as type
Definition property.h:283
std::vector< Property * > Select(const std::string &filter)
select property based on a filter
Definition property.cc:185
void LoadFromXML(std::string filename)
Definition property.cc:238
const std::string & getError() const
Definition job.h:65
bool hasError() const
Definition job.h:64
bool hasOutput() const
Definition job.h:60
const tools::Property & getOutput() const
Definition job.h:61
JobStatus getStatus() const
Definition job.h:59
bool hasHost() const
Definition job.h:92
bool has_error_
Definition job.h:149
const tools::Property & getOutput() const
Definition job.h:126
std::string tag_
Definition job.h:138
void ToStream(std::ofstream &ofs) const
Definition job.cc:121
const std::string & getError() const
Definition job.h:130
bool has_time_
Definition job.h:147
const std::string & getTime() const
Definition job.h:122
const std::string & getHost() const
Definition job.h:118
void Reset()
Definition job.cc:113
std::string error_
Definition job.h:151
tools::Property output_
Definition job.h:148
tools::Property input_
Definition job.h:141
Job(const tools::Property &prop)
Definition job.cc:35
std::string time_
Definition job.h:146
bool has_host_
Definition job.h:145
bool hasTime() const
Definition job.h:93
Index attemptsCount_
Definition job.h:140
void UpdateFrom(const Job &ext)
Definition job.cc:149
bool hasOutput() const
Definition job.h:94
Index id_
Definition job.h:137
const JobStatus & getStatus() const
Definition job.h:89
bool hasError() const
Definition job.h:95
JobStatus status_
Definition job.h:139
std::string ConvertStatus(JobStatus) const
Definition job.cc:75
Index getId() const
Definition job.h:85
void UpdateFromResult(const JobResult &res)
Definition job.cc:170
bool has_output_
Definition job.h:150
std::string host_
Definition job.h:144
void WRITE_JOBS(const std::vector< Job > &jobs, const std::string &job_file)
Definition job.cc:199
void UPDATE_JOBS(const std::vector< Job > &from, std::vector< Job > &to, const std::string &thisHost)
Definition job.cc:215
std::vector< Job > LOAD_JOBS(const std::string &xml_file)
Definition job.cc:184
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26