CHROMA
eigen_io.cc
Go to the documentation of this file.
1 #include "chromabase.h"
2 #include "io/eigen_io.h"
3 #include "io/readszinferm_w.h"
4 #include <iostream>
5 #include <iomanip>
6 
7 namespace Chroma {
8 
9 void read(XMLReader& xml, const std::string& path, RitzParams_t& header)
10 {
11 
12 
13  try {
14  XMLReader paramtop(xml, path);
15  read(paramtop, "Neig", header.Neig);
16  read(paramtop, "RsdR", header.RsdR);
17  read(paramtop, "RsdA", header.RsdA);
18  read(paramtop, "RsdZero", header.RsdZero);
19  read(paramtop, "ProjApsiP", header.ProjApsiP);
20  read(paramtop, "Ndummy", header.Ndummy);
21  read(paramtop, "GammaFactor", header.GammaFactor);
22  read(paramtop, "MaxKS", header.MaxKS);
23  read(paramtop, "MaxCG", header.MaxCG);
24  read(paramtop, "MinKSIter", header.MinKSIter);
25  read(paramtop, "MaxKSIter", header.MaxKSIter);
26  read(paramtop, "Nrenorm", header.Nrenorm);
27 
28  }
29  catch( const std::string& error ) {
30  QDPIO::cerr << "Caught Exception " << error << std::endl;
31  QDP_error_exit("Exiting\n");
32  }
33 }
34 
35 void write(XMLWriter& xml, const std::string& path, const RitzParams_t& header)
36 {
37  push(xml, path);
38 
39  write(xml, "Neig", header.Neig);
40  write(xml, "RsdR", header.RsdR);
41  write(xml, "RsdA", header.RsdA);
42  write(xml, "RsdZero", header.RsdZero);
43  write(xml, "ProjApsiP", header.ProjApsiP);
44  write(xml, "Ndummy", header.Ndummy);
45  write(xml, "GammaFactor", header.GammaFactor);
46  write(xml, "MaxKS", header.MaxKS);
47  write(xml, "MaxCG", header.MaxCG);
48  write(xml, "MinKSIter", header.MinKSIter);
49  write(xml, "MaxKSIter", header.MaxKSIter);
50  write(xml, "Nrenorm", header.Nrenorm);
51  pop(xml);
52 }
53 
54 void read(XMLReader& xml, const std::string& path, EigenIO_t& io_header)
55 {
56 
57  try {
58  XMLReader paramtop(xml, path);
59  read(paramtop, "eigen_file_stem", io_header.eigen_file);
60  read(paramtop, "eigen_volfmt", io_header.eigen_volfmt);
61 
62  // If user specifies file format then read it
63  if( paramtop.count("eigen_filefmt") == 1 ) {
64  read(paramtop, "eigen_filefmt", io_header.eigen_filefmt);
65  }
66  else {
67  // Otherwise assume default file format -- SciDAC format
68  io_header.eigen_filefmt = EVEC_TYPE_SCIDAC;
69  }
70  }
71  catch( const std::string& error ) {
72  QDPIO::cerr << "Caught exception " << error << std::endl;
73  QDP_error_exit("Exiting\n");
74  }
75 
76 
77 
78 }
79 
80 void write(XMLWriter& xml, const std::string& path, const EigenIO_t& io_header)
81 {
82  push(xml, path);
83  write(xml,"eigen_filefmt", io_header.eigen_filefmt);
84  write(xml,"eigen_file_stem", io_header.eigen_file);
85  write(xml,"eigen_volfmt", io_header.eigen_volfmt);
86  pop(xml);
87 
88 }
89 
90 void read(XMLReader &xml, const std::string& path, ChromaWilsonRitz_t& param)
91 {
92  multi1d<int> seed_int(4);
93  try {
94  XMLReader paramtop(xml, path);
95  read(paramtop, "Param/version", param.version);
96 
97  // Read the fermion action
98  XMLReader xml_tmp(paramtop, "Param/FermionAction");
99  std::ostringstream os;
100  xml_tmp.print(os);
101  param.fermact = os.str();
102 
103  read(paramtop, "Param/nrow", param.nrow);
104  read(paramtop, "Param/rng", param.seed);
105  read(paramtop, "RitzParams", param.ritz_params);
106  read(paramtop, "Cfg", param.cfg);
107  read(paramtop, "Eigen", param.eigen_io_params);
108 
109  if( paramtop.count("StateInfo") == 1 ) {
110  XMLReader xml_state_info(paramtop, "StateInfo");
111  std::ostringstream state_info_os;
112  xml_state_info.print(state_info_os);
113  param.state_info = state_info_os.str();
114  }
115  else {
116  XMLBufferWriter s_i_xml;
117  push(s_i_xml,"StateInfo");
118  pop(s_i_xml);
119  param.state_info = s_i_xml.str();
120  }
121 
122  }
123  catch( const std::string& error) {
124  QDPIO::cerr << "Caught exception " << error << std::endl;
125  QDP_error_exit("Exiting \n");
126  }
127 }
128 
129 void write(XMLWriter &xml, const std::string& path, const ChromaWilsonRitz_t& param)
130 {
131  push( xml, path );
132 
133  push( xml, "Param");
134  write(xml, "version", param.version);
135  write(xml, "FermionAction", param.fermact);
136  write(xml, "nrow", param.nrow);
137  write(xml, "rng", param.seed);
138  pop(xml);
139 
140  write(xml, "RitzParams", param.ritz_params);
141  write(xml, "Cfg", param.cfg);
142 
143  // Write out the state info struct, even if it is empty
144  try {
145  // Make an istream from the XML
146  std::istringstream s_i_i(param.state_info);
147 
148  // Make the XMLReader (toplevel)
149  XMLReader r1(s_i_i);
150 
151  // Set context tag to "/StateInfo"
152  XMLReader r2(r1, "/StateInfo");
153 
154  // Dump the StateInfo tag and descendets into an ostream
155  std::ostringstream s_i_o;
156  r2.print(s_i_o);
157 
158  // Dump the XML String without an extra header
159  xml << s_i_o.str() ;
160  }
161  catch( const std::string& e) {
162  QDPIO::cerr << "Caught exception : " << e << std::endl;
163  QDP_abort(1);
164  }
165  write(xml, "Eigen", param.eigen_io_params);
166 
167  pop(xml);
168 }
169 
170 void writeEigen(const ChromaWilsonRitz_t& header, multi1d<Real>& lambda_lo,
171  multi1d<LatticeFermion>& eigv_lo, Real& lambda_hi,
172  QDP_serialparallel_t serpar)
173 {
174 
176  QDPIO::cerr << "Writing Eigenvectors only supported in SciDAC format" << std::endl;
177  QDP_abort(1);
178  }
179 
180 
181  XMLBufferWriter file_xml;
182  int file_neig=1;
183 
184  push(file_xml, "WilsonRitzEigen");
185  write(file_xml, "InputParams", header);
186  write(file_xml, "lambda_hi", lambda_hi);
187  write(file_xml, "Neig_file", file_neig);
188  pop(file_xml);
189 
190  for(int lo=0; lo < header.ritz_params.Neig; lo++) {
191  std::ostringstream filename;
192  filename << header.eigen_io_params.eigen_file << "_" << std::setw(3) << std::setfill('0') << lo;
193 
194  XMLBufferWriter record_eval_xml;
195  push(record_eval_xml, "record_eval");
196  write(record_eval_xml, "eval_num", lo);
197  pop(record_eval_xml);
198 
199  XMLBufferWriter record_evec_xml;
200  push(record_evec_xml, "record_evec");
201  write(record_evec_xml, "evec_num", lo);
202  pop(record_evec_xml);
203 
204  QDPIO::cout << "Opening eigenstd::vector file: "<< filename.str() << std::endl;
205  QDPFileWriter outfile(file_xml, filename.str(),
207  serpar, QDPIO_OPEN);
208 
209  multi1d<Real> lambda_lo_arr(1);
210  lambda_lo_arr[0] = lambda_lo[lo];
211  write(outfile, record_eval_xml, lambda_lo_arr);
212  write(outfile, record_evec_xml, eigv_lo[lo]);
213  close(outfile);
214  }
215 }
216 
217 void readEigenPair(Real& lambda_lo, int& eig_index,
218  LatticeFermion& eigv,
219  const std::string& filename,
220  QDP_serialparallel_t serpar,
221  XMLReader& file_xml)
222  /* XMLReader& record_xml) */
223 {
224 
225  XMLReader record_eval_xml;
226  XMLReader record_evec_xml;
227 
228  QDPIO::cout << "Attempting to read from " << filename << std::endl;
229  QDPFileReader from(file_xml, filename, serpar);
230 
231 
232  // Now read the first eigenvalue/std::vector
233  multi1d<Real> lambda_lo_aux(1);
234  read(from, record_eval_xml, lambda_lo_aux);
235  lambda_lo = lambda_lo_aux[0];
236  read(from, record_evec_xml, eigv);
237  /*
238  // Get the lambda_lo
239  try {
240  read( record_xml, "/record_evalue/lambda", lambda_lo);
241  }
242  catch( const std::string& e) {
243  QDPIO::cerr << "Caught exception reading e-value " << e << std::endl;
244  QDP_error_exit("Exiting\n");
245  }
246  */
247  // Get the eval_num
248  int eval_num;
249  int evec_num;
250  try {
251  read( record_eval_xml, "/record_eval/eval_num", eval_num);
252  }
253  catch( const std::string& e) {
254  QDPIO::cerr << "Caught exception reading eval_num " << e << std::endl;
255  QDP_error_exit("Exiting\n");
256  }
257 
258  try {
259  read( record_evec_xml, "/record_evec/evec_num", evec_num);
260  }
261  catch( const std::string& e) {
262  QDPIO::cerr << "Caught exception reading evec_num " << e << std::endl;
263  QDP_error_exit("Exiting\n");
264  }
265 
266  if( eval_num != evec_num) {
267  QDP_error_exit("Eval num and evec_num are different\n");
268  }
269 
270  eig_index = eval_num;
271  // Done with file
272  close(from);
273 }
274 
275 
276 void readEigen(ChromaWilsonRitz_t& header, multi1d<Real>& lambda_lo,
277  multi1d<LatticeFermion>& eigv_lo, Real& lambda_hi,
278  const std::string& filename_stem,
279  int Neig,
280  QDP_serialparallel_t serpar)
281 {
282 
283  int neig_to_load = Neig;
284 
285  for(int lo=0; lo < Neig && lo < neig_to_load; lo++) {
286  XMLReader file_xml;
287 
288  int eig_index;
289 
290  Real lambda_lo_tmp;
291  LatticeFermion eigv_tmp;
292 
293  std::ostringstream filename;
294 
295  filename << filename_stem << "_" << std::setw(3) << std::setfill('0') << lo;
296  readEigenPair(lambda_lo_tmp, eig_index, eigv_tmp,
297  filename.str(), serpar,
298  file_xml);
299 
300 
301  // Stuff we need to do once
302  if( lo == 0 ) {
303  // Read header -- should be the same for all but I wont check for that
304  try {
305  read(file_xml, "/WilsonRitzEigen/InputParams", header);
306  }
307  catch ( const std::string& e ) {
308  QDPIO::cerr << "Caught Exception reading header: " << e << std::endl;
309  QDP_error_exit("Exiting\n");
310  }
311 
312 
313  // Get the highest e-value
314  try {
315  read(file_xml, "/WilsonRitzEigen/lambda_hi", lambda_hi);
316  }
317  catch ( const std::string& e ) {
318  QDPIO::cerr << "Caught Exception reading lambda_hi: " << e << std::endl;
319  QDP_error_exit("Exiting\n");
320  }
321 
322  // Check how many e-values there are
323  if( header.ritz_params.Neig < Neig ) {
324  QDPIO::cout << "Requested " << Neig << " eigenpairs but only " << header.ritz_params.Neig << " were computed. Will only read " << header.ritz_params.Neig << " pairs" << std::endl;
325  neig_to_load = header.ritz_params.Neig;
326  }
327  else {
328  header.ritz_params.Neig = Neig;
329  }
330 
331  // Resize arrays to accomodate
332  lambda_lo.resize(neig_to_load);
333  eigv_lo.resize(neig_to_load);
334  }
335 
336  // Check index
337  if( eig_index != lo) {
338  QDPIO::cerr << "Error: index and eig_index dont match lo = " << lo << " eig_index = " << eig_index << std::endl;
339  QDP_error_exit("Exiting\n");
340  }
341 
342  // Put things in their place
343  lambda_lo[eig_index] = lambda_lo_tmp;
344  eigv_lo[eig_index] = eigv_tmp;
345  }
346 }
347 
348 
349 // Read SZIN eigenvalues.
350 // Expects the following:
351 // filename_stem.xml -- contains the Eigenvalues under tag Eigenvalues
352 // filename_stem_XXX contains the SZIN eigenvectors
353 void readEigenSzin(multi1d<Real>& lambda_lo,
354  multi1d<LatticeFermion>& eigv_lo, Real& lambda_hi,
355  const int Neig,
356  const std::string& filename_stem)
357 {
358 
359  std::ostringstream xml_filename;
360  xml_filename << filename_stem << ".xml" ;
361 
362  QDPIO::cout << "Attempting to open " << xml_filename.str() << std::endl;
363  // Try and get the e-values.
364  XMLReader reader(xml_filename.str());
365 
366  try {
367  XMLReader paramtop(reader, "/Eigenvalues");
368  read(paramtop, "lambda_lo", lambda_lo);
369  read(paramtop, "lambda_hi", lambda_hi);
370  }
371  catch( const std::string& e) {
372  QDPIO::cerr << "Caught exception : " << e << std::endl;
373  QDP_abort(1);
374  }
375 
376  if ( lambda_lo.size() != Neig ) {
377  QDPIO::cerr << "Mismatch in no of low eigenvalues. Neig = " << Neig <<
378  "but read " << lambda_lo.size() << std::endl;
379  QDP_abort(1);
380  }
381 
382  eigv_lo.resize(lambda_lo.size());
383 
384  for(int evec = 0; evec < lambda_lo.size(); evec++) {
385 
386  // Create filename
387  std::ostringstream filename;
388  filename << filename_stem << "_" << std::setw(3) << std::setfill('0') << evec;
389  QDPIO::cout << "Attempting to open " << filename.str() << std::endl;
390  // read the evec
391  try {
392 
393  readSzinFerm(eigv_lo[evec], filename.str());
394  }
395  catch (const std::string& e) {
396  QDPIO::cerr << "Caught exception " << e << std::endl;
397  QDP_abort(1);
398  }
399 
400 
401  }
402 
403 
404 }
405 
406 } // end namespace Chroma
407 
Primary include file for CHROMA library code.
Eigenvalue IO.
void read(XMLReader &xml, const std::string &path, AsqtadFermActParams &param)
Read parameters.
void write(XMLWriter &xml, const std::string &path, const AsqtadFermActParams &param)
Writer parameters.
void readSzinFerm(LatticeFermion &q, const std::string &file)
Read a SZIN fermion. This is a simple memory dump reader.
void readEigenSzin(multi1d< Real > &lambda_lo, multi1d< LatticeFermion > &eigv_lo, Real &lambda_hi, const int Neig, const std::string &filename_stem)
Definition: eigen_io.cc:353
void readEigenPair(Real &lambda_lo, int &eig_index, LatticeFermion &eigv, const std::string &filename, QDP_serialparallel_t serpar, XMLReader &file_xml)
Definition: eigen_io.cc:217
void readEigen(ChromaWilsonRitz_t &header, multi1d< Real > &lambda_lo, multi1d< LatticeFermion > &eigv_lo, Real &lambda_hi, const std::string &filename_stem, int Neig, QDP_serialparallel_t serpar)
Definition: eigen_io.cc:276
void writeEigen(const ChromaWilsonRitz_t &header, multi1d< Real > &lambda_lo, multi1d< LatticeFermion > &eigv_lo, Real &lambda_hi, QDP_serialparallel_t serpar)
Definition: eigen_io.cc:170
Handle< FermBC< LatticeStaggeredFermion, multi1d< LatticeColorMatrix >, multi1d< LatticeColorMatrix > > > reader(XMLReader &xml_in, const std::string &path)
Helper function for the FermionAction readers.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
QDP_error_exit("too many BiCG iterations", n_count, rsd_sq, cp, c, re_rvr, im_rvr, re_a, im_a, re_b, im_b)
push(xml_out,"Condensates")
pop(xml_out)
::std::string string
Definition: gtest.h:1979
Read an old SZIN-style (checkerboarded) lattice Dirac fermion.
Struct for the overall application.
Definition: eigen_io.h:50
multi1d< int > nrow
Definition: eigen_io.h:54
RitzParams_t ritz_params
Definition: eigen_io.h:56
std::string state_info
Definition: eigen_io.h:53
Struct for dumping the eigenvalues/vectors.
Definition: eigen_io.h:40
EigenVecType eigen_filefmt
Definition: eigen_io.h:41
std::string eigen_file
Definition: eigen_io.h:42
QDP_volfmt_t eigen_volfmt
Definition: eigen_io.h:43
Struct for parameters needed for a Ritz KS type solve.
Definition: eigen_io.h:22