Commit c7a244c9 by Francois Gygi

Reorganized Function3d and Function3dHandler classes

parent 55e1553e
...@@ -16,13 +16,10 @@ ...@@ -16,13 +16,10 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#include "Function3d.h" #include "Function3d.h"
#include "Timer.h"
#include "Function3dHandler.h" #include "Function3dHandler.h"
#include "Base64Transcoder.h" #include "Timer.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <iomanip>
#include <cstdio>
#include <sys/stat.h> #include <sys/stat.h>
using namespace std; using namespace std;
#include <xercesc/util/XMLUniDefs.hpp> #include <xercesc/util/XMLUniDefs.hpp>
...@@ -56,13 +53,14 @@ void Function3d::read(string filename) ...@@ -56,13 +53,14 @@ void Function3d::read(string filename)
// determine size // determine size
off_t sz = statbuf.st_size; off_t sz = statbuf.st_size;
// use contiguous read buffer, to be copied later to a string
char *rdbuf = new char[sz];
cout << "Function3d::read: file size: " << sz << endl; cout << "Function3d::read: file size: " << sz << endl;
// read file into xmlcontent string
string xmlcontent;
xmlcontent.resize(sz);
tm.start(); tm.start();
size_t items_read = fread(rdbuf,sizeof(char),sz,infile); size_t items_read = fread(&xmlcontent[0],sizeof(char),sz,infile);
assert(items_read==sz); assert(items_read==sz);
fclose(infile); fclose(infile);
tm.stop(); tm.stop();
...@@ -71,10 +69,6 @@ void Function3d::read(string filename) ...@@ -71,10 +69,6 @@ void Function3d::read(string filename)
cout << "Function3d::read: read rate: " cout << "Function3d::read: read rate: "
<< sz/(tm.real()*1024*1024) << " MB/s" << endl; << sz/(tm.real()*1024*1024) << " MB/s" << endl;
string xmlcontent;
xmlcontent.assign(rdbuf,sz);
delete [] rdbuf;
tm.reset(); tm.reset();
tm.start(); tm.start();
...@@ -163,19 +157,9 @@ void Function3d::read(string filename) ...@@ -163,19 +157,9 @@ void Function3d::read(string filename)
XMLPlatformUtils::Terminate(); XMLPlatformUtils::Terminate();
tm.stop(); tm.stop();
cout << "Function3d::read: parse time: " << tm.real() << endl;
tm.reset(); cout << "Function3d::read: parse time: " << tm.real() << endl;
tm.start();
val_.resize(gnx_*gny_*gnz_);
cout << "Function3d::read: grid size: " cout << "Function3d::read: grid size: "
<< gnx_ << " " << gny_ << " " << gnz_ << endl; << nx << " " << ny << " " << nz << endl;
cout << "Function3d::read: str_ size: " << str_.size() << endl; cout << "Function3d::read: val size: " << val.size() << endl;
Base64Transcoder xcdr;
size_t nbytes = xcdr.decode(str_.size(),str_.data(),(byte*)&val_[0]);
tm.stop();
assert(nbytes==val_.size()*sizeof(double));
cout << "Function3d::read: base64 transcode time: " << tm.real() << endl;
str_.clear();
} }
...@@ -28,22 +28,10 @@ class Function3d ...@@ -28,22 +28,10 @@ class Function3d
{ {
public: public:
D3vector a_, b_, c_; D3vector a, b, c; // domain size
int gnx_, gny_, gnz_, nx_, ny_, nz_, x0_, y0_, z0_; int nx, ny, nz; // grid size
std::string str_; std::string name;
std::string name_; std::vector<double> val;
std::vector<double> val_;
const D3vector a(void) const { return a_; }
const D3vector b(void) const { return b_; }
const D3vector c(void) const { return c_; }
int nx(void) const { return nx_; }
int ny(void) const { return ny_; }
int nz(void) const { return nz_; }
int size(void) const { return val_.size(); }
std::vector<double>& val(void) { return val_; }
void read(std::string filename); void read(std::string filename);
void write(std::string filename, std::string encoding) const; void write(std::string filename, std::string encoding) const;
}; };
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "Function3d.h" #include "Function3d.h"
#include "Function3dHandler.h" #include "Function3dHandler.h"
#include "Base64Transcoder.h"
#include "StrX.h" #include "StrX.h"
#include "Timer.h" #include "Timer.h"
#include <xercesc/util/XMLUniDefs.hpp> #include <xercesc/util/XMLUniDefs.hpp>
...@@ -47,7 +48,7 @@ void Function3dHandler::startElement(const XMLCh* const uri, ...@@ -47,7 +48,7 @@ void Function3dHandler::startElement(const XMLCh* const uri,
string attrname = StrX(attributes.getLocalName(index)).localForm(); string attrname = StrX(attributes.getLocalName(index)).localForm();
if ( attrname == "name" ) if ( attrname == "name" )
{ {
f_.name_ = StrX(attributes.getValue(index)).localForm(); f_.name = StrX(attributes.getValue(index)).localForm();
} }
} }
} }
...@@ -60,17 +61,17 @@ void Function3dHandler::startElement(const XMLCh* const uri, ...@@ -60,17 +61,17 @@ void Function3dHandler::startElement(const XMLCh* const uri,
if ( attrname == "a" ) if ( attrname == "a" )
{ {
istringstream stst(StrX(attributes.getValue(index)).localForm()); istringstream stst(StrX(attributes.getValue(index)).localForm());
stst >> f_.a_; stst >> f_.a;
} }
if ( attrname == "b" ) else if ( attrname == "b" )
{ {
istringstream stst(StrX(attributes.getValue(index)).localForm()); istringstream stst(StrX(attributes.getValue(index)).localForm());
stst >> f_.b_; stst >> f_.b;
} }
if ( attrname == "c" ) else if ( attrname == "c" )
{ {
istringstream stst(StrX(attributes.getValue(index)).localForm()); istringstream stst(StrX(attributes.getValue(index)).localForm());
stst >> f_.c_; stst >> f_.c;
} }
} }
} }
...@@ -82,20 +83,23 @@ void Function3dHandler::startElement(const XMLCh* const uri, ...@@ -82,20 +83,23 @@ void Function3dHandler::startElement(const XMLCh* const uri,
string attrname = StrX(attributes.getLocalName(index)).localForm(); string attrname = StrX(attributes.getLocalName(index)).localForm();
if ( attrname == "nx" ) if ( attrname == "nx" )
{ {
f_.gnx_ = atoi(StrX(attributes.getValue(index)).localForm()); f_.nx = atoi(StrX(attributes.getValue(index)).localForm());
} }
if ( attrname == "ny" ) else if ( attrname == "ny" )
{ {
f_.gny_ = atoi(StrX(attributes.getValue(index)).localForm()); f_.ny = atoi(StrX(attributes.getValue(index)).localForm());
} }
if ( attrname == "nz" ) else if ( attrname == "nz" )
{ {
f_.gnz_ = atoi(StrX(attributes.getValue(index)).localForm()); f_.nz = atoi(StrX(attributes.getValue(index)).localForm());
} }
} }
} }
else if ( locname == "grid_function" ) else if ( locname == "grid_function" )
{ {
x0_ = 0;
y0_ = 0;
z0_ = 0;
unsigned int len = attributes.getLength(); unsigned int len = attributes.getLength();
for ( unsigned int index = 0; index < len; index++ ) for ( unsigned int index = 0; index < len; index++ )
{ {
...@@ -110,19 +114,31 @@ void Function3dHandler::startElement(const XMLCh* const uri, ...@@ -110,19 +114,31 @@ void Function3dHandler::startElement(const XMLCh* const uri,
assert(!"Function3dHandler: incorrect grid_function type"); assert(!"Function3dHandler: incorrect grid_function type");
} }
} }
if ( attrname == "nx" ) else if ( attrname == "nx" )
{
fnx_ = atoi(StrX(attributes.getValue(index)).localForm());
}
else if ( attrname == "ny" )
{
fny_ = atoi(StrX(attributes.getValue(index)).localForm());
}
else if ( attrname == "nz" )
{
fnz_ = atoi(StrX(attributes.getValue(index)).localForm());
}
else if ( attrname == "x0" )
{ {
f_.nx_ = atoi(StrX(attributes.getValue(index)).localForm()); x0_ = atoi(StrX(attributes.getValue(index)).localForm());
} }
if ( attrname == "ny" ) else if ( attrname == "y0" )
{ {
f_.ny_ = atoi(StrX(attributes.getValue(index)).localForm()); y0_ = atoi(StrX(attributes.getValue(index)).localForm());
} }
if ( attrname == "nz" ) else if ( attrname == "z0" )
{ {
f_.nz_ = atoi(StrX(attributes.getValue(index)).localForm()); z0_ = atoi(StrX(attributes.getValue(index)).localForm());
} }
if ( attrname == "encoding" ) else if ( attrname == "encoding" )
{ {
string type = StrX(attributes.getValue(index)).localForm(); string type = StrX(attributes.getValue(index)).localForm();
if ( type != "base64" ) if ( type != "base64" )
...@@ -133,7 +149,7 @@ void Function3dHandler::startElement(const XMLCh* const uri, ...@@ -133,7 +149,7 @@ void Function3dHandler::startElement(const XMLCh* const uri,
} }
} }
} }
buf = ""; buf_ = "";
} }
} }
...@@ -153,7 +169,7 @@ void Function3dHandler::characters(const XMLCh* const chars, ...@@ -153,7 +169,7 @@ void Function3dHandler::characters(const XMLCh* const chars,
#endif #endif
char *str = XMLString::transcode(chars); char *str = XMLString::transcode(chars);
buf += str; buf_ += str;
XMLString::release(&str); XMLString::release(&str);
#if TIMING #if TIMING
...@@ -169,6 +185,25 @@ void Function3dHandler::endElement(const XMLCh* const uri, ...@@ -169,6 +185,25 @@ void Function3dHandler::endElement(const XMLCh* const uri,
string locname = StrX(localname).localForm(); string locname = StrX(localname).localForm();
if ( locname == "grid_function" ) if ( locname == "grid_function" )
{ {
f_.str_ = buf; if ( (fnx_ > f_.nx) || (fny_ > f_.ny) || (fnz_ > f_.nz) )
{
assert(!"Function3dHandler:: fragment size > grid size");
}
if ( (fnx_ != f_.nx) || (fny_ != f_.ny) || (fnz_ != f_.nz) )
{
assert(!"Function3dHandler:: fragment processing not implemented");
}
if ( x0_ != 0 || y0_ != 0 || z0_ != 0 )
{
assert(!"Function3dHandler:: fragment offset not implemented");
}
// process base64 data in buf
f_.val.resize(f_.nx*f_.ny*f_.nz);
Timer tm;
tm.start();
Base64Transcoder xcdr;
size_t nbytes = xcdr.decode(buf_.size(),buf_.data(),(byte*)&f_.val[0]);
assert(nbytes==f_.val.size()*sizeof(double));
buf_.clear();
} }
} }
...@@ -26,10 +26,14 @@ ...@@ -26,10 +26,14 @@
class Function3dHandler: public DefaultHandler class Function3dHandler: public DefaultHandler
{ {
public: private:
std::string buf; std::string buf_;
Function3d& f_; Function3d& f_;
int fnx_, fny_, fnz_; // size of fragment read
int x0_, y0_, z0_; // offset of fragment read
public:
Function3dHandler(Function3d& f); Function3dHandler(Function3d& f);
virtual ~Function3dHandler(void); virtual ~Function3dHandler(void);
......
...@@ -31,14 +31,14 @@ int main(int argc, char** argv) ...@@ -31,14 +31,14 @@ int main(int argc, char** argv)
} }
f.read(argv[1]); f.read(argv[1]);
cout << "function name: " << f.name_ << endl; cout << "function name: " << f.name << endl;
cout << "function base64 size: " << f.str_.size() << endl; cout << "function grid size: " << f.nx << " " << f.ny << " " << f.nz << endl;
cout << "function array size: " << f.val_.size() << endl; cout << "function array size: " << f.val.size() << endl;
double sum = 0.0; double sum = 0.0;
for ( int i = 0; i < f.val_.size(); i++ ) for ( int i = 0; i < f.val.size(); i++ )
{ {
sum += f.val_[i]*f.val_[i]; sum += f.val[i]*f.val[i];
} }
cout << "function norm2: " << sum / f.val_.size() << endl; cout << "function norm2: " << sum / f.val.size() << endl;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment