10#include <kernel/util/memory_pool.hpp>
12#include <kernel/util/type_traits.hpp>
13#include <kernel/util/random.hpp>
14#include <kernel/util/pack.hpp>
115 XABORTM(
"Zlib compression was enabled for elements, but zlib bibliography was not loaded. Please configure FEAT with -zlib flag!");
119 XASSERTM(tolerance >
FEAT::Real(0.),
"tolerance ist smaller or equall 0 while zfp is enabled!");
122 XABORTM(
"Zfp compression was enabled for elements, but zfp bibliography was not loaded. Please configure FEAT with -zfp flag!");
125 XABORTM(
"No legal CompressionModes was given");
127 elements_compression = temp;
146 XABORTM(
"Zlib compression was enabled for indices, but zlib bibliography was not loaded. Please configure FEAT with -zlib flag!");
149 XABORTM(
"No legal CompressionModes was given");
151 indices_compression = temp;
164 XASSERTM(tol >
FEAT::Real(0.),
"Zfp enabled, but tolerance is smaller or equal to 0!");
176 return elements_compression;
187 return indices_compression;
218 template <
typename DT_,
typename IT_>
221 template <
typename DT2_,
typename IT2_>
240 void _copy_content(
const Container & other,
bool full)
246 XASSERTM(
_elements.size() == other.get_elements().size(),
"Container size mismatch!");
247 XASSERTM(
_indices.size() == other.get_indices().size(),
"Container size mismatch!");
249 XASSERTM(
_scalar_dt.size() == other.get_scalar_dt().size(),
"Container size mismatch!");
253 this->_scalar_index.assign(other._scalar_index.begin(), other._scalar_index.end());
254 this->_scalar_dt.assign(other._scalar_dt.begin(), other._scalar_dt.end());
279 template <
typename DT2_,
typename IT2_>
282 XASSERTM(!other._foreign_memory,
"assign/convert is forbidden with foreign memory source objects.");
283 if (!this->_foreign_memory)
285 for (
Index i(0) ; i < this->_elements.size() ; ++i)
287 for (
Index i(0) ; i < this->_indices.size() ; ++i)
291 this->_foreign_memory =
false;
293 this->_elements.clear();
294 this->_indices.clear();
295 this->_elements_size.clear();
296 this->_indices_size.clear();
297 this->_scalar_index.clear();
298 this->_scalar_dt.clear();
300 this->_elements_size.assign(other.get_elements_size().begin(), other.get_elements_size().end());
301 this->_indices_size.assign(other.get_indices_size().begin(), other.get_indices_size().end());
302 this->_scalar_index.assign(other.get_scalar_index().begin(), other.get_scalar_index().end());
303 if constexpr (std::is_same<DT_, DT2_>::value)
305 this->_scalar_dt.assign(other.get_scalar_dt().begin(), other.get_scalar_dt().end());
310 for(
auto t : other.get_scalar_dt())
312 this->_scalar_dt.push_back(DT_(t));
316 if constexpr (std::is_same<DT_, DT2_>::value)
318 this->_elements.assign(other.get_elements().begin(), other.get_elements().end());
320 for (
Index i(0) ; i < this->_elements.size() ; ++i)
325 for (
Index i(0) ; i < this->_elements_size.size() ; ++i)
327 const Index tsize(this->_elements_size.at(i));
328 this->_elements.push_back(MemoryPool::template allocate_memory<DT_>(tsize));
333 if constexpr (std::is_same<IT_, IT2_>::value)
335 this->_indices.assign(other.get_indices().begin(), other.get_indices().end());
337 for (
Index i(0) ; i < this->_indices.size() ; ++i)
342 for (
Index i(0) ; i < this->_indices_size.size() ; ++i)
344 const Index tsize(this->_indices_size.at(i));
345 this->_indices.push_back(MemoryPool::template allocate_memory<IT_>(tsize));
360 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
366 std::uint64_t gsize(4 *
sizeof(std::uint64_t));
367 gsize += 7 *
sizeof(std::uint64_t);
369 gsize += 2 * tc.
_indices_size.size() *
sizeof(std::uint64_t);
373 CompressionModes compress = config.get_elements_compression() | config.get_indices_compression();
374 Pack::Type compression_type_elements = Pack::deduct_type<DT2_>();
375 Pack::Type compression_type_index = Pack::deduct_type<IT2_>();
378 compression_type_elements = compression_type_elements | Pack::Type::Mask_P;
382 compression_type_elements = compression_type_elements | Pack::Type::Mask_Z;
385 compression_type_index = compression_type_index | Pack::Type::Mask_Z;
388 FEAT::Real tolerance = config.get_tolerance();
443 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
446 std::uint64_t raw_size = 0u;
447 FEAT::Real tolerance = config.get_tolerance();
449 CompressionModes compress = config.get_elements_compression() | config.get_indices_compression();
450 Pack::Type compression_type_elements = Pack::deduct_type<DT2_>();
451 Pack::Type compression_type_index = Pack::deduct_type<IT2_>();
454 compression_type_elements = compression_type_elements | Pack::Type::Mask_P;
458 compression_type_elements = compression_type_elements | Pack::Type::Mask_Z;
461 compression_type_index = compression_type_index | Pack::Type::Mask_Z;
466 std::uint64_t gsize = this->
template _serialized_size<DT2_, IT2_>(config);
468 std::vector<char> result((
size_t(gsize)));
469 char * array(result.data());
470 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
471 DT2_ * dtarray(
reinterpret_cast<DT2_ *
>(array));
472 IT2_ * itarray(
reinterpret_cast<IT2_ *
>(array));
475 #if defined(FEAT_COMPILER_CLANG)
476 std::uint64_t magic = (std::uint64_t)
static_cast<__underlying_type(
FileMode)
>(mode);
478 std::uint64_t magic = (std::uint64_t)
static_cast<typename std::underlying_type<FileMode>::type
>(mode);
490 uiarray[10] = (std::uint64_t)compress;
491 raw_size += 11 * (std::uint64_t)
sizeof(std::uint64_t);
494 Index local_elements(0);
495 Index local_index(0);
502 local_elements = global_i;
505 uiarray[i + global_i] = (std::uint64_t)0u;
508 raw_size += 2 * std::uint64_t(tc.
_elements_size.size()) * std::uint64_t(
sizeof(std::uint64_t));
515 local_index = global_i;
518 uiarray[i + global_i] = 0;
521 raw_size += 2 * std::uint64_t(tc.
_indices_size.size()) * std::uint64_t(
sizeof(std::uint64_t));
528 raw_size += std::uint64_t(tc.
_scalar_index.size()) * std::uint64_t(
sizeof(std::uint64_t));
530 global_i =
Index((global_i *
sizeof(std::uint64_t) +
sizeof(DT2_) - 1u) /
sizeof(DT2_));
537 raw_size += std::uint64_t(tc.
_scalar_dt.size()) * std::uint64_t(
sizeof(DT2_));
541 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(
char) - 1u) /
sizeof(
char));
544 char* dest = &result[global_i];
546 std::size_t real_size = Pack::encode<DT2_>(dest, tc.
_elements.at(i), est_buff_size, tc.
_elements_size.at(i), compression_type_elements,
false, (
double)tolerance);
547 XASSERTM(real_size != 0u,
"could not encode elements");
548 uiarray[i + local_elements] = (std::uint64_t)real_size;
549 global_i += (
Index)real_size;
550 raw_size += (std::uint64_t)real_size * (std::uint64_t)
sizeof(char);
552 global_i =
Index((global_i *
sizeof(
char) +
sizeof(DT2_) - 1u) /
sizeof(DT2_));
559 uiarray[i + local_elements] = (std::uint64_t) tc.
_elements_size.at(i) *
sizeof(DT2_);
561 raw_size += (std::uint64_t) tc.
_elements_size.at(i) *
sizeof(DT2_);
566 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(
char) - 1u) /
sizeof(
char));
569 char* dest = &result[global_i];
571 std::size_t real_size = Pack::encode<IT2_>(dest, tc.
_indices.at(i), est_buff_size, tc.
_indices_size.at(i), compression_type_index,
false);
572 XASSERTM(real_size != 0u,
"could not encode indices");
573 uiarray[i + local_index] = (std::uint64_t) real_size;
574 global_i +=
Index(real_size);
575 raw_size += std::uint64_t(real_size) * std::uint64_t(
sizeof(
char));
580 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(IT2_) - 1u) /
sizeof(IT2_));
584 uiarray[i + local_index] = (std::uint64_t) tc.
_indices_size.at(i) *
sizeof(IT2_);
586 raw_size += (std::uint64_t) tc.
_indices_size.at(i) *
sizeof(IT2_);
589 uiarray[0] = raw_size + 16u;
592 result.resize(std::size_t(uiarray[0]));
608 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
611 auto temp(this->
template _serialize<DT2_, IT2_>(mode, config));
612 file.write(temp.data(),
long(temp.size()));
614 XABORTM(
"Error in _serialize - file ostream is not good anymore!");
625 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
632 char * array(input.data());
633 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
634 DT2_ * dtarray(
reinterpret_cast<DT2_ *
>(array));
635 IT2_ * itarray(
reinterpret_cast<IT2_ *
>(array));
638#if defined(FEAT_COMPILER_CLANG)
639 std::uint64_t magic = (std::uint64_t)
static_cast<__underlying_type(
FileMode)
>(mode);
641 std::uint64_t magic = (std::uint64_t)
static_cast<typename std::underlying_type<FileMode>::type
>(mode);
643 XASSERTM(magic == uiarray[1],
"_deserialize: given FileMode incompatible with given array!");
654 std::cerr<<
"Warning: You are reading a container floating point in higher precision then it was saved before!\n";
657 std::cerr<<
"Warning: You are reading a container integral type in higher precision then it was saved before!\n";
661#ifndef FEAT_HAVE_ZLIB
663 "Data was compressed with ZLIB! To read in data, please configure FEAT with zlib enabled.\n For more information see FEAT documentation.");
665 "Data was compressed with ZLIB! To read in data, please configure FEAT with zlib enabled.\n For more information see FEAT documentation.");
669 "Data was compressed with zfp! To read in data, please configure FEAT with zfp enabled.\n For more information see FEAT documentation.");
671 std::vector<std::uint64_t> elements_bytes(uiarray[6]);
672 std::vector<std::uint64_t> indices_bytes(uiarray[7]);
674 for (std::uint64_t i(0) ; i < uiarray[6] ; ++i)
678 global_i +=
Index(uiarray[6]);
679 for (std::uint64_t i(0) ; i < uiarray[6] ; ++i)
681 elements_bytes[i] = uiarray[i + global_i];
683 global_i +=
Index(uiarray[6]);
685 for (std::uint64_t i(0) ; i < uiarray[7] ; ++i)
689 global_i +=
Index(uiarray[7]);
690 for (std::uint64_t i(0) ; i < uiarray[7] ; ++i)
692 indices_bytes[i] = uiarray[i + global_i];
694 global_i +=
Index(uiarray[7]);
696 for (std::uint64_t i(0) ; i < uiarray[8] ; ++i)
700 global_i +=
Index(uiarray[8]);
702 global_i =
Index((global_i *
sizeof(std::uint64_t) +
sizeof(DT2_) - 1u) /
sizeof(DT2_));
703 for (std::uint64_t i(0) ; i < uiarray[9] ; ++i)
705 tc.
_scalar_dt.push_back(dtarray[i + global_i]);
707 global_i +=
Index(uiarray[9]);
709 Pack::Type compression_type_elements = Pack::deduct_type<DT2_>();
710 Pack::Type compression_type_index = Pack::deduct_type<IT2_>();
713 compression_type_elements = compression_type_elements | Pack::Type::Mask_P;
717 compression_type_elements = compression_type_elements | Pack::Type::Mask_Z;
721 compression_type_index = compression_type_index | Pack::Type::Mask_Z;
726 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(
char) - 1u) /
sizeof(
char));
731 XABORTM(
"Cannot decode compressed elements array");
732 global_i += (
Index)elements_bytes[i];
734 global_i =
Index((global_i *
sizeof(
char) +
sizeof(DT2_) - 1u) /
sizeof(DT2_));
738 for (
Index i(0) ; i <
Index(uiarray[4]) ; ++i)
748 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(
char) - 1u) /
sizeof(
char));
749 for (
Index i(0) ; i <
Index(uiarray[5]) ; ++i)
753 XABORTM(
"Cannot decode compressed indice array");
754 global_i += (
Index)indices_bytes[i];
759 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(IT2_) - 1u) /
sizeof(IT2_));
760 for (
Index i(0) ; i <
Index(uiarray[5]) ; ++i)
779 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
783 file.read((
char *)&tsize, (
long)(
sizeof(std::uint64_t)));
784 std::vector<char> temp((
size_t(tsize)));
785 file.seekg(-(
long)
sizeof(std::uint64_t), file.cur);
786 file.read(temp.data(), (
long)(tsize));
788 XABORTM(
"Error in _deserialize - file istream is not good anymore!");
789 this->
template _deserialize<DT2_, IT2_>(mode, temp);
838 other._elements.clear();
839 other._indices.clear();
840 other._elements_size.clear();
841 other._indices_size.clear();
842 other._scalar_index.clear();
843 other._scalar_dt.clear();
885 this->_elements.clear();
886 this->_indices.clear();
887 this->_elements_size.clear();
888 this->_indices_size.clear();
889 this->_scalar_index.clear();
890 this->_scalar_dt.clear();
891 this->_foreign_memory =
false;
906 XABORTM(
"Trying to self-clone a lafem container!");
909 XASSERTM(other._foreign_memory ==
false || clone_mode ==
CloneMode::Deep,
"Must use deep cloning with ranged based source containers");
913 this->_scalar_index.assign(other._scalar_index.begin(), other._scalar_index.end());
914 this->_scalar_dt.assign(other._scalar_dt.begin(), other._scalar_dt.end());
915 this->_elements_size.assign(other._elements_size.begin(), other._elements_size.end());
916 this->_indices_size.assign(other._indices_size.begin(), other._indices_size.end());
921 for (
Index i(0) ; i < other._indices.size() ; ++i)
923 this->_indices.push_back(MemoryPool::template allocate_memory<IT_>(this->_indices_size.at(i)));
925 MemoryPool::template copy<IT_>(this->_indices.at(i), other._indices.at(i), this->_indices_size.at(i));
928 for (
Index i(0) ; i < other._elements.size() ; ++i)
930 this->_elements.push_back(MemoryPool::template allocate_memory<DT_>(this->_elements_size.at(i)));
932 MemoryPool::template copy<DT_>(this->_elements.at(i), other._elements.at(i), this->_elements_size.at(i));
939 this->_indices.assign(other._indices.begin(), other._indices.end());
940 for (
Index i(0) ; i < this->_indices.size() ; ++i)
946 this->_elements.assign(other._elements.begin(), other._elements.end());
947 for (
Index i(0) ; i < this->_elements.size() ; ++i)
954 for (
Index i(0) ; i < other._elements.size() ; ++i)
956 this->_elements.push_back(MemoryPool::template allocate_memory<DT_>(this->_elements_size.at(i)));
963 for (
Index i(0) ; i < other._elements.size() ; ++i)
965 this->_elements.push_back(MemoryPool::template allocate_memory<DT_>(this->_elements_size.at(i)));
966 MemoryPool::template copy<DT_>(this->_elements.at(i), other._elements.at(i), this->_elements_size.at(i));
974 template <
typename DT2_,
typename IT2_>
979 clone(t, clone_mode);
994 if (!this->_foreign_memory)
996 for (
Index i(0) ; i < this->_elements.size() ; ++i)
998 for (
Index i(0) ; i < this->_indices.size() ; ++i)
1002 this->_elements = std::move(other._elements);
1003 this->_indices = std::move(other._indices);
1004 this->_elements_size = std::move(other._elements_size);
1005 this->_indices_size = std::move(other._indices_size);
1006 this->_scalar_index = std::move(other._scalar_index);
1007 this->_scalar_dt = std::move(other._scalar_dt);
1009 other._elements.clear();
1010 other._indices.clear();
1011 other._elements_size.clear();
1012 other._indices_size.clear();
1014 this->_foreign_memory = other._foreign_memory;
1020 return this->
template _serialized_size<>(config);
1033 data.insert(std::end(data), std::begin(buffer), std::end(buffer));
1034 return std::uint64_t(buffer.size());
1044 std::size_t tbytes(0);
1057 tbytes += std::size_t(
_scalar_dt.size() *
sizeof(DT_));
1135 template <Perspective = Perspective::native>
1154 template <Perspective = Perspective::native>
1157 return this->
size();
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERT(expr)
Assertion macro definition.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
void _deserialize(FileMode mode, std::istream &file)
Deserialization of complete container entity with possible decompression.
Container(Index size_in)
Constructor.
const std::vector< IT_ * > & get_indices() const
Returns a list of all Index arrays.
std::size_t bytes() const
Returns the total amount of bytes allocated.
Container(Container &&other)
Move Constructor.
bool _foreign_memory
do we use memory that we did not allocate, nor are we allowed to free it - this mostly holds true,...
std::vector< char > _serialize(FileMode mode, const SerialConfig &config=SerialConfig()) const
Serialization of complete container entity(with options of lossless and lossy compression of data arr...
std::vector< DT_ * > _elements
List of pointers to all datatype dependent arrays.
Index used_elements() const
Returns the number of effective stored elements.
const std::vector< DT_ > & get_scalar_dt() const
Returns a list of all scalar values with datatype dt.
const std::vector< Index > & get_indices_size() const
Returns a list of all Index array sizes.
bool empty() const
Checks whether the container is empty.
const std::vector< Index > & get_elements_size() const
Returns a list of all data array sizes.
std::uint64_t get_checkpoint_size(const LAFEM::SerialConfig &config)
Calculate size.
const std::vector< DT_ * > & get_elements() const
Returns a list of all data arrays.
std::vector< Index > _elements_size
List of corresponding datatype array sizes.
std::uint64_t _serialized_size(const LAFEM::SerialConfig &config=LAFEM::SerialConfig()) const
Calculation of the serialized size with optional compression of complete container entity.
Index size() const
Returns the containers size.
void clone(const Container< DT2_, IT2_ > &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
void assign(const Container< DT2_, IT2_ > &other)
Assignment operation.
static String name()
Returns a descriptive string.
void restore_from_checkpoint_data(std::vector< char > &data)
Extract object from checkpoint.
std::vector< IT_ * > _indices
List of pointers to all IT_ dependent arrays.
std::uint64_t set_checkpoint_data(std::vector< char > &data, const LAFEM::SerialConfig &config)
void format(Random &rng, DT_ min, DT_ max)
Reset all elements of the container to random values.
virtual ~Container()
Destructor.
void clone(const Container &other, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
const std::vector< Index > & get_scalar_index() const
Returns a list of all scalar values with datatype index.
void move(Container &&other)
Assignment move operation.
virtual void clear()
Free all allocated arrays.
void _serialize(FileMode mode, std::ostream &file, const SerialConfig &config=SerialConfig()) const
Serialization of complete container entity(with options of lossless and lossy compression of data arr...
std::vector< Index > _indices_size
List of corresponding IT_ array sizes.
std::vector< DT_ > _scalar_dt
List of scalars with datatype DT_.
void _deserialize(FileMode mode, std::vector< char > &input)
Deserialization of complete container entity with possible decompression.
void format(DT_ value=DT_(0))
Reset all elements of the container to a given value or zero if missing.
std::vector< Index > _scalar_index
List of scalars with datatype index.
Config class for serialize parameter.
void set_tolerance(FEAT::Real tol)
method for setting tolerance
SerialConfig()
Default Constructor.
CompressionModes get_elements_compression() const
method for getting elements_compression
FEAT::Real get_tolerance() const
method for getting tolerance
SerialConfig(bool zlib_compression, bool zfp_compression, FEAT::Real tol=FEAT::Real(-1.))
Constructor.
void set_indices_compression(CompressionModes comp)
method for setting indices_compression
void set_elements_compression(CompressionModes comp)
method for setting elements_compression
CompressionModes get_indices_compression() const
method for getting indices_compression
static void copy(DT_ *dest, const DT_ *src, const Index count)
Copy memory area from src to dest.
static void convert(DT_ *dest, const DT_ *src, const Index count)
Copy memory area from src to dest.
static void set_memory(DT_ *address, const DT_ val, const Index count=1)
set memory to specific value
static void release_memory(void *address)
release memory or decrease reference counter
static void increase_memory(void *address)
increase memory counter
Pseudo-Random Number Generator.
String class implementation.
LAFEM common type definitions.
std::size_t estimate_size(const std::size_t count, const Pack::Type type, const double tolerance)
Computes the estimated (upper bound) pack buffer size for an array.
std::size_t decode(T_ *dst, void *buf, const std::size_t count, const std::size_t buf_size, const Pack::Type pack_type, bool swap_bytes)
Decodes an array from a packed buffer.
double Real
Real data type.
@ value
specifies whether the space should supply basis function values
std::uint64_t Index
Index data type.
static bool extract_signedness(uint64_t feature_hash)
extracts sign feature from a given types feature hash
static bool extract_intness(uint64_t feature_hash)
extracts integral feature from a given types feature hash
static bool extract_floatness(uint64_t feature_hash)
extracts floating point feature from a given types feature hash
static size_t extract_type_size(uint64_t feature_hash)
extracts sizeof datatype from a given types feature hash