CHROMA
inline_multi_propagator_w.cc
Go to the documentation of this file.
1 /*! \file
2  * \brief Inline construction of propagator
3  *
4  * Propagator calculations
5  */
6 
7 #include "fermact.h"
10 #include "meas/glue/mesplq.h"
11 #include "util/ft/sftmom.h"
12 #include "util/info/proginfo.h"
13 #include "util/info/unique_id.h"
18 
20 #include <iomanip>
21 
22 namespace Chroma
23 {
24  namespace InlineMultiPropagatorEnv
25  {
26  namespace
27  {
28  AbsInlineMeasurement* createMeasurement(XMLReader& xml_in,
29  const std::string& path)
30  {
31  return new InlineMultiPropagator(InlineMultiPropagatorParams(xml_in, path));
32  }
33 
34  //! Local registration flag
35  bool registered = false;
36  }
37 
38  const std::string name = "MULTI_PROPAGATOR";
39 
40  //! Register all the factories
41  bool registerAll()
42  {
43  bool success = true;
44  if (! registered)
45  {
47  success &= TheInlineMeasurementFactory::Instance().registerObject(name, createMeasurement);
48  registered = true;
49  }
50  return success;
51  }
52  }
53 
54 
55  //! MultiPropagator input
56  void read(XMLReader& xml, const std::string& path, InlineMultiPropagatorParams::NamedObject_t& input)
57  {
58  XMLReader inputtop(xml, path);
59 
60  read(inputtop, "gauge_id", input.gauge_id);
61  read(inputtop, "source_id", input.source_id);
62  read(inputtop, "prop_id", input.prop_id);
63  }
64 
65  //! MultiPropagator output
66  void write(XMLWriter& xml, const std::string& path, const InlineMultiPropagatorParams::NamedObject_t& input)
67  {
68  push(xml, path);
69 
70  write(xml, "gauge_id", input.gauge_id);
71  write(xml, "source_id", input.source_id);
72  write(xml, "prop_id", input.prop_id);
73 
74  pop(xml);
75  }
76 
77 
78  // Param stuff
80 
82  {
83  try
84  {
85  XMLReader paramtop(xml_in, path);
86 
87  if (paramtop.count("Frequency") == 1)
88  read(paramtop, "Frequency", frequency);
89  else
90  frequency = 1;
91 
92  // Parameters for source construction
93  read(paramtop, "Param", param);
94 
95  // Read any auxiliary state information
96  if( paramtop.count("Param/StateInfo") == 1 ) {
97  XMLReader xml_state_info(paramtop, "Param/StateInfo");
98  std::ostringstream os;
99  xml_state_info.print(os);
100  stateInfo = os.str();
101  }
102  else {
103  XMLBufferWriter s_i_xml;
104  push(s_i_xml, "StateInfo");
105  pop(s_i_xml);
106  stateInfo = s_i_xml.printCurrentContext();
107  }
108 
109  // Ids
110  read(paramtop, "NamedObject", named_obj);
111 
112  // Possible alternate XML file pattern
113  if (paramtop.count("xml_file") != 0)
114  {
115  read(paramtop, "xml_file", xml_file);
116  }
117  }
118  catch(const std::string& e)
119  {
120  QDPIO::cerr << __func__ << ": Caught Exception reading XML: " << e << std::endl;
121  QDP_abort(1);
122  }
123  }
124 
125 
126  void
127  InlineMultiPropagatorParams::write(XMLWriter& xml_out, const std::string& path)
128  {
129  push(xml_out, path);
130 
131  Chroma::write(xml_out, "Param", param);
132  {
133  //QDP::write(xml_out, "StateInfo", stateInfo);
134  std::istringstream header_is(stateInfo);
135  XMLReader xml_header(header_is);
136  xml_out << xml_header;
137  }
138  Chroma::write(xml_out, "NamedObject", named_obj);
139 
140  pop(xml_out);
141  }
142 
143 
144  // Function call
145  void
146  InlineMultiPropagator::operator()(unsigned long update_no,
147  XMLWriter& xml_out)
148  {
149  // If xml file not empty, then use alternate
150  if (params.xml_file != "")
151  {
152  std::string xml_file = makeXMLFileName(params.xml_file, update_no);
153 
154  push(xml_out, "multi_propagator");
155  write(xml_out, "update_no", update_no);
156  write(xml_out, "xml_file", xml_file);
157  pop(xml_out);
158 
159  XMLFileWriter xml(xml_file);
160  func(update_no, xml);
161  }
162  else
163  {
164  func(update_no, xml_out);
165  }
166  }
167 
168 
169  // Real work done here
170  void
171  InlineMultiPropagator::func(unsigned long update_no,
172  XMLWriter& xml_out)
173  {
174  START_CODE();
175 
176  StopWatch snoop;
177  snoop.reset();
178  snoop.start();
179 
180  // Test and grab a reference to the gauge field
181  XMLBufferWriter gauge_xml;
182  try
183  {
184  TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(params.named_obj.gauge_id);
185  TheNamedObjMap::Instance().get(params.named_obj.gauge_id).getRecordXML(gauge_xml);
186  }
187  catch( std::bad_cast )
188  {
189  QDPIO::cerr << InlineMultiPropagatorEnv::name << ": caught dynamic cast error"
190  << std::endl;
191  QDP_abort(1);
192  }
193  catch (const std::string& e)
194  {
195  QDPIO::cerr << InlineMultiPropagatorEnv::name << ": std::map call failed: " << e
196  << std::endl;
197  QDP_abort(1);
198  }
199  const multi1d<LatticeColorMatrix>& u =
200  TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(params.named_obj.gauge_id);
201 
202  push(xml_out, "multi_propagator");
203 
204  write(xml_out, "update_no", update_no);
205 
206  QDPIO::cout << "MULTI_PROPAGATOR: multimass propagator calculation" << std::endl;
207 
208  // Write out the input
209  params.write(xml_out, "Input");
210 
211  // Write out the config header
212  write(xml_out, "Config_info", gauge_xml);
213 
214  push(xml_out, "Output_version");
215  write(xml_out, "out_version", 1);
216  pop(xml_out);
217 
218  // Calculate some gauge invariant observables just for info.
219  MesPlq(xml_out, "Observables", u);
220 
221  //
222  // Read in the source along with relevant information.
223  //
224  XMLReader source_file_xml, source_record_xml;
225  bool make_sourceP = false;
226  bool seqsourceP = false;
227  try
228  {
229  // Create the space
230  // TheNamedObjMap::Instance().create<LatticePropagator>(params.named_obj.source_id);
231 
232  // Snarf the prop info. This is will throw if the source_id is not there
233  TheNamedObjMap::Instance().get(params.named_obj.source_id).getFileXML(source_file_xml);
234  TheNamedObjMap::Instance().get(params.named_obj.source_id).getRecordXML(source_record_xml);
235 
236  // Try to invert this record XML into a source struct
237  // First identify what kind of source might be here
238  if (source_record_xml.count("/MakeSource") != 0)
239  {
240  PropSourceConst_t source_header;
241 
242  read(source_record_xml, "/MakeSource/PropSource", source_header);
243  make_sourceP = true;
244  }
245  else if (source_record_xml.count("/SequentialSource") != 0)
246  {
247  SeqSource_t seqsource_header;
248 
249  read(source_record_xml, "/SequentialSource/SeqSource", seqsource_header);
250  seqsourceP = true;
251  }
252  else
253  throw std::string("No appropriate header found");
254 
255  // Write out the source header
256  write(xml_out, "Source_file_info", source_file_xml);
257  write(xml_out, "Source_record_info", source_record_xml);
258  }
259  catch (std::bad_cast)
260  {
261  QDPIO::cerr << InlineMultiPropagatorEnv::name << ": caught dynamic cast error"
262  << std::endl;
263  QDP_abort(1);
264  }
265  catch (const std::string& e)
266  {
267  QDPIO::cerr << InlineMultiPropagatorEnv::name << ": error message: " << e << std::endl;
268  QDP_abort(1);
269  }
270 
271  // Sanity check
272  if (seqsourceP) {
273  QDPIO::cerr << "Sequential propagator not supportd under multi-mass " << std::endl;
274  QDPIO::cerr << "since source is not mass independent " << std::endl;
275  QDP_abort(1);
276  }
277 
278  proginfo(xml_out); // Print out basic program info
279 
280  // Sanity check - write out the norm2 of the source in the Nd-1 direction
281  // Use this for any possible verification
282  {
283  // Initialize the slow Fourier transform phases
284  SftMom phases(0, true, Nd-1);
285 
286  multi1d<Double> source_corr =
287  sumMulti(localNorm2(TheNamedObjMap::Instance().getData<LatticePropagator>(params.named_obj.source_id)),
288  phases.getSet());
289 
290  push(xml_out, "Source_correlator");
291  write(xml_out, "source_corr", source_corr);
292  pop(xml_out);
293  }
294 
295  int num_mass = params.param.MultiMasses.size();
296  multi1d<LatticePropagator> quark_propagator(num_mass);
297  int ncg_had = 0;
298 
299  //
300  // Initialize fermion action
301  //
302  std::istringstream xml_s(params.param.fermact.xml);
303  XMLReader fermacttop(xml_s);
304  QDPIO::cout << "FermAct = " << params.param.fermact.id << std::endl;
305 
306 
307  // Deal with auxiliary (and polymorphic) state information
308  // eigenvectors, eigenvalues etc. The XML for this should be
309  // stored as a std::string called "stateInfo" in the param struct.
310 
311  // Make a reader for the stateInfo
312  QDPIO::cout << "State info is " << params.stateInfo << std::endl;
313  std::istringstream state_info_is(params.stateInfo);
314 
315 
316 
317  XMLReader state_info_xml(state_info_is);
318  std::string state_info_path="/StateInfo";
319 
320  //
321  // Try the factories
322  //
323  try {
324  QDPIO::cout << "Try the various factories" << std::endl;
325 
326  // Typedefs to save typing
327  typedef LatticeFermion T;
328  typedef multi1d<LatticeColorMatrix> P;
329  typedef multi1d<LatticeColorMatrix> Q;
330 
331  // Generic Wilson-Type stuff
334  fermacttop,
336 
337 
338  // If this cast fails a bad cast exception is thrown.
339  OverlapFermActBase& S_ov = dynamic_cast<OverlapFermActBase&>(*S_f);
340 
342  state_info_xml,
343  state_info_path)); // uses phase-multiplied u-fields
344 
345  QDPIO::cout << "Suitable factory found: compute the quark prop" << std::endl;
346 
347  S_ov.multiQuarkProp(quark_propagator,
348  xml_out,
349  TheNamedObjMap::Instance().getData<LatticePropagator>(params.named_obj.source_id),
350  state,
353  1,
354  ncg_had);
355  }
356  catch (const std::string& e) {
357  QDPIO::cout << "4D: " << e << std::endl;
358  }
359  catch(std::bad_cast) {
360  QDPIO::cerr << "Fermion action created cannot be used for multi mass qprop" << std::endl;
361  QDP_abort(1);
362  }
363 
364  push(xml_out,"Relaxation_Iterations");
365  write(xml_out, "ncg_had", ncg_had);
366  pop(xml_out);
367 
368  // Sanity check - write out the propagator (pion) correlator in the Nd-1 direction
369  {
370  // Initialize the slow Fourier transform phases
371  SftMom phases(0, true, Nd-1);
372  for(int m=0; m < num_mass; m++) {
373  multi1d<Double> prop_corr = sumMulti(localNorm2(quark_propagator[m]),
374  phases.getSet());
375 
376  push(xml_out, "Prop_correlator");
377  write(xml_out, "Number", m);
378  write(xml_out, "Mass", params.param.MultiMasses[m]);
379  write(xml_out, "prop_corr", prop_corr);
380  pop(xml_out);
381  }
382  }
383 
384 
385  // Save the propagators
386  for(int m=0; m < num_mass; m++)
387  {
388  try
389  {
390  XMLBufferWriter file_xml;
391  push(file_xml, "propagator");
392  write(file_xml, "id", uniqueId()); // NOTE: new ID form
393  pop(file_xml);
394 
395  XMLBufferWriter record_xml;
396 
397 
398  push(record_xml, "Propagator");
399 
400  // Jiggery pokery. Substitute the ChromaMultiProp_t with a
401  // ChromaProp. This is a pisser because of the FermActParams
402  // THIS IS NOT TOTALLY KOSHER AS IT CHANGES THE MASS IN INPUT
403  // PARAM as well. However, at this stage we have no further need
404  // for input param.
405  // I Will eventually write Copy Constructors.
406 
407  // ChromaProp_t out_param(input.param, m);
408 
409  ChromaProp_t out_param;
411 
412  // I need a way to glom a mass into an XML document
413  // and this is it
414 
415  // make a reader
416  std::istringstream fermact_is(params.param.fermact.xml);
417  XMLReader fermact_reader(fermact_is);
418 
419 
420  // Replace mass using BasicXPathReader interface directly
421  try {
422  fermact_reader.set<QDP::Real>("/FermionAction/Mass", params.param.MultiMasses[m]);
423  }
424  catch(const std::string& e) {
425  QDPIO::cerr << "Caught exception processing XML: " << e << std::endl;
426  QDP_abort(1);
427  }
428 
429  // turn back into a group
430  // this should not be needed, need to fix "path" in readXMLGroup
431  XMLReader xml_read(fermact_reader, "/");
432  out_param.fermact = readXMLGroup(xml_read, "FermionAction", "FermAct");
433 
434  // print to debug
435  QDPIO::cout << "Modified fermact is: " << out_param.fermact.xml << std::endl << std::flush;
436 
437 // out_param.invParam.invType = params.param.invParam.invType;
438 // out_param.invParam.MROver = params.param.invParam.MROver;
439 // out_param.invParam.MaxCG = params.param.invParam.MaxCG;
440 // out_param.invParam.RsdCG = params.param.invParam.RsdCG[m];
441 
442 
443  write(record_xml, "ForwardProp", out_param);
444  XMLReader xml_tmp(source_record_xml, "/MakeSource");
445  record_xml << xml_tmp;
446  pop(record_xml);
447 
448  std::ostringstream outfile;
449  outfile << params.named_obj.prop_id << "_" << std::setw(3) << std::setfill('0') << m;
450 
451  QDPIO::cout << "Attempting to save " << outfile.str() << std::endl;
452 
453  // Create the space
454  TheNamedObjMap::Instance().create<LatticePropagator>(outfile.str());
455  TheNamedObjMap::Instance().getData<LatticePropagator>(outfile.str())
456  = quark_propagator[m];
457 
458  // Write the propagator xml info
459  TheNamedObjMap::Instance().get(outfile.str()).setFileXML(file_xml);
460  TheNamedObjMap::Instance().get(outfile.str()).setRecordXML(record_xml);
461  }
462  catch (std::bad_cast)
463  {
464  QDPIO::cerr << InlineMultiPropagatorEnv::name << ": caught dynamic cast error"
465  << std::endl;
466  QDP_abort(1);
467  }
468  catch (const std::string& e)
469  {
470  QDPIO::cerr << InlineMultiPropagatorEnv::name << ": error extracting prop_header: " << e << std::endl;
471  QDP_abort(1);
472  }
473  }
474 
475 
476  pop(xml_out); // propagator
477 
478  snoop.stop();
479  QDPIO::cout << InlineMultiPropagatorEnv::name << ": total time = "
480  << snoop.getTimeInSeconds()
481  << " secs" << std::endl;
482 
483  QDPIO::cout << InlineMultiPropagatorEnv::name << ": ran successfully" << std::endl;
484 
485  END_CODE();
486  }
487 
488 }
Inline measurement factory.
virtual FermState< T, P, Q > * createState(const Q &q) const
Given links (coordinates Q) create the state needed for the linear operators.
Definition: fermact.h:59
Class for counted reference semantics.
Definition: handle.h:33
Inline task for multi-mass propagators.
InlineMultiPropagatorParams params
void operator()(const unsigned long update_no, XMLWriter &xml_out)
Do the measurement.
void func(const unsigned long update_no, XMLWriter &xml_out)
Do the measurement.
Base class for unpreconditioned overlap-like fermion actions.
Fourier transform phase factor support.
Definition: sftmom.h:35
const Set & getSet() const
The set to be used in sumMulti.
Definition: sftmom.h:57
static T & Instance()
Definition: singleton.h:432
Class structure for fermion actions.
Fermion action factories.
All Wilson-type fermion actions.
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.
std::string uniqueId()
Return a unique id.
Definition: unique_id.cc:18
void proginfo(XMLWriter &xml)
Print out basic information about this program.
Definition: proginfo.cc:24
std::string makeXMLFileName(std::string xml_file, unsigned long update_no)
Return a xml file name for inline measurements.
GroupXML_t readXMLGroup(XMLReader &xml_in, const std::string &path, const std::string &type_name)
Read group and return as a std::string.
void multiQuarkProp(multi1d< LatticePropagator > &q_sol, XMLWriter &xml_out, const LatticePropagator &q_src, Handle< FermState< T, P, Q > > state, const multi1d< Real > &masses, const GroupXML_t &invParam, const int n_soln, int &ncg_had)
Define a multi mass qprop.
Inline construction of multi_propagator – overlap only.
static int m[4]
Definition: make_seeds.cc:16
Make xml file writer.
Nd
Definition: meslate.cc:74
Named object function std::map.
static bool registered
Local registration flag.
multi1d< LatticeColorMatrix > P
bool registerAll()
Register all the factories.
bool registerAll()
Register all the factories.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
static multi1d< LatticeColorMatrix > u
LinOpSysSolverMGProtoClover::Q Q
push(xml_out,"Condensates")
LinOpSysSolverMGProtoClover::T T
pop(xml_out)
void MesPlq(const multi1d< LatticeColorMatrixF3 > &u, multi2d< Double > &plane_plaq, Double &link)
Definition: mesplq.cc:83
START_CODE()
const WilsonTypeFermAct< multi1d< LatticeFermion > > Handle< const ConnectState > state
Definition: pbg5p_w.cc:28
const WilsonTypeFermAct< multi1d< LatticeFermion > > & S_f
Definition: pbg5p_w.cc:27
::std::string string
Definition: gtest.h:1979
Base class for unpreconditioned overlap-like fermion actions.
Print out basic info about this program.
Fourier transform phase factor support.
QuarkSpinType quarkSpinType
Definition: qprop_io.h:63
GroupXML_t fermact
String holding XML of the FermionAction section.
Definition: qprop_io.h:66
multi1d< Real > MultiMasses
Definition: qprop_io.h:69
Propagator parameters.
Definition: qprop_io.h:75
GroupXML_t fermact
Definition: qprop_io.h:80
QuarkSpinType quarkSpinType
Definition: qprop_io.h:77
struct Chroma::InlineMultiPropagatorParams::NamedObject_t named_obj
void write(XMLWriter &xml_out, const std::string &path)
Propagator source construction parameters.
Definition: qprop_io.h:27
Sequential source parameters.
Definition: qprop_io.h:90
Generate a unique id.