19 #include <boost/make_shared.hpp> 26 int Serializer::enabled_ = 0;
28 Serializer::Serializer() { serializerImpl_ =
nullptr; }
31 switch(serializerImpl_->mode()) {
32 case OpenModeKind::Read:
33 return SerializerOpenModeRead;
34 case OpenModeKind::Write:
35 return SerializerOpenModeWrite;
36 case OpenModeKind::Append:
37 return SerializerOpenModeAppend;
54 const char* envvar = std::getenv(
"STELLA_SERIALIZATION_DISABLED");
55 if(envvar && std::atoi(envvar) > 0)
62 case SerializerOpenModeRead:
64 boost::make_shared<SerializerImpl>(OpenModeKind::Read,
directory,
prefix,
"Binary");
66 case SerializerOpenModeWrite:
68 boost::make_shared<SerializerImpl>(OpenModeKind::Write,
directory,
prefix,
"Binary");
70 case SerializerOpenModeAppend:
72 boost::make_shared<SerializerImpl>(OpenModeKind::Append,
directory,
prefix,
"Binary");
76 internal::throwSerializationException(
"Error: %s", e.what());
81 internal::make_shared_ptr<MetainfoMapImpl>(serializerImpl_->globalMetainfoPtr()));
84 for(
auto& savepoint : serializerImpl_->savepoints())
85 savepoints_.emplace_back(internal::make_shared_ptr(savepoint));
88 for(
auto it = serializerImpl_->fieldMap().begin(), end = serializerImpl_->fieldMap().end();
90 datafieldInfos_.insert({it->first, internal::make_shared_ptr<FieldMetainfoImpl>(it->second)});
94 int iSize,
int jSize,
int kSize,
int lSize,
int iMinusHalo,
95 int iPlusHalo,
int jMinusHalo,
int jPlusHalo,
int kMinusHalo,
96 int kPlusHalo,
int lMinusHalo,
int lPlusHalo) {
98 TypeID typeID = internal::TypeNameToTypeID(type);
100 throw Exception(
"inconsistent bytes-per-element: got '%i' but according to passed type " 101 "'%s' expected '%i'",
105 (iSize != 1 ? 1 : 0) + (jSize != 1 ? 1 : 0) + (kSize != 1 ? 1 : 0) + (lSize != 1 ? 1 : 0);
107 std::vector<int> dims{iSize, jSize, kSize, lSize};
109 metaInfo.
insert(
"__name", name);
110 metaInfo.
insert(
"__elementtype", type);
111 metaInfo.
insert(
"__bytesperelement", bytesPerElement);
112 metaInfo.
insert(
"__rank", rank);
113 metaInfo.
insert(
"__isize", iSize);
114 metaInfo.
insert(
"__jsize", jSize);
115 metaInfo.
insert(
"__ksize", kSize);
116 metaInfo.
insert(
"__lsize", lSize);
117 metaInfo.
insert(
"__iminushalosize", iMinusHalo);
118 metaInfo.
insert(
"__iplushalosize", iPlusHalo);
119 metaInfo.
insert(
"__jminushalosize", jMinusHalo);
120 metaInfo.
insert(
"__jplushalosize", jPlusHalo);
121 metaInfo.
insert(
"__kminushalosize", kMinusHalo);
122 metaInfo.
insert(
"__kplushalosize", kPlusHalo);
123 metaInfo.
insert(
"__lminushalosize", lMinusHalo);
124 metaInfo.
insert(
"__lplushalosize", lPlusHalo);
129 if(serializerImpl_->hasField(name) &&
130 (serializerImpl_->getFieldMetainfoImplOf(name) == fieldMetainfo)) {
134 serializerImpl_->registerField(name, fieldMetainfo);
137 auto it = serializerImpl_->fieldMap().findField(name);
138 datafieldInfos_.insert({it->first, internal::make_shared_ptr<FieldMetainfoImpl>(it->second)});
141 internal::throwSerializationException(
"Error: %s", e.what());
148 auto it = datafieldInfos_.find(fieldname);
149 if(it == datafieldInfos_.end())
150 throw Exception(
"field %s is not registered", fieldname);
154 internal::throwSerializationException(
"Error: %s", e.what());
161 if(!serializerImpl_->addFieldMetainfoImpl(fieldname, key, value))
162 throw Exception(
"metainfo with key = %s exists already in field %s", key, fieldname);
164 internal::throwSerializationException(
"Error: %s", e.what());
170 if(!serializerImpl_->addFieldMetainfoImpl(fieldname, key, value))
171 throw Exception(
"metainfo with key = %s exists already in field %s", key, fieldname);
173 internal::throwSerializationException(
"Error: %s", e.what());
180 if(!serializerImpl_->addFieldMetainfoImpl(fieldname, key, value))
181 throw Exception(
"metainfo with key = %s exists already in field %s", key, fieldname);
183 internal::throwSerializationException(
"Error: %s", e.what());
190 if(!serializerImpl_->addFieldMetainfoImpl(fieldname, key, value))
191 throw Exception(
"metainfo with key = %s exists already in field %s", key, fieldname);
193 internal::throwSerializationException(
"Error: %s", e.what());
200 if(!serializerImpl_->addFieldMetainfoImpl(fieldname, key, value))
201 throw Exception(
"metainfo with key = %s exists already in field %s", key, fieldname);
203 internal::throwSerializationException(
"Error: %s", e.what());
210 std::vector<std::string> fields;
213 int idx = serializerImpl_->savepointVector().find(*savepoint.
getImpl());
216 const auto& fieldsPerSavepointMap = serializerImpl_->savepointVector().fieldsOf(idx);
217 for(
auto it = fieldsPerSavepointMap.begin(), end = fieldsPerSavepointMap.end(); it != end; ++it)
218 fields.push_back(it->first);
223 static StorageView makeStorageView(
const void* pData,
TypeID type,
const std::vector<int>& dims,
224 std::vector<int> strides) {
229 for(std::size_t i = 0; i < dims.size(); ++i)
230 strides[i] /= bytesPerElement;
234 throw Exception(
"the STELLA frontend does not support %i dimensional storages", dims.size());
237 while(strides.size() != dims.size())
240 return StorageView(const_cast<void*>(pData), type, dims, std::vector<int>(strides));
244 const void* pData,
int iStride,
int jStride,
int kStride,
int lStride) {
249 std::size_t numSavepoints = serializerImpl_->savepointVector().size();
250 const FieldMetainfoImpl& info = serializerImpl_->getFieldMetainfoImplOf(fieldName);
253 makeStorageView(pData, info.
type(), info.
dims(), {iStride, jStride, kStride, lStride});
255 serializerImpl_->write(fieldName, *savepoint.
getImpl(), storageView);
258 if(numSavepoints < serializerImpl_->savepointVector().size())
259 savepoints_.emplace_back(
260 internal::make_shared_ptr<SavepointImpl>(serializerImpl_->savepointVector().back()));
263 internal::throwSerializationException(
"Error: %s", e.what());
268 int iStride,
int jStride,
int kStride,
int lStride,
269 bool alsoPrevious)
const {
274 const FieldMetainfoImpl& info = serializerImpl_->getFieldMetainfoImplOf(fieldName);
277 makeStorageView(pData, info.
type(), info.
dims(), {iStride, jStride, kStride, lStride});
279 serializerImpl_->read(fieldName, *savepoint.
getImpl(), storageView, alsoPrevious);
282 internal::throwSerializationException(
"Error: %s", e.what());
287 std::ostringstream ss;
288 ss << serializerImpl_;
static void EnableSerialization()
Enable serialization.
std::vector< std::string > fieldnames() const
Gives access to the registered fields.
Information of a data-field.
static int serializationStatus() noexcept
Get the status of serialization.
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.
std::string ToString() const
Convert to string.
std::string prefix() const
Prefix of the files.
static int sizeOf(TypeID id)
Get size of the type.
Represent a mutable view to a multi-dimensional storage.
Namespace of the STELLA frontend.
Namespace of the serialbox library.
static void disableSerialization() noexcept
Disable serialization.
TypeID
Type-id of types recognized by serialbox.
static void DisableSerialization()
Disable serialization.
Implementation of the STELLA Savepoint.
std::vector< std::string > FieldsAtSavepoint(const Savepoint &savepoint) const
Gives all fields which are available at the given savepoint and return their names.
SerializerOpenMode
OpenPolicy of the Serializer.
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.
static void enableSerialization() noexcept
Enable serialization.
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.
boost::shared_ptr< SavepointImpl > & getImpl()
Get implementation pointer.
#define serialbox_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Exception class which stores a human-readable error description.
std::string directory() const
Directory where the data are stored.