CHROMA
inline_copy_map_obj.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 
12 
16 
17 namespace Chroma
18 {
19  namespace InlineCopyMapObjEnv
20  {
21  namespace CopyMapObjCallEnv
22  {
23  struct DumbDisambiguator {};
24 
25  //! Write object function std::map
26  /*! \ingroup inlineio */
27  typedef SingletonHolder<
29  void,
31  TYPELIST_1(const Params&),
32  void (*)(const Params& named_obj),
35 
36 
37  namespace
38  {
39  template<typename K, typename V>
40  void copyMapObj(const Params& params)
41  {
42  // Input object
43  Handle< QDP::MapObject<K,V> > input_obj = TheNamedObjMap::Instance().getData< Handle< QDP::MapObject<K,V> > >(params.named_obj.input_id);
44  std::vector<K> keys; input_obj->keys(keys);
45 
46  std::string meta_data;
47  input_obj->getUserdata(meta_data);
48 
49  // Create output object
50  std::istringstream xml_s(params.named_obj.output_obj.xml);
51  XMLReader MapObjReader(xml_s);
52 
53  // Create the entry
54  Handle< QDP::MapObject<K,V> > output_obj(
55  SingletonHolder< ObjectFactory<QDP::MapObject<K,V>,
57  TYPELIST_3(XMLReader&, const std::string&, const std::string&),
58  QDP::MapObject<K,V>* (*)(XMLReader&, const std::string&, const std::string&),
59  StringFactoryError> >::Instance().createObject(params.named_obj.output_obj.id,
60  MapObjReader,
61  params.named_obj.output_obj.path,
62  meta_data) );
63 
64  TheNamedObjMap::Instance().create< Handle< QDP::MapObject<K,V> >, Handle< QDP::MapObject<K,V> > >(params.named_obj.output_id, output_obj);
65 
66  // Copy the key/value-s
67  for(int i=0; i < keys.size(); i++)
68  {
69  V v;
70  input_obj->get(keys[i],v);
71  output_obj->insert(keys[i],v);
72  }
73  }
74 
75 
76  bool registerAll(void)
77  {
78  bool success = true;
79 
80  success &= TheCopyMapObjFuncMap::Instance().registerFunction("KeyTKeyPropColorVec_tValTLatticeFermion",
81  copyMapObj<KeyPropColorVec_t, LatticeFermion>);
82 
83  success &= TheCopyMapObjFuncMap::Instance().registerFunction("KeyTintValTEVPairLatticeColorVector",
84  copyMapObj<int, EVPair<LatticeColorVector> >);
85 
86  success &= TheCopyMapObjFuncMap::Instance().registerFunction("KeyTcharValTfloat",
87  copyMapObj<char, float>);
88  return success;
89  }
90  }
91  }
92 
93 
94  namespace
95  {
96  AbsInlineMeasurement* createMeasurement(XMLReader& xml_in,
97  const std::string& path)
98  {
99  return new InlineMeas(Params(xml_in, path));
100  }
101 
102  //! Local registration flag
103  bool registered = false;
104 
105  const std::string name = "COPY_MAP_OBJECT";
106  }
107 
108  //! Register all the factories
109  bool registerAll()
110  {
111  bool success = true;
112  if (! registered)
113  {
114  success &= TheInlineMeasurementFactory::Instance().registerObject(name, createMeasurement);
115  success &= CopyMapObjCallEnv::registerAll();
116  registered = true;
117  }
118  return success;
119  }
120 
121 
122  //! Object buffer
123  void read(XMLReader& xml, const std::string& path, Params::NamedObject_t& input)
124  {
125  XMLReader inputtop(xml, path);
126 
127  read(inputtop, "object_type", input.object_type);
128  read(inputtop, "input_id", input.input_id);
129  read(inputtop, "output_id", input.output_id);
130  input.output_obj = readXMLGroup(inputtop, "MapObject", "MapObjType");
131  }
132 
133  // Param stuff
135 
136  Params::Params(XMLReader& xml_in, const std::string& path)
137  {
138  try
139  {
140  XMLReader paramtop(xml_in, path);
141 
142  if (paramtop.count("Frequency") == 1)
143  read(paramtop, "Frequency", frequency);
144  else
145  frequency = 1;
146 
147  // Parameters for source construction
148  read(paramtop, "NamedObject", named_obj);
149  }
150  catch(const std::string& e)
151  {
152  QDPIO::cerr << __func__ << ": caught Exception reading XML: " << e << std::endl;
153  QDP_abort(1);
154  }
155  }
156 
157 
158  void
159  InlineMeas::operator()(unsigned long update_no, XMLWriter& xml_out)
160  {
161  START_CODE();
162 
163  registerAll();
164 
165  push(xml_out, "read_map_object_disk");
166  write(xml_out, "update_no", update_no);
167 
168  QDPIO::cout << name << ": std::map object copy" << std::endl;
169  StopWatch swatch;
170 
171  // Copy the object
172  // ONLY named object format is supported in this task
173  // Other tasks could support other disk formats
174  QDPIO::cout << "Attempt to copy input object name = " << params.named_obj.input_id << std::endl;
175 
176  write(xml_out, "object_type", params.named_obj.object_type);
177  write(xml_out, "input_id", params.named_obj.input_id);
178  write(xml_out, "output_id", params.named_obj.output_id);
179 
180  try
181  {
182  swatch.reset();
183  swatch.start();
184 
185  // Copy the object
187 
188  // Use the xml from the first object
189  XMLReader file_xml, record_xml;
190  TheNamedObjMap::Instance().get(params.named_obj.input_id).getFileXML(file_xml);
191  TheNamedObjMap::Instance().get(params.named_obj.input_id).getRecordXML(record_xml);
192 
193  TheNamedObjMap::Instance().get(params.named_obj.output_id).setFileXML(file_xml);
194  TheNamedObjMap::Instance().get(params.named_obj.output_id).setRecordXML(record_xml);
195 
196  swatch.stop();
197 
198  QDPIO::cout << "Object successfully copied: time= "
199  << swatch.getTimeInSeconds()
200  << " secs" << std::endl;
201  }
202  catch( std::bad_cast )
203  {
204  QDPIO::cerr << name
205  << ": cast error for input_id= " << params.named_obj.input_id
206  << " with type= " << params.named_obj.object_type
207  << std::endl;
208  QDP_abort(1);
209  }
210  catch (const std::string& e)
211  {
212  QDPIO::cerr << name << ": error message: " << e << std::endl;
213  QDP_abort(1);
214  }
215  catch(const char* e)
216  {
217  QDPIO::cout << name << ": Caught const char * exception: " << e << std::endl;
218  QDP_abort(1);
219  }
220 
221  QDPIO::cout << name << ": ran successfully" << std::endl;
222 
223  pop(xml_out); // read_named_obj
224 
225  END_CODE();
226  }
227 
228  }
229 }
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.
Object factory class.
Definition: objfactory.h:82
static T & Instance()
Definition: singleton.h:432
Map of function calls.
void write(XMLWriter &xml, const std::string &path, const AsqtadFermActParams &param)
Writer parameters.
GroupXML_t readXMLGroup(XMLReader &xml_in, const std::string &path, const std::string &type_name)
Read group and return as a std::string.
Inline task to copy std::map objects.
Params params
Named object function std::map.
static bool registered
Local registration flag.
const std::string name
Name to be used.
void read(XMLReader &xml, const std::string &path, Params::NamedObject_t &input)
Object buffer.
bool registerAll()
Register all the factories.
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
push(xml_out,"Condensates")
int i
Definition: pbg5p_w.cc:55
pop(xml_out)
START_CODE()
::std::string string
Definition: gtest.h:1979
Singleton support.
A Pair type.
Holds of vectors and eigenvalues.
#define TYPELIST_3(T1, T2, T3)
Definition: typelist.h:45
#define TYPELIST_1(T1)
Definition: typelist.h:41