QMCPACK
BranchIO.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) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Cynthia Gu, zg1@ornl.gov, Oak Ridge National Laboratory
9 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
10 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
11 //
12 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
16 #include "BranchIO.h"
17 #include "hdf/HDFVersion.h"
18 #include "Message/CommOperators.h"
19 #include "hdf/hdf_archive.h"
20 #if defined(HAVE_LIBBOOST)
21 #include <boost/property_tree/ptree.hpp>
22 #include <boost/property_tree/xml_parser.hpp>
23 #include <boost/foreach.hpp>
24 #include <string>
25 #include <set>
26 #include <exception>
27 #include <iostream>
28 #endif
29 
30 //#include <boost/archive/text_oarchive.hpp>
31 //
32 namespace qmcplusplus
33 {
34 #if defined(HAVE_LIBBOOST)
35 template<typename T>
36 inline void put_histogram(std::string name, accumulator_set<T> a, boost::property_tree::ptree& pt)
37 {
38  pt.put(name + ".value", a.properties[0]);
39  pt.put(name + ".value_squared", a.properties[1]);
40  pt.put(name + ".weight", a.properties[2]);
41 }
42 
43 template<typename T>
44 inline void get_histogram(std::string name, accumulator_set<T>& a, boost::property_tree::ptree& pt)
45 {
46  a.properties[0] = pt.get<T>(name + ".value");
47  a.properties[1] = pt.get<T>(name + ".value_squared");
48  a.properties[2] = pt.get<T>(name + ".weight");
49 }
50 #endif
51 
52 template<typename T>
53 struct h5data_proxy<accumulator_set<T>> : public h5_space_type<T, 1>
54 {
55  enum
56  {
58  };
62 
63  inline h5data_proxy(const data_type& a) { dims[0] = CAPACITY; }
64 
65  inline bool read(data_type& ref, hid_t grp, const std::string& aname, hid_t xfer_plist = H5P_DEFAULT)
66  {
67  return h5d_read(grp, aname, get_address(ref.properties), xfer_plist);
68  }
69 
70  inline bool write(const data_type& ref, hid_t grp, const std::string& aname, hid_t xfer_plist = H5P_DEFAULT) const
71  {
72  return h5d_write(grp, aname.c_str(), this->size(), dims, get_address(ref.properties), xfer_plist);
73  }
74 };
75 
76 template<class SFNB>
77 std::vector<std::string> BranchIO<SFNB>::vParamName;
78 template<class SFNB>
79 std::vector<std::string> BranchIO<SFNB>::iParamName;
80 
81 template<class SFNB>
83 {
84  if (vParamName.size())
85  return;
86  vParamName.resize(10);
87  vParamName[0] = "tau";
88  vParamName[1] = "taueff";
89  vParamName[2] = "etrial";
90  vParamName[3] = "eref";
91  vParamName[4] = "branchmax";
92  vParamName[5] = "branchcutoff";
93  vParamName[6] = "branchfilter";
94  vParamName[7] = "sigma";
95  vParamName[8] = "acc_energy";
96  vParamName[9] = "acc_samples";
97 
98  iParamName.resize(7);
99  iParamName[0] = "warmupsteps";
100  iParamName[1] = "energyupdateinterval";
101  iParamName[2] = "counter";
102  iParamName[3] = "targetwalkers";
103  iParamName[4] = "maxwalkers";
104  iParamName[5] = "minwalkers";
105  iParamName[6] = "brnachinterval";
106 }
107 
108 template<class SFNB>
109 bool BranchIO<SFNB>::write(const std::string& fname)
110 {
111  if (myComm->rank())
112  return true;
113 
114 #if defined(HAVE_LIBBOOST)
115  initAttributes();
116  using boost::property_tree::ptree;
117  ptree pt;
118 
119  // Put log filename in property tree
120  pt.put("state.branchmode", ref.BranchMode);
121 
122  auto it_vparam = ref.vParam.begin();
123  for (auto& name : vParamName)
124  pt.put("state.vparam." + name, *(it_vparam++));
125  for (int i = 0; i < iParamName.size(); ++i)
126  pt.put("state.iparam." + iParamName[i], ref.iParam[i]);
127 
128  put_histogram("state.energy", ref.EnergyHist, pt);
129  put_histogram("state.variance", ref.VarianceHist, pt);
130  put_histogram("state.r2accepted", ref.R2Accepted, pt);
131  put_histogram("state.r2proposed", ref.R2Proposed, pt);
132  std::string xname = fname + ".qmc.xml";
133  write_xml(xname, pt);
134 #else
135  //append .qmc.h5 if missing
136  std::string h5name(fname);
137  if (fname.find("qmc.h5") >= fname.size())
138  h5name.append(".qmc.h5");
139  hdf_archive dump(myComm);
140  hid_t fid = dump.create(h5name);
141  dump.push(hdf::main_state);
142  dump.push(hdf::qmc_status);
143  std::string v_header("tau:taueff:etrial:eref:branchmax:branchcutoff:branchfilter:sigma:acc_energy:acc_samples");
144  std::string i_header("warmupsteps:energyupdateinterval:counter:targetwalkers:maxwalkers:minwalkers:branchinterval");
145  dump.write(v_header, "vparam_def");
146  dump.write(i_header, "iparam_def");
147  dump.write(ref.vParam, "vparam");
148  dump.write(ref.iParam, "iparam");
149  dump.write(ref.BranchMode, "branchmode");
150  dump.push("histogram");
151  dump.write(ref.EnergyHist, "energy");
152  dump.write(ref.VarianceHist, "variance");
153  dump.write(ref.R2Accepted, "r2accepted");
154  dump.write(ref.R2Proposed, "r2proposed");
155  //PopHist is not being used in 2010-10-19
156  //if(ref.BranchMode[SimpleFixedNodeBranch::B_DMC])
157  //{
158  // dump.push("population");
159  // dump.write(ref.PopHist.myData,"histogram");
160  //}
161 #endif
162  return true;
163 }
164 
165 template<class SFNB>
166 bool BranchIO<SFNB>::read(const std::string& fname)
167 {
168  int found_config = 0;
169 
170  if (myComm->rank() == 0)
171  {
172  initAttributes();
173  using boost::property_tree::ptree;
174  ptree pt;
175  std::string xname = fname + ".qmc.xml";
176  read_xml(xname, pt);
177  if (!pt.empty())
178  {
179  ref.BranchMode = pt.get<BranchModeType>("state.branchmode");
180 
181  get_histogram("state.energy", ref.EnergyHist, pt);
182  get_histogram("state.variance", ref.VarianceHist, pt);
183  get_histogram("state.r2accepted", ref.R2Accepted, pt);
184  get_histogram("state.r2proposed", ref.R2Proposed, pt);
185 
186  int i = 0;
187  BOOST_FOREACH (const ptree::value_type& v, pt.get_child("state.iparam"))
188  {
189  ref.iParam[i++] = v.second.get_value<int>();
190  }
191  auto it_vparam = ref.vParam.begin();
192  BOOST_FOREACH (const ptree::value_type& v, pt.get_child("state.vparam"))
193  {
194  *(it_vparam++) = v.second.get_value<double>();
195  }
196  found_config = 1;
197  }
198  }
199  myComm->bcast(found_config);
200 
201  if (!found_config)
202  return false;
203 
204  bcast_state();
205 
206  return true;
207 }
208 
209 template<class SFNB>
211 {
212  int n = ref.vParam.size() + ref.iParam.size();
213  std::vector<RealType> pdata(n + 1 + 16, -1);
214 
215  if (myComm->rank() == 0)
216  {
217  copy(ref.vParam.begin(), ref.vParam.end(), pdata.begin());
218  copy(ref.iParam.begin(), ref.iParam.end(), pdata.begin() + ref.vParam.size());
219  int offset = n;
220  pdata[offset++] = ref.BranchMode.to_ulong();
221  copy(ref.EnergyHist.properties, ref.EnergyHist.properties + 4, pdata.begin() + offset);
222  offset += 4;
223  copy(ref.VarianceHist.properties, ref.VarianceHist.properties + 4, pdata.begin() + offset);
224  offset += 4;
225  copy(ref.R2Accepted.properties, ref.R2Accepted.properties + 4, pdata.begin() + offset);
226  offset += 4;
227  copy(ref.R2Proposed.properties, ref.R2Proposed.properties + 4, pdata.begin() + offset);
228  }
229 
230  //broadcast to the nodes : need to add a namespace mpi::
231  myComm->bcast(pdata);
232 
233  if (myComm->rank())
234  {
235  int ii = 0;
236  for (auto& vpar : ref.vParam)
237  vpar = pdata[ii++];
238  for (int i = 0; i < ref.iParam.size(); ++i, ++ii)
239  ref.iParam[i] = static_cast<int>(pdata[ii]);
240  ref.BranchMode = static_cast<unsigned long>(pdata[ii]);
241  }
242  {
243  //update historgram
244  int ii = n + 1;
245  ref.EnergyHist.reset(pdata[ii], pdata[ii + 1], pdata[ii + 2]);
246  ii += 4;
247  ref.VarianceHist.reset(pdata[ii], pdata[ii + 1], pdata[ii + 2]);
248  ii += 4;
249  ref.R2Accepted.reset(pdata[ii], pdata[ii + 1], pdata[ii + 2]);
250  ii += 4;
251  ref.R2Proposed.reset(pdata[ii], pdata[ii + 1], pdata[ii + 2]);
252  }
253 }
254 
255 template class BranchIO<SimpleFixedNodeBranch>;
256 } // namespace qmcplusplus
default struct to define a h5 dataspace, any intrinsic type T
Definition: hdf_dataspace.h:44
void write(T &data, const std::string &aname)
write the data to the group aname and check status runtime error is issued on I/O error ...
Definition: hdf_archive.h:259
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
static void initAttributes()
Definition: BranchIO.cpp:82
class to handle hdf file
Definition: hdf_archive.h:51
bool write(const data_type &ref, hid_t grp, const std::string &aname, hid_t xfer_plist=H5P_DEFAULT) const
Definition: BranchIO.cpp:70
typename SFNB::BranchModeType BranchModeType
Definition: BranchIO.h:26
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Definition: Blitz.h:639
bool read(const std::string &fname)
Definition: BranchIO.cpp:166
const char qmc_status[]
Definition: HDFVersion.h:41
generic accumulator of a scalar type
Definition: accumulators.h:32
hsize_t dims[RANK > 0 ? RANK :1]
shape of the dataspace, protected for zero size array, hdf5 support scalar as rank = 0 ...
Definition: hdf_dataspace.h:47
T properties[CAPACITY]
Definition: accumulators.h:44
static std::vector< std::string > iParamName
Definition: BranchIO.h:40
const char main_state[]
Definition: HDFVersion.h:31
void push(const std::string &gname, bool createit=true)
push a group to the group stack
static auto get_address(T *a)
return the address
Definition: hdf_dataspace.h:53
bool create(const std::filesystem::path &fname, unsigned flags=H5F_ACC_TRUNC)
create a file
static std::vector< std::string > vParamName
Definition: BranchIO.h:39
bool write(const std::string &fname)
Definition: BranchIO.cpp:109
bool read(data_type &ref, hid_t grp, const std::string &aname, hid_t xfer_plist=H5P_DEFAULT)
Definition: BranchIO.cpp:65
bool h5d_read(hid_t grp, const std::string &aname, T *first, hid_t xfer_plist)
return true, if successful
QMCTraits::FullPrecRealType value_type
bool h5d_write(hid_t grp, const std::string &aname, hsize_t ndims, const hsize_t *dims, const T *first, hid_t xfer_plist)
generic h5data_proxy<T> for scalar basic datatypes defined in hdf_dataspace.h Note if the dataset to ...
Definition: hdf_dataproxy.h:29