CHROMA
inline_milc_write_stag_source.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 "util/ferm/transf.h"
15 #include "qio.h"
16 
17 
18 using namespace QDP;
19 
20 namespace Chroma
21 {
22 
23  namespace InlineMILCWriteStagSourceEnv
24  {
25  namespace
26  {
27  AbsInlineMeasurement* createMeasurement(XMLReader& xml_in,
28  const std::string& path)
29  {
31  }
32 
33  //! Local registration flag
34  bool registered = false;
35  }
36 
37  const std::string name = "MILC_WRITE_STAGGERED_SOURCE";
38 
39  //! Register all the factories
40  bool registerAll()
41  {
42  bool success = true;
43  if (! registered)
44  {
45  // Inline measurement
46  success &= TheInlineMeasurementFactory::Instance().registerObject(name, createMeasurement);
47 
48  registered = true;
49  }
50  return success;
51  }
52  }
53 
54 
55  // Some very private things
56  namespace {
57 
58  // The tag in the record XML for sinks. This needs to be decided on.
59  // For now I go with the latest version of QIO which says "usqcdInfo"
60  // as opposed to "usqcdPropInfo" in the manual. The reader can eat both
61  // supposedly, so once they decide which one we can change to that.
62 
63  const std::string sinkRecordName="usqcdPropInfo";
64  }
65 
66 
67  //! Object buffer
68  void write(XMLWriter& xml, const std::string& path, const InlineMILCWriteStagSourceParams& p)
69  {
70  push(xml, path);
71  write(xml, "Frequency", p.frequency);
72  push(xml, "Param");
73  write(xml, "OutputFile", p.output_file_name);
74  write(xml, "OutputVolfmt", p.qio_volfmt);
75  bool parallel_io_choice = false;
76  if( p.parallel_io == QDPIO_PARALLEL) {
77  parallel_io_choice = true;
78  }
79  write(xml, "parallel_io", parallel_io_choice);
80  write(xml, "Precision", p.precision);
81 
82  pop(xml); // Param
83 
84  push(xml, "NamedObject");
85  write(xml, "source_id", p.source_id); // ID Of prop to create
86  write(xml, "t_slice", p.t_slice);
87  pop(xml);
88 
89  if ( p.xml_file != "") {
90  write(xml, "xml_file", p.xml_file);
91  }
92  pop(xml);
93  }
94 
95  // Param stuff
96  InlineMILCWriteStagSourceParams::InlineMILCWriteStagSourceParams() { frequency = 0; xml_file=""; }
97 
98  InlineMILCWriteStagSourceParams::InlineMILCWriteStagSourceParams(XMLReader& xml_in, const std::string& path)
99  {
100  try
101  {
102  XMLReader paramtop(xml_in, path);
103 
104  if (paramtop.count("Frequency") == 1)
105  read(paramtop, "Frequency", frequency);
106  else
107  frequency = 1;
108 
109 
110  read(paramtop, "Param/OutputFile", output_file_name);
111  read(paramtop, "Param/OutputVolfmt", qio_volfmt);
112  bool parallel_io_choice = Layout::isIOGridDefined() && ( Layout::numIONodeGrid() > 1);
113 
114  if( paramtop.count("Param/parallel_io") == 1) {
115  read(paramtop, "Param/parallel_io", parallel_io_choice);
116  }
117 
118  read(paramtop, "Param/t_slice", t_slice);
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/source_id", source_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  InlineMILCWriteStagSource::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 InlineMILCWriteStagSource::func(unsigned long update_no, XMLWriter& xml_out)
166  {
167  START_CODE();
168  if ( Nc != 3 ){ /* Code is specific to Ns=4 and Nc=3. */
169  QDPIO::cerr<<" code only works for Nc=3 \n";
170  QDP_abort(111) ;
171  }
172 #if QDP_NC == 3
173 
174 
175  push(xml_out, "MILCWriteStaggSource");
176  write(xml_out, "update_no", update_no);
177 
178  QDPIO::cout << InlineMILCWriteStagSourceEnv::name << ":" << std::endl;
179  StopWatch swatch;
180  swatch.start();
181 
182  QDPIO::cout << "Attempt to write object name = " << params.source_id << std::endl;
183  QDPIO::cout << "On timeslice: " << params.t_slice << 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  LatticeStaggeredPropagator& trial_source=TheNamedObjMap::Instance().getData<LatticeStaggeredPropagator>(params.source_id);
197  }
198  catch(...) {
199  QDPIO::cout << "Could not get the source from the named ObjectMap. Missing ID"<< params.source_id << std::endl;
200  QDP_abort(1);
201  }
202 
203  // OK If we're here, the prop and gauge field are in the store
204  // so bind them
205  const LatticeStaggeredPropagator& theSrc=
206  TheNamedObjMap::Instance().getData<LatticeStaggeredPropagator>(params.source_id);
207 
208 
209  XMLBufferWriter file_xml;
210  push(file_xml, "ChromaMILCStaggeredSource");
211  write(file_xml, "note", "Staggered Source written from Chroma. Metadata irrelevant");
212  pop(file_xml);
213 
214  // Open the QIO output file
215  QDPFileWriter qio_out(file_xml, params.output_file_name,
216  params.qio_volfmt,
217  params.parallel_io,
218  QDPIO_CREATE);
219 
220 
221  multi1d<int> lower_left(Nd);
222  multi1d<int> upper_right(Nd);
223 
224  for(int i=0; i < Nd-1; ++i) {
225  lower_left[i] = 0;
226  upper_right[i] = Layout::lattSize()[i]-1;
227  }
228  lower_left[Nd-1] = params.t_slice;
229  upper_right[Nd-1] = params.t_slice;
230 
231  for(int col=0; col < 3; ++col) {
232  XMLBufferWriter record_xml;
233  push(record_xml, "usqcdKSPropInfo");
234  write(record_xml, "version", "1.0");
235  write(record_xml, "color", col);
236  write(record_xml, "info", " ");
237  pop(record_xml);
238 
239  // Extract the component
240  LatticeStaggeredFermion tmpFerm;
241  PropToFerm(theSrc,tmpFerm,col);
242 
243  // Now depending on precision, cast and write
244  if ( params.precision == "single" ) {
245  LatticeStaggeredFermionF castFerm=(tmpFerm) ; // Single Precision cast
246  LatticeColorVectorF fermOut;
247  FermToCv(castFerm,fermOut);
248 
249  // Write
250  QDPIO::cout << "About to write single prec. source component: " << col << std::endl;
251  QDPIO::cout << "TypeSize is: " << sizeof(PColorVector<RComplex<REAL32>,3>) << std::endl;
252  write(qio_out,record_xml, fermOut, lower_left, upper_right);
253 
254  }
255  else {
256  LatticeStaggeredFermionD castFerm(tmpFerm); // Double precision cast
257  LatticeColorVector fermOut;
258  FermToCv(castFerm,fermOut);
259 
260  /* Write */
261  QDPIO::cout << "About to write double prec. source component." << col << std::endl;
262  write(qio_out,record_xml,fermOut, lower_left, upper_right);
263  }
264  } // End loop over color
265  qio_out.close();
266  pop(xml_out);
267 #endif
268  END_CODE();
269 }
270 }
271 
Inline measurement factory.
Primary include file for CHROMA library code.
#define END_CODE()
Definition: chromabase.h:65
#define START_CODE()
Definition: chromabase.h:64
Inline writing of memory objects.
Enums.
void FermToCv(const LatticeStaggeredFermionD &a, LatticeColorVectorD &b)
Convert a LatticeStaggeredFermion into a LatticeColorVector (extract)
Definition: transf.cc:70
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.
Inline task to read a USQCD DD Pairs Prop.
Params params
unsigned i
Definition: ldumul_w.cc:34
int t_slice
Definition: meslate.cc:23
Nd
Definition: meslate.cc:74
Named object function std::map.
static bool registered
Local registration flag.
bool registerAll()
Register all the factories.
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 InlineMILCWriteStagSourceParams &p)
Object buffer.
::std::string string
Definition: gtest.h:1979
func
Definition: t_preccfz.cc:17
push(xml_out,"Cooled_Topology")
pop(xml_out)