15#ifndef dealii_utilities_h
16#define dealii_utilities_h
23#include <boost/archive/binary_iarchive.hpp>
24#include <boost/archive/binary_oarchive.hpp>
25#include <boost/core/demangle.hpp>
26#include <boost/iostreams/device/array.hpp>
27#include <boost/iostreams/device/back_inserter.hpp>
28#include <boost/iostreams/filtering_streambuf.hpp>
29#include <boost/serialization/array.hpp>
30#include <boost/serialization/complex.hpp>
31#include <boost/serialization/vector.hpp>
42#ifdef DEAL_II_WITH_ZLIB
43# include <boost/iostreams/filter/gzip.hpp>
50template <
int dim,
typename Number>
89 template <
int dim,
typename Number>
90 std::vector<std::array<std::uint64_t, dim>>
93 const int bits_per_dim = 64);
99 std::vector<std::array<std::uint64_t, dim>>
101 const std::vector<std::array<std::uint64_t, dim>> &points,
102 const int bits_per_dim = 64);
122 const int bits_per_dim);
153 decompress(
const std::string &compressed_input);
169 encode_base64(
const std::vector<unsigned char> &binary_input);
179 std::vector<unsigned char>
218 template <
typename number>
238 template <
typename Number>
261 dim_string(
const int dim,
const int spacedim);
328 std::vector<std::string>
336 std::vector<std::string>
349 std::vector<std::string>
351 const unsigned int width,
352 const char delimiter =
' ');
369 std::pair<int, unsigned int>
378 const std::string &from,
379 const std::string &to);
387 trim(
const std::string &input);
456 template <
int N,
typename T>
467 template <
typename T,
typename = std::enable_if_t<std::is_arithmetic_v<T>>>
469 pow(
const T base,
const int iexp);
492 template <
typename Iterator,
typename T>
501 template <
typename Iterator,
typename T,
typename Comp>
510 template <
typename Integer>
519 template <
typename Integer>
577 template <
typename T>
579 pack(
const T &
object,
580 std::vector<char> &dest_buffer,
581 const bool allow_compression =
true);
591 template <
typename T>
593 pack(
const T &
object,
const bool allow_compression =
true);
626 template <
typename T>
628 unpack(
const std::vector<char> &buffer,
const bool allow_compression =
true);
638 template <
typename T>
640 unpack(
const std::vector<char>::const_iterator &cbegin,
641 const std::vector<char>::const_iterator &cend,
642 const bool allow_compression =
true);
676 template <
typename T,
int N>
678 unpack(
const std::vector<char> &buffer,
679 T (&unpacked_object)[N],
680 const bool allow_compression =
true);
690 template <
typename T,
int N>
692 unpack(
const std::vector<char>::const_iterator &cbegin,
693 const std::vector<char>::const_iterator &cend,
694 T (&unpacked_object)[N],
695 const bool allow_compression =
true);
701 get_bit(
const unsigned char number,
const unsigned int n);
708 set_bit(
unsigned char &number,
const unsigned int n,
const bool x);
768 template <
typename To,
typename From>
775 template <
typename T>
782 template <
typename T>
789 template <
typename T>
796 template <
typename T>
803 template <
typename T>
941 template <
int N,
typename T>
945 Assert(((std::is_integral_v<T> ==
true) && (N >= 0)) ||
946 (std::is_integral_v<T> ==
false),
947 ExcMessage(
"If the type of the argument, T, is an integer type, "
948 "then the exponent N must be a non-negative integer "
949 "because the result would otherwise not be an integer."));
959 return ((N % 2 == 1) ? x *
fixed_power<N / 2>(x * x) :
965 template <
typename T,
typename>
967 pow(
const T base,
const int iexp)
969#if defined(DEBUG) && !defined(DEAL_II_CXX14_CONSTEXPR_BUG)
972# if DEAL_II_KOKKOS_VERSION_GTE(3, 6, 0)
977 abort_or_throw_on_exception,
982 "ExcMessage(\"The exponent must not be negative!\")",
983 ExcMessage(
"The exponent must not be negative!"));
986# ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
990 abort_or_throw_on_exception,
995 "ExcMessage(\"The exponent must not be negative!\")",
996 ExcMessage(
"The exponent must not be negative!"));
967 pow(
const T base,
const int iexp) {
…}
1026 return boost::core::demangle(
typeid(t).name());
1031 template <
typename Iterator,
typename T>
1040 template <
typename Iterator,
typename T,
typename Comp>
1049 "The given iterators do not satisfy the proper ordering."));
1051 unsigned int len =
static_cast<unsigned int>(last -
first);
1068 if (!comp(*
first, val))
1073 if (!comp(*
first, val))
1078 if (!comp(*
first, val))
1083 if (!comp(*
first, val))
1088 if (!comp(*
first, val))
1093 if (!comp(*
first, val))
1098 if (!comp(*
first, val))
1117 const unsigned int half = len >> 1;
1118 const Iterator middle =
first + half;
1125 if (comp(*middle, val))
1145 template <
typename T>
1153 template <
typename T>
1157 std::is_trivially_copyable_v<T> && !std::is_same_v<T, bool>;
1162 template <
typename T>
1166 std::is_trivially_copyable_v<T> && !std::is_same_v<T, bool>;
1179 template <
typename T>
1182 std::vector<char> &)
1190 template <
typename T,
1191 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1192 std::is_trivially_copyable_v<T>>>
1195 const std::vector<T> &
object,
1196 std::vector<char> &dest_buffer)
1198 const typename std::vector<T>::size_type vector_size =
object.size();
1202 dest_buffer.reserve(dest_buffer.size() +
sizeof(vector_size) +
1203 vector_size *
sizeof(T));
1206 dest_buffer.insert(dest_buffer.end(),
1207 reinterpret_cast<const char *
>(&vector_size),
1208 reinterpret_cast<const char *
>(&vector_size + 1));
1211 if (vector_size > 0)
1212 dest_buffer.insert(dest_buffer.end(),
1213 reinterpret_cast<const char *
>(
object.data()),
1214 reinterpret_cast<const char *
>(
object.data() +
1220 template <
typename T,
1221 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1222 std::is_trivially_copyable_v<T>>>
1225 const std::vector<std::vector<T>> &
object,
1226 std::vector<char> &dest_buffer)
1228 using size_type =
typename std::vector<T>::size_type;
1229 const size_type vector_size =
object.size();
1231 typename std::vector<T>::size_type aggregated_size = 0;
1232 std::vector<size_type> sizes;
1233 sizes.reserve(vector_size);
1234 for (
const auto &a : object)
1236 aggregated_size += a.size();
1237 sizes.push_back(a.size());
1242 dest_buffer.reserve(dest_buffer.size() +
1243 sizeof(vector_size) * (1 + vector_size) +
1244 aggregated_size *
sizeof(T));
1247 dest_buffer.insert(dest_buffer.end(),
1248 reinterpret_cast<const char *
>(&vector_size),
1249 reinterpret_cast<const char *
>(&vector_size + 1));
1252 if (vector_size > 0)
1253 dest_buffer.insert(dest_buffer.end(),
1254 reinterpret_cast<const char *
>(sizes.data()),
1255 reinterpret_cast<const char *
>(sizes.data() +
1259 for (
const auto &a : object)
1260 dest_buffer.insert(dest_buffer.end(),
1261 reinterpret_cast<const char *
>(a.data()),
1262 reinterpret_cast<const char *
>(a.data() + a.size()));
1267 template <
typename T>
1270 const std::vector<char>::const_iterator &,
1271 const std::vector<char>::const_iterator &,
1280 template <
typename T,
1281 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1282 std::is_trivially_copyable_v<T>>>
1285 const std::vector<char>::const_iterator &cbegin,
1286 const std::vector<char>::const_iterator &cend,
1287 std::vector<T> &
object)
1293 typename std::vector<T>::size_type vector_size;
1294 memcpy(&vector_size, &*cbegin,
sizeof(vector_size));
1296 Assert(
static_cast<std::ptrdiff_t
>(cend - cbegin) ==
1297 static_cast<std::ptrdiff_t
>(
sizeof(vector_size) +
1298 vector_size *
sizeof(T)),
1299 ExcMessage(
"The given buffer has the wrong size."));
1320 object.resize(vector_size);
1321 if (vector_size > 0)
1322 std::memcpy(
object.
data(),
1323 &*cbegin +
sizeof(vector_size),
1324 vector_size *
sizeof(T));
1329 template <
typename T,
1330 typename = std::enable_if_t<!std::is_same_v<T, bool> &&
1331 std::is_trivially_copyable_v<T>>>
1334 const std::vector<char>::const_iterator &cbegin,
1335 const std::vector<char>::const_iterator &cend,
1336 std::vector<std::vector<T>> &
object)
1339 using size_type =
typename std::vector<T>::size_type;
1340 std::vector<char>::const_iterator iterator = cbegin;
1341 size_type vector_size;
1342 memcpy(&vector_size, &*iterator,
sizeof(vector_size));
1344 object.resize(vector_size);
1345 std::vector<size_type> sizes(vector_size);
1346 if (vector_size > 0)
1347 std::memcpy(sizes.data(),
1348 &*iterator +
sizeof(vector_size),
1349 vector_size *
sizeof(size_type));
1351 iterator +=
sizeof(vector_size) * (1 + vector_size);
1352 size_type aggregated_size = 0;
1353 for (
const auto a : sizes)
1354 aggregated_size += a;
1356 Assert(
static_cast<std::ptrdiff_t
>(cend - iterator) ==
1357 static_cast<std::ptrdiff_t
>(aggregated_size *
sizeof(T)),
1358 ExcMessage(
"The given buffer has the wrong size."));
1364 for (
unsigned int i = 0; i < vector_size; ++i)
1367 object[i].resize(sizes[i]);
1368 std::memcpy(
object[i].
data(), &*iterator, sizes[i] *
sizeof(T));
1369 iterator += sizes[i] *
sizeof(T);
1373 ExcMessage(
"The given buffer has the wrong size."));
1380 template <
typename T>
1383 std::vector<char> &dest_buffer,
1384 const bool allow_compression)
1386 std::size_t
size = 0;
1392 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1400 size = (std::is_same_v<T, std::tuple<>> ? 0 :
sizeof(T));
1402 (void)allow_compression;
1403 const std::size_t previous_size = dest_buffer.size();
1404 dest_buffer.resize(previous_size +
size);
1407 std::memcpy(dest_buffer.data() + previous_size, &
object,
size);
1415 (allow_compression ==
false))
1417 const std::size_t previous_size = dest_buffer.size();
1426 size = dest_buffer.size() - previous_size;
1432 const std::size_t previous_size = dest_buffer.size();
1434 boost::iostreams::filtering_ostreambuf fosb;
1435#ifdef DEAL_II_WITH_ZLIB
1436 if (allow_compression)
1437 fosb.push(boost::iostreams::gzip_compressor());
1439 (void)allow_compression;
1441 fosb.push(boost::iostreams::back_inserter(dest_buffer));
1443 boost::archive::binary_oarchive boa(fosb);
1448 size = dest_buffer.size() - previous_size;
1455 template <
typename T>
1457 pack(
const T &
object,
const bool allow_compression)
1459 std::vector<char> buffer;
1460 pack<T>(
object, buffer, allow_compression);
1457 pack(
const T &
object,
const bool allow_compression) {
…}
1466 template <
typename T>
1468 unpack(
const std::vector<char>::const_iterator &cbegin,
1469 const std::vector<char>::const_iterator &cend,
1470 const bool allow_compression)
1475 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1483 const std::size_t
size =
1484 (std::is_same_v<T, std::tuple<>> ? 0 :
sizeof(T));
1488 (void)allow_compression;
1492 std::memcpy(&
object, &*cbegin,
size);
1502 (allow_compression ==
false))
1517 boost::iostreams::filtering_istreambuf fisb;
1518#ifdef DEAL_II_WITH_ZLIB
1519 if (allow_compression)
1520 fisb.push(boost::iostreams::gzip_decompressor());
1522 (void)allow_compression;
1524 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1526 boost::archive::binary_iarchive bia(fisb);
1468 unpack(
const std::vector<char>::const_iterator &cbegin, {
…}
1537 template <
typename T>
1539 unpack(
const std::vector<char> &buffer,
const bool allow_compression)
1541 return unpack<T>(buffer.cbegin(), buffer.cend(), allow_compression);
1539 unpack(
const std::vector<char> &buffer,
const bool allow_compression) {
…}
1545 template <
typename T,
int N>
1547 unpack(
const std::vector<char>::const_iterator &cbegin,
1548 const std::vector<char>::const_iterator &cend,
1549 T (&unpacked_object)[N],
1550 const bool allow_compression)
1555 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) * N < 256)
1557 Assert(std::distance(cbegin, cend) ==
sizeof(T) * N,
1559 std::memcpy(unpacked_object, &*cbegin,
sizeof(T) * N);
1564 boost::iostreams::filtering_istreambuf fisb;
1565#ifdef DEAL_II_WITH_ZLIB
1566 if (allow_compression)
1567 fisb.push(boost::iostreams::gzip_decompressor());
1569 (void)allow_compression;
1571 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1573 boost::archive::binary_iarchive bia(fisb);
1574 bia >> unpacked_object;
1547 unpack(
const std::vector<char>::const_iterator &cbegin, {
…}
1579 template <
typename T,
int N>
1582 T (&unpacked_object)[N],
1583 const bool allow_compression)
1585 unpack<T, N>(buffer.cbegin(),
1594 get_bit(
const unsigned char number,
const unsigned int n)
1601 return ((number >> n) & 1U) != 0u;
1594 get_bit(
const unsigned char number,
const unsigned int n) {
…}
1607 set_bit(
unsigned char &number,
const unsigned int n,
const bool x)
1614 number ^= (-
static_cast<unsigned char>(x) ^ number) & (1U << n);
1607 set_bit(
unsigned char &number,
const unsigned int n,
const bool x) {
…}
1619 template <
typename To,
typename From>
1620 inline std::unique_ptr<To>
1626 if (To *cast =
dynamic_cast<To *
>(p.get()))
1628 std::unique_ptr<To> result(cast);
1633 throw std::bad_cast();
1638 template <
typename T>
1647 template <
typename T>
1656 template <
typename T>
1665 template <
typename T>
1674 template <
typename T>
1683 template <
typename Integer>
1684 std::vector<Integer>
1687 const std::size_t n = permutation.size();
1689 std::vector<Integer> out(n);
1690 for (std::size_t i = 0; i < n; ++i)
1691 out[i] = n - 1 - permutation[i];
1698 template <
typename Integer>
1699 std::vector<Integer>
1702 const std::size_t n = permutation.size();
1706 for (std::size_t i = 0; i < n; ++i)
1709 out[permutation[i]] = i;
1714 for (std::size_t i = 0; i < n; ++i)
1716 ExcMessage(
"The given input permutation had duplicate entries!"));
1728 namespace serialization
1735 template <
class Archive,
typename... Args>
1737 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1739 ar &std::get<
N - 1>(t);
1740 Serialize<N - 1>::serialize(ar, t, version);
1747 template <
class Archive,
typename... Args>
1749 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1757 template <
class Archive,
typename... Args>
1759 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1761 Serialize<
sizeof...(Args)>::serialize(ar, t, version);
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_CXX20_REQUIRES(condition)
#define DEAL_II_HOST_DEVICE
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_FALLTHROUGH
#define DEAL_II_ASSERT_UNREACHABLE()
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
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::size_t pack(const T &object, std::vector< char > &dest_buffer, const bool allow_compression=true)
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)
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)
constexpr unsigned int invalid_unsigned_int
static constexpr bool value