1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-09 23:45:36 +00:00

Separated IO reader/writers into a proper abstract base,

derived relationship. Have Text/Binary/Xml versions of
Reader & Writer.

Any new Reader/Writer class inheriting the interface can give object serialisation
to any desired format now.

      new file:   lib/serialisation/BaseIO.h
      modified:   lib/serialisation/BinaryIO.h
      modified:   lib/serialisation/Serialisation.h
      modified:   lib/serialisation/TextIO.h
      modified:   lib/serialisation/XmlIO.h

The test uses the Xml, Binary and Text formats as well as cout << Object.
This commit is contained in:
Peter Boyle 2015-08-21 10:06:33 +01:00
parent 35818fdf6c
commit 476da3ee62
7 changed files with 183 additions and 157 deletions

13
TODO
View File

@ -1,9 +1,9 @@
- PseudoFermions
TODO:
=> Clean up HMC
- Link smearing/boundary conds; Policy class based implementation
* Support different boundary conditions (finite temp, chem. potential ... )
* Support different fermion representations?
- Integrators
- Force Gradient
- Multi-timescale looks broken and operating on single timescale for now.
@ -14,8 +14,6 @@ TODO:
=> Lanczos
=> generalise to non-const EE ; likely defer (??)
- Rectangle gauge actions.
Iwasaki,
Symanzik,
@ -24,10 +22,6 @@ TODO:
- Prepare multigrid for HMC.
Alternate setup schemes.
* Support different boundary conditions (finite temp, chem. potential ... )
* Support different fermion representations?
* Parallel io improvements
- optional parallel MPI2 IO
- move Plaquette and link trace checks into nersc reader from the Grid_nersc_io.cc test.
@ -85,6 +79,9 @@ Not sure of status of this -- reverify. Things are working nicely now though.
Done: Cayley, Partial , ContFrac force terms.
DONE
- PseudoFermions
=> generalise to non-const EE ; likely defer (??) (NOT DONE)
Done:
- TwoFlavour
- TwoFlavourEvenOdd

View File

@ -0,0 +1,49 @@
#ifndef GRID_SERIALISATION_ABSTRACT_READER_H
#define GRID_SERIALISATION_ABSTRACT_READER_H
namespace Grid {
class Writer {
public:
virtual ~Writer() {};
virtual void push(const std::string &s) = 0;
virtual void pop(void) =0;
virtual void write( const std::string& s,const std::string &output ) =0;
virtual void write( const std::string& s, int16_t output ) =0;
virtual void write( const std::string& s, uint16_t output ) =0;
virtual void write( const std::string& s, int32_t output ) =0;
virtual void write( const std::string& s, uint32_t output ) =0;
virtual void write( const std::string& s, int64_t output ) =0;
virtual void write( const std::string& s, uint64_t output ) =0;
virtual void write( const std::string& s, float output ) =0;
virtual void write( const std::string& s, double output ) =0;
virtual void write( const std::string& s, bool output ) =0;
};
class Reader {
public:
virtual ~Reader() {};
virtual void read( const std::string& s,std::string &output ) =0;
virtual void push(const std::string &s) =0;
virtual void pop(void) = 0;
virtual void read( const std::string& s, int16_t &output ) =0;
virtual void read( const std::string& s, uint16_t &output ) =0;
virtual void read( const std::string& s, int32_t &output ) =0;
virtual void read( const std::string& s, uint32_t &output ) =0;
virtual void read( const std::string& s, int64_t &output ) =0;
virtual void read( const std::string& s, uint64_t &output ) =0;
virtual void read( const std::string& s, float &output ) =0;
virtual void read( const std::string& s, double &output ) =0;
virtual void read( const std::string& s, bool &output ) =0;
};
}
#endif

View File

@ -12,7 +12,7 @@
namespace Grid {
class BinaryWriter {
class BinaryWriter : public Writer {
private:
std::ofstream file;
@ -27,24 +27,24 @@ public:
void push(const std::string &s) {}
void pop(void) {}
void iwrite(const std::string& s,const std::string &output) {
void write(const std::string& s,const std::string &output) {
uint32_t sz = output.size();
iwrite(s,sz);
write(s,sz);
const char * cstr = output.c_str();
for(int c=0;c<output.size();c++){
iwrite(s,cstr[c]);
write(s,cstr[c]);
}
};
void iwrite( const std::string& s, char output ) { writeInternal(s,output); };
void iwrite( const std::string& s, int16_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, uint16_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, int32_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, uint32_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, int64_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, uint64_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, float output ) { writeInternal(s,output); };
void iwrite( const std::string& s, double output ) { writeInternal(s,output); };
void iwrite( const std::string& s, bool output ) { writeInternal(s,output); };
void write( const std::string& s, char output ) { writeInternal(s,output); };
void write( const std::string& s, int16_t output ) { writeInternal(s,output); };
void write( const std::string& s, uint16_t output ) { writeInternal(s,output); };
void write( const std::string& s, int32_t output ) { writeInternal(s,output); };
void write( const std::string& s, uint32_t output ) { writeInternal(s,output); };
void write( const std::string& s, int64_t output ) { writeInternal(s,output); };
void write( const std::string& s, uint64_t output ) { writeInternal(s,output); };
void write( const std::string& s, float output ) { writeInternal(s,output); };
void write( const std::string& s, double output ) { writeInternal(s,output); };
void write( const std::string& s, bool output ) { writeInternal(s,output); };
private:
template<class T> void writeInternal( const std::string& s, T output ){
@ -55,7 +55,7 @@ private:
};
class BinaryReader {
class BinaryReader : public Reader{
private:
std::ifstream file;
@ -71,7 +71,7 @@ public:
void push(const std::string &s) { }
void pop(void) { }
void iread( const std::string& s,std::string &output ) {
void read( const std::string& s,std::string &output ) {
output.clear();
@ -86,30 +86,15 @@ public:
};
template<class T> void iread( const std::string& s, std::vector<T> &output ) {
T tmp;
uint64_t n;
iread("N",n);
output.resize(0);
for(int i=0;i<n;i++){
std::ostringstream oss; oss << "elem" << i;
read(*this,oss.str(),tmp);
output.push_back(tmp);
}
};
void iread( const std::string& s, int16_t &output ) { readInternal(s,output); };
void iread( const std::string& s, uint16_t &output ) { readInternal(s,output); };
void iread( const std::string& s, int32_t &output ) { readInternal(s,output); };
void iread( const std::string& s, uint32_t &output ) { readInternal(s,output); };
void iread( const std::string& s, int64_t &output ) { readInternal(s,output); };
void iread( const std::string& s, uint64_t &output ) { readInternal(s,output); };
void iread( const std::string& s, float &output ) { readInternal(s,output); };
void iread( const std::string& s, double &output ) { readInternal(s,output); };
void iread( const std::string& s, bool &output ) { readInternal(s,output); };
void read( const std::string& s, int16_t &output ) { readInternal(s,output); };
void read( const std::string& s, uint16_t &output ) { readInternal(s,output); };
void read( const std::string& s, int32_t &output ) { readInternal(s,output); };
void read( const std::string& s, uint32_t &output ) { readInternal(s,output); };
void read( const std::string& s, int64_t &output ) { readInternal(s,output); };
void read( const std::string& s, uint64_t &output ) { readInternal(s,output); };
void read( const std::string& s, float &output ) { readInternal(s,output); };
void read( const std::string& s, double &output ) { readInternal(s,output); };
void read( const std::string& s, bool &output ) { readInternal(s,output); };
private:

View File

@ -2,40 +2,45 @@
#define GRID_SERIALISATION_READER_H
#include <serialisation/MacroMagic.h>
#include <serialisation/BaseIO.h>
namespace Grid {
// Generic reader writer interface
inline void push(Writer & WR,const std::string &s) { WR.push(s);}
inline void push(Writer & WR,const char *s) { WR.push(std::string(s));}
inline void pop (Writer & WR) { WR.pop();}
template< class Writer> void push(Writer & WR,const std::string &s) { WR.push(s);}
template< class Writer> void push(Writer & WR,const char *s) { WR.push(std::string(s));}
template< class Writer> void pop (Writer & WR) { WR.pop();}
inline void write(Writer& wr, const std::string& s,const char * output ) { wr.write(s,std::string(output)); };
inline void write(Writer& wr, const std::string& s,const std::string &output) { wr.write(s,output); };
inline void write(Writer& wr, const std::string& s, int16_t output ) { wr.write(s,output); };
inline void write(Writer& wr, const std::string& s, uint16_t output ) { wr.write(s,output); };
inline void write(Writer& wr, const std::string& s, int32_t output ) { wr.write(s,output); };
inline void write(Writer& wr, const std::string& s, uint32_t output ) { wr.write(s,output); };
inline void write(Writer& wr, const std::string& s, int64_t output ) { wr.write(s,output); };
inline void write(Writer& wr, const std::string& s, uint64_t output ) { wr.write(s,output); };
inline void write(Writer& wr, const std::string& s, float output ) { wr.write(s,output); };
inline void write(Writer& wr, const std::string& s, double output ) { wr.write(s,output); };
inline void write(Writer& wr, const std::string& s, bool output ) { wr.write(s,output); };
template< class Writer> void write(Writer& wr, const std::string& s,const char * output ) { wr.iwrite(s,std::string(output)); };
template< class Writer> void write(Writer& wr, const std::string& s,const std::string &output) { wr.iwrite(s,output); };
template< class Writer> void write(Writer& wr, const std::string& s, int16_t output ) { wr.iwrite(s,output); };
template< class Writer> void write(Writer& wr, const std::string& s, uint16_t output ) { wr.iwrite(s,output); };
template< class Writer> void write(Writer& wr, const std::string& s, int32_t output ) { wr.iwrite(s,output); };
template< class Writer> void write(Writer& wr, const std::string& s, uint32_t output ) { wr.iwrite(s,output); };
template< class Writer> void write(Writer& wr, const std::string& s, int64_t output ) { wr.iwrite(s,output); };
template< class Writer> void write(Writer& wr, const std::string& s, uint64_t output ) { wr.iwrite(s,output); };
template< class Writer> void write(Writer& wr, const std::string& s, float output ) { wr.iwrite(s,output); };
template< class Writer> void write(Writer& wr, const std::string& s, double output ) { wr.iwrite(s,output); };
template< class Writer> void write(Writer& wr, const std::string& s, bool output ) { wr.iwrite(s,output); };
inline void push(Reader & WR,const std::string &s) { WR.push(s);}
inline void push(Reader & WR,const char *s) { WR.push(std::string(s));}
inline void pop (Reader & WR) { WR.pop();}
template< class Reader> void read(Reader& rd, const std::string& s,std::string &output) { rd.iread(s,output); };
template< class Reader> void read(Reader& rd, const std::string& s, int16_t &output ) { rd.iread(s,output); };
template< class Reader> void read(Reader& rd, const std::string& s, uint16_t &output ) { rd.iread(s,output); };
template< class Reader> void read(Reader& rd, const std::string& s, int32_t &output ) { rd.iread(s,output); };
template< class Reader> void read(Reader& rd, const std::string& s, uint32_t &output ) { rd.iread(s,output); };
template< class Reader> void read(Reader& rd, const std::string& s, int64_t &output ) { rd.iread(s,output); };
template< class Reader> void read(Reader& rd, const std::string& s, uint64_t &output ) { rd.iread(s,output); };
template< class Reader> void read(Reader& rd, const std::string& s, float &output ) { rd.iread(s,output); };
template< class Reader> void read(Reader& rd, const std::string& s, double &output ) { rd.iread(s,output); };
template< class Reader> void read(Reader& rd, const std::string& s, bool &output ) { rd.iread(s,output); };
inline void read(Reader& rd, const std::string& s,std::string &output) { rd.read(s,output); };
inline void read(Reader& rd, const std::string& s, int16_t &output ) { rd.read(s,output); };
inline void read(Reader& rd, const std::string& s, uint16_t &output ) { rd.read(s,output); };
inline void read(Reader& rd, const std::string& s, int32_t &output ) { rd.read(s,output); };
inline void read(Reader& rd, const std::string& s, uint32_t &output ) { rd.read(s,output); };
inline void read(Reader& rd, const std::string& s, int64_t &output ) { rd.read(s,output); };
inline void read(Reader& rd, const std::string& s, uint64_t &output ) { rd.read(s,output); };
inline void read(Reader& rd, const std::string& s, float &output ) { rd.read(s,output); };
inline void read(Reader& rd, const std::string& s, double &output ) { rd.read(s,output); };
inline void read(Reader& rd, const std::string& s, bool &output ) { rd.read(s,output); };
template<class Writer, class T> void write(Writer& wr, const std::string& s,const std::vector<T> output ) {
template<class T> void write(Writer& wr, const std::string& s,const std::vector<T> output ) {
push(wr,s);
uint64_t sz =output.size();
write(wr,"N",sz);
@ -45,14 +50,28 @@ namespace Grid {
}
pop(wr);
};
template<class Reader, class T>
void read(Reader& rd, const std::string& s,std::vector<T> &output ) {
rd.iread(s,output);
template<class T>
void read(Reader& rd, const std::string& s,std::vector<T> &output ) {
push(rd,s);
uint64_t sz; read(rd,"N",sz);
// skip the vector length
T tmp;
output.resize(0);
for(int i=0;i<sz;i++){
std::ostringstream oss; oss << "elem" << i;
read(rd,oss.str(),tmp);
output.push_back(tmp);
}
pop(rd);
};
template < class T >
inline std::ostream& operator << (std::ostream& os, const std::vector<T>& v)
inline std::ostream& operator << (std::ostream& os, const std::vector<T>& v)
{
os << "[";
for (typename std::vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
@ -77,7 +96,7 @@ namespace Grid {
// Select the default serialiser use ifdef's
//////////////////////////////////////////
namespace Grid {
typedef XMLReader Reader;
typedef XMLWriter Writer;
typedef XMLReader DefaultReader;
typedef XMLWriter DefaultWriter;
}
#endif

View File

@ -11,7 +11,7 @@
namespace Grid {
class TextWriter {
class TextWriter : public Writer {
private:
std::ofstream file;
@ -32,26 +32,26 @@ public:
void push(const std::string &s)
{
// std::string tmp = s;
// iwrite(s,tmp);
// write(s,tmp);
level++;
}
void pop(void) {
level--;
}
void iwrite( const std::string& s,const std::string &output ) {
void write( const std::string& s,const std::string &output ) {
indent();
file<<output<<std::endl;
};
void iwrite( const std::string& s, int16_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, uint16_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, int32_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, uint32_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, int64_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, uint64_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, float output ) { writeInternal(s,output); };
void iwrite( const std::string& s, double output ) { writeInternal(s,output); };
void iwrite( const std::string& s, bool output ) { writeInternal(s,output); };
void write( const std::string& s, int16_t output ) { writeInternal(s,output); };
void write( const std::string& s, uint16_t output ) { writeInternal(s,output); };
void write( const std::string& s, int32_t output ) { writeInternal(s,output); };
void write( const std::string& s, uint32_t output ) { writeInternal(s,output); };
void write( const std::string& s, int64_t output ) { writeInternal(s,output); };
void write( const std::string& s, uint64_t output ) { writeInternal(s,output); };
void write( const std::string& s, float output ) { writeInternal(s,output); };
void write( const std::string& s, double output ) { writeInternal(s,output); };
void write( const std::string& s, bool output ) { writeInternal(s,output); };
private:
@ -62,7 +62,7 @@ private:
};
class TextReader {
class TextReader : public Reader {
private:
std::ifstream file;
@ -75,7 +75,7 @@ public:
~TextReader() { }
void iread( const std::string& s,std::string &output ) {
void read( const std::string& s,std::string &output ) {
char c='a';
for(int i=0;i<level;i++){
file.get(c);
@ -86,17 +86,17 @@ public:
std::getline(file,output);
};
void push(const std::string &s) {
// std::string tmp; iread(s,tmp);
// std::string tmp; read(s,tmp);
level++;
}
void pop(void) { level--; }
template<class T>
void iread( const std::string& s, std::vector<T> &output ) {
void read( const std::string& s, std::vector<T> &output ) {
push(s);
uint64_t n; iread("N",n);
uint64_t n; read("N",n);
// skip the vector length
T tmp;
@ -111,22 +111,22 @@ public:
};
void iread( const std::string& s, int16_t &output ) { readInternal(s,output); };
void iread( const std::string& s, uint16_t &output ) { readInternal(s,output); };
void iread( const std::string& s, int32_t &output ) { readInternal(s,output); };
void iread( const std::string& s, uint32_t &output ) { readInternal(s,output); };
void iread( const std::string& s, int64_t &output ) { readInternal(s,output); };
void iread( const std::string& s, uint64_t &output ) { readInternal(s,output); };
void iread( const std::string& s, float &output ) { readInternal(s,output); };
void iread( const std::string& s, double &output ) { readInternal(s,output); };
void iread( const std::string& s, bool &output ) { readInternal(s,output); };
void read( const std::string& s, int16_t &output ) { readInternal(s,output); };
void read( const std::string& s, uint16_t &output ) { readInternal(s,output); };
void read( const std::string& s, int32_t &output ) { readInternal(s,output); };
void read( const std::string& s, uint32_t &output ) { readInternal(s,output); };
void read( const std::string& s, int64_t &output ) { readInternal(s,output); };
void read( const std::string& s, uint64_t &output ) { readInternal(s,output); };
void read( const std::string& s, float &output ) { readInternal(s,output); };
void read( const std::string& s, double &output ) { readInternal(s,output); };
void read( const std::string& s, bool &output ) { readInternal(s,output); };
private:
template<class T> void readInternal( const std::string& path, T &output ){
std::string asString;
iread(path,asString);
read(path,asString);
convert(asString,output);
}
@ -145,3 +145,4 @@ private:
}
#endif

View File

@ -14,7 +14,7 @@
namespace Grid {
class XMLWriter {
class XMLWriter : public Writer {
private:
pugi::xml_document doc;
@ -45,32 +45,32 @@ public:
node = node.parent();
}
void iwrite( const std::string& s,const std::string &output ) {
void write( const std::string& s,const std::string &output ) {
pugi::xml_node leaf=node.append_child(s.c_str());
leaf.append_child(pugi::node_pcdata).set_value(output.c_str());
};
void iwrite( const std::string& s, int16_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, uint16_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, int32_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, uint32_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, int64_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, uint64_t output ) { writeInternal(s,output); };
void iwrite( const std::string& s, float output ) { writeInternal(s,output); };
void iwrite( const std::string& s, double output ) { writeInternal(s,output); };
void iwrite( const std::string& s, bool output ) { writeInternal(s,output); };
void write( const std::string& s, int16_t output ) { writeInternal(s,output); };
void write( const std::string& s, uint16_t output ) { writeInternal(s,output); };
void write( const std::string& s, int32_t output ) { writeInternal(s,output); };
void write( const std::string& s, uint32_t output ) { writeInternal(s,output); };
void write( const std::string& s, int64_t output ) { writeInternal(s,output); };
void write( const std::string& s, uint64_t output ) { writeInternal(s,output); };
void write( const std::string& s, float output ) { writeInternal(s,output); };
void write( const std::string& s, double output ) { writeInternal(s,output); };
void write( const std::string& s, bool output ) { writeInternal(s,output); };
private:
template<class T> void writeInternal( const std::string& s, T output ){
std::ostringstream os;
os << std::boolalpha << output;
iwrite(s,os.str());
write(s,os.str());
}
};
class XMLReader {
class XMLReader : public Reader {
private:
pugi::xml_document doc;
@ -98,7 +98,7 @@ public:
~XMLReader() { }
void iread( const std::string& s,std::string &output ) {
void read( const std::string& s,std::string &output ) {
output=node.child(s.c_str()).first_child().value();
};
void push(const std::string &s)
@ -109,47 +109,22 @@ public:
node = node.parent();
}
template<class T>
void iread( const std::string& s, std::vector<T> &output ) {
push(s);
uint64_t n;
pugi::xml_node it=node.first_child();
// skip the vector length
T tmp;
int i=0;
output.resize(0);
for(it = it.next_sibling(); it; it = it.next_sibling() ){
std::ostringstream oss; oss << "elem" << i;
read(*this,oss.str(),tmp);
output.push_back(tmp);
i++;
}
assert(i == n );
pop();
};
void iread( const std::string& s, int16_t &output ) { readInternal(s,output); };
void iread( const std::string& s, uint16_t &output ) { readInternal(s,output); };
void iread( const std::string& s, int32_t &output ) { readInternal(s,output); };
void iread( const std::string& s, uint32_t &output ) { readInternal(s,output); };
void iread( const std::string& s, int64_t &output ) { readInternal(s,output); };
void iread( const std::string& s, uint64_t &output ) { readInternal(s,output); };
void iread( const std::string& s, float &output ) { readInternal(s,output); };
void iread( const std::string& s, double &output ) { readInternal(s,output); };
void iread( const std::string& s, bool &output ) { readInternal(s,output); };
void read( const std::string& s, int16_t &output ) { readInternal(s,output); };
void read( const std::string& s, uint16_t &output ) { readInternal(s,output); };
void read( const std::string& s, int32_t &output ) { readInternal(s,output); };
void read( const std::string& s, uint32_t &output ) { readInternal(s,output); };
void read( const std::string& s, int64_t &output ) { readInternal(s,output); };
void read( const std::string& s, uint64_t &output ) { readInternal(s,output); };
void read( const std::string& s, float &output ) { readInternal(s,output); };
void read( const std::string& s, double &output ) { readInternal(s,output); };
void read( const std::string& s, bool &output ) { readInternal(s,output); };
private:
template<class T> void readInternal( const std::string& path, T &output ){
std::string asString;
iread(path,asString);
read(path,asString);
convert(asString,output);
}

View File

@ -38,7 +38,7 @@ bool b = false;
int main(int argc,char **argv)
{
{
Writer WR("bother.xml");
XMLWriter WR("bother.xml");
push(WR,"BasicTypes");
write(WR,"i16",i16);
@ -57,7 +57,7 @@ int main(int argc,char **argv)
};
Reader RD("bother.xml");
XMLReader RD("bother.xml");
myclass copy1;
myclass copy2;