QMCPACK
ProjectData.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2022 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
10 // Mark Dewing, markdewing@gmail.com, University of Illinois at Urbana-Champaign
11 // Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
12 //
13 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
14 //////////////////////////////////////////////////////////////////////////////////////
15 
16 
17 #include "ProjectData.h"
18 #include "Message/Communicate.h"
19 #include "Host/sysutil.h"
20 #include "Utilities/qmc_common.h"
21 #include "OhmmsData/ParameterSet.h"
23 #include "ModernStringUtils.hpp"
24 
25 #include <array>
26 
27 namespace qmcplusplus
28 {
29 
30 // PUBLIC
31 //----------------------------------------------------------------------------
32 // ProjectData
33 //----------------------------------------------------------------------------
34 // constructors and destructors
35 ProjectData::ProjectData(const std::string& atitle, ProjectData::DriverVersion driver_version)
36  : title_(atitle),
37  host_("none"),
38  date_("none"),
39  series_(0),
40  cur_(NULL),
41  max_cpu_secs_(360000),
42  driver_version_(driver_version),
43  runtime_options_(RuntimeOptions())
44 {
46  if (title_.empty())
47  title_ = getDateAndTime("%Y%m%dT%H%M");
48 }
49 
51 
52 bool ProjectData::get(std::ostream& os) const
53 {
54  os << " Project = " << title_ << "\n";
55  os << " date = " << getDateAndTime("%Y-%m-%d %H:%M:%S %Z\n");
56  os << " host = " << host_ << "\n";
57  return true;
58 }
59 
60 bool ProjectData::put(std::istream& is)
61 {
62  std::string t1;
63  while (!is.eof())
64  {
65  if (isdigit(t1[0]))
66  series_ = atoi(t1.c_str());
67  is >> t1;
68  if (t1 == "series")
69  is >> series_;
70  else if (t1 == "host")
71  is >> host_;
72  else if (t1 == "date")
73  is >> date_;
74  else
75  title_ = t1;
76  }
77  reset();
78  return true;
79 }
80 
82 {
83  series_++;
84  reset();
85 }
86 
88 {
89  if (series_ > 0)
90  series_--;
91  reset();
92 }
93 
94 /**\fn void ProjectData::reset()
95  *\brief Construct the root name with title_ and m_series.
96  */
98 {
99  //int nproc_g = OHMMS::Controller->size();
100  int nproc = my_comm_->size();
101  int nodeid = my_comm_->rank();
102  int groupid = my_comm_->getGroupID();
103  std::array<char, 256> fileroot;
104  std::array<char, 256> nextroot;
105 
106  bool no_gtag = (qmc_common.mpi_groups == 1);
107  int file_len{0};
108  if (no_gtag) //qnproc_g == nproc)
109  file_len = std::snprintf(fileroot.data(), fileroot.size(), "%s.s%03d", title_.c_str(), series_);
110  else
111  file_len = std::snprintf(fileroot.data(), fileroot.size(), "%s.g%03d.s%03d", title_.c_str(), groupid, series_);
112 
113  project_main_ = std::string(fileroot.data(), file_len);
114  //set the communicator name
115  my_comm_->setName(fileroot.data(), file_len);
116  int next_len{0};
117  if (no_gtag)
118  {
119  if (nproc > 1)
120  {
121  file_len = std::snprintf(fileroot.data(), fileroot.size(), ".s%03d.p%03d", series_, nodeid);
122  next_len = std::snprintf(nextroot.data(), nextroot.size(), ".s%03d.p%03d", series_ + 1, nodeid);
123  }
124  else
125  {
126  file_len = std::snprintf(fileroot.data(), fileroot.size(), ".s%03d", series_);
127  next_len = std::snprintf(nextroot.data(), nextroot.size(), ".s%03d", series_ + 1);
128  }
129  }
130  else
131  {
132  if (nproc > 1)
133  {
134  file_len = std::snprintf(fileroot.data(), fileroot.size(), ".g%03d.s%03d.p%03d", groupid, series_, nodeid);
135  next_len = std::snprintf(nextroot.data(), nextroot.size(), ".g%03d.s%03d.p%03d", groupid, series_ + 1, nodeid);
136  }
137  else
138  {
139  file_len = std::snprintf(fileroot.data(), fileroot.size(), ".g%03d.s%03d", groupid, series_);
140  next_len = std::snprintf(nextroot.data(), nextroot.size(), ".g%03d.s%03d", groupid, series_ + 1);
141  }
142  }
143  if (file_len < 0)
144  throw std::runtime_error("Error generating project_root");
145  if (next_len < 0)
146  throw std::runtime_error("Error generating next_root");
147 
149  project_root_.append(fileroot.data(), file_len);
150  next_root_ = title_;
151  next_root_.append(nextroot.data(), next_len);
152  std::stringstream s;
153  s << series_ + 1;
154  if (cur_)
155  xmlSetProp(cur_, (const xmlChar*)"series", (const xmlChar*)(s.str().c_str()));
156 }
157 
158 bool ProjectData::previousRoot(std::string& oldroot) const
159 {
160  oldroot.clear();
161  if (series_)
162  {
163  //int nproc_g = OHMMS::Controller->size();
164  int nproc = my_comm_->size();
165  int nodeid = my_comm_->rank();
166  int groupid = my_comm_->getGroupID();
167  bool no_gtag = (qmc_common.mpi_groups == 1);
168  std::array<char, 128> fileroot;
169  int file_len{0};
170  if (no_gtag)
171  {
172  if (nproc > 1)
173  file_len = std::snprintf(fileroot.data(), fileroot.size(), ".s%03d.p%03d", series_ - 1, nodeid);
174  else
175  file_len = std::snprintf(fileroot.data(), fileroot.size(), ".s%03d", series_ - 1);
176  }
177  else
178  {
179  if (nproc > 1)
180  file_len = std::snprintf(fileroot.data(), fileroot.size(), ".g%03d.s%03d.p%03d", groupid, series_ - 1, nodeid);
181  else
182  file_len = std::snprintf(fileroot.data(), fileroot.size(), ".g%03d.s%03d", groupid, series_ - 1);
183  }
184  if (file_len < 0)
185  throw std::runtime_error("Error generating olfroot");
186  oldroot = title_;
187  oldroot.append(fileroot.data(), file_len);
188  return true;
189  }
190  else
191  {
192  return false;
193  }
194 }
195 
196 bool ProjectData::put(xmlNodePtr cur)
197 {
198  cur_ = cur;
199  title_ = getXMLAttributeValue(cur, "id");
200  const std::string series_str(getXMLAttributeValue(cur, "series"));
201  if (!series_str.empty())
202  series_ = std::stoi(series_str);
203 
204  std::string driver_version_str;
205  ParameterSet m_param;
206  m_param.add(max_cpu_secs_, "max_seconds");
207  m_param.add(driver_version_str, "driver_version");
208  m_param.put(cur);
209 
210  if (!driver_version_str.empty())
211  driver_version_ = lookupDriverVersion(driver_version_str);
212 
213  ///first, overwrite the existing xml nodes
214  cur = cur->xmlChildrenNode;
215  while (cur != NULL)
216  {
217  std::string cname((const char*)(cur->name));
218  if (cname == "user")
219  {
220  // Removed
221  }
222  if (cname == "host")
223  {
224  host_ = getHostName();
225  const XMLNodeString node_string(host_);
226  node_string.setXMLNodeContent(cur);
227  }
228  if (cname == "date")
229  {
230  date_ = getDateAndTime();
231  const XMLNodeString node_string(date_);
232  node_string.setXMLNodeContent(cur);
233  }
234  cur = cur->next;
235  }
236  ///second, add xml nodes, if missing
237  if (host_ == "none")
238  {
239  host_ = getHostName();
240  xmlNewChild(cur_, cur_->ns, (const xmlChar*)"host", (const xmlChar*)(host_.c_str()));
241  }
242  if (date_ == "none")
243  {
244  date_ = getDateAndTime();
245  xmlNewChild(cur_, cur_->ns, (const xmlChar*)"date", (const xmlChar*)(date_.c_str()));
246  }
247  reset();
248  return true;
249 }
250 
251 const std::string& ProjectData::getTitle() const noexcept { return title_; }
252 
253 const std::string& ProjectData::currentMainRoot() const noexcept { return project_main_; }
254 
255 const std::string& ProjectData::nextRoot() const noexcept { return next_root_; }
256 
257 int ProjectData::getSeriesIndex() const noexcept { return series_; }
258 
259 int ProjectData::getMaxCPUSeconds() const noexcept { return max_cpu_secs_; }
260 
262 
263 bool ProjectData::isComplex() const noexcept { return runtime_options_.is_complex; }
264 
266 
267 // PRIVATE
269 {
270  std::string enum_value_str(lowerCase(enum_value));
271  try
272  {
273  return lookup_input_enum_value.at(enum_value_str);
274  }
275  catch (std::out_of_range& oor_exc)
276  {
277  throw UniformCommunicateError("bad_enum_tag_value: " + enum_value_str);
278  }
279 }
280 
281 } // namespace qmcplusplus
const std::string & getTitle() const noexcept
returns the title of the project <project id="det_qmc_short_sdbatch_vmcbatch_mwalkers" series="0"> tr...
bool previousRoot(std::string &oldroot) const
return the root of the previous sequence
DriverVersion driver_version_
Definition: ProjectData.h:147
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
int rank() const
return the rank
Definition: Communicate.h:116
void setCommunicator(Communicate *c)
Definition: ProjectData.cpp:50
ProjectData(const std::string &atitle="", DriverVersion de=DriverVersion::LEGACY)
constructor
Definition: ProjectData.cpp:35
string getDateAndTime()
Definition: sysutil.cpp:31
bool isComplex() const noexcept
void advance()
increment a series number and reset project_root_
Definition: ProjectData.cpp:81
static const std::unordered_map< std::string, DriverVersion > lookup_input_enum_value
Definition: ProjectData.h:50
int series_
series index
Definition: ProjectData.h:135
const std::string & nextRoot() const noexcept
returns the nextroot of the project, the series id is incremented at every QMC section <project id="d...
Communicate * Controller
Global Communicator for a process.
Definition: Communicate.cpp:35
int size() const
return the number of tasks
Definition: Communicate.h:118
const RuntimeOptions & getRuntimeOptions() const noexcept
bool put(std::istream &is) override
read from std::istream
Definition: ParameterSet.h:42
std::string next_root_
root for the next run
Definition: ProjectData.h:132
Communicate * my_comm_
communicator
Definition: ProjectData.h:138
Wrapping information on parallelism.
Definition: Communicate.h:68
std::string date_
date when the job is executed
Definition: ProjectData.h:123
int getGroupID() const
return the group id
Definition: Communicate.h:121
int getMaxCPUSeconds() const noexcept
DriverVersion getDriverVersion() const noexcept
class to handle a set of parameters
Definition: ParameterSet.h:27
bool put(std::istream &is)
Definition: ProjectData.cpp:60
std::string project_main_
main root for all the output engines
Definition: ProjectData.h:126
DriverVersion
Enum for global scope switch of design from legacy driver based to batch driver based.
Definition: ProjectData.h:43
std::string project_root_
processor-dependent root for all the output engines
Definition: ProjectData.h:129
int max_cpu_secs_
max cpu seconds
Definition: ProjectData.h:144
This a subclass for runtime errors that will occur on all ranks.
std::string lowerCase(const std::string_view s)
++17
xmlNodePtr cur_
the xml node for <Project>
Definition: ProjectData.h:141
void setXMLNodeContent(xmlNodePtr cur) const
write a string to an xmlNode
void setName(const std::string &aname)
Definition: Communicate.h:129
std::string getXMLAttributeValue(const xmlNodePtr cur, const std::string_view name)
get the value string for attribute name if name is unfound in cur you get an empty string back this i...
int mpi_groups
number of mpi groups
Definition: qmc_common.h:33
string getHostName()
< return the host name
Definition: sysutil.cpp:24
const std::string & currentMainRoot() const noexcept
returns the projectmain of the project, the series id is incremented at every QMC section <project id...
void add(PDT &aparam, const std::string &aname_in, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new parameter corresponding to an xmlNode <parameter>
convert xmlNode contents into a std::string
void rewind()
roll-back a series number and reset project_root_ by one
Definition: ProjectData.cpp:87
bool get(std::ostream &os) const
Definition: ProjectData.cpp:52
int getSeriesIndex() const noexcept
QMCState qmc_common
a unique QMCState during a run
Definition: qmc_common.cpp:111
std::string host_
name of the host where the job is running
Definition: ProjectData.h:120
static DriverVersion lookupDriverVersion(const std::string &enum_value)
RuntimeOptions runtime_options_
Definition: ProjectData.h:150
std::string title_
title of the project
Definition: ProjectData.h:117
void reset()
Construct the root name with title_ and m_series.
Definition: ProjectData.cpp:97