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);
455 template <
int N,
typename T>
466 template <
typename T,
typename = std::enable_if_t<std::is_arithmetic_v<T>>>
468 pow(
const T base,
const int iexp);
491 template <
typename Iterator,
typename T>
500 template <
typename Iterator,
typename T,
typename Comp>
509 template <
typename Integer>
518 template <
typename Integer>
576 template <
typename T>
578 pack(
const T &
object,
579 std::vector<char> &dest_buffer,
580 const bool allow_compression =
true);
590 template <
typename T>
592 pack(
const T &
object,
const bool allow_compression =
true);
625 template <
typename T>
627 unpack(
const std::vector<char> &buffer,
const bool allow_compression =
true);
637 template <
typename T>
639 unpack(
const std::vector<char>::const_iterator &cbegin,
640 const std::vector<char>::const_iterator &cend,
641 const bool allow_compression =
true);
675 template <
typename T,
int N>
677 unpack(
const std::vector<char> &buffer,
678 T (&unpacked_object)[N],
679 const bool allow_compression =
true);
689 template <
typename T,
int N>
691 unpack(
const std::vector<char>::const_iterator &cbegin,
692 const std::vector<char>::const_iterator &cend,
693 T (&unpacked_object)[N],
694 const bool allow_compression =
true);
700 get_bit(
const unsigned char number,
const unsigned int n);
707 set_bit(
unsigned char &number,
const unsigned int n,
const bool x);
767 template <
typename To,
typename From>
774 template <
typename T>
781 template <
typename T>
788 template <
typename T>
795 template <
typename T>
802 template <
typename T>
940 template <
int N,
typename T>
944 Assert(((std::is_integral_v<T> ==
true) && (N >= 0)) ||
945 (std::is_integral_v<T> ==
false),
946 ExcMessage(
"If the type of the argument, T, is an integer type, "
947 "then the exponent N must be a non-negative integer "
948 "because the result would otherwise not be an integer."));
958 return ((N % 2 == 1) ? x *
fixed_power<N / 2>(x * x) :
964 template <
typename T,
typename>
966 pow(
const T base,
const int iexp)
968#if defined(DEBUG) && !defined(DEAL_II_CXX14_CONSTEXPR_BUG)
971# if KOKKOS_VERSION >= 30600
976 abort_or_throw_on_exception,
981 "ExcMessage(\"The exponent must not be negative!\")",
982 ExcMessage(
"The exponent must not be negative!"));
985# ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
989 abort_or_throw_on_exception,
994 "ExcMessage(\"The exponent must not be negative!\")",
995 ExcMessage(
"The exponent must not be negative!"));
1025 return boost::core::demangle(
typeid(t).name());
1030 template <
typename Iterator,
typename T>
1039 template <
typename Iterator,
typename T,
typename Comp>
1048 "The given iterators do not satisfy the proper ordering."));
1050 unsigned int len =
static_cast<unsigned int>(last -
first);
1067 if (!comp(*
first, val))
1072 if (!comp(*
first, val))
1077 if (!comp(*
first, val))
1082 if (!comp(*
first, val))
1087 if (!comp(*
first, val))
1092 if (!comp(*
first, val))
1097 if (!comp(*
first, val))
1116 const unsigned int half = len >> 1;
1117 const Iterator middle =
first + half;
1124 if (comp(*middle, val))
1144 template <
typename T>
1152 template <
typename T>
1156 std::is_trivially_copyable_v<T> && !std::is_same_v<T, bool>;
1161 template <
typename T>
1165 std::is_trivially_copyable_v<T> && !std::is_same_v<T, bool>;
1178 template <
typename T>
1181 std::vector<char> &)
1189 template <
typename T,
1190 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1191 std::is_trivially_copyable_v<T>>>
1194 const std::vector<T> &
object,
1195 std::vector<char> &dest_buffer)
1197 const typename std::vector<T>::size_type vector_size =
object.size();
1201 dest_buffer.reserve(dest_buffer.size() +
sizeof(vector_size) +
1202 vector_size *
sizeof(T));
1205 dest_buffer.insert(dest_buffer.end(),
1206 reinterpret_cast<const char *
>(&vector_size),
1207 reinterpret_cast<const char *
>(&vector_size + 1));
1210 if (vector_size > 0)
1211 dest_buffer.insert(dest_buffer.end(),
1212 reinterpret_cast<const char *
>(
object.data()),
1213 reinterpret_cast<const char *
>(
object.data() +
1219 template <
typename T,
1220 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1221 std::is_trivially_copyable_v<T>>>
1224 const std::vector<std::vector<T>> &
object,
1225 std::vector<char> &dest_buffer)
1227 using size_type =
typename std::vector<T>::size_type;
1228 const size_type vector_size =
object.size();
1230 typename std::vector<T>::size_type aggregated_size = 0;
1231 std::vector<size_type> sizes;
1232 sizes.reserve(vector_size);
1233 for (
const auto &a : object)
1235 aggregated_size += a.size();
1236 sizes.push_back(a.size());
1241 dest_buffer.reserve(dest_buffer.size() +
1242 sizeof(vector_size) * (1 + vector_size) +
1243 aggregated_size *
sizeof(T));
1246 dest_buffer.insert(dest_buffer.end(),
1247 reinterpret_cast<const char *
>(&vector_size),
1248 reinterpret_cast<const char *
>(&vector_size + 1));
1251 if (vector_size > 0)
1252 dest_buffer.insert(dest_buffer.end(),
1253 reinterpret_cast<const char *
>(sizes.data()),
1254 reinterpret_cast<const char *
>(sizes.data() +
1258 for (
const auto &a : object)
1259 dest_buffer.insert(dest_buffer.end(),
1260 reinterpret_cast<const char *
>(a.data()),
1261 reinterpret_cast<const char *
>(a.data() + a.size()));
1266 template <
typename T>
1269 const std::vector<char>::const_iterator &,
1270 const std::vector<char>::const_iterator &,
1279 template <
typename T,
1280 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1281 std::is_trivially_copyable_v<T>>>
1284 const std::vector<char>::const_iterator &cbegin,
1285 const std::vector<char>::const_iterator &cend,
1286 std::vector<T> &
object)
1292 typename std::vector<T>::size_type vector_size;
1293 memcpy(&vector_size, &*cbegin,
sizeof(vector_size));
1295 Assert(
static_cast<std::ptrdiff_t
>(cend - cbegin) ==
1296 static_cast<std::ptrdiff_t
>(
sizeof(vector_size) +
1297 vector_size *
sizeof(T)),
1298 ExcMessage(
"The given buffer has the wrong size."));
1319 object.resize(vector_size);
1320 if (vector_size > 0)
1321 std::memcpy(
object.
data(),
1322 &*cbegin +
sizeof(vector_size),
1323 vector_size *
sizeof(T));
1328 template <
typename T,
1329 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1330 std::is_trivially_copyable_v<T>>>
1333 const std::vector<char>::const_iterator &cbegin,
1334 const std::vector<char>::const_iterator &cend,
1335 std::vector<std::vector<T>> &
object)
1338 using size_type =
typename std::vector<T>::size_type;
1339 std::vector<char>::const_iterator iterator = cbegin;
1340 size_type vector_size;
1341 memcpy(&vector_size, &*iterator,
sizeof(vector_size));
1343 object.resize(vector_size);
1344 std::vector<size_type> sizes(vector_size);
1345 if (vector_size > 0)
1346 std::memcpy(sizes.data(),
1347 &*iterator +
sizeof(vector_size),
1348 vector_size *
sizeof(size_type));
1350 iterator +=
sizeof(vector_size) * (1 + vector_size);
1351 size_type aggregated_size = 0;
1352 for (
const auto a : sizes)
1353 aggregated_size += a;
1355 Assert(
static_cast<std::ptrdiff_t
>(cend - iterator) ==
1356 static_cast<std::ptrdiff_t
>(aggregated_size *
sizeof(T)),
1357 ExcMessage(
"The given buffer has the wrong size."));
1363 for (
unsigned int i = 0; i < vector_size; ++i)
1366 object[i].resize(sizes[i]);
1367 std::memcpy(
object[i].
data(), &*iterator, sizes[i] *
sizeof(T));
1368 iterator += sizes[i] *
sizeof(T);
1372 ExcMessage(
"The given buffer has the wrong size."));
1379 template <
typename T>
1382 std::vector<char> &dest_buffer,
1383 const bool allow_compression)
1385 std::size_t
size = 0;
1391 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1399 size = (std::is_same_v<T, std::tuple<>> ? 0 :
sizeof(T));
1401 (void)allow_compression;
1402 const std::size_t previous_size = dest_buffer.size();
1403 dest_buffer.resize(previous_size +
size);
1406 std::memcpy(dest_buffer.data() + previous_size, &
object,
size);
1414 (allow_compression ==
false))
1416 const std::size_t previous_size = dest_buffer.size();
1425 size = dest_buffer.size() - previous_size;
1431 const std::size_t previous_size = dest_buffer.size();
1433 boost::iostreams::filtering_ostreambuf fosb;
1434#ifdef DEAL_II_WITH_ZLIB
1435 if (allow_compression)
1436 fosb.push(boost::iostreams::gzip_compressor());
1438 (void)allow_compression;
1440 fosb.push(boost::iostreams::back_inserter(dest_buffer));
1442 boost::archive::binary_oarchive boa(fosb);
1447 size = dest_buffer.size() - previous_size;
1454 template <
typename T>
1456 pack(
const T &
object,
const bool allow_compression)
1458 std::vector<char> buffer;
1459 pack<T>(
object, buffer, allow_compression);
1465 template <
typename T>
1467 unpack(
const std::vector<char>::const_iterator &cbegin,
1468 const std::vector<char>::const_iterator &cend,
1469 const bool allow_compression)
1474 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1482 const std::size_t
size =
1483 (std::is_same_v<T, std::tuple<>> ? 0 :
sizeof(T));
1487 (void)allow_compression;
1491 std::memcpy(&
object, &*cbegin,
size);
1501 (allow_compression ==
false))
1516 boost::iostreams::filtering_istreambuf fisb;
1517#ifdef DEAL_II_WITH_ZLIB
1518 if (allow_compression)
1519 fisb.push(boost::iostreams::gzip_decompressor());
1521 (void)allow_compression;
1523 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1525 boost::archive::binary_iarchive bia(fisb);
1536 template <
typename T>
1538 unpack(
const std::vector<char> &buffer,
const bool allow_compression)
1540 return unpack<T>(buffer.cbegin(), buffer.cend(), allow_compression);
1544 template <
typename T,
int N>
1546 unpack(
const std::vector<char>::const_iterator &cbegin,
1547 const std::vector<char>::const_iterator &cend,
1548 T (&unpacked_object)[N],
1549 const bool allow_compression)
1554 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) * N < 256)
1556 Assert(std::distance(cbegin, cend) ==
sizeof(T) * N,
1558 std::memcpy(unpacked_object, &*cbegin,
sizeof(T) * N);
1563 boost::iostreams::filtering_istreambuf fisb;
1564#ifdef DEAL_II_WITH_ZLIB
1565 if (allow_compression)
1566 fisb.push(boost::iostreams::gzip_decompressor());
1568 (void)allow_compression;
1570 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1572 boost::archive::binary_iarchive bia(fisb);
1573 bia >> unpacked_object;
1578 template <
typename T,
int N>
1581 T (&unpacked_object)[N],
1582 const bool allow_compression)
1584 unpack<T, N>(buffer.cbegin(),
1593 get_bit(
const unsigned char number,
const unsigned int n)
1600 return ((number >> n) & 1U) != 0u;
1606 set_bit(
unsigned char &number,
const unsigned int n,
const bool x)
1613 number ^= (-
static_cast<unsigned char>(x) ^ number) & (1U << n);
1618 template <
typename To,
typename From>
1619 inline std::unique_ptr<To>
1625 if (To *cast =
dynamic_cast<To *
>(p.get()))
1627 std::unique_ptr<To> result(cast);
1632 throw std::bad_cast();
1637 template <
typename T>
1646 template <
typename T>
1655 template <
typename T>
1664 template <
typename T>
1673 template <
typename T>
1682 template <
typename Integer>
1683 std::vector<Integer>
1686 const std::size_t n = permutation.size();
1688 std::vector<Integer> out(n);
1689 for (std::size_t i = 0; i < n; ++i)
1690 out[i] = n - 1 - permutation[i];
1697 template <
typename Integer>
1698 std::vector<Integer>
1701 const std::size_t n = permutation.size();
1705 for (std::size_t i = 0; i < n; ++i)
1708 out[permutation[i]] = i;
1713 for (std::size_t i = 0; i < n; ++i)
1715 ExcMessage(
"The given input permutation had duplicate entries!"));
1727 namespace serialization
1734 template <
class Archive,
typename... Args>
1736 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1738 ar &std::get<N - 1>(t);
1739 Serialize<N - 1>::serialize(ar, t, version);
1746 template <
class Archive,
typename... Args>
1748 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1756 template <
class Archive,
typename... Args>
1758 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1760 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()
std::vector< index_type > data
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