15 #ifndef SERIALBOX_CORE_FRONTEND_STELLA_SERIALIZER_H 16 #define SERIALBOX_CORE_FRONTEND_STELLA_SERIALIZER_H 18 #ifdef SERIALBOX_HAS_STELLA 19 #include "DataFieldStorageFormat.h" 20 #include "DataFieldStorageStrides.h" 29 #include <boost/shared_ptr.hpp> 44 SerializerOpenModeRead,
45 SerializerOpenModeWrite,
46 SerializerOpenModeAppend
91 std::string
prefix()
const;
118 template <
typename ValueType>
153 bool RegisterField(
const std::string& name, std::string type,
int bytesPerElement,
int iSize,
154 int jSize,
int kSize,
int lSize,
int iMinusHalo,
int iPlusHalo,
int jMinusHalo,
155 int jPlusHalo,
int kMinusHalo,
int kPlusHalo,
int lMinusHalo,
int lPlusHalo);
166 void AddFieldMetainfo(
const std::string& fieldname,
const std::string& key,
bool value);
167 void AddFieldMetainfo(
const std::string& fieldname,
const std::string& key,
int value);
168 void AddFieldMetainfo(
const std::string& fieldname,
const std::string& key,
float value);
169 void AddFieldMetainfo(
const std::string& fieldname,
const std::string& key,
double value);
170 void AddFieldMetainfo(
const std::string& fieldname,
const std::string& key, std::string value);
171 void AddFieldMetainfo(
const std::string& fieldname,
const std::string& key,
const char* value) {
186 const std::vector<Savepoint>&
savepoints()
const {
return savepoints_; }
215 void WriteField(
const std::string& fieldName,
const Savepoint& savepoint,
const void* pData,
216 int iStride,
int jStride,
int kStride,
int lStride);
218 #ifdef SERIALBOX_HAS_STELLA 219 template <
typename TDataField>
263 void ReadField(
const std::string& fieldName,
const Savepoint& savepoint,
void* pData,
int iStride,
264 int jStride,
int kStride,
int lStride,
bool alsoPrevious =
false)
const;
266 #ifdef SERIALBOX_HAS_STELLA 267 template <
typename TDataField>
286 bool alsoPrevious =
false)
const;
293 boost::shared_ptr<SerializerImpl>&
getImpl() {
return serializerImpl_; }
294 const boost::shared_ptr<SerializerImpl>&
getImpl()
const {
return serializerImpl_; }
298 boost::shared_ptr<SerializerImpl> serializerImpl_;
303 std::vector<Savepoint> savepoints_;
304 std::map<std::string, DataFieldInfo> datafieldInfos_;
323 #ifdef SERIALBOX_HAS_STELLA 324 template <
typename TDataField>
332 size.
Init(field.storage().allocatedSize().iSize(), field.storage().allocatedSize().jSize(),
333 field.storage().allocatedSize().kSize());
336 boundary.
Init(field.boundary().iMinusOffset(), field.boundary().iPlusOffset(),
337 field.boundary().jMinusOffset(), field.boundary().jPlusOffset(),
338 field.boundary().kMinusOffset(), field.boundary().kPlusOffset());
340 const int bytesPerElement =
sizeof(
typename TDataField::ValueType);
342 DataFieldStorageStrides<typename TDataField::StorageFormat::StorageOrder> strides;
343 strides.Init(field.storage().paddedSize());
344 const int iStride = strides.ComputeStride(1, 0, 0) * bytesPerElement;
345 const int jStride = strides.ComputeStride(0, 1, 0) * bytesPerElement;
346 const int kStride = strides.ComputeStride(0, 0, 1) * bytesPerElement;
349 this->
RegisterField(name, type_name<typename TDataField::ValueType>(), bytesPerElement,
354 this->
WriteField(name, savepoint, field.storage().pStorageBase(), iStride, jStride, kStride, 0);
357 template <
typename TDataField>
359 bool alsoPrevious)
const {
360 typedef typename TDataField::ValueType ValueType;
367 const int bytesPerElement =
sizeof(
typename TDataField::ValueType);
370 DataFieldStorageStrides<typename TDataField::StorageFormat::StorageOrder> stridesSTELLA;
371 stridesSTELLA.Init(field.storage().paddedSize());
374 std::vector<int> allSizes, size, allStrides, strides;
375 allSizes.push_back(field.storage().allocatedSize().iSize());
376 allSizes.push_back(field.storage().allocatedSize().jSize());
377 allSizes.push_back(field.storage().allocatedSize().kSize());
378 allSizes.push_back(0);
379 allStrides.push_back(stridesSTELLA.ComputeStride(1, 0, 0) * bytesPerElement);
380 allStrides.push_back(stridesSTELLA.ComputeStride(0, 1, 0) * bytesPerElement);
381 allStrides.push_back(stridesSTELLA.ComputeStride(0, 0, 1) * bytesPerElement);
382 for (
int i = 0; i < 4; ++i) {
385 size.push_back(allSizes[i]);
386 strides.push_back(allStrides[i]);
392 boundary.
Init(field.boundary().iMinusOffset(), field.boundary().iPlusOffset(),
393 field.boundary().jMinusOffset(), field.boundary().jPlusOffset(),
394 field.boundary().kMinusOffset(), field.boundary().kPlusOffset());
397 if((info.
iSize() != size[0]) || (info.
jSize() != size[1]) ||
398 (info.
kSize() != size[2]) || (info.
lSize() != 0)) {
400 std::ostringstream errorstr;
401 errorstr <<
"Error: the requested field " << name <<
" has a different size than" 402 <<
" the provided data field.\n";
403 errorstr <<
"Registerd as: " << info.
iSize() <<
"x" << info.
jSize() <<
"x" << info.
kSize()
404 <<
"x" << info.
lSize() <<
"\n";
405 errorstr <<
"Given : " << size[0] <<
"x" << size[1] <<
"x" << size[2]
406 <<
"x" << size[3] <<
"x0" <<
"\n";
408 exception.
Init(errorstr.str());
413 if(info.
type() != type_name<ValueType>()) {
415 std::ostringstream errorstr;
416 errorstr <<
"Error: the requested field " << name <<
" has different type than" 417 <<
" the provided data field (expected " << info.
type() <<
", got " 418 << type_name<ValueType>() <<
")";
420 exception.Init(errorstr.str());
430 this->
ReadField(name, savepoint, data, strides[0], strides[1], strides[2], 0, alsoPrevious);
boost::shared_ptr< SerializerImpl > & getImpl()
Get implementation pointer.
int iSize() const
The size of the field in i-direction, including the halo.
static void EnableSerialization()
Enable serialization.
std::vector< std::string > fieldnames() const
Gives access to the registered fields.
int kSize() const
The size of the field in k-direction, including the halo.
Information of a data-field.
int kMinusOffset() const
Offset in k-minus direction.
int lSize() const
The size of the field in l-direction, including the halo.
void AddMetainfo(const std::string &key, ValueType value)
void Init(const std::string &directory, const std::string &prefix, SerializerOpenMode mode)
Initializes the serializer.
void ReadField(const std::string &fieldName, const Savepoint &savepoint, void *pData, int iStride, int jStride, int kStride, int lStride, bool alsoPrevious=false) const
Deserializes data from file.
int jSize() const
Size in j dimension.
int jPlusOffset() const
Offset in j-plus direction.
std::string type() const
The field scalar type.
std::string ToString() const
Convert to string.
std::string prefix() const
Prefix of the files.
Namespace of the STELLA frontend.
Namespace of the serialbox library.
static void DisableSerialization()
Disable serialization.
Implementation of the STELLA Savepoint.
int jMinusOffset() const
Offset in j-minus direction.
std::vector< std::string > FieldsAtSavepoint(const Savepoint &savepoint) const
Gives all fields which are available at the given savepoint and return their names.
Exception of the STELLA frontend.
const MetainfoSet & globalMetainfo() const
Read-only access to the metainformation.
const std::vector< Savepoint > & savepoints() const
Gives access to the list of savepoints.
SerializerOpenMode
OpenPolicy of the Serializer.
void Init(const int iMinusOffset, const int iPlusOffset, const int jMinusOffset, const int jPlusOffset, const int kMinusOffset, const int kPlusOffset)
Init the container.
int kPlusOffset() const
Offset in k-plus direction.
void AddFieldMetainfo(const std::string &fieldname, const std::string &key, bool value)
Adds a key-value metainformation pair to a field.
bool RegisterField(const std::string &name, std::string type, int bytesPerElement, int iSize, int jSize, int kSize, int lSize, int iMinusHalo, int iPlusHalo, int jMinusHalo, int jPlusHalo, int kMinusHalo, int kPlusHalo, int lMinusHalo, int lPlusHalo)
Register a field into the serializer.
const DataFieldInfo & FindField(const std::string &fieldname) const
Gives access to field info.
int iSize() const
Size in i dimension.
Container for i, j, k Size.
SerializerOpenMode mode() const
Open mode.
void WriteField(const std::string &fieldName, const Savepoint &savepoint, const void *pData, int iStride, int jStride, int kStride, int lStride)
Serializes data to file.
void Init(const int iSize, const int jSize, const int kSize)
Init the container.
Container for the boundary size.
int kSize() const
Size in k dimension.
int iPlusOffset() const
Offset in i-plus direction.
int iMinusOffset() const
Offset in i-minus direction.
void Init(const std::string &errormsg)
Initialize the exception with an explanatory string errormsg
Implementation of the STELLA Serializer.
std::string directory() const
Directory where the data are stored.
int jSize() const
The size of the field in j-direction, including the halo.