15 #include "serialbox-c/FortranWrapper.h" 16 #include "serialbox-c/Serializer.h" 17 #include "serialbox-c/Utility.h" 29 void make_4D(std::vector<int>& v) {
32 "The FortranWrapper supports up to 4 dimensions (field with %i dimensions was passed).",
38 std::vector<int> make_strides(
int istride,
int jstride,
int kstride,
int lstride) {
39 std::vector<int> strides;
41 strides.push_back(istride);
43 strides.push_back(jstride);
45 strides.push_back(kstride);
47 strides.push_back(lstride);
57 void* originPtr,
int istride,
int jstride,
int kstride,
59 auto strides = ::make_strides(istride, jstride, kstride, lstride);
61 static_cast<const serialboxSavepoint_t*>(savepoint), originPtr,
62 strides.data(), strides.size());
66 void* originPtr,
int istride,
int jstride,
int kstride,
68 auto strides = ::make_strides(istride, jstride, kstride, lstride);
70 static_cast<const serialboxSavepoint_t*>(savepoint), originPtr,
71 strides.data(), strides.size());
75 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
76 std::cout << ser << std::endl;
79 template <
class ArrayType1,
class ArrayType2>
80 static void checkRank(
const char* name, ArrayType1&& array, ArrayType2&& arrayRef) {
81 const int rank = (array[0] > 0 ? 1 : 0) + (array[1] > 0 ? 1 : 0) + (array[2] > 0 ? 1 : 0) +
82 (array[3] > 0 ? 1 : 0);
84 const int refRank = (arrayRef[0] > 0 ? 1 : 0) + (arrayRef[1] > 0 ? 1 : 0) +
85 (arrayRef[2] > 0 ? 1 : 0) + (arrayRef[3] > 0 ? 1 : 0);
87 bool scalar = rank == 0 && refRank == 1 && arrayRef[0] == 1;
89 if(rank != refRank && !scalar)
90 throw Exception(
"field '%s' has rank %i but field with rank %i was passed", name, refRank,
95 int* isize,
int* jsize,
int* ksize,
int* lsize) {
97 const Serializer*
ser = toConstSerializer(static_cast<const serialboxSerializer_t*>(serializer));
100 const auto& info = ser->getFieldMetainfoImplOf(name);
102 std::array<int, 4> actualSizes{{*isize, *jsize, *ksize, *lsize}};
103 auto refSizes = info.dims();
107 checkRank(name, actualSizes, refSizes);
110 TypeID typeID = *type <= Float64 ? (
TypeID)*type : TypeID::Invalid;
111 if(typeID != info.type())
112 throw Exception(
"field '%s' has type '%s' but was registered as type '%s'", name,
117 for(
int i = 0; i < 4; ++i) {
118 if(actualSizes[i] == refSizes[i])
121 if(refSizes[i] == 1) {
122 for(
int j = 3; j > i; --j)
123 actualSizes[j] = actualSizes[j - 1];
127 throw Exception(
"dimensions of field '%s' do not match registered ones:" 128 "\nRegistered as: [ %i, %i, %i, %i ]" 129 "\nGiven as: [ %i, %i, %i, %i ]",
130 name, refSizes[0], refSizes[1], refSizes[2], refSizes[3], actualSizes[0],
131 actualSizes[1], actualSizes[2], actualSizes[3]);
139 const void* iplus1,
const void* jplus1,
const void* kplus1,
140 const void* lplus1,
int* istride,
int* jstride,
int* kstride,
142 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
145 const auto& info = ser->getFieldMetainfoImplOf(fieldname);
147 std::array<long, 4> strides{
148 {
reinterpret_cast<const char*
>(iplus1) - reinterpret_cast<const char*>(basePtr),
149 reinterpret_cast<const char*
>(jplus1) - reinterpret_cast<const char*>(basePtr),
150 reinterpret_cast<const char*
>(kplus1) - reinterpret_cast<const char*>(basePtr),
151 reinterpret_cast<const char*
>(lplus1) - reinterpret_cast<const char*>(basePtr)}};
154 for(
int i = 2; i >= 0; --i)
156 strides[i] = strides[i + 1];
160 *istride = strides[0] / bytesPerElement;
161 *jstride = strides[1] / bytesPerElement;
162 *kstride = strides[2] / bytesPerElement;
163 *lstride = strides[3] / bytesPerElement;
171 int* isize,
int* jsize,
int* ksize,
int* lsize) {
173 const Serializer*
ser = toConstSerializer(static_cast<const serialboxSerializer_t*>(serializer));
175 auto dims = ser->getFieldMetainfoImplOf(name).dims();
186 int* iMinusHalo,
int* iPlusHalo,
int* jMinusHalo,
187 int* jPlusHalo,
int* kMinusHalo,
int* kPlusHalo,
188 int* lMinusHalo,
int* lPlusHalo) {
190 char *notUsedHere_storedName, *notUsedHere_elementType;
191 int notUsedHere_bytesPerElement, notUsedHere_rank;
192 int notUsedHere_iSize, notUsedHere_jSize, notUsedHere_kSize, notUsedHere_lSize;
195 static_cast<const serialboxSerializer_t*>(serializer), name, ¬UsedHere_storedName,
196 ¬UsedHere_elementType, ¬UsedHere_bytesPerElement, ¬UsedHere_rank, ¬UsedHere_iSize,
197 ¬UsedHere_jSize, ¬UsedHere_kSize, ¬UsedHere_lSize, iMinusHalo, iPlusHalo, jMinusHalo,
198 jPlusHalo, kMinusHalo, kPlusHalo, lMinusHalo, lPlusHalo);
202 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
204 ser->addGlobalMetainfo(key, (
bool)value);
210 void serialboxFortranSerializerAddMetainfoInt32(
void* serializer,
const char* key,
int value) {
211 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
213 ser->addGlobalMetainfo(key, value);
219 void serialboxFortranSerializerAddMetainfoFloat32(
void* serializer,
const char* key,
float value) {
220 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
222 ser->addGlobalMetainfo(key, value);
228 void serialboxFortranSerializerAddMetainfoFloat64(
void* serializer,
const char* key,
double value) {
229 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
231 ser->addGlobalMetainfo(key, value);
237 void serialboxFortranSerializerAddMetainfoString(
void* serializer,
const char* key,
239 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
241 ser->addGlobalMetainfo(key, value);
248 int bytesPerElement,
int iSize,
int jSize,
int kSize,
249 int lSize,
int iMinusHalo,
int iPlusHalo,
250 int jMinusHalo,
int jPlusHalo,
int kMinusHalo,
251 int kPlusHalo,
int lMinusHalo,
int lPlusHalo) {
253 bytesPerElement, iSize, jSize, kSize, lSize, iMinusHalo, iPlusHalo,
254 jMinusHalo, jPlusHalo, kMinusHalo, kPlusHalo, lMinusHalo, lPlusHalo);
262 const char* key,
int value) {
263 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
265 if(!ser->addFieldMetainfoImpl(field, key, (
bool)value))
267 "cannot add element with key '%s' to field meta-info of '%s': element already exists",
274 void serialboxFortranSerializerAddFieldMetainfoInt32(
void* serializer,
const char* field,
275 const char* key,
int value) {
276 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
278 if(!ser->addFieldMetainfoImpl(field, key, value))
280 "cannot add element with key '%s' to field meta-info of '%s': element already exists",
287 void serialboxFortranSerializerAddFieldMetainfoFloat32(
void* serializer,
const char* field,
288 const char* key,
float value) {
289 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
291 if(!ser->addFieldMetainfoImpl(field, key, value))
293 "cannot add element with key '%s' to field meta-info of '%s': element already exists",
300 void serialboxFortranSerializerAddFieldMetainfoFloat64(
void* serializer,
const char* field,
301 const char* key,
double value) {
302 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
304 if(!ser->addFieldMetainfoImpl(field, key, value))
306 "cannot add element with key '%s' to field meta-info of '%s': element already exists",
313 void serialboxFortranSerializerAddFieldMetainfoString(
void* serializer,
const char* field,
314 const char* key,
const char* value) {
315 Serializer*
ser = toSerializer(static_cast<serialboxSerializer_t*>(serializer));
317 if(!ser->addFieldMetainfoImpl(field, key, value))
319 "cannot add element with key '%s' to field meta-info of '%s': element already exists",
331 Savepoint* sp = toSavepoint(static_cast<serialboxSavepoint_t*>(savepoint));
333 sp->addMetainfo(key, (
bool)value);
339 void serialboxFortranSavepointAddMetainfoInt32(
void* savepoint,
const char* key,
int value) {
340 Savepoint* sp = toSavepoint(static_cast<serialboxSavepoint_t*>(savepoint));
342 sp->addMetainfo(key, value);
348 void serialboxFortranSavepointAddMetainfoFloat32(
void* savepoint,
const char* key,
float value) {
349 Savepoint* sp = toSavepoint(static_cast<serialboxSavepoint_t*>(savepoint));
351 sp->addMetainfo(key, value);
357 void serialboxFortranSavepointAddMetainfoFloat64(
void* savepoint,
const char* key,
double value) {
358 Savepoint* sp = toSavepoint(static_cast<serialboxSavepoint_t*>(savepoint));
360 sp->addMetainfo(key, value);
366 void serialboxFortranSavepointAddMetainfoString(
void* savepoint,
const char* key,
368 Savepoint* sp = toSavepoint(static_cast<serialboxSavepoint_t*>(savepoint));
370 sp->addMetainfo(key, value);
void serialboxFortranSerializerWrite(void *serializer, const void *savepoint, const char *name, void *originPtr, int istride, int jstride, int kstride, int lstride)
Wrapper for serialboxSerializerWrite.
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 serialboxFortranSerializerRegisterField(void *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.
static int sizeOf(TypeID id)
Get size of the type.
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
TypeID
Type-id of types recognized by serialbox.
void serialboxFortranSavepointAddMetainfoBoolean(void *savepoint, const char *key, int value)
Add a meta-information key=value pair to the savepoint
void serialboxFortranSerializerAddFieldMetainfoBoolean(void *serializer, const char *field, const char *key, int value)
Add a meta-information key=value pair to field of the serializer.
void serialboxFortranComputeStrides(void *serializer, const char *fieldname, const void *basePtr, const void *iplus1, const void *jplus1, const void *kplus1, const void *lplus1, int *istride, int *jstride, int *kstride, int *lstride)
Compute unit-strides of registered field fieldname
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.
void serialboxFortranSerializerGetFieldHalos(const void *serializer, const char *name, int *iMinusHalo, int *iPlusHalo, int *jMinusHalo, int *jPlusHalo, int *kMinusHalo, int *kPlusHalo, int *lMinusHalo, int *lPlusHalo)
Returns the halos of the field name
void serialboxFortranSerializerGetFieldDimensions(const void *serializer, const char *name, int *isize, int *jsize, int *ksize, int *lsize)
Returns the dimensions of the field name
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 serialboxFortranSerializerRead(void *serializer, const void *savepoint, const char *name, void *originPtr, int istride, int jstride, int kstride, int lstride)
Wrapper for serialboxSerializerRead.
void serialboxFatalError(const char *reason)
Report a fatal error.
void serialboxFortranSerializerCheckField(const void *serializer, const char *name, int *type, int *isize, int *jsize, int *ksize, int *lsize)
Perform consistency checks concerning dimensions and type of the field name
Exception class which stores a human-readable error description.
void serialboxFortranSerializerAddMetainfoBoolean(void *serializer, const char *key, int value)
Add a global meta-information key=value pair to the Serializer.
void serialboxFortranSerializerPrintDebugInfo(void *serializer)
Print debug information (i.e convert serializer to string)