5 #include <boost/foreach.hpp>
9 #include "flame/h5writer.h"
10 #include "flame/h5loader.h"
15 #define CATCH() catch(H5::Exception& he) { \
16 std::ostringstream strm; \
17 strm<<"H5 Error "<<he.getDetailMsg(); \
18 throw std::runtime_error(strm.str()); \
27 StateElement() :idx((unsigned)-1), nextrow(0u) {}
31 struct H5StateWriter::Pvt {
34 std::vector<StateElement> elements;
37 H5StateWriter::H5StateWriter() :pvt(new Pvt) {}
39 H5StateWriter::H5StateWriter(
const char *spec) :pvt(new Pvt)
44 H5StateWriter::H5StateWriter(
const std::string& spec) :pvt(new Pvt)
49 H5StateWriter::~H5StateWriter()
53 }
catch(std::runtime_error& e) {
54 std::cerr<<
"H5StateWriter is ignoring exception in dtor : "<<e.what()<<
"\n";
59 void H5StateWriter::open(
const char *spec)
61 open(std::string(spec));
64 void H5StateWriter::open(
const std::string& spec)
74 size_t sep = spec.npos;
77 sep = spec.find_last_of(
'/', sep-1);
79 std::string fname(spec.substr(0, sep));
82 pvt->file.openFile(fname, H5F_ACC_RDWR|H5F_ACC_CREAT);
83 }
catch(H5::FileIException& e) {
86 throw std::runtime_error(
"Unable to open file");
92 std::string group(spec.substr(sep+1));
96 pvt->file.getObjinfo(group, stat);
97 pvt->file.unlink(group);
98 }
catch(H5::FileIException&) {
102 pvt->group = pvt->file.createGroup(group);
106 pvt->group = pvt->file.openGroup(
"/");
114 void H5StateWriter::close()
122 void H5StateWriter::prepare(
const StateBase *RS)
129 assert(pvt->elements.empty());
131 for(
unsigned idx=0;
true; idx++) {
139 case StateBase::ArrayInfo::Double:
140 dtype = H5::DataType(H5::PredType::NATIVE_DOUBLE);
142 case StateBase::ArrayInfo::Sizet:
143 if(
sizeof(
size_t)==8)
144 dtype = H5::DataType(H5::PredType::NATIVE_UINT64);
145 else if(
sizeof(
size_t)==4)
146 dtype = H5::DataType(H5::PredType::NATIVE_UINT32);
148 throw std::logic_error(
"unsupported size_t");
159 hsize_t dims[StateBase::ArrayInfo::maxdims+1],
160 maxdims[StateBase::ArrayInfo::maxdims+1];
161 std::fill(maxdims, maxdims+info.
ndim+1, H5S_UNLIMITED);
169 H5::DataSpace dspace(info.
ndim+1, &dims[0], &maxdims[0]);
173 H5::DSetCreatPropList props;
174 props.setChunk(info.
ndim+1, &dims[0]);
179 elem.dset = pvt->group.createDataSet(info.
name, dtype, dspace, props);
181 pvt->elements.push_back(elem);
184 if(pvt->elements.empty()) {
185 throw std::logic_error(
"state type has not elements to store?");
190 void H5StateWriter::append(
const StateBase *RS)
195 if(pvt->elements.empty())
198 BOOST_FOREACH(StateElement& elem, pvt->elements)
203 throw std::logic_error(
"can't re-fetch state parameter?");
205 assert((elem.info.ndim==info.
ndim) && (elem.info.type==info.type));
207 hsize_t shape[StateBase::ArrayInfo::maxdims+1];
208 shape[0] = ++elem.nextrow;
215 elem.dset.extend(shape);
218 hsize_t start[StateBase::ArrayInfo::maxdims+1];
219 start[0] = shape[0]-1;
220 std::fill(start+1, start+info.
ndim+1, 0);
223 H5::DataSpace memspace(info.
ndim+1, shape);
225 H5::DataSpace filespace(elem.dset.getSpace());
226 filespace.selectHyperslab(H5S_SELECT_SET, shape, start);
228 H5::DataType dtype(elem.dset.getDataType());
230 elem.dset.write(info.
ptr, dtype, memspace, filespace);
236 void H5StateWriter::setAttr(
const char *name,
const char *val)
239 if(H5Aexists(pvt->group.getId(), name)>0)
242 pvt->group.removeAttr(name);
246 H5::DataSpace scalar;
247 H5::StrType dtype(H5::PredType::C_S1, std::max((
size_t)16u, strlen(val)));
248 H5::Attribute attr(pvt->group.createAttribute(name, dtype, scalar));
249 attr.write(dtype, val);
254 void H5StateWriter::dontPrint()
257 H5::Exception::dontPrint();
The abstract base class for all simulation state objects.
Used with StateBase::getArray() to describe a single parameter.
size_t dim[maxdims]
Array dimensions in elements.
virtual bool getArray(unsigned index, ArrayInfo &Info)
Introspect named parameter of the derived class.
const char * name
The parameter name.