CHROMA
inline_read_map_obj_disk.cc
Go to the documentation of this file.
1 /*! \file
2  * \brief Inline task to read an object into a named buffer
3  */
4 
5 #include "chromabase.h"
6 #include "singleton.h"
7 #include "funcmap.h"
8 
14 #include "qdp_map_obj_disk.h"
15 #include <string>
16 
17 namespace Chroma
18 {
19  namespace InlineReadMapObjDiskEnv
20  {
21  namespace ReadMapObjCallEnv
22  {
23  struct DumbDisambiguator {};
24 
25  typedef SingletonHolder<
29  TYPELIST_2(const std::string&, const std::string&),
30  std::string (*)(const std::string&, const std::string&),
33 
34  namespace
35  {
36  static bool registered = false;
37 
38  template<typename K, typename V>
39  std::string readMapObj(const std::string& object_id,
40  const std::string& file_name)
41  {
42  QDP::MapObjectDisk<K,V>* obj_obj = new QDP::MapObjectDisk<K,V>();
43  obj_obj->open(file_name);
44 
45  Handle<QDP::MapObject<K,V> > obj_handle(obj_obj);
47 
48  std::string meta_data;
49  obj_handle->getUserdata(meta_data);
50 
51  return meta_data;
52  }
53 
54  bool registerAll(void)
55  {
56  bool success = true;
57  if (! registered )
58  {
59  success &= TheReadMapObjFuncMap::Instance().registerFunction("KeyTKeyPropColorVec_tValTLatticeFermion",
60  readMapObj<KeyPropColorVec_t, LatticeFermion>);
61 
62  success &= TheReadMapObjFuncMap::Instance().registerFunction("KeyTintValTEVPairLatticeColorVector",
63  readMapObj<int, EVPair<LatticeColorVector> >);
64 
65  success &= TheReadMapObjFuncMap::Instance().registerFunction("KeyTcharValTfloat",
66  readMapObj<char, float>);
67  registered = true;
68  }
69  return success;
70  }
71  }
72  }
73 
74 
75  namespace
76  {
77  AbsInlineMeasurement* createMeasurement(XMLReader& xml_in,
78  const std::string& path)
79  {
80  return new InlineMeas(Params(xml_in, path));
81  }
82 
83  //! Local registration flag
84  bool registered = false;
85 
86  const std::string name = "READ_MAP_OBJECT_DISK";
87  }
88 
89  //! Register all the factories
90  bool registerAll()
91  {
92  bool success = true;
93  if (! registered)
94  {
95  success &= TheInlineMeasurementFactory::Instance().registerObject(name, createMeasurement);
96  success &= ReadMapObjCallEnv::registerAll();
97  registered = true;
98  }
99  return success;
100  }
101 
102 
103  //! Object buffer
104  void read(XMLReader& xml, const std::string& path, Params::NamedObject_t& input)
105  {
106  XMLReader inputtop(xml, path);
107 
108  read(inputtop, "object_type", input.object_type);
109  read(inputtop, "object_id", input.object_id);
110  }
111 
112  //! Object buffer
113  void read(XMLReader& xml, const std::string& path, Params::File& input)
114  {
115  XMLReader inputtop(xml, path);
116 
117  read(inputtop, "file_name", input.file_name);
118  }
119 
120  Params::Params(XMLReader& reader, const std::string& path)
121  {
122  try
123  {
124  XMLReader paramtop(reader, path);
125 
126  if (paramtop.count("Frequency") == 1)
127  read(paramtop, "Frequency", frequency);
128  else
129  frequency = 1;
130 
131  read(paramtop, "NamedObject", named_obj);
132  read(paramtop, "File", file);
133  }
134  catch(const std::string& e)
135  {
136  QDPIO::cerr << __func__ << ": caught Exception reading XML: " << e << std::endl;
137  QDP_abort(1);
138  }
139  }
140 
141 
142  void
143  InlineMeas::operator()(unsigned long update_no, XMLWriter& xml_out)
144  {
145  START_CODE();
146 
147  push(xml_out, "read_map_object_disk");
148  write(xml_out, "update_no", update_no);
149 
150  QDPIO::cout << name << ": object reader" << std::endl;
151  StopWatch swatch;
152 
153  // Read the object
154  // ONLY MapObject output format is supported in this task
155  // Other tasks could support other disk formats
156  QDPIO::cout << "Attempt to read object name = " << params.named_obj.object_id << std::endl;
157 
158  write(xml_out, "object_type", params.named_obj.object_type);
159  write(xml_out, "object_id", params.named_obj.object_id);
160  write(xml_out, "file_name", params.file.file_name);
161 
162  try
163  {
164  swatch.reset();
165  swatch.start();
166 
167  // Read the object
169 
170  std::istringstream xml_s(meta_data);
171  XMLReader file_xml(xml_s);
172 
173  XMLBufferWriter record_xml_buf;
174  push(record_xml_buf, "RecordXML");
175  write(record_xml_buf, "object_type", params.named_obj.object_type);
176  write(record_xml_buf, "object_id", params.named_obj.object_id);
177  write(record_xml_buf, "file_name", params.file.file_name);
178  pop(record_xml_buf);
179 
180  XMLReader record_xml(record_xml_buf);
181 
182  TheNamedObjMap::Instance().get(params.named_obj.object_id).setFileXML( file_xml );
183  TheNamedObjMap::Instance().get(params.named_obj.object_id).setRecordXML( record_xml );
184 
185  swatch.stop();
186 
187  QDPIO::cout << "Object successfully read: time= "
188  << swatch.getTimeInSeconds()
189  << " secs" << std::endl;
190  }
191  catch( std::bad_cast )
192  {
193  QDPIO::cerr << name << ": cast error"
194  << std::endl;
195  QDP_abort(1);
196  }
197  catch (const std::string& e)
198  {
199  QDPIO::cerr << name << ": error message: " << e
200  << std::endl;
201  QDP_abort(1);
202  }
203 
204  QDPIO::cout << name << ": ran successfully" << std::endl;
205 
206  pop(xml_out); // read_named_obj
207 
208  END_CODE();
209  }
210 
211  }
212 }
Inline measurement factory.
Primary include file for CHROMA library code.
Object factory class.
Definition: funcmap.h:89
Class for counted reference semantics.
Definition: handle.h:33
void operator()(const unsigned long update_no, XMLWriter &xml_out)
Do the writing.
static T & Instance()
Definition: singleton.h:432
Map of function calls.
void write(XMLWriter &xml, const std::string &path, const AsqtadFermActParams &param)
Writer parameters.
Inline task to read an object into a named buffer.
Key for propagator colorstd::vector sources.
std::string file_name
Named object function std::map.
static bool registered
Local registration flag.
const std::string name
Name to be used.
bool registerAll()
Register all the factories.
void read(XMLReader &xml, const std::string &path, Params::NamedObject_t &input)
Object buffer.
Handle< FermBC< LatticeStaggeredFermion, multi1d< LatticeColorMatrix >, multi1d< LatticeColorMatrix > > > reader(XMLReader &xml_in, const std::string &path)
Helper function for the FermionAction readers.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
push(xml_out,"Condensates")
pop(xml_out)
START_CODE()
::std::string string
Definition: gtest.h:1979
Singleton support.
A Pair type.
Params(XMLReader &xml_in, const std::string &path)
struct Chroma::InlineReadMapObjDiskEnv::Params::NamedObject_t named_obj
struct Chroma::InlineReadMapObjDiskEnv::Params::File file
Holds of vectors and eigenvalues.
#define TYPELIST_2(T1, T2)
Definition: typelist.h:43