CHROMA
inline_usqcd_read_ddpairs_prop.cc
Go to the documentation of this file.
1 /*! \file
2  * \brief Inline task to read an object from a named buffer
3  *
4  * Named object writing
5  */
6 
7 
8 #include "chromabase.h"
9 
13 #include "util/info/unique_id.h"
14 #include "util/ferm/transf.h"
15 #include "util/ft/sftmom.h"
16 #include "io/qprop_io.h"
17 
18 using namespace QDP;
19 
20 namespace Chroma
21 {
22  namespace InlineUSQCDReadDDPairsPropEnv
23  {
24  namespace
25  {
26  AbsInlineMeasurement* createMeasurement(XMLReader& xml_in,
27  const std::string& path)
28  {
30  }
31 
32  //! Local registration flag
33  bool registered = false;
34  }
35 
36  const std::string name = "USQCD_READ_DD_PAIRS_PROP";
37 
38  //! Register all the factories
39  bool registerAll()
40  {
41  bool success = true;
42  if (! registered)
43  {
44  // Inline measurement
45  success &= TheInlineMeasurementFactory::Instance().registerObject(name, createMeasurement);
46 
47  registered = true;
48  }
49  return success;
50  }
51  }
52 
53 
54  //! Object buffer
55  void write(XMLWriter& xml, const std::string& path, const InlineUSQCDReadDDPairsPropParams& p)
56  {
57  push(xml, path);
58  write(xml, "Frequency", p.frequency);
59  push(xml, "Param");
60  write(xml, "InputFile", p.input_file_name);
61 
62  bool parallel_io_choice = false;
63  if( p.parallel_io == QDPIO_PARALLEL) {
64  parallel_io_choice = true;
65  }
66  write(xml, "parallel_io", parallel_io_choice);
67  write(xml, "PropXML", p.prop_xml);
68  pop(xml); // Param
69 
70  push(xml, "NamedObject");
71  write(xml, "source_id", p.source_id); // ID Of source to create
72  write(xml, "prop_id", p.prop_id); // ID Of prop to create
73  pop(xml);
74 
75  if ( p.xml_file != "") {
76  write(xml, "xml_file", p.xml_file);
77  }
78  pop(xml);
79  }
80 
81  // Param stuff
82  InlineUSQCDReadDDPairsPropParams::InlineUSQCDReadDDPairsPropParams() { frequency = 0; xml_file=""; }
83 
84  InlineUSQCDReadDDPairsPropParams::InlineUSQCDReadDDPairsPropParams(XMLReader& xml_in, const std::string& path)
85  {
86  try
87  {
88  XMLReader paramtop(xml_in, path);
89 
90  if (paramtop.count("Frequency") == 1)
91  read(paramtop, "Frequency", frequency);
92  else
93  frequency = 1;
94 
95  // Read input Filename
96  read(paramtop, "Param/InputFile", input_file_name);
97 
98  // Read Parallel IO flag
99  // If user doesn't specify, the choice will depend on how many I/O nodes
100  // there are in the layout I/O grid. If more than 1, we will attempt parallel io.
101 
102  bool parallel_io_choice = Layout::isIOGridDefined() && ( Layout::numIONodeGrid() > 1);
103 
104  if( paramtop.count("Param/parallel_io") == 1) {
105  read(paramtop, "Param/parallel_io", parallel_io_choice);
106  }
107 
108  if( parallel_io_choice ) {
109  parallel_io = QDPIO_PARALLEL;
110  }
111  else {
112  parallel_io = QDPIO_SERIAL;
113  }
114 
115  if ( paramtop.count("Param/PropXML") == 1 ) {
116 
117  XMLReader prop_header(paramtop, "Param/PropXML");
118  std::ostringstream dummy;
119  prop_header.printCurrentContext(dummy);
120  prop_xml = dummy.str();
121  }
122  else {
123  QDPIO::cout << "NO source XML in Parameter file. Will look in Prop Info" << std::endl;
124  prop_xml = "";
125  }
126 
127 
128  read(paramtop, "./NamedObject/source_id", source_id);
129  read(paramtop, "./NamedObject/prop_id", prop_id);
130 
131  if( paramtop.count("xml_file") == 1 ) {
132  read(paramtop, "./xml_file", xml_file);
133  }
134  else {
135  xml_file = "";
136  }
137  }
138  catch(const std::string& e)
139  {
140  QDPIO::cerr << __func__ << ": caught Exception reading XML: " << e << std::endl;
141  QDP_abort(1);
142  }
143  }
144 
145 
146 
147 
148  void
149  InlineUSQCDReadDDPairsProp::operator()(unsigned long update_no,
150  XMLWriter& xml_out)
151  {
152  START_CODE();
153  if( params.xml_file == "" ) {
154  func(update_no, xml_out);
155  }
156  else {
157  XMLFileWriter separate_output(params.xml_file);
158  func(update_no, separate_output);
159  }
160 
161  END_CODE();
162  }
163 
164 
165  void InlineUSQCDReadDDPairsProp::func(unsigned long update_no, XMLWriter& xml_out)
166  {
167  START_CODE();
168 
169  push(xml_out, "USQCDReadDDPairsProp");
170  write(xml_out, "update_no", update_no);
171 
172  QDPIO::cout << InlineUSQCDReadDDPairsPropEnv::name << ":" << std::endl;
173  StopWatch swatch;
174  swatch.reset();
175  swatch.start();
176 
177  QDPIO::cout << "Attempt to read object name = " << params.input_file_name << std::endl;
178 
179 
180 
181  /* Do the IO here */
182  /* Create the named objects */
183  try {
184  TheNamedObjMap::Instance().create<LatticePropagator>(params.source_id);
185  TheNamedObjMap::Instance().create<LatticePropagator>(params.prop_id);
186  }
187  catch(...) {
188  QDPIO::cerr << "Failed to create the source and prop" <<std::endl;
189  QDP_abort(1);
190  }
191 
192  /* Get references to the data */
193  LatticePropagator& source_ref=TheNamedObjMap::Instance().getData<LatticePropagator>(params.source_id);
194  LatticePropagator& prop_ref=TheNamedObjMap::Instance().getData<LatticePropagator>(params.prop_id);
195 
196 
197 
198  if ( params.parallel_io == QDPIO_PARALLEL ) {
199 
200  QDPIO::cout << "Attempting Parallel IO for reading" << std::endl;
201  }
202  else {
203  QDPIO::cout << "Attempting Serial IO for reading " << std::endl;
204  }
205 
206 
207  XMLReader file_xml;
208  QDPFileReader input_file(file_xml,
209  params.input_file_name,
210  params.parallel_io);
211 
212 
213  push(xml_out, "FileXML");
214  xml_out << file_xml;
215  pop(xml_out);
216 
217 
218  /* Try to bind the prop and source info up front, so you can know if the metadata is OK
219  before reading in the whole file */
220 
221  /* Strategy: Look first in the file xml, and if you cant find the info there, look in the
222  User supplied <PropXML> tags */
223  Propagator_t prop_h_info;
224  MakeSourceProp_t make_source_header;
225 
226  try {
227  // go to the file <info> tag in the file XML
228  XMLReader prop_info_xml(file_xml, "/usqcdPropFile/info");
229 
230  // check for the existence of a <Propagator> subtag
231  if( prop_info_xml.count("./Propagator") != 0 ) {
232 
233  // It's there
234  QDPIO::cout << "Found header in Prop File <info> record" << std::endl;
235  QDPIO::cout << "Attempting to bind" << std::endl;
236 
237  // Try to bind it
238  read(prop_info_xml, "Propagator", prop_h_info);
239 
240  }
241  else {
242 
243  // The <info> tag doesn't contain what we want
244  // Let's look at what the user specified in the <PropXML>
245  // Tags...
246 
247  // Make a reader out of the prop_xml;
248  std::istringstream is(params.prop_xml);
249  XMLReader source_top(is);
250 
251  QDPIO::cout << "Looking for <Propagator> tag in the user supplied XML" << std::endl;
252  // Check if the tag is there
253  if( source_top.count("/Propagator") == 0 ) {
254  QDPIO::cout << "<Propagator> tag not found!" << std::endl;
255  QDP_abort(1);
256  }
257 
258  // Yes, try to bind it.
259  QDPIO::cout << "Attempting to bind" << std::endl;
260  read(source_top, "/Propagator", prop_h_info);
261 
262  }
263 
264  // Make a source header. The info is there in the propagato
265  QDPIO::cout << "<Propagator> header XML successfully bound" << std::endl;
266  make_source_header.source_header = prop_h_info.source_header;
267  make_source_header.gauge_header = prop_h_info.gauge_header;
268  }
269  catch(const std::string& e) {
270  // We've exceptioned so the XML is neither in <info> tag or in the input params. Barf with message
271  QDPIO::cout << "Caught exception: " << e << std::endl;
272 
273  QDP_abort(1);
274  }
275  catch(...) {
276  QDPIO::cout << "Caught generic exception. Most likely I failed to open a reader on th std::string you provided for <PropXML>" << std::endl;
277 
278  QDP_abort(1);
279  }
280 
281  {
282  LatticeFermion tmpSource;
283  LatticeFermion tmpProp;
284 
285  // Cycle through the spin color components
286  // Assume strict ordering of source spin pairs
287  for(int i=0; i < 12; i++) {
288 
289  // For the record XML
290  XMLReader rec_src_xml, rec_prop_xml;
291 
292  // To get the color and spin values
293  int col, spin;
294 
295 
296  // Read - this will do the precision casting into
297  // my precision for me
298 
299  // Read source
300  read(input_file, rec_src_xml, tmpSource);
301 
302  // read prop
303  read(input_file, rec_prop_xml, tmpProp);
304 
305  // Look for spin/color info in the Propagator record.
306  // This either has tag <usqcdPropInfo> or <usqcdInfo>
307  // depending on your QIO version apparently.
308  try {
309 
310  // Find Spin Color Components
311  if ( rec_prop_xml.count("/usqcdPropInfo") != 0 ) {
312  //
313  // As per Manual, the record tag is <usqcdPropInfo>
314  //
315  read(rec_prop_xml, "/usqcdPropInfo/spin", spin);
316  read(rec_prop_xml, "/usqcdPropInfo/color", col);
317  QDPIO::cout << "Record is sink record with spin=" << spin << " color = "<< col << std::endl;
318 
319  // If you find it, it is a prop and we can shove it off
320  }
321  else if( rec_prop_xml.count("/usqcdInfo" ) != 0 ) {
322  //
323  // As per QIO v2.3.4 and Sergey, the record tag is <usqcdInfo>
324  //
325  read(rec_prop_xml, "/usqcdInfo/spin", spin);
326  read(rec_prop_xml, "/usqcdInfo/color", col);
327  QDPIO::cout << "Record is sink record with spin=" << spin << " color = "<< col << std::endl;
328  }
329  else {
330  // As yet unforseen cases
331  QDPIO::cout << "Found neither usqcdInfo nor usqcdPropInfo tag in the propagator record XML" << std::endl;
332  QDPIO::cout << "Aborting" << std::endl;
333  QDP_abort(1);
334  }
335  }
336  catch(const std::string& e) {
337  // We didn't find it so neither a source nor a prop
338  // and we just quietly barf
339  QDPIO::cout << "Caught exception reading XML" << e << std::endl;
340  QDP_abort(1);
341 
342  }
343 
344  FermToProp(tmpSource, source_ref, col, spin);
345  FermToProp(tmpProp, prop_ref, col, spin);
346 
347  }
348  }
349 
350  // We will also need source and prop record XMLs. To conform
351  // with what we do now, I give them a unique ID
352  XMLBufferWriter source_file_xml;
353  XMLBufferWriter prop_file_xml;
354  push(source_file_xml, "source");
355  write(source_file_xml, "id", uniqueId());
356  pop(source_file_xml);
357 
358  push(prop_file_xml, "propagator");
359  write(prop_file_xml, "id", uniqueId());
360  pop(prop_file_xml);
361 
362 
363  // Create the usual Chroma Record XMLs
364  // From the structures we bound earlier
365  XMLBufferWriter prop_info_out;
366  XMLBufferWriter source_info_out;
367  write(prop_info_out, "Propagator", prop_h_info);
368  write(source_info_out, "MakeSource", make_source_header);
369 
370  // Set the source and prop XMLs
371  TheNamedObjMap::Instance().get(params.prop_id).setFileXML(prop_file_xml);
372  TheNamedObjMap::Instance().get(params.prop_id).setRecordXML(prop_info_out);
373 
374  TheNamedObjMap::Instance().get(params.source_id).setFileXML(source_file_xml);
375  TheNamedObjMap::Instance().get(params.source_id).setRecordXML(source_info_out);
376 
377 
378 
379 
380  // Print out debugging info about the source correlator...
381  {
382  SftMom phases(0, true, Nd-1);
383  multi1d<Double> source_corr = sumMulti(localNorm2(source_ref),
384  phases.getSet());
385 
386  multi1d<Double> prop_corr = sumMulti(localNorm2(prop_ref),
387  phases.getSet());
388 
389  write(xml_out, "source_corr", source_corr);
390  write(xml_out, "prop_corr", prop_corr);
391  }
392 
393 
394  swatch.stop();
395  QDPIO::cout << InlineUSQCDReadDDPairsPropEnv::name << ": total time = " << swatch.getTimeInSeconds() << " secs"<< std::endl;
396  QDPIO::cout << InlineUSQCDReadDDPairsPropEnv::name << ": ran successfully" << std::endl;
397 
398  pop(xml_out); // qio_read_named_obj
399 
400  END_CODE();
401 }
402 }
403 
Inline measurement factory.
Primary include file for CHROMA library code.
#define END_CODE()
Definition: chromabase.h:65
#define START_CODE()
Definition: chromabase.h:64
Fourier transform phase factor support.
Definition: sftmom.h:35
const Set & getSet() const
The set to be used in sumMulti.
Definition: sftmom.h:57
void FermToProp(const LatticeFermionF &a, LatticePropagatorF &b, int color_index, int spin_index)
Insert a LatticeFermion into a LatticePropagator.
Definition: transf.cc:98
void read(XMLReader &xml, const std::string &path, AsqtadFermActParams &param)
Read parameters.
std::string uniqueId()
Return a unique id.
Definition: unique_id.cc:18
ForwardProp_t prop_header
Params params
Inline task to read a USQCD DD Pairs Prop.
unsigned i
Definition: ldumul_w.cc:34
Nd
Definition: meslate.cc:74
Named object function std::map.
static bool registered
Local registration flag.
multi1d< Hadron2PtContraction_t > operator()(const multi1d< LatticeColorMatrix > &u)
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
void write(XMLWriter &xml, const std::string &path, const InlineUSQCDReadDDPairsPropParams &p)
Object buffer.
::std::string string
Definition: gtest.h:1979
Routines associated with Chroma propagator IO.
Real dummy
Definition: qtopcor.cc:36
Fourier transform phase factor support.
Mega structure holding a propagator source.
Definition: qprop_io.h:103
std::string gauge_header
Definition: qprop_io.h:105
PropSourceConst_t source_header
Definition: qprop_io.h:104
Mega structure holding a full forward prop (not sink smeared)
Definition: qprop_io.h:111
std::string gauge_header
Definition: qprop_io.h:114
PropSourceConst_t source_header
Definition: qprop_io.h:113
func
Definition: t_preccfz.cc:17
push(xml_out,"Cooled_Topology")
pop(xml_out)
Generate a unique id.