32 #include <alps/parser/parser.h>
33 #include <alps/parser/xslt_path.h>
34 #include <alps/parser/xmlstream.h>
36 #include <boost/filesystem/fstream.hpp>
40 : stop_callback(opt.time_limit)
45 infilepath = boost::filesystem::absolute(infilepath);
46 outfilepath = boost::filesystem::absolute(outfilepath);
49 parse_job_file(infilepath);
52 void Scheduler::parse_job_file(
const boost::filesystem::path& filename)
54 boost::filesystem::ifstream infile(filename);
55 alps::XMLTag tag = alps::parse_tag(infile,
true);
56 if (tag.name !=
"JOB")
57 boost::throw_exception(std::runtime_error(
"missing <JOB> element in jobfile"));
58 tag = alps::parse_tag(infile);
59 if (tag.name ==
"OUTPUT") {
60 if(tag.attributes[
"file"] !=
"")
61 outfilepath = boost::filesystem::absolute(boost::filesystem::path(tag.attributes[
"file"]), filename.parent_path());
63 boost::throw_exception(std::runtime_error(
"missing 'file' attribute in <OUTPUT> element in jobfile"));
64 tag = alps::parse_tag(infile);
65 if (tag.name ==
"/OUTPUT")
66 tag = alps::parse_tag(infile);
69 while (tag.name ==
"TASK") {
71 if (tag.attributes[
"status"] ==
"" || tag.attributes[
"status"] ==
"new")
73 else if (tag.attributes[
"status"] ==
"running")
75 else if (tag.attributes[
"status"] ==
"finished")
78 boost::throw_exception(std::runtime_error(
"illegal status attribute in <TASK> element in jobfile"));
80 tag = alps::parse_tag(infile);
81 if (tag.name ==
"INPUT") {
82 task.
in = boost::filesystem::path(tag.attributes[
"file"]);
84 boost::throw_exception(std::runtime_error(
"missing 'file' attribute in <INPUT> element in jobfile"));
85 tag = alps::parse_tag(infile);
86 if (tag.name ==
"/INPUT")
87 tag = alps::parse_tag(infile);
90 boost::throw_exception(std::runtime_error(
"missing <INPUT> element in jobfile"));
92 if (tag.name ==
"OUTPUT") {
93 task.
out = boost::filesystem::path(tag.attributes[
"file"]);
95 boost::throw_exception(std::runtime_error(
"missing 'file' attribute in <OUTPUT> element in jobfile"));
96 tag = alps::parse_tag(infile);
97 if (tag.name ==
"/OUTPUT")
98 tag = alps::parse_tag(infile);
100 if (task.
out.empty())
102 task.
in = boost::filesystem::absolute(task.
in, filename.parent_path());
103 task.
out = boost::filesystem::absolute(task.
out, filename.parent_path());
104 if (tag.name !=
"/TASK")
105 boost::throw_exception(std::runtime_error(
"missing </TASK> tag in jobfile"));
106 tag = alps::parse_tag(infile);
107 tasks.push_back(task);
109 if (tag.name !=
"/JOB")
110 boost::throw_exception(std::runtime_error(
"missing </JOB> tag in jobfile"));
114 void Scheduler::checkpoint_status()
const
116 bool make_backup = boost::filesystem::exists(outfilepath);
117 boost::filesystem::path filename = outfilepath;
118 boost::filesystem::path dir = outfilepath.parent_path();
120 filename = dir / (filename.filename().string() +
".bak");
122 alps::oxstream out(filename);
124 out << alps::header(
"UTF-8") << alps::stylesheet(alps::xslt_path(
"ALPS.xsl"));
125 out << alps::start_tag(
"JOB")
126 << alps::xml_namespace(
"xsi",
"http://www.w3.org/2001/XMLSchema-instance")
127 << alps::attribute(
"xsi:noNamespaceSchemaLocation",
128 "http://xml.comp-phys.org/2003/8/job.xsd");
130 for (
unsigned int i=0; i<tasks.size(); i++) {
139 if (tasks[i].status ==
TaskNotStarted && !boost::filesystem::exists(tasks[i].out))
140 copy(tasks[i].in, tasks[i].out);
142 out << alps::start_tag(
"TASK") << alps::attribute(
"status", status)
143 << alps::start_tag(
"INPUT")
144 << alps::attribute(
"file", tasks[i].out.string())
145 << alps::end_tag() << alps::end_tag();
147 out << alps::end_tag(
"JOB");
150 boost::filesystem::remove(outfilepath);
151 boost::filesystem::rename(filename, outfilepath);
160 for(
unsigned int i=0; i<tasks.size(); i++) {
161 boost::chrono::high_resolution_clock::time_point t0 = boost::chrono::high_resolution_clock::now();
162 double time_left = stop_callback.
time_left().count();
163 if (stop_callback.
valid() && time_left < 0)
167 std::cout <<
"Task " << i+1 <<
" finished." << std::endl;
172 std::cout <<
"Running task " << i+1 <<
"." << std::endl;
173 if (!boost::filesystem::exists(tasks[i].out))
174 copy(tasks[i].in, tasks[i].out);
177 run_sim(tasks[i].in, tasks[i].out, time_left);
181 boost::throw_exception( std::logic_error(
"illegal Task status"));
183 boost::chrono::high_resolution_clock::time_point t1 = boost::chrono::high_resolution_clock::now();
184 std::cout <<
"This task took " << boost::chrono::duration<double>(t1-t0) <<
"." << std::endl;
187 std::cout <<
"Finished with everything." << std::endl;
189 std::cout <<
"Time limit exceeded." << std::endl;
boost::chrono::duration< double > time_left() const
boost::filesystem::path jobfilename
boost::filesystem::path in
boost::filesystem::path out
Scheduler(const Options &)
parameter scans scheduler
void run_sim(const boost::filesystem::path &infile, const boost::filesystem::path &outfile, bool write_xml=false, double time_limit=-1.)