15#ifndef dealii_utilities_h
16#define dealii_utilities_h
22#include <boost/archive/binary_iarchive.hpp>
23#include <boost/archive/binary_oarchive.hpp>
24#include <boost/core/demangle.hpp>
25#include <boost/iostreams/device/array.hpp>
26#include <boost/iostreams/device/back_inserter.hpp>
27#include <boost/iostreams/filtering_streambuf.hpp>
28#include <boost/serialization/array.hpp>
29#include <boost/serialization/complex.hpp>
30#include <boost/serialization/vector.hpp>
41#ifdef DEAL_II_WITH_ZLIB
42# include <boost/iostreams/filter/gzip.hpp>
49template <
int dim,
typename Number>
88 template <
int dim,
typename Number>
89 std::vector<std::array<std::uint64_t, dim>>
92 const int bits_per_dim = 64);
98 std::vector<std::array<std::uint64_t, dim>>
100 const std::vector<std::array<std::uint64_t, dim>> &points,
101 const int bits_per_dim = 64);
121 const int bits_per_dim);
152 decompress(
const std::string &compressed_input);
168 encode_base64(
const std::vector<unsigned char> &binary_input);
178 std::vector<unsigned char>
217 template <
typename number>
237 template <
typename Number>
260 dim_string(
const int dim,
const int spacedim);
327 std::vector<std::string>
335 std::vector<std::string>
348 std::vector<std::string>
350 const unsigned int width,
351 const char delimiter =
' ');
368 std::pair<int, unsigned int>
377 const std::string &from,
378 const std::string &to);
386 trim(
const std::string &input);
452 template <
int N,
typename T>
463 template <
typename T,
typename = std::enable_if_t<std::is_arithmetic_v<T>>>
465 pow(
const T base,
const int iexp);
488 template <
typename Iterator,
typename T>
497 template <
typename Iterator,
typename T,
typename Comp>
506 template <
typename Integer>
515 template <
typename Integer>
573 template <
typename T>
575 pack(
const T &
object,
576 std::vector<char> &dest_buffer,
577 const bool allow_compression =
true);
587 template <
typename T>
589 pack(
const T &
object,
const bool allow_compression =
true);
622 template <
typename T>
624 unpack(
const std::vector<char> &buffer,
const bool allow_compression =
true);
634 template <
typename T>
636 unpack(
const std::vector<char>::const_iterator &cbegin,
637 const std::vector<char>::const_iterator &cend,
638 const bool allow_compression =
true);
672 template <
typename T,
int N>
674 unpack(
const std::vector<char> &buffer,
675 T (&unpacked_object)[N],
676 const bool allow_compression =
true);
686 template <
typename T,
int N>
688 unpack(
const std::vector<char>::const_iterator &cbegin,
689 const std::vector<char>::const_iterator &cend,
690 T (&unpacked_object)[N],
691 const bool allow_compression =
true);
697 get_bit(
const unsigned char number,
const unsigned int n);
704 set_bit(
unsigned char &number,
const unsigned int n,
const bool x);
764 template <
typename To,
typename From>
771 template <
typename T>
778 template <
typename T>
785 template <
typename T>
792 template <
typename T>
799 template <
typename T>
928 posix_memalign(
void **memptr, std::size_t alignment, std::size_t size);
937 template <
int N,
typename T>
941 Assert(((std::is_integral_v<T> ==
true) && (N >= 0)) ||
942 (std::is_integral_v<T> ==
false),
943 ExcMessage(
"If the type of the argument, T, is an integer type, "
944 "then the exponent N must be a non-negative integer "
945 "because the result would otherwise not be an integer."));
955 return ((N % 2 == 1) ? x *
fixed_power<N / 2>(x * x) :
961 template <
typename T,
typename>
963 pow(
const T base,
const int iexp)
965#if defined(DEBUG) && !defined(DEAL_II_CXX14_CONSTEXPR_BUG)
968# if KOKKOS_VERSION >= 30600
973 abort_or_throw_on_exception,
978 "ExcMessage(\"The exponent must not be negative!\")",
979 ExcMessage(
"The exponent must not be negative!"));
982# ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
986 abort_or_throw_on_exception,
991 "ExcMessage(\"The exponent must not be negative!\")",
992 ExcMessage(
"The exponent must not be negative!"));
1022 return boost::core::demangle(
typeid(t).name());
1027 template <
typename Iterator,
typename T>
1036 template <
typename Iterator,
typename T,
typename Comp>
1045 "The given iterators do not satisfy the proper ordering."));
1047 unsigned int len =
static_cast<unsigned int>(last -
first);
1064 if (!comp(*
first, val))
1069 if (!comp(*
first, val))
1074 if (!comp(*
first, val))
1079 if (!comp(*
first, val))
1084 if (!comp(*
first, val))
1089 if (!comp(*
first, val))
1094 if (!comp(*
first, val))
1113 const unsigned int half = len >> 1;
1114 const Iterator middle =
first + half;
1121 if (comp(*middle, val))
1141 template <
typename T>
1149 template <
typename T>
1153 std::is_trivially_copyable_v<T> && !std::is_same_v<T, bool>;
1158 template <
typename T>
1162 std::is_trivially_copyable_v<T> && !std::is_same_v<T, bool>;
1175 template <
typename T>
1178 std::vector<char> &)
1186 template <
typename T,
1187 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1188 std::is_trivially_copyable_v<T>>>
1191 const std::vector<T> &
object,
1192 std::vector<char> &dest_buffer)
1194 const typename std::vector<T>::size_type vector_size =
object.size();
1198 dest_buffer.reserve(dest_buffer.size() +
sizeof(vector_size) +
1199 vector_size *
sizeof(T));
1202 dest_buffer.insert(dest_buffer.end(),
1203 reinterpret_cast<const char *
>(&vector_size),
1204 reinterpret_cast<const char *
>(&vector_size + 1));
1207 if (vector_size > 0)
1208 dest_buffer.insert(dest_buffer.end(),
1209 reinterpret_cast<const char *
>(
object.data()),
1210 reinterpret_cast<const char *
>(
object.data() +
1216 template <
typename T,
1217 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1218 std::is_trivially_copyable_v<T>>>
1221 const std::vector<std::vector<T>> &
object,
1222 std::vector<char> &dest_buffer)
1224 using size_type =
typename std::vector<T>::size_type;
1225 const size_type vector_size =
object.size();
1227 typename std::vector<T>::size_type aggregated_size = 0;
1228 std::vector<size_type> sizes;
1229 sizes.reserve(vector_size);
1230 for (
const auto &a : object)
1232 aggregated_size += a.size();
1233 sizes.push_back(a.size());
1238 dest_buffer.reserve(dest_buffer.size() +
1239 sizeof(vector_size) * (1 + vector_size) +
1240 aggregated_size *
sizeof(T));
1243 dest_buffer.insert(dest_buffer.end(),
1244 reinterpret_cast<const char *
>(&vector_size),
1245 reinterpret_cast<const char *
>(&vector_size + 1));
1248 if (vector_size > 0)
1249 dest_buffer.insert(dest_buffer.end(),
1250 reinterpret_cast<const char *
>(sizes.data()),
1251 reinterpret_cast<const char *
>(sizes.data() +
1255 for (
const auto &a : object)
1256 dest_buffer.insert(dest_buffer.end(),
1257 reinterpret_cast<const char *
>(a.data()),
1258 reinterpret_cast<const char *
>(a.data() + a.size()));
1263 template <
typename T>
1266 const std::vector<char>::const_iterator &,
1267 const std::vector<char>::const_iterator &,
1276 template <
typename T,
1277 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1278 std::is_trivially_copyable_v<T>>>
1281 const std::vector<char>::const_iterator &cbegin,
1282 const std::vector<char>::const_iterator &cend,
1283 std::vector<T> &
object)
1289 typename std::vector<T>::size_type vector_size;
1290 memcpy(&vector_size, &*cbegin,
sizeof(vector_size));
1292 Assert(
static_cast<std::ptrdiff_t
>(cend - cbegin) ==
1293 static_cast<std::ptrdiff_t
>(
sizeof(vector_size) +
1294 vector_size *
sizeof(T)),
1295 ExcMessage(
"The given buffer has the wrong size."));
1300 if (vector_size > 0)
1302 const T *
const buffer_data_begin =
1303 reinterpret_cast<const T *
>(&*cbegin +
sizeof(vector_size));
1304 const T *
const buffer_data_end = buffer_data_begin + vector_size;
1305 object.insert(
object.end(), buffer_data_begin, buffer_data_end);
1311 template <
typename T,
1312 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1313 std::is_trivially_copyable_v<T>>>
1316 const std::vector<char>::const_iterator &cbegin,
1317 const std::vector<char>::const_iterator &cend,
1318 std::vector<std::vector<T>> &
object)
1321 using size_type =
typename std::vector<T>::size_type;
1322 std::vector<char>::const_iterator iterator = cbegin;
1323 size_type vector_size;
1324 memcpy(&vector_size, &*iterator,
sizeof(vector_size));
1326 object.resize(vector_size);
1327 std::vector<size_type> sizes(vector_size);
1328 if (vector_size > 0)
1329 memcpy(sizes.data(),
1330 &*iterator +
sizeof(vector_size),
1331 vector_size *
sizeof(size_type));
1333 iterator +=
sizeof(vector_size) * (1 + vector_size);
1334 size_type aggregated_size = 0;
1335 for (
const auto a : sizes)
1336 aggregated_size += a;
1338 Assert(
static_cast<std::ptrdiff_t
>(cend - iterator) ==
1339 static_cast<std::ptrdiff_t
>(aggregated_size *
sizeof(T)),
1340 ExcMessage(
"The given buffer has the wrong size."));
1344 for (
unsigned int i = 0; i < vector_size; ++i)
1347 object[i].insert(
object[i].end(),
1348 reinterpret_cast<const T *
>(&*iterator),
1349 reinterpret_cast<const T *
>(&*iterator +
1350 sizeof(T) * sizes[i]));
1351 iterator +=
sizeof(T) * sizes[i];
1355 ExcMessage(
"The given buffer has the wrong size."));
1362 template <
typename T>
1365 std::vector<char> &dest_buffer,
1366 const bool allow_compression)
1368 std::size_t size = 0;
1374 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1382 size = (std::is_same_v<T, std::tuple<>> ? 0 :
sizeof(T));
1384 (void)allow_compression;
1385 const std::size_t previous_size = dest_buffer.size();
1386 dest_buffer.resize(previous_size + size);
1389 std::memcpy(dest_buffer.data() + previous_size, &
object, size);
1397 (allow_compression ==
false))
1399 const std::size_t previous_size = dest_buffer.size();
1408 size = dest_buffer.size() - previous_size;
1414 const std::size_t previous_size = dest_buffer.size();
1416 boost::iostreams::filtering_ostreambuf fosb;
1417#ifdef DEAL_II_WITH_ZLIB
1418 if (allow_compression)
1419 fosb.push(boost::iostreams::gzip_compressor());
1421 (void)allow_compression;
1423 fosb.push(boost::iostreams::back_inserter(dest_buffer));
1425 boost::archive::binary_oarchive boa(fosb);
1430 size = dest_buffer.size() - previous_size;
1437 template <
typename T>
1439 pack(
const T &
object,
const bool allow_compression)
1441 std::vector<char> buffer;
1442 pack<T>(
object, buffer, allow_compression);
1448 template <
typename T>
1450 unpack(
const std::vector<char>::const_iterator &cbegin,
1451 const std::vector<char>::const_iterator &cend,
1452 const bool allow_compression)
1457 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1465 const std::size_t size =
1466 (std::is_same_v<T, std::tuple<>> ? 0 :
sizeof(T));
1470 (void)allow_compression;
1474 std::memcpy(&
object, &*cbegin, size);
1484 (allow_compression ==
false))
1499 boost::iostreams::filtering_istreambuf fisb;
1500#ifdef DEAL_II_WITH_ZLIB
1501 if (allow_compression)
1502 fisb.push(boost::iostreams::gzip_decompressor());
1504 (void)allow_compression;
1506 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1508 boost::archive::binary_iarchive bia(fisb);
1519 template <
typename T>
1521 unpack(
const std::vector<char> &buffer,
const bool allow_compression)
1523 return unpack<T>(buffer.cbegin(), buffer.cend(), allow_compression);
1527 template <
typename T,
int N>
1529 unpack(
const std::vector<char>::const_iterator &cbegin,
1530 const std::vector<char>::const_iterator &cend,
1531 T (&unpacked_object)[N],
1532 const bool allow_compression)
1537 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) * N < 256)
1539 Assert(std::distance(cbegin, cend) ==
sizeof(T) * N,
1541 std::memcpy(unpacked_object, &*cbegin,
sizeof(T) * N);
1546 boost::iostreams::filtering_istreambuf fisb;
1547#ifdef DEAL_II_WITH_ZLIB
1548 if (allow_compression)
1549 fisb.push(boost::iostreams::gzip_decompressor());
1551 (void)allow_compression;
1553 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1555 boost::archive::binary_iarchive bia(fisb);
1556 bia >> unpacked_object;
1561 template <
typename T,
int N>
1564 T (&unpacked_object)[N],
1565 const bool allow_compression)
1567 unpack<T, N>(buffer.cbegin(),
1576 get_bit(
const unsigned char number,
const unsigned int n)
1583 return ((number >> n) & 1U) != 0u;
1589 set_bit(
unsigned char &number,
const unsigned int n,
const bool x)
1596 number ^= (-
static_cast<unsigned char>(x) ^ number) & (1U << n);
1601 template <
typename To,
typename From>
1602 inline std::unique_ptr<To>
1608 if (To *cast =
dynamic_cast<To *
>(p.get()))
1610 std::unique_ptr<To> result(cast);
1615 throw std::bad_cast();
1620 template <
typename T>
1629 template <
typename T>
1638 template <
typename T>
1647 template <
typename T>
1656 template <
typename T>
1665 template <
typename Integer>
1666 std::vector<Integer>
1669 const std::size_t n = permutation.size();
1671 std::vector<Integer> out(n);
1672 for (std::size_t i = 0; i < n; ++i)
1673 out[i] = n - 1 - permutation[i];
1680 template <
typename Integer>
1681 std::vector<Integer>
1684 const std::size_t n = permutation.size();
1688 for (std::size_t i = 0; i < n; ++i)
1691 out[permutation[i]] = i;
1696 for (std::size_t i = 0; i < n; ++i)
1698 ExcMessage(
"The given input permutation had duplicate entries!"));
1710 namespace serialization
1717 template <
class Archive,
typename... Args>
1719 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1721 ar &std::get<N - 1>(t);
1722 Serialize<N - 1>::serialize(ar, t, version);
1729 template <
class Archive,
typename... Args>
1731 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1739 template <
class Archive,
typename... Args>
1741 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1743 Serialize<
sizeof...(Args)>::serialize(ar, t, version);
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_CXX20_REQUIRES(condition)
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_FALLTHROUGH
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define DEAL_II_ASSERT_UNREACHABLE()
void get_memory_stats(MemoryStats &stats)
std::string get_hostname()
void posix_memalign(void **memptr, std::size_t alignment, std::size_t size)
std::string get_current_vectorization_level()
void create_vector_of_trivially_copyable_from_buffer(const std::vector< char >::const_iterator &, const std::vector< char >::const_iterator &, T &)
void append_vector_of_trivially_copyable_to_buffer(const T &, std::vector< char > &)
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
T & get_underlying_value(T &p)
Number truncate_to_n_digits(const Number number, const unsigned int n_digits)
std::string type_to_string(const T &t)
size_t pack(const T &object, std::vector< char > &dest_buffer, const bool allow_compression=true)
std::string dim_string(const int dim, const int spacedim)
std::uint64_t pack_integers(const std::array< std::uint64_t, dim > &index, const int bits_per_dim)
bool get_bit(const unsigned char number, const unsigned int n)
std::pair< int, unsigned int > get_integer_at_position(const std::string &name, const unsigned int position)
std::string encode_base64(const std::vector< unsigned char > &binary_input)
std::unique_ptr< To > dynamic_unique_cast(std::unique_ptr< From > &&p)
constexpr T fixed_power(const T t)
std::string replace_in_string(const std::string &input, const std::string &from, const std::string &to)
std::vector< unsigned char > decode_base64(const std::string &base64_input)
std::vector< std::string > break_text_into_lines(const std::string &original_text, const unsigned int width, const char delimiter=' ')
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
std::vector< std::array< std::uint64_t, dim > > inverse_Hilbert_space_filling_curve(const std::vector< Point< dim, Number > > &points, const int bits_per_dim=64)
std::string compress(const std::string &input)
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
bool match_at_string_start(const std::string &name, const std::string &pattern)
T unpack(const std::vector< char > &buffer, const bool allow_compression=true)
std::string decompress(const std::string &compressed_input)
unsigned int needed_digits(const unsigned int max_number)
Iterator lower_bound(Iterator first, Iterator last, const T &val)
double string_to_double(const std::string &s)
std::string dealii_version_string()
void set_bit(unsigned char &number, const unsigned int n, const bool x)
constexpr T pow(const T base, const int iexp)
std::string trim(const std::string &input)
double generate_normal_random_number(const double a, const double sigma)
std::vector< Integer > reverse_permutation(const std::vector< Integer > &permutation)
std::vector< Integer > invert_permutation(const std::vector< Integer > &permutation)
int string_to_int(const std::string &s)
void issue_error_noreturn(ExceptionHandling handling, const char *file, int line, const char *function, const char *cond, const char *exc_name, ExceptionType e)
static const unsigned int invalid_unsigned_int
#define DEAL_II_HOST_DEVICE
static constexpr bool value