CHROMA
qio_write_obj_funcmap.cc
Go to the documentation of this file.
1 /*! \file
2  * \brief Write object function std::map
3  */
4 
5 #include "named_obj.h"
8 
10 #include "util/info/unique_id.h"
11 #include "util/ferm/eigeninfo.h"
14 #include "handle.h"
15 #include "qdp_map_obj_memory.h"
16 
17 
19 
20 namespace Chroma
21 {
22 
23  //! IO function std::map environment
24  /*! \ingroup inlineio */
25  namespace QIOWriteObjCallMapEnv
26  {
27  // Anonymous namespace
28  namespace
29  {
30  //------------------------------------------------------------------------
31  //! Write a propagator
32  void QIOWriteLatProp(const std::string& buffer_id,
33  const std::string& file,
34  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
35  {
36  LatticePropagator obj;
37  XMLBufferWriter file_xml, record_xml;
38 
39  obj = TheNamedObjMap::Instance().getData<LatticePropagator>(buffer_id);
40  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
41  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
42 
43  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
44  write(to,record_xml,obj);
45  close(to);
46  }
47 
48 
49  //! Write a single prec propagator
50  void QIOWriteLatPropF(const std::string& buffer_id,
51  const std::string& file,
52  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
53  {
54  LatticePropagatorF obj;
55  XMLBufferWriter file_xml, record_xml;
56 
57  obj = TheNamedObjMap::Instance().getData<LatticePropagator>(buffer_id);
58  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
59  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
60 
61  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
62  write(to,record_xml,obj);
63  close(to);
64  }
65 
66 
67  //! Write a double prec propagator
68  void QIOWriteLatPropD(const std::string& buffer_id,
69  const std::string& file,
70  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
71  {
72  LatticePropagatorD obj;
73  XMLBufferWriter file_xml, record_xml;
74 
75  obj = TheNamedObjMap::Instance().getData<LatticePropagator>(buffer_id);
76  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
77  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
78 
79  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
80  write(to,record_xml,obj);
81  close(to);
82  }
83 
84 
85  //------------------------------------------------------------------------
86  //! Write a fermion
87  template<typename T>
88  void QIOWriteLatFerm(const std::string& buffer_id,
89  const std::string& file,
90  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
91  {
92  T obj;
93  XMLBufferWriter file_xml, record_xml;
94 
95  obj = TheNamedObjMap::Instance().getData<T>(buffer_id);
96  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
97  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
98 
99  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
100  write(to,record_xml,obj);
101  close(to);
102  }
103 
104 
105 #if 0
106  // RGE: FOR SOME REASON, QDP CANNOT CAST A DOUBLE TO FLOATING HERE. NEED TO FIX.
107 
108  //! Write a single prec fermion
109  void QIOWriteLatFermF(const std::string& buffer_id,
110  const std::string& file,
111  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
112  {
113  LatticeFermionF obj;
114  XMLBufferWriter file_xml, record_xml;
115 
116  obj = TheNamedObjMap::Instance().getData<LatticeFermion>(buffer_id);
117  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
118  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
119 
120  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
121  write(to,record_xml,obj);
122  close(to);
123  }
124 
125 
126  //! Write a double prec fermion
127  void QIOWriteLatFermD(const std::string& buffer_id,
128  const std::string& file,
129  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
130  {
131  LatticeFermionD obj;
132  XMLBufferWriter file_xml, record_xml;
133 
134  obj = TheNamedObjMap::Instance().getData<LatticeFermion>(buffer_id);
135  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
136  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
137 
138  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
139  write(to,record_xml,obj);
140  close(to);
141  }
142 #endif
143 
144 
145  //------------------------------------------------------------------------
146  //! Write a propagator
147  void QIOWriteLatStagProp(const std::string& buffer_id,
148  const std::string& file,
149  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
150  {
151  LatticeStaggeredPropagator obj;
152  XMLBufferWriter file_xml, record_xml;
153 
154  obj = TheNamedObjMap::Instance().getData<LatticeStaggeredPropagator>(buffer_id);
155  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
156  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
157 
158  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
159  write(to,record_xml,obj);
160  close(to);
161  }
162 
163 
164  //! Write a single prec propagator
165  void QIOWriteLatStagPropF(const std::string& buffer_id,
166  const std::string& file,
167  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
168  {
169  LatticeStaggeredPropagatorF obj;
170  XMLBufferWriter file_xml, record_xml;
171 
172  obj = TheNamedObjMap::Instance().getData<LatticeStaggeredPropagator>(buffer_id);
173  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
174  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
175 
176  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
177  write(to,record_xml,obj);
178  close(to);
179  }
180 
181 
182  //! Write a double prec propagator
183  void QIOWriteLatStagPropD(const std::string& buffer_id,
184  const std::string& file,
185  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
186  {
187  LatticeStaggeredPropagatorD obj;
188  XMLBufferWriter file_xml, record_xml;
189 
190  obj = TheNamedObjMap::Instance().getData<LatticeStaggeredPropagator>(buffer_id);
191  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
192  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
193 
194  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
195  write(to,record_xml,obj);
196  close(to);
197  }
198 
199 
200  //------------------------------------------------------------------------
201  //! Write a gauge field in floating precision
202  void QIOWriteArrayLatColMat(const std::string& buffer_id,
203  const std::string& file,
204  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
205  {
206  multi1d<LatticeColorMatrix> obj;
207  XMLBufferWriter file_xml, record_xml;
208 
209  obj = TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(buffer_id);
210  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
211  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
212 
213  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
214  write(to,record_xml,obj);
215  close(to);
216  }
217 
218  //! Write a gauge field in single precision
219  void QIOWriteArrayLatColMatF(const std::string& buffer_id,
220  const std::string& file,
221  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
222  {
223  XMLBufferWriter file_xml, record_xml;
224 
225  multi1d<LatticeColorMatrix>& obj
226  = TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(buffer_id);
227  multi1d<LatticeColorMatrixF> u_f(obj.size());
228  for(int mu=0; mu < obj.size(); ++mu)
229  u_f[mu] = obj[mu];
230 
231  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
232  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
233 
234  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
235  write(to,record_xml,u_f);
236  close(to);
237  }
238 
239  //! Write a gauge field in double precision
240  void QIOWriteArrayLatColMatD(const std::string& buffer_id,
241  const std::string& file,
242  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
243  {
244  XMLBufferWriter file_xml, record_xml;
245 
246  multi1d<LatticeColorMatrix>& obj
247  = TheNamedObjMap::Instance().getData< multi1d<LatticeColorMatrix> >(buffer_id);
248  multi1d<LatticeColorMatrixD> u_f(obj.size());
249  for(int mu=0; mu < obj.size(); ++mu)
250  u_f[mu] = obj[mu];
251 
252  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
253  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
254 
255  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
256  write(to,record_xml,u_f);
257  close(to);
258  }
259 
260 
261  //------------------------------------------------------------------------
262  //! Write a QQDiquark object in floating precision
263  void QIOWriteQQDiquarkContract(const std::string& buffer_id,
264  const std::string& file,
265  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
266  {
267  XMLBufferWriter file_xml, record_xml;
268 
269  const QQDiquarkContract_t& obj =
270  TheNamedObjMap::Instance().getData<QQDiquarkContract_t>(buffer_id);
271  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
272  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
273 
274  // QIO cannot deal with multiNd arrays, so we have to flatten to a
275  // 1-D array. This is okay since a QQDiquarkContract object has a fixed
276  // size.
277 
278  const int sz_qq = Ns*Ns*Ns*Ns*Nc*Nc;
279  multi1d<LatticeComplex> obj_1d(sz_qq);
280 
281  int cnt = 0;
282  multi1d<int> ranks(6);
283  const multi1d<int>& sz = obj.comp.size();
284  for(ranks[0]=0; ranks[0] < sz[0]; ++ranks[0])
285  for(ranks[1]=0; ranks[1] < sz[1]; ++ranks[1])
286  for(ranks[2]=0; ranks[2] < sz[2]; ++ranks[2])
287  for(ranks[3]=0; ranks[3] < sz[3]; ++ranks[3])
288  for(ranks[4]=0; ranks[4] < sz[4]; ++ranks[4])
289  for(ranks[5]=0; ranks[5] < sz[5]; ++ranks[5])
290  {
291  // Sanity check - the size better match
292  if (cnt >= sz_qq)
293  {
294  QDPIO::cerr << __func__ << ": size mismatch for multi1Nd object" << std::endl;
295  QDP_abort(1);
296  }
297 
298  obj_1d[cnt++] = obj.comp[ranks];
299  }
300 
301 
302  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
303  write(to,record_xml,obj_1d);
304  close(to);
305  }
306 
307 
308  //-----------------------------------------------------------------------
309  //! Write out an EigenInfo Type
310  template<typename T>
311  void QIOWriteEigenInfo(const std::string& buffer_id,
312  const std::string& file,
313  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
314  {
315  // This is needed for QIO writing
316  XMLBufferWriter file_xml, record_xml;
317 
318  // A shorthand for the object
319  EigenInfo<T>& obj=TheNamedObjMap::Instance().getData< EigenInfo<T> >(buffer_id);
320 
321  // get the file XML and record XML out of the named buffer
322  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
323  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
324 
325  // RGE: I BELIEVE ALL THE COMPLAINTS BELOW ARE NOW FIXED IN QDP++,
326  // BUT I DO NOT WANT TO REMOVE THE CODE YET - CANNOT BE BOTHERED DEBUGGING
327 
328  // Write out the largest EV in BINARY so as not to loose precision
329  // BUG!?? :For some reason I need to write this as a 1 element array and
330  // read it as a 1 element array.
331 
332  // BUG!?? I need to know my array length in advance to read back the
333  // data so I need to dump the number of Ev pairs
334 
335  // BUG!?? What is more because I cannot write primitive types using QIO
336  // (such as int or float) as it is integratedi into QDP++, I have
337  // to write the number of evalues/vectors into a record XML for this
338  // largest ev.
339  multi1d<Real64> largestD(1);
340  largestD[0] = obj.getLargest();
341 
342  // SHorthand
343  multi1d<Real> evals = obj.getEvalues();
344 
345  // Open file
346  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
347 
348  // Make up an XML doc with the number of ev pairs in it
349  XMLBufferWriter largest_record_xml;
350  push(largest_record_xml, "NumElem");
351  write(largest_record_xml, "n_vec", evals.size());
352  pop(largest_record_xml);
353 
354  // Write largest EV plus XML doc
355  write(to, largest_record_xml, largestD);
356 
357  // Convert ev-s to double if they are not that already
358  multi1d<Real64> evalsD(evals.size());
359 
360  for (int i=0; i<evals.size(); i++)
361  evalsD[i] = Real64(evals[i]);
362 
363  // Write them with record XML
364  write(to, record_xml, evalsD);
365 
366  // Now write the evectors 1 by 1 to avoid double storing
367  // This is EigenInfo not SubsetVectors
368  multi1d<T>& evecs=obj.getEvectors();
369  for (int i=0; i<evecs.size(); i++)
370  {
371  XMLBufferWriter record_xml_dummy;
372  push(record_xml_dummy, "dummy_record_xml");
373  pop(record_xml_dummy);
374 
375  // evec=<T>(evecs[i]);
376  write(to, record_xml_dummy, evecs[i]);
377  }
378 
379  // Done! That was unnecessarily painful
380  close(to);
381  }
382 
383  //------------------------------------------------------------------------
384  //! Write out an RitzPairs Type
385  void QIOWriteRitzPairsLatticeFermion(const std::string& buffer_id,
386  const std::string& file,
387  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
388  {
389  // A shorthand for the object
392 
393  // File XML
394  XMLBufferWriter file_xml;
395  push(file_xml, "RitzPairs");
396  write(file_xml, "id", uniqueId());
397  write(file_xml, "Nmax", obj.evec.size());
398  write(file_xml, "Neig", obj.Neig);
399  pop(file_xml);
400 
401  // Open file
402  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
403 
404  // Write a record for each eigenvalue (in xml) and eigenstd::vector
405  for(int i=0; i < obj.Neig; ++i)
406  {
407  XMLBufferWriter record_xml;
408  push(record_xml, "Eigenstd::vector");
409  write(record_xml, "eigenNum", i);
410  write(record_xml, "eigenValue", obj.eval.vec[i]);
411  pop(record_xml);
412 
413  write(to, record_xml, obj.evec.vec[i]);
414  }
415 
416  // Close
417  close(to);
418  }
419 
420  //----------------------------------------------------------------------
421  void QIOWriteSubsetVectors(const std::string& buffer_id,
422  const std::string& file,
423  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
424  {
425  // A shorthand for the object
426  QDP::MapObject<int,EVPair<LatticeColorVector> >& obj =
428 
429  // Yuk. Could read this back in.
430  int decay_dir = Nd-1;
431 
432  // Write number of EVs to XML
433  XMLBufferWriter file_xml;
434 
435  push(file_xml, "AllVectors");
436  write(file_xml, "n_vec", obj.size());
437  write(file_xml, "decay_dir", decay_dir);
438  pop(file_xml);
439 
440  // Open file
441  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
442 
443  // Loop and read evecs
444  for(int n=0; n < obj.size(); n++)
445  {
446  EVPair<LatticeColorVector> write_pair;
447  obj.get(n, write_pair);
448 
449  XMLBufferWriter record_xml;
450  push(record_xml, "VectorInfo");
451  write(record_xml, "weights", write_pair.eigenValue.weights);
452  pop(record_xml);
453  write(to, record_xml, write_pair.eigenVector);
454  }
455 
456  // Done
457  close(to);
458  }
459 
460  //------------------------------------------------------------------------
461  //! Write out a MapObject Type
462  template<typename K, typename V>
463  void QIOWriteMapObjMemory(const std::string& buffer_id,
464  const std::string& file,
465  QDP_volfmt_t volfmt, QDP_serialparallel_t serpar)
466  {
467  // This is needed for QIO writing
468  XMLBufferWriter file_xml, record_xml;
469 
470  // A shorthand for the object
471  MapObjectMemory<K,V>& obj =
472  dynamic_cast<MapObjectMemory<K,V>&>(*(TheNamedObjMap::Instance().getData< Handle<QDP::MapObject<K,V> > >(buffer_id)));
473 
474  // Get the file XML and record XML out of the named buffer
475  TheNamedObjMap::Instance().get(buffer_id).getFileXML(file_xml);
476  TheNamedObjMap::Instance().get(buffer_id).getRecordXML(record_xml);
477 
478  // Open file
479  QDPFileWriter to(file_xml,file,volfmt,serpar,QDPIO_OPEN);
480 
481  // Use the iterators to run through the object, saving each
482  // in a separate record
483  std::vector<K> keys;
484  obj.keys(keys);
485 
486  for(typename std::vector<K>::const_iterator mm = keys.begin();
487  mm != keys.end();
488  ++mm)
489  {
490  XMLBufferWriter local_record_xml;
491  write(local_record_xml, "MapEntry", *mm);
492 
493  write(to, local_record_xml, obj[*mm]);
494  }
495 
496  // Close and bolt
497  close(to);
498  }
499 
500  //! Local registration flag
501  bool registered = false;
502 
503  } // end namespace WriteObjCallMap
504 
505 
506  //! Register all the factories
507  bool registerAll()
508  {
509  bool success = true;
510  if (! registered)
511  {
512  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("LatticePropagator"),
513  QIOWriteLatProp);
514  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("LatticePropagatorF"),
515  QIOWriteLatPropF);
516  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("LatticePropagatorD"),
517  QIOWriteLatPropD);
518 
519  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("LatticeFermion"),
520  QIOWriteLatFerm<LatticeFermion>);
521  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("LatticeStaggeredFermion"),
522  QIOWriteLatFerm<LatticeStaggeredFermion>);
523 
524 // success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("LatticeFermionF"),
525 // QIOWriteLatFermF);
526 // success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("LatticeFermionD"),
527 // QIOWriteLatFermD);
528 
529  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("LatticeStaggeredPropagator"),
530  QIOWriteLatStagProp);
531  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("LatticeStaggeredPropagatorF"),
532  QIOWriteLatStagPropF);
533  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("LatticeStaggeredPropagatorD"),
534  QIOWriteLatStagPropD);
535 
536  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("Multi1dLatticeColorMatrix"),
537  QIOWriteArrayLatColMat);
538  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("Multi1dLatticeColorMatrixF"),
539  QIOWriteArrayLatColMatF);
540  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("Multi1dLatticeColorMatrixD"),
541  QIOWriteArrayLatColMatD);
542 
543  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("QQDiquarkContract"),
544  QIOWriteQQDiquarkContract);
545 
546  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("EigenInfoLatticeFermion"),
547  QIOWriteEigenInfo<LatticeFermion>);
548 
549  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("RitzPairsLatticeFermion"),
550  QIOWriteRitzPairsLatticeFermion);
551 
552  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("SubsetVectorsLatticeColorVector"),
553  QIOWriteSubsetVectors);
554 
555  success &= TheQIOWriteObjFuncMap::Instance().registerFunction(std::string("MapObjMemoryKeyPropColorVecLatticeFermion"),
556  QIOWriteMapObjMemory<KeyPropColorVec_t,LatticeFermion>);
557 
558  registered = true;
559  }
560  return success;
561  }
562  }
563 
564 }
Hold eigenvalues and eigenvectors.
Definition: eigeninfo.h:19
Class for counted reference semantics.
Definition: handle.h:33
Holds eigenvalues and eigenvectors.
Definition: containers.h:282
static T & Instance()
Definition: singleton.h:432
int mu
Definition: cool.cc:24
Construct a diquark object.
Hold eigenvalues and eigenvectors.
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
Class for counted reference semantics.
Handle< MapObject< int, EVPair< LatticeColorVector > > > obj
Key for propagator colorstd::vector sources.
unsigned n
Definition: ldumul_w.cc:36
Nd
Definition: meslate.cc:74
Named object support.
Named object function std::map.
static bool registered
Local registration flag.
bool registerAll()
Register all the factories.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
push(xml_out,"Condensates")
LinOpSysSolverMGProtoClover::T T
int i
Definition: pbg5p_w.cc:55
pop(xml_out)
::std::string string
Definition: gtest.h:1979
Write object function std::map.
A Pair type.
SubsetVectorWeight_t eigenValue
Dense QQDiquark object.
Definition: diquark_w.h:18
Holds of vectors and weights.
Generate a unique id.