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."));
1316 object.resize(vector_size);
1317 if (vector_size > 0)
1318 std::memcpy(
object.data(),
1319 &*cbegin +
sizeof(vector_size),
1320 vector_size *
sizeof(T));
1325 template <
typename T,
1326 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1327 std::is_trivially_copyable_v<T>>>
1330 const std::vector<char>::const_iterator &cbegin,
1331 const std::vector<char>::const_iterator &cend,
1332 std::vector<std::vector<T>> &
object)
1335 using size_type =
typename std::vector<T>::size_type;
1336 std::vector<char>::const_iterator iterator = cbegin;
1337 size_type vector_size;
1338 memcpy(&vector_size, &*iterator,
sizeof(vector_size));
1340 object.resize(vector_size);
1341 std::vector<size_type> sizes(vector_size);
1342 if (vector_size > 0)
1343 std::memcpy(sizes.data(),
1344 &*iterator +
sizeof(vector_size),
1345 vector_size *
sizeof(size_type));
1347 iterator +=
sizeof(vector_size) * (1 + vector_size);
1348 size_type aggregated_size = 0;
1349 for (
const auto a : sizes)
1350 aggregated_size += a;
1352 Assert(
static_cast<std::ptrdiff_t
>(cend - iterator) ==
1353 static_cast<std::ptrdiff_t
>(aggregated_size *
sizeof(T)),
1354 ExcMessage(
"The given buffer has the wrong size."));
1360 for (
unsigned int i = 0; i < vector_size; ++i)
1363 object[i].resize(sizes[i]);
1364 std::memcpy(
object[i].data(), &*iterator, sizes[i] *
sizeof(T));
1365 iterator += sizes[i] *
sizeof(T);
1369 ExcMessage(
"The given buffer has the wrong size."));
1376 template <
typename T>
1379 std::vector<char> &dest_buffer,
1380 const bool allow_compression)
1382 std::size_t size = 0;
1388 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1396 size = (std::is_same_v<T, std::tuple<>> ? 0 :
sizeof(T));
1398 (void)allow_compression;
1399 const std::size_t previous_size = dest_buffer.size();
1400 dest_buffer.resize(previous_size + size);
1403 std::memcpy(dest_buffer.data() + previous_size, &
object, size);
1411 (allow_compression ==
false))
1413 const std::size_t previous_size = dest_buffer.size();
1422 size = dest_buffer.size() - previous_size;
1428 const std::size_t previous_size = dest_buffer.size();
1430 boost::iostreams::filtering_ostreambuf fosb;
1431#ifdef DEAL_II_WITH_ZLIB
1432 if (allow_compression)
1433 fosb.push(boost::iostreams::gzip_compressor());
1435 (void)allow_compression;
1437 fosb.push(boost::iostreams::back_inserter(dest_buffer));
1439 boost::archive::binary_oarchive boa(fosb);
1444 size = dest_buffer.size() - previous_size;
1451 template <
typename T>
1453 pack(
const T &
object,
const bool allow_compression)
1455 std::vector<char> buffer;
1456 pack<T>(
object, buffer, allow_compression);
1462 template <
typename T>
1464 unpack(
const std::vector<char>::const_iterator &cbegin,
1465 const std::vector<char>::const_iterator &cend,
1466 const bool allow_compression)
1471 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1479 const std::size_t size =
1480 (std::is_same_v<T, std::tuple<>> ? 0 :
sizeof(T));
1484 (void)allow_compression;
1488 std::memcpy(&
object, &*cbegin, size);
1498 (allow_compression ==
false))
1513 boost::iostreams::filtering_istreambuf fisb;
1514#ifdef DEAL_II_WITH_ZLIB
1515 if (allow_compression)
1516 fisb.push(boost::iostreams::gzip_decompressor());
1518 (void)allow_compression;
1520 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1522 boost::archive::binary_iarchive bia(fisb);
1533 template <
typename T>
1535 unpack(
const std::vector<char> &buffer,
const bool allow_compression)
1537 return unpack<T>(buffer.cbegin(), buffer.cend(), allow_compression);
1541 template <
typename T,
int N>
1543 unpack(
const std::vector<char>::const_iterator &cbegin,
1544 const std::vector<char>::const_iterator &cend,
1545 T (&unpacked_object)[N],
1546 const bool allow_compression)
1551 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) * N < 256)
1553 Assert(std::distance(cbegin, cend) ==
sizeof(T) * N,
1555 std::memcpy(unpacked_object, &*cbegin,
sizeof(T) * N);
1560 boost::iostreams::filtering_istreambuf fisb;
1561#ifdef DEAL_II_WITH_ZLIB
1562 if (allow_compression)
1563 fisb.push(boost::iostreams::gzip_decompressor());
1565 (void)allow_compression;
1567 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1569 boost::archive::binary_iarchive bia(fisb);
1570 bia >> unpacked_object;
1575 template <
typename T,
int N>
1578 T (&unpacked_object)[N],
1579 const bool allow_compression)
1581 unpack<T, N>(buffer.cbegin(),
1590 get_bit(
const unsigned char number,
const unsigned int n)
1597 return ((number >> n) & 1U) != 0u;
1603 set_bit(
unsigned char &number,
const unsigned int n,
const bool x)
1610 number ^= (-
static_cast<unsigned char>(x) ^ number) & (1U << n);
1615 template <
typename To,
typename From>
1616 inline std::unique_ptr<To>
1622 if (To *cast =
dynamic_cast<To *
>(p.get()))
1624 std::unique_ptr<To> result(cast);
1629 throw std::bad_cast();
1634 template <
typename T>
1643 template <
typename T>
1652 template <
typename T>
1661 template <
typename T>
1670 template <
typename T>
1679 template <
typename Integer>
1680 std::vector<Integer>
1683 const std::size_t n = permutation.size();
1685 std::vector<Integer> out(n);
1686 for (std::size_t i = 0; i < n; ++i)
1687 out[i] = n - 1 - permutation[i];
1694 template <
typename Integer>
1695 std::vector<Integer>
1698 const std::size_t n = permutation.size();
1702 for (std::size_t i = 0; i < n; ++i)
1705 out[permutation[i]] = i;
1710 for (std::size_t i = 0; i < n; ++i)
1712 ExcMessage(
"The given input permutation had duplicate entries!"));
1724 namespace serialization
1731 template <
class Archive,
typename... Args>
1733 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1735 ar &std::get<N - 1>(t);
1736 Serialize<N - 1>::serialize(ar, t, version);
1743 template <
class Archive,
typename... Args>
1745 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1753 template <
class Archive,
typename... Args>
1755 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1757 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