15 #include "serialbox-c/Serializer.h" 16 #include "serialbox-c/FieldMetainfo.h" 17 #include "serialbox-c/Logging.h" 18 #include "serialbox-c/Metainfo.h" 19 #include "serialbox-c/Savepoint.h" 20 #include "serialbox-c/Utility.h" 33 template <
class VecType>
34 static std::string vecToString(VecType&& vec) {
37 for(std::size_t i = 0; i < vec.size() - 1; ++i)
44 std::vector<int> make_dims(
int iSize,
int jSize,
int kSize,
int lSize) {
45 std::vector<int> dims;
47 dims.push_back(iSize);
49 dims.push_back(jSize);
51 dims.push_back(kSize);
53 dims.push_back(lSize);
63 const char* prefix,
const char* archive) {
69 serializer->impl =
new Serializer(serialbox::OpenModeKind::Read, directory, prefix, archive);
72 serializer->impl =
new Serializer(serialbox::OpenModeKind::Write, directory, prefix, archive);
76 new Serializer(serialbox::OpenModeKind::Append, directory, prefix, archive);
81 serializer->ownsData = 1;
83 std::free(serializer);
92 Serializer*
ser = toSerializer(serializer);
93 if(serializer->ownsData)
95 std::free(serializer);
104 const Serializer*
ser = toConstSerializer(serializer);
105 return static_cast<int>(ser->mode());
109 const Serializer*
ser = toConstSerializer(serializer);
110 return allocateAndCopyString(ser->directory().native());
114 const Serializer*
ser = toConstSerializer(serializer);
115 return allocateAndCopyString(ser->prefix());
119 Serializer*
ser = toSerializer(serializer);
121 ser->updateMetaData();
134 const Serializer*
ser = toConstSerializer(serializer);
135 std::stringstream ss;
137 return allocateAndCopyString(ss.str());
145 Serializer*
ser = toSerializer(serializer);
147 metaInfo->impl = ser->globalMetainfoPtr().get();
148 metaInfo->ownsData = 0;
158 const Savepoint* sp = toConstSavepoint(savepoint);
159 Serializer*
ser = toSerializer(serializer);
160 return ser->registerSavepoint(*sp);
165 const Savepoint* sp = toConstSavepoint(savepoint);
166 const Serializer*
ser = toConstSerializer(serializer);
167 return ser->savepointVector().exists(*sp);
171 const Serializer*
ser = toConstSerializer(serializer);
172 return (
int)ser->savepointVector().size();
177 const Serializer*
ser = toConstSerializer(serializer);
178 const auto& savepointVector = ser->savepointVector().savepoints();
185 for(std::size_t i = 0; i < savepointVector.size(); ++i) {
187 savepoint->impl = savepointVector[i].get();
188 savepoint->ownsData = 0;
189 savepoints[i] = savepoint;
196 for(
int i = 0; i < len; ++i)
197 std::free(savepointVector[i]);
198 std::free(savepointVector);
204 const Savepoint* sp = toConstSavepoint(savepoint);
205 const Serializer*
ser = toConstSerializer(serializer);
209 const auto& fieldnameMap = ser->savepointVector().fieldsOf(*sp);
210 array = serialboxArrayOfStringCreate(fieldnameMap.size());
213 for(
auto it = fieldnameMap.begin(), end = fieldnameMap.end(); it != end; ++it, ++i)
214 array->data[i] = allocateAndCopyString(it->first);
228 Serializer*
ser = toSerializer(serializer);
229 const FieldMetainfo* info = toConstFieldMetainfo(fieldMetainfo);
232 ser->registerField(name, *info);
234 LOG(warning) << e.what();
241 Serializer*
ser = toSerializer(serializer);
242 return ser->hasField(field);
246 int bytesPerElement,
int iSize,
int jSize,
int kSize,
int lSize,
247 int iMinusHalo,
int iPlusHalo,
int jMinusHalo,
int jPlusHalo,
248 int kMinusHalo,
int kPlusHalo,
int lMinusHalo,
int lPlusHalo) {
249 Serializer*
ser = toSerializer(serializer);
257 "type '%s' expected '%i'",
260 auto dims = ::make_dims(iSize, jSize, kSize, lSize);
261 int rank = dims.size();
263 MetainfoMap metaInfo;
264 metaInfo.insert(
"__name", name);
265 metaInfo.insert(
"__elementtype", typeName);
266 metaInfo.insert(
"__bytesperelement", bytesPerElement);
267 metaInfo.insert(
"__rank", rank);
268 metaInfo.insert(
"__isize", iSize);
269 metaInfo.insert(
"__jsize", jSize);
270 metaInfo.insert(
"__ksize", kSize);
271 metaInfo.insert(
"__lsize", lSize);
272 metaInfo.insert(
"__iminushalosize", iMinusHalo);
273 metaInfo.insert(
"__iplushalosize", iPlusHalo);
274 metaInfo.insert(
"__jminushalosize", jMinusHalo);
275 metaInfo.insert(
"__jplushalosize", jPlusHalo);
276 metaInfo.insert(
"__kminushalosize", kMinusHalo);
277 metaInfo.insert(
"__kplushalosize", kPlusHalo);
278 metaInfo.insert(
"__lminushalosize", lMinusHalo);
279 metaInfo.insert(
"__lplushalosize", lPlusHalo);
281 FieldMetainfo fieldMetainfo(typeID, dims, metaInfo);
284 if(ser->hasField(name) && (ser->getFieldMetainfoImplOf(name) == fieldMetainfo))
287 ser->registerField(name, fieldMetainfo);
297 const Serializer*
ser = toConstSerializer(serializer);
301 const auto fieldnameVector = ser->fieldnames();
302 array = serialboxArrayOfStringCreate(fieldnameVector.size());
304 for(std::size_t i = 0; i < fieldnameVector.size(); ++i)
305 array->data[i] = allocateAndCopyString(fieldnameVector[i]);
316 const Serializer*
ser = toConstSerializer(serializer);
318 auto it = ser->fieldMap().findField(name);
319 if(it != ser->fieldMap().end()) {
321 info->impl = it->second.get();
329 char** storedName,
char** elementType,
330 int* bytesPerElement,
int* rank,
int* iSize,
int* jSize,
331 int* kSize,
int* lSize,
int* iMinusHalo,
int* iPlusHalo,
332 int* jMinusHalo,
int* jPlusHalo,
int* kMinusHalo,
333 int* kPlusHalo,
int* lMinusHalo,
int* lPlusHalo) {
339 *storedName = serialboxMetainfoGetString(metainfo,
"__name");
340 *elementType = serialboxMetainfoGetString(metainfo,
"__elementtype");
341 *bytesPerElement = serialboxMetainfoGetInt32(metainfo,
"__bytesperelement");
342 *rank = serialboxMetainfoGetInt32(metainfo,
"__rank");
343 *iSize = serialboxMetainfoGetInt32(metainfo,
"__isize");
344 *jSize = serialboxMetainfoGetInt32(metainfo,
"__jsize");
345 *kSize = serialboxMetainfoGetInt32(metainfo,
"__ksize");
346 *lSize = serialboxMetainfoGetInt32(metainfo,
"__lsize");
347 *iMinusHalo = serialboxMetainfoGetInt32(metainfo,
"__iminushalosize");
348 *jMinusHalo = serialboxMetainfoGetInt32(metainfo,
"__jminushalosize");
349 *kMinusHalo = serialboxMetainfoGetInt32(metainfo,
"__kminushalosize");
350 *lMinusHalo = serialboxMetainfoGetInt32(metainfo,
"__lminushalosize");
351 *iPlusHalo = serialboxMetainfoGetInt32(metainfo,
"__iplushalosize");
352 *jPlusHalo = serialboxMetainfoGetInt32(metainfo,
"__jplushalosize");
353 *kPlusHalo = serialboxMetainfoGetInt32(metainfo,
"__kplushalosize");
354 *lPlusHalo = serialboxMetainfoGetInt32(metainfo,
"__lplushalosize");
367 const int* strides,
int numStrides) {
370 auto it = ser->fieldMap().findField(name);
371 if(it == ser->fieldMap().end())
375 const auto& dims = it->second->dims();
376 std::vector<int> stridesVec(strides, strides + numStrides);
378 if(dims.size() != stridesVec.size())
380 "\nDimensions as: [ %s ]" 381 "\nStrides as: [ %s ]",
382 name, ::vecToString(dims), ::vecToString(stridesVec));
391 const int* strides,
int numStrides) {
392 Serializer*
ser = toSerializer(serializer);
393 const Savepoint* sp = toConstSavepoint(savepoint);
397 internal::makeStorageView(ser, name, originPtr, strides, numStrides));
398 ser->write(name, *sp, storageView);
406 const int* strides,
int numStrides) {
407 Serializer*
ser = toSerializer(serializer);
408 const Savepoint* sp = toConstSavepoint(savepoint);
412 internal::makeStorageView(ser, name, originPtr, strides, numStrides));
413 ser->read(name, *sp, storageView);
421 const int* strides,
int numStrides,
const int*
slice) {
422 Serializer*
ser = toSerializer(serializer);
423 const Savepoint* sp = toConstSavepoint(savepoint);
427 internal::makeStorageView(ser, name, originPtr, strides, numStrides));
430 for(
int i = 0; i < numStrides; ++i)
431 sliceObj.
sliceTriples().push_back({slice[3 * i], slice[3 * i + 1], slice[3 * i + 2]});
434 ser->read(name, *sp, storageView);
442 const int* strides,
int numStrides) {
443 Serializer*
ser = toSerializer(serializer);
444 const Savepoint* sp = toConstSavepoint(savepoint);
448 internal::makeStorageView(ser, name, originPtr, strides, numStrides));
449 ser->readAsync(name, *sp, storageView);
456 Serializer*
ser = toSerializer(serializer);
469 int numDims,
const int* strides,
const char* fieldname,
470 const char* archivename) {
473 std::vector<int> dimsVec(dims, dims + numDims);
474 std::vector<int> stridesVec(strides, strides + numDims);
484 int numDims,
const int* strides,
const char* fieldname,
485 const char* archivename) {
488 std::vector<int> dimsVec(dims, dims + numDims);
489 std::vector<int> stridesVec(strides, strides + numDims);
void serialboxReadFromFile(const char *filename, void *originPtr, int typeID, const int *dims, int numDims, const int *strides, const char *fieldname, const char *archivename)
Deserialize field name (given by originPtr and strides) directly from file.
void serialboxSerializerDestroy(serialboxSerializer_t *serializer)
Destroy the serializer and deallocate all memory.
int serialboxSerializerGetNumSavepoints(const serialboxSerializer_t *serializer)
Get number of registered savepoints.
void serialboxSerializerDestroySavepointVector(serialboxSavepoint_t **savepointVector, int len)
Deallocate a savepoint vector retrieved via serialboxSerializerGetSavepointVector.
void serialboxSerializerWrite(serialboxSerializer_t *serializer, const char *name, const serialboxSavepoint_t *savepoint, void *originPtr, const int *strides, int numStrides)
Serialize field name (given by originPtr and strides) at savepoint to disk.
void serialboxEnableSerialization(void)
Enabled serialization.
serialboxSavepoint_t ** serialboxSerializerGetSavepointVector(const serialboxSerializer_t *serializer)
Get an array of refrences to the registered savepoints.
#define LOG(severity)
Logging infrastructure.
static int sizeOf(TypeID id)
Get size of the type.
Represent a mutable view to a multi-dimensional storage.
char * serialboxSerializerGetDirectory(const serialboxSerializer_t *serializer)
Return the directory of the Serializer.
void setSlice(Slice slice)
Set the slice of the StorageView
void serialboxSerializerGetFieldMetainfo2(const serialboxSerializer_t *serializer, const char *name, char **storedName, char **elementType, int *bytesPerElement, int *rank, int *iSize, int *jSize, int *kSize, int *lSize, int *iMinusHalo, int *iPlusHalo, int *jMinusHalo, int *jPlusHalo, int *kMinusHalo, int *kPlusHalo, int *lMinusHalo, int *lPlusHalo)
Get values of standard meta info pairs of field with name name
serialboxMetainfo_t * serialboxSerializerGetGlobalMetainfo(serialboxSerializer_t *serializer)
Allocate a new serialboxMetainfo_t which maps to the global meta-information of the Serializer...
TypeID
Type-id of types recognized by serialbox.
void serialboxSerializerWaitForAll(serialboxSerializer_t *serializer)
Wait for all pending asynchronous read operations and reset the internal queue.
int serialboxSerializationStatus(void)
Indicate whether serialization is enabled [default: enabled].
void serialboxSerializerUpdateMetaData(serialboxSerializer_t *serializer)
Write meta-data to disk.
int serialboxSerializerAddSavepoint(serialboxSerializer_t *serializer, const serialboxSavepoint_t *savepoint)
Register savepoint within the serializer.
static void readFromFile(std::string filename, StorageView &storageView, std::string archiveName, std::string fieldname)
Directly read field (given by storageView) from file.
int serialboxSerializerAddField2(serialboxSerializer_t *serializer, const char *name, int 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 field within the serializer.
Refrence to a Serializer.
char * serialboxSerializerToString(const serialboxSerializer_t *serializer)
Convert serializer to string.
char * serialboxSerializerGetPrefix(const serialboxSerializer_t *serializer)
Return the prefix of all filenames.
void serialboxSerializerRead(serialboxSerializer_t *serializer, const char *name, const serialboxSavepoint_t *savepoint, void *originPtr, const int *strides, int numStrides)
Deserialize field name (given by originPtr and strides) at savepoint from disk.
static std::string toString(TypeID id)
Convert to string.
void serialboxSerializerReadSliced(serialboxSerializer_t *serializer, const char *name, const serialboxSavepoint_t *savepoint, void *originPtr, const int *strides, int numStrides, const int *slice)
Deserialize sliced field name (given by originPtr, strides and slice) at savepoint from disk...
int serialboxSerializerAddField(serialboxSerializer_t *serializer, const char *name, const serialboxFieldMetainfo_t *fieldMetainfo)
Register field given as fieldMetainfo within the serializer.
int serialboxSerializerHasSavepoint(const serialboxSerializer_t *serializer, const serialboxSavepoint_t *savepoint)
Check if savepoint exists within the serializer.
std::vector< SliceTriple > & sliceTriples() noexcept
Get slice triples.
static void writeToFile(std::string filename, const StorageView &storageView, std::string archiveName, std::string fieldname)
Directly write field (given by storageView) to file.
void serialboxSerializerReadAsync(serialboxSerializer_t *serializer, const char *name, const serialboxSavepoint_t *savepoint, void *originPtr, const int *strides, int numStrides)
Asynchronously deserialize field name (given as storageView) at savepoint from disk using std::async...
serialboxArrayOfString_t * serialboxSerializerGetFieldnames(const serialboxSerializer_t *serializer)
Get an array of C-strings of all names of the registered fields.
void serialboxFatalError(const char *reason)
Report a fatal error.
int serialboxSerializerGetMode(const serialboxSerializer_t *serializer)
Return mode of the Serializer.
int serialboxSerializerHasField(serialboxSerializer_t *serializer, const char *field)
Check if field is registered within the serializer.
serialboxSerializer_t * serialboxSerializerCreate(int mode, const char *directory, const char *prefix, const char *archive)
Create a new Serializer.
#define serialbox_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void serialboxWriteToFile(const char *filename, void *originPtr, int typeID, const int *dims, int numDims, const int *strides, const char *fieldname, const char *archivename)
Serialize field name (given by originPtr and strides) directly to file.
serialboxFieldMetainfo_t * serialboxSerializerGetFieldMetainfo(const serialboxSerializer_t *serializer, const char *name)
Get FieldMetainfoImpl of field with name name
Specification of the slice indices which is used for partial loading of serialized data...
Exception class which stores a human-readable error description.
serialboxArrayOfString_t * serialboxSerializerGetFieldnamesAtSavepoint(const serialboxSerializer_t *serializer, const serialboxSavepoint_t *savepoint)
Get an array of C-strings of the field names registered at savepoint
void serialboxDisableSerialization(void)
Disable serialization.