CHROMA
inline_usqcd_write_ddpairs_prop.cc
Go to the documentation of this file.
1 /*! \file
2  * \brief Inline task to write an object from a named buffer
3  *
4  * Named object writing
5  */
6 
7 
8 #include "chromabase.h"
9 #include "handle.h"
13 #include "io/enum_io/enum_io.h"
14 #include "io/qprop_io.h"
16 #include "util/ferm/transf.h"
17 #include "qio.h"
18 #include "util/ft/sftmom.h"
19 
20 using namespace QDP;
21 
22 namespace Chroma
23 {
24 
25  namespace InlineUSQCDWriteDDPairsPropEnv
26  {
27  namespace
28  {
29  AbsInlineMeasurement* createMeasurement(XMLReader& xml_in,
30  const std::string& path)
31  {
33  }
34 
35  //! Local registration flag
36  bool registered = false;
37  }
38 
39  const std::string name = "USQCD_WRITE_DD_PAIRS_PROP";
40 
41  //! Register all the factories
42  bool registerAll()
43  {
44  bool success = true;
45  if (! registered)
46  {
47  // Inline measurement
48  success &= TheInlineMeasurementFactory::Instance().registerObject(name, createMeasurement);
49 
50  registered = true;
51  }
52  return success;
53  }
54  }
55 
56 
57  // Some very private things
58  namespace {
59 
60  // The tag in the record XML for sinks. This needs to be decided on.
61  // For now I go with the latest version of QIO which says "usqcdInfo"
62  // as opposed to "usqcdPropInfo" in the manual. The reader can eat both
63  // supposedly, so once they decide which one we can change to that.
64 
65  const std::string sinkRecordName="usqcdPropInfo";
66  }
67 
68 
69  //! Object buffer
70  void write(XMLWriter& xml, const std::string& path, const InlineUSQCDWriteDDPairsPropParams& p)
71  {
72  push(xml, path);
73  write(xml, "Frequency", p.frequency);
74  push(xml, "Param");
75  write(xml, "OutputFile", p.output_file_name);
76  write(xml, "OutputVolfmt", p.qio_volfmt);
77  bool parallel_io_choice = false;
78  if( p.parallel_io == QDPIO_PARALLEL) {
79  parallel_io_choice = true;
80  }
81  write(xml, "parallel_io", parallel_io_choice);
82  write(xml, "Precision", p.precision);
83 
84  pop(xml); // Param
85 
86  push(xml, "NamedObject");
87  write(xml, "prop_id", p.prop_id); // ID Of prop to create
88  write(xml, "gauge_id", p.gauge_id);
89  pop(xml);
90 
91  if ( p.xml_file != "") {
92  write(xml, "xml_file", p.xml_file);
93  }
94  pop(xml);
95  }
96 
97  // Param stuff
98  InlineUSQCDWriteDDPairsPropParams::InlineUSQCDWriteDDPairsPropParams() { frequency = 0; xml_file=""; }
99 
100  InlineUSQCDWriteDDPairsPropParams::InlineUSQCDWriteDDPairsPropParams(XMLReader& xml_in, const std::string& path)
101  {
102  try
103  {
104  XMLReader paramtop(xml_in, path);
105 
106  if (paramtop.count("Frequency") == 1)
107  read(paramtop, "Frequency", frequency);
108  else
109  frequency = 1;
110 
111 
112  read(paramtop, "Param/OutputFile", output_file_name);
113  read(paramtop, "Param/OutputVolfmt", qio_volfmt);
114  bool parallel_io_choice = Layout::isIOGridDefined() && ( Layout::numIONodeGrid() > 1);
115 
116  if( paramtop.count("Param/parallel_io") == 1) {
117  read(paramtop, "Param/parallel_io", parallel_io_choice);
118  }
119 
120  if( parallel_io_choice ) {
121  parallel_io = QDPIO_PARALLEL;
122  }
123  else {
124  parallel_io = QDPIO_SERIAL;
125  }
126  read(paramtop, "Param/Precision", precision);
127 
128 
129  read(paramtop, "./NamedObject/prop_id", prop_id);
130  read(paramtop, "./NamedObject/gauge_id", gauge_id);
131 
132  if( paramtop.count("xml_file") == 1 ) {
133  read(paramtop, "./xml_file", xml_file);
134  }
135  else {
136  xml_file="";
137  }
138  }
139  catch(const std::string& e)
140  {
141  QDPIO::cerr << __func__ << ": caught Exception reading XML: " << e << std::endl;
142  QDP_abort(1);
143  }
144  }
145 
146 
147 
148 
149  void
151  XMLWriter& xml_out)
152  {
153  START_CODE();
154  if( params.xml_file == "" ) {
155  func(update_no, xml_out);
156  }
157  else {
158  XMLFileWriter separate_output(params.xml_file);
159  func(update_no, separate_output);
160  }
161 
162  END_CODE();
163  }
164 
165 
166  void InlineUSQCDWriteDDPairsProp::func(unsigned long update_no, XMLWriter& xml_out)
167  {
168  START_CODE();
169  if ( Nc != 3 ){ /* Code is specific to Ns=4 and Nc=3. */
170  QDPIO::cerr<<" code only works for Nc=3 and Ns=4\n";
171  QDP_abort(111) ;
172  }
173 #if QDP_NC == 3
174 
175 
176  push(xml_out, "USQCDWriteDDPairsProp");
177  write(xml_out, "update_no", update_no);
178 
179  QDPIO::cout << InlineUSQCDWriteDDPairsPropEnv::name << ":" << std::endl;
180  StopWatch swatch;
181  swatch.start();
182 
183  QDPIO::cout << "Attempt to write object name = " << params.prop_id << std::endl;
184  QDPIO::cout << "Output file = " << params.output_file_name << std::endl;
185 
186  /* ================== Routine to write the prop here =============== */
187 
188  /* ---------------------------------------------
189  * Step 1: Get hold of the prop and gauge field
190  * --------------------------------------------- */
191 
192  /* Local references are used to try and see if the gets fail.
193  If not, a wider scope reference will be used to bind the prop
194  and u. This is to avoid a very very long try {} catch block */
195  try {
196  LatticeDiracPropagator& trial_prop=TheNamedObjMap::Instance().getData<LatticePropagator>(params.prop_id);
197 
198  const multi1d<LatticeColorMatrix>& u_trial =
199  TheNamedObjMap::Instance().getData<multi1d<LatticeColorMatrix> >(params.gauge_id);
200  }
201  catch(...) {
202  QDPIO::cout << "Could not get the prop from the named ObjectMap. Missing ID"<< params.prop_id << std::endl;
203  QDP_abort(1);
204  }
205 
206  // OK If we're here, the prop and gauge field are in the store
207  // so bind them
208  const LatticePropagator& theProp=
209  TheNamedObjMap::Instance().getData<LatticePropagator>(params.prop_id);
210 
211  const multi1d<LatticeColorMatrix>& u =
212  TheNamedObjMap::Instance().getData<multi1d<LatticeColorMatrix> >(params.gauge_id);
213 
214 
215  // Now get the Record XML for the prop where all the important stuff is
216  XMLReader thePropHeader;
217  TheNamedObjMap::Instance().get(params.prop_id).getRecordXML(thePropHeader);
218 
219 
220  // Grab the header for the source
221  XMLReader theSourceHeader(thePropHeader,"//PropSource");
222 
223  /* -------------------------------------------------------------
224  * Make the source here. Use the XML for the source, to return the
225  * appropriate source creation function. We apply this to - u -
226  * to get the appropriate source. WARNING WARNING WARNING: If the
227  * user uses the wrong gauge field this can all go horribly pear
228  * shaped.
229  *------------------------------------------------------------------*/
230 
231  LatticePropagator theSource; // the source in floating precision
232  // We'll cast to single/double later
233  try
234  {
235  // Read the factory key for the source
237  read(theSourceHeader, "Source/SourceType", source_type);
238 
239  QDPIO::cout << "Source = " << source_type << std::endl;
240 
241  // Get the constructor function-object from the factory
243  sourceConstruction(ThePropSourceConstructionFactory::Instance().createObject(source_type,theSourceHeader,"Source"));
244 
245  // Apply the source construction
246  theSource = (*sourceConstruction)(u); // And we are done.
247 
248  }
249  catch(const std::string& e)
250  {
251  QDPIO::cerr << InlineUSQCDWriteDDPairsPropEnv::name << ": Caught Exception creating source: " << e << std::endl;
252  QDP_abort(1);
253  }
254 
255  /* -------------------------------------------------------------------
256  * OK. To output the source timeslice with QIO, we need to get the
257  * timeslice information out of the source header. The way we'll
258  * do this is just to make a source construction data object from
259  * th XML header -- ie data bind the header -- and interrogate the
260  * structure
261  *------------------------------------------------------------------ */
262 
263  int t_slice;
264  int j_decay;
265  PropSourceConst_t source_info;
266  {
267  // Data bind the header info
268  read(thePropHeader, "//PropSource", source_info);
269 
270  QDPIO::cout << "Source timeslice is " << source_info.t_source << std::endl;
271 
272  // Get timeslice and decay direction
273  t_slice=source_info.t_source;
274  j_decay=source_info.j_decay;
275  }
276 
277 
278  // Bind our Prop header to the relevant QPropIO datatype for ease of writing
280  read(thePropHeader, "//Propagator", prop_header);
281  /* ------------------------------------------------------------------
282  * Right. We're almost ready to write now. We'll need a couple of
283  * Stock XML snippets, like the File XML with our 'record' XML header
284  * info being jammed into the <info> tag. Yuck!
285  * ------------------------------------------------------------------ */
286 
287  XMLBufferWriter file_xml;
288  push(file_xml, "usqcdPropFile");
289  write(file_xml, "version", "1.0");
290  write(file_xml, "type", "USQCD_DiracFermion_Source_Sink_Pairs");
291  push(file_xml, "info");
292  write(file_xml, "Propagator", prop_header);
293  pop(file_xml); // Info
294  pop(file_xml); // usqcdPropFile
295 
296  /* Echo back for human checking */
297  QDPIO::cout << "File XML is:" << std::endl;
298  QDPIO::cout << file_xml.str();
299 
300  /* --------------------------------------------------------------------
301  * For each source, we'll also need a source record XML. It appears to
302  * be content free, but I can always jam the source header file into
303  * its <info> tag
304  * ------------------------------------------------------------------- */
305  XMLBufferWriter source_xml;
306  push(source_xml, "usqcdSourceInfo");
307  write(source_xml, "version", "1.0");
308  push(source_xml, "info");
309 
310  // Jam in our source info by writing (marshalling?) the PropSourceConst_t
311  // write(source_xml, "PropSource", source_info);
312  pop(source_xml);
313  pop(source_xml);
314 
315  // Display for the user...
316  QDPIO::cout << "Source XML is:" << std::endl;
317  QDPIO::cout << source_xml.str();
318 
319  if ( params.parallel_io == QDPIO_PARALLEL ) {
320  QDPIO::cout << "Attempting Parallel IO for writing" << std::endl;
321  }
322  else {
323  QDPIO::cout << "Attempting Serial IO for reading " << std::endl;
324  }
325 
326  // Open the QIO output file
327  QDPFileWriter qio_out(file_xml, params.output_file_name,
328  params.qio_volfmt,
329  params.parallel_io,
330  QDPIO_CREATE);
331 
332 
333  /* Set up the hypercube info */
334  multi1d<int> lower(Nd);
335  multi1d<int> upper(Nd);
336 
337  /* This should set the lower corner to be the origin and the max
338  corner to be the largest accessible */
339  for(int i=0; i < Nd; i++) {
340  lower[i] = 0;
341  upper[i] = Layout::lattSize()[i]-1;
342  }
343 
344  /* We overwrite the time direction coordinates of the slice
345  with the source timeslice */
346  upper[j_decay] = t_slice;
347  lower[j_decay] = t_slice;
348 
349 
350  // Next we have to loop through the spin-color components and write
351  for(int spin=0; spin < Ns; spin++) {
352  for(int color=0; color < Nc; color++) {
353 
354  /* ---------------------------------------------------------------
355  * Make some XML for the spinor
356  * -- I couldn't pre-prepare this as it contains
357  * the spin color information
358  * --------------------------------------------------------------- */
359 
360  XMLBufferWriter prop_sink_xml;
361  push(prop_sink_xml, sinkRecordName);
362  write(prop_sink_xml, "version", "1.0");
363  write(prop_sink_xml, "spin", spin);
364  write(prop_sink_xml, "color", color);
365  push(prop_sink_xml, "info");
366  pop(prop_sink_xml); // Info
367  pop(prop_sink_xml); // usqcdPropInfo
368 
369  /* ----------------------------------------------------
370  * Write the source first.
371  * ---------------------------------------------------- */
372 
373  //
374  // Bring the source into the temporary fermion.
375  //
376  LatticeDiracFermion tmpFerm=zero;
377  PropToFerm(theSource, tmpFerm, color, spin);
378 
379  // Now depending on precision, cast and write
380  if ( params.precision == "single" ) {
381  LatticeDiracFermionF3 ferm_out(tmpFerm) ; // Single Precision cast
382 
383  // Write
384  QDPIO::cout << "About to write single prec. source record...." << std::endl;
385  write(qio_out, source_xml, ferm_out, lower, upper);
386 
387  }
388  else {
389  LatticeDiracFermionD3 ferm_out(tmpFerm); // Double precision cast
390 
391  /* Write */
392  QDPIO::cout << "About to write double prec. source record....";
393  write(qio_out, source_xml,ferm_out, lower, upper);
394  }
395 
396  /* ---------------------------------------------------------------
397  * Done with source. Now do the prop component
398  * --------------------------------------------------------------- */
399  tmpFerm=zero;
400  // Bring the prop component into the tmpFerm
401  PropToFerm(theProp, tmpFerm, color, spin);
402 
403  /* Now depending on the precision, cast and write */
404  if ( params.precision == "single" ) {
405  LatticeDiracFermionF3 ferm_out = tmpFerm ; // Single precision Cast
406 
407  /* Write */
408  QDPIO::cout << "About to write single prec. prop component record...." << std::endl;
409  write(qio_out, prop_sink_xml, ferm_out);
410 
411  }
412  else {
413  LatticeDiracFermionD3 ferm_out(tmpFerm); // Double precision cast
414 
415  /* Write */
416  QDPIO::cout << "About to write double prec. prop component record...." << std::endl;
417  write(qio_out, prop_sink_xml, ferm_out);
418  }
419 
420 
421  } // color
422  } // spin
423 
424  qio_out.close();
425 
426  /* And we are done. */
427  swatch.stop();
428 
429  QDPIO::cout << InlineUSQCDWriteDDPairsPropEnv::name << ": total time = " << swatch.getTimeInSeconds() <<" secs" << std::endl;
430  QDPIO::cout << InlineUSQCDWriteDDPairsPropEnv::name << ": ran successfully" << std::endl;
431 
432 
433  pop(xml_out); // qio_write_named_obj
434 
435 #endif
436  END_CODE();
437 }
438 }
439 
Inline measurement factory.
Primary include file for CHROMA library code.
#define END_CODE()
Definition: chromabase.h:65
#define START_CODE()
Definition: chromabase.h:64
Class for counted reference semantics.
Definition: handle.h:33
Enums.
void PropToFerm(const LatticePropagatorF &b, LatticeFermionF &a, int color_index, int spin_index)
Extract a LatticeFermion from a LatticePropagator.
Definition: transf.cc:226
void read(XMLReader &xml, const std::string &path, AsqtadFermActParams &param)
Read parameters.
Class for counted reference semantics.
std::string source_type
ForwardProp_t prop_header
Params params
Inline task to read a USQCD DD Pairs Prop.
unsigned i
Definition: ldumul_w.cc:34
int j_decay
Definition: meslate.cc:22
int t_slice
Definition: meslate.cc:23
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
static multi1d< LatticeColorMatrix > u
void write(XMLWriter &xml, const std::string &path, const InlineUSQCDWriteDDPairsPropParams &p)
Object buffer.
Double zero
Definition: invbicg.cc:106
::std::string string
Definition: gtest.h:1979
Routines associated with Chroma propagator IO.
Fourier transform phase factor support.
Factory for producing quark prop sources.
Propagator source construction parameters.
Definition: qprop_io.h:27
Mega structure holding a full forward prop (not sink smeared)
Definition: qprop_io.h:111
func
Definition: t_preccfz.cc:17
push(xml_out,"Cooled_Topology")
pop(xml_out)