10#include <kernel/util/memory_arbiter.hpp>
12#include <kernel/util/type_traits.hpp>
13#include <kernel/util/random.hpp>
14#include <kernel/util/pack.hpp>
118 XABORTM(
"Zlib compression was enabled for elements, but zlib bibliography was not loaded. Please configure FEAT with -zlib flag!");
122 XASSERTM(tolerance >
FEAT::Real(0.),
"tolerance ist smaller or equall 0 while zfp is enabled!");
125 XABORTM(
"Zfp compression was enabled for elements, but zfp bibliography was not loaded. Please configure FEAT with -zfp flag!");
128 XABORTM(
"No legal CompressionModes was given");
130 elements_compression = temp;
149 XABORTM(
"Zlib compression was enabled for indices, but zlib bibliography was not loaded. Please configure FEAT with -zlib flag!");
152 XABORTM(
"No legal CompressionModes was given");
154 indices_compression = temp;
167 XASSERTM(tol >
FEAT::Real(0.),
"Zfp enabled, but tolerance is smaller or equal to 0!");
179 return elements_compression;
190 return indices_compression;
222 template <
typename DT_,
typename IT_>
225 template <
typename DT2_,
typename IT2_>
245 void _copy_content(
const Container & other,
bool full)
251 XASSERTM(
_elements.size() == other._elements.size(),
"Container size mismatch!");
252 XASSERTM(
_indices.size() == other._indices.size(),
"Container size mismatch!");
258 this->_scalar_index.assign(other._scalar_index.begin(), other._scalar_index.end());
259 this->_scalar_dt.assign(other._scalar_dt.begin(), other._scalar_dt.end());
264 _indices.at(i).copy(other._indices.at(i));
271 _elements.at(i).copy(other._elements.at(i));
284 template <
typename DT2_,
typename IT2_>
288 this->_elements.clear();
289 this->_indices.clear();
290 this->_elements_size.clear();
291 this->_indices_size.clear();
292 this->_scalar_index.clear();
293 this->_scalar_dt.clear();
295 this->_elements_size.assign(other.get_elements_size().begin(), other.get_elements_size().end());
296 this->_indices_size.assign(other.get_indices_size().begin(), other.get_indices_size().end());
297 this->_scalar_index.assign(other.get_scalar_index().begin(), other.get_scalar_index().end());
298 if constexpr (std::is_same<DT_, DT2_>::value)
300 this->_scalar_dt.assign(other.get_scalar_dt().begin(), other.get_scalar_dt().end());
304 for(
auto t : other.get_scalar_dt())
306 this->_scalar_dt.push_back(DT_(t));
310 this->_elements.resize(other._elements.size());
312 if constexpr (std::is_same<DT_, DT2_>::value)
314 for (
Index i(0) ; i < this->_elements.size() ; ++i)
315 this->_elements.at(i).attach(other._elements.at(i));
319 for (
Index i(0) ; i < this->_elements_size.size() ; ++i)
321 this->_elements.at(i) =
Memory::Arbiter(
sizeof(DT_) * this->_elements_size.at(i));
322 this->_elements.at(i).template convert<DT_, DT2_>(other._elements.at(i));
326 this->_indices.resize(other._indices.size());
328 if constexpr (std::is_same<IT_, IT2_>::value)
330 for (
Index i(0) ; i < this->_indices.size() ; ++i)
331 this->_indices.at(i).attach(other._indices.at(i));
335 for (
Index i(0) ; i < this->_indices_size.size() ; ++i)
337 this->_indices.at(i) =
Memory::Arbiter(
sizeof(IT_) * this->_indices_size.at(i));
338 this->_indices.at(i).template convert<IT_, IT2_>(other._indices.at(i));
352 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
358 std::uint64_t gsize(4 *
sizeof(std::uint64_t));
359 gsize += 7 *
sizeof(std::uint64_t);
361 gsize += 2 * tc.
_indices_size.size() *
sizeof(std::uint64_t);
365 CompressionModes compress = config.get_elements_compression() | config.get_indices_compression();
366 Pack::Type compression_type_elements = Pack::deduct_type<DT2_>();
367 Pack::Type compression_type_index = Pack::deduct_type<IT2_>();
370 compression_type_elements = compression_type_elements | Pack::Type::Mask_P;
374 compression_type_elements = compression_type_elements | Pack::Type::Mask_Z;
377 compression_type_index = compression_type_index | Pack::Type::Mask_Z;
380 FEAT::Real tolerance = config.get_tolerance();
435 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
438 std::uint64_t raw_size = 0u;
439 FEAT::Real tolerance = config.get_tolerance();
441 CompressionModes compress = config.get_elements_compression() | config.get_indices_compression();
442 Pack::Type compression_type_elements = Pack::deduct_type<DT2_>();
443 Pack::Type compression_type_index = Pack::deduct_type<IT2_>();
446 compression_type_elements = compression_type_elements | Pack::Type::Mask_P;
450 compression_type_elements = compression_type_elements | Pack::Type::Mask_Z;
453 compression_type_index = compression_type_index | Pack::Type::Mask_Z;
458 std::uint64_t gsize = this->
template _serialized_size<DT2_, IT2_>(config);
460 std::vector<char> result((
size_t(gsize)));
461 char * array(result.data());
462 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
463 DT2_ * dtarray(
reinterpret_cast<DT2_ *
>(array));
464 IT2_ * itarray(
reinterpret_cast<IT2_ *
>(array));
467 #if defined(FEAT_COMPILER_CLANG)
468 std::uint64_t magic = (std::uint64_t)
static_cast<__underlying_type(
FileMode)
>(mode);
470 std::uint64_t magic = (std::uint64_t)
static_cast<typename std::underlying_type<FileMode>::type
>(mode);
482 uiarray[10] = (std::uint64_t)compress;
483 raw_size += 11 * (std::uint64_t)
sizeof(std::uint64_t);
486 Index local_elements(0);
487 Index local_index(0);
494 local_elements = global_i;
497 uiarray[i + global_i] = (std::uint64_t)0u;
500 raw_size += 2 * std::uint64_t(tc.
_elements_size.size()) * std::uint64_t(
sizeof(std::uint64_t));
507 local_index = global_i;
510 uiarray[i + global_i] = 0;
513 raw_size += 2 * std::uint64_t(tc.
_indices_size.size()) * std::uint64_t(
sizeof(std::uint64_t));
520 raw_size += std::uint64_t(tc.
_scalar_index.size()) * std::uint64_t(
sizeof(std::uint64_t));
522 global_i =
Index((global_i *
sizeof(std::uint64_t) +
sizeof(DT2_) - 1u) /
sizeof(DT2_));
529 raw_size += std::uint64_t(tc.
_scalar_dt.size()) * std::uint64_t(
sizeof(DT2_));
533 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(
char) - 1u) /
sizeof(
char));
537 char* dest = &result[global_i];
539 std::size_t real_size = Pack::encode<DT2_>(dest, v.
get_r(), est_buff_size, tc.
_elements_size.at(i), compression_type_elements,
false, (
double)tolerance);
540 XASSERTM(real_size != 0u,
"could not encode elements");
541 uiarray[i + local_elements] = (std::uint64_t)real_size;
542 global_i += (
Index)real_size;
543 raw_size += (std::uint64_t)real_size * (std::uint64_t)
sizeof(char);
545 global_i =
Index((global_i *
sizeof(
char) +
sizeof(DT2_) - 1u) /
sizeof(DT2_));
553 uiarray[i + local_elements] = (std::uint64_t) tc.
_elements_size.at(i) *
sizeof(DT2_);
555 raw_size += (std::uint64_t) tc.
_elements_size.at(i) *
sizeof(DT2_);
560 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(
char) - 1u) /
sizeof(
char));
564 char* dest = &result[global_i];
566 std::size_t real_size = Pack::encode<IT2_>(dest, v.
get_r(), est_buff_size, tc.
_indices_size.at(i), compression_type_index,
false);
567 XASSERTM(real_size != 0u,
"could not encode indices");
568 uiarray[i + local_index] = (std::uint64_t) real_size;
569 global_i +=
Index(real_size);
570 raw_size += std::uint64_t(real_size) * std::uint64_t(
sizeof(
char));
575 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(IT2_) - 1u) /
sizeof(IT2_));
580 uiarray[i + local_index] = (std::uint64_t) tc.
_indices_size.at(i) *
sizeof(IT2_);
582 raw_size += (std::uint64_t) tc.
_indices_size.at(i) *
sizeof(IT2_);
585 uiarray[0] = raw_size + 16u;
588 result.resize(std::size_t(uiarray[0]));
604 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
607 auto temp(this->
template _serialize<DT2_, IT2_>(mode, config));
608 file.write(temp.data(),
long(temp.size()));
610 XABORTM(
"Error in _serialize - file ostream is not good anymore!");
621 template <
typename DT2_ = DT_,
typename IT2_ = IT_>
628 char * array(input.data());
629 std::uint64_t * uiarray(
reinterpret_cast<std::uint64_t *
>(array));
630 DT2_ * dtarray(
reinterpret_cast<DT2_ *
>(array));
631 IT2_ * itarray(
reinterpret_cast<IT2_ *
>(array));
634#if defined(FEAT_COMPILER_CLANG)
635 std::uint64_t magic = (std::uint64_t)
static_cast<__underlying_type(
FileMode)
>(mode);
637 std::uint64_t magic = (std::uint64_t)
static_cast<typename std::underlying_type<FileMode>::type
>(mode);
639 XASSERTM(magic == uiarray[1],
"_deserialize: given FileMode incompatible with given array!");
650 std::cerr<<
"Warning: You are reading a container floating point in higher precision then it was saved before!\n";
653 std::cerr<<
"Warning: You are reading a container integral type in higher precision then it was saved before!\n";
657#ifndef FEAT_HAVE_ZLIB
659 "Data was compressed with ZLIB! To read in data, please configure FEAT with zlib enabled.\n For more information see FEAT documentation.");
661 "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 zfp! To read in data, please configure FEAT with zfp enabled.\n For more information see FEAT documentation.");
667 std::vector<std::uint64_t> elements_bytes(uiarray[6]);
668 std::vector<std::uint64_t> indices_bytes(uiarray[7]);
670 for (std::uint64_t i(0) ; i < uiarray[6] ; ++i)
674 global_i +=
Index(uiarray[6]);
675 for (std::uint64_t i(0) ; i < uiarray[6] ; ++i)
677 elements_bytes[i] = uiarray[i + global_i];
679 global_i +=
Index(uiarray[6]);
681 for (std::uint64_t i(0) ; i < uiarray[7] ; ++i)
685 global_i +=
Index(uiarray[7]);
686 for (std::uint64_t i(0) ; i < uiarray[7] ; ++i)
688 indices_bytes[i] = uiarray[i + global_i];
690 global_i +=
Index(uiarray[7]);
692 for (std::uint64_t i(0) ; i < uiarray[8] ; ++i)
696 global_i +=
Index(uiarray[8]);
698 global_i =
Index((global_i *
sizeof(std::uint64_t) +
sizeof(DT2_) - 1u) /
sizeof(DT2_));
699 for (std::uint64_t i(0) ; i < uiarray[9] ; ++i)
701 tc.
_scalar_dt.push_back(dtarray[i + global_i]);
703 global_i +=
Index(uiarray[9]);
705 Pack::Type compression_type_elements = Pack::deduct_type<DT2_>();
706 Pack::Type compression_type_index = Pack::deduct_type<IT2_>();
709 compression_type_elements = compression_type_elements | Pack::Type::Mask_P;
713 compression_type_elements = compression_type_elements | Pack::Type::Mask_Z;
717 compression_type_index = compression_type_index | Pack::Type::Mask_Z;
722 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(
char) - 1u) /
sizeof(
char));
727 if(0u ==
Pack::decode(v.
get_w(), &array[global_i], (std::size_t) tc.
_elements_size.at(i), (std::size_t) elements_bytes[i], compression_type_elements,
false))
728 XABORTM(
"Cannot decode compressed elements array");
729 global_i += (
Index)elements_bytes[i];
731 global_i =
Index((global_i *
sizeof(
char) +
sizeof(DT2_) - 1u) /
sizeof(DT2_));
735 for (
Index i(0) ; i <
Index(uiarray[4]) ; ++i)
746 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(
char) - 1u) /
sizeof(
char));
747 for (
Index i(0) ; i <
Index(uiarray[5]) ; ++i)
751 if(0u ==
Pack::decode(v.
get_w(), &array[global_i], (std::size_t) tc.
_indices_size.at(i), (std::size_t) indices_bytes[i], compression_type_index,
false))
752 XABORTM(
"Cannot decode compressed indice array");
753 global_i += (
Index)indices_bytes[i];
758 global_i =
Index((global_i *
sizeof(DT2_) +
sizeof(IT2_) - 1u) /
sizeof(IT2_));
759 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);
818 other._elements.clear();
819 other._indices.clear();
820 other._elements_size.clear();
821 other._indices_size.clear();
822 other._scalar_index.clear();
823 other._scalar_dt.clear();
862 v[j] = rng(min, max);
866 void init_workspace()
const
888 this->_elements.clear();
889 this->_indices.clear();
890 this->_elements_size.clear();
891 this->_indices_size.clear();
892 this->_scalar_index.clear();
893 this->_scalar_dt.clear();
908 XABORTM(
"Trying to self-clone a lafem container!");
918 this->_indices.resize(source.
_indices.size());
919 this->_elements.resize(source.
_elements.size());
922 int what_indices = 0;
923 int what_elements = 0;
953 if(what_indices == 1)
956 this->_indices.at(i).attach(source.
_indices.at(i));
961 this->_indices.at(i) =
Memory::Arbiter(this->_indices_size.at(i) *
sizeof(IT_));
965 if(what_indices == 2)
968 this->_indices.at(i).copy(source.
_indices.at(i));
972 if(what_elements == 1)
975 this->_elements.at(i).attach(source.
_elements.at(i));
980 this->_elements.at(i) =
Memory::Arbiter(this->_elements_size.at(i) *
sizeof(DT_));
984 if(what_elements == 2)
987 this->_elements.at(i).copy(source.
_elements.at(i));
992 template <
typename DT2_,
typename IT2_>
997 this->
clone(t, clone_mode);
1009 if (
this == &source)
1012 this->_elements = std::move(source._elements);
1013 this->_indices = std::move(source._indices);
1014 this->_elements_size = std::move(source._elements_size);
1015 this->_indices_size = std::move(source._indices_size);
1016 this->_scalar_index = std::move(source._scalar_index);
1017 this->_scalar_dt = std::move(source._scalar_dt);
1018 this->_backend_workspaces = std::move(source._backend_workspaces);
1020 source._elements.clear();
1021 source._indices.clear();
1022 source._elements_size.clear();
1023 source._indices_size.clear();
1029 return this->
template _serialized_size<>(config);
1042 data.insert(std::end(data), std::begin(buffer), std::end(buffer));
1043 return std::uint64_t(buffer.size());
1053 std::size_t tbytes(0);
1066 tbytes += std::size_t(
_scalar_dt.size() *
sizeof(DT_));
#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.
static BackendIdx backend_to_index(PreferredBackend backend)
Helper function for idx mapping.
static PreferredBackend get_preferred_backend()
get current preferred backend
static void warn_missing(const char *operation, PreferredBackend preferred_backend, PreferredBackend fallback_backend=PreferredBackend::generic)
issues a warning about unsupported backend usage
void _deserialize(FileMode mode, std::istream &file)
Deserialization of complete container entity with possible decompression.
std::size_t bytes() const
Returns the total amount of bytes allocated.
Container(Container &&other)
Move Constructor.
virtual ~Container()=default
Destructor.
void reset_workspace() const
Resets the workspace allocation.
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::array< std::shared_ptr< void >, to_underlying(BackendIdx::num_backends)> _backend_workspaces
List of backend workspaces.
void format()
Reset all elements of the container to a given value or zero if missing.
void clone(const Container< DT2_, IT2_ > &source, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
const std::vector< DT_ > & get_scalar_dt() const
Returns a list of all scalar values with datatype dt.
void move(Container &&source)
Assignment move operation.
const std::vector< Index > & get_indices_size() const
Returns a list of all Index array sizes.
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.
void clone(const Container &source, CloneMode clone_mode=CloneMode::Weak)
Clone operation.
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.
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::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.
const std::vector< Index > & get_scalar_index() const
Returns a list of all scalar values with datatype index.
std::vector< Memory::Arbiter > _indices
List of pointers to all IT_ dependent arrays.
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_.
std::vector< Memory::Arbiter > _elements
List of pointers to all datatype dependent arrays.
void _deserialize(FileMode mode, std::vector< char > &input)
Deserialization of complete container entity with possible decompression.
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
Index size() const
Returns the number of array elements mapped to this view.
const T_ * get_r() const
Returns a const typed pointer to the array that grants read access.
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
void copy_raw_from(const void *src_ptr, Location src_loc)
Copies the contents of a given memory region into the memory of this memory view.
const void * raw_r() const
Returns a const raw pointer to the memory block that grants read access.
Pseudo-Random Number Generator.
String class implementation.
LAFEM common type definitions.
void memcopy_main(void *dst, const void *src, std::size_t bytes)
Copies a region of a main memory block into another main memory block.
@ write
write-only access (see attention note above)
Location
Memory location enumeration.
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.
PreferredBackend
The backend that shall be used in all compute heavy calculations.
double Real
Real data type.
@ value
specifies whether the space should supply basis function values
constexpr auto to_underlying(E e) noexcept
Cast enum to underlying type.
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