16#ifndef dealii_utilities_h
17#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);
436 template <
int N,
typename T>
445 template <
typename T>
447 pow(
const T base,
const int iexp)
449#if defined(DEBUG) && !defined(DEAL_II_CXX14_CONSTEXPR_BUG)
452# if KOKKOS_VERSION >= 30600
457 abort_or_throw_on_exception,
462 "ExcMessage(\"The exponent must not be negative!\")",
463 ExcMessage(
"The exponent must not be negative!"));
466# ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
470 abort_or_throw_on_exception,
475 "ExcMessage(\"The exponent must not be negative!\")",
476 ExcMessage(
"The exponent must not be negative!"));
501 static_assert(std::is_integral<T>::value,
"Only integral types supported");
506 (((iexp % 2 == 1) ? base : 1) *
531 template <
typename Iterator,
typename T>
541 template <
typename Iterator,
typename T,
typename Comp>
550 template <
typename Integer>
559 template <
typename Integer>
617 template <
typename T>
619 pack(
const T &
object,
620 std::vector<char> &dest_buffer,
621 const bool allow_compression =
true);
631 template <
typename T>
633 pack(
const T &
object,
const bool allow_compression =
true);
666 template <
typename T>
668 unpack(
const std::vector<char> &buffer,
const bool allow_compression =
true);
678 template <
typename T>
680 unpack(
const std::vector<char>::const_iterator &cbegin,
681 const std::vector<char>::const_iterator &cend,
682 const bool allow_compression =
true);
716 template <
typename T,
int N>
718 unpack(
const std::vector<char> &buffer,
719 T (&unpacked_object)[N],
720 const bool allow_compression =
true);
730 template <
typename T,
int N>
732 unpack(
const std::vector<char>::const_iterator &cbegin,
733 const std::vector<char>::const_iterator &cend,
734 T (&unpacked_object)[N],
735 const bool allow_compression =
true);
741 get_bit(
const unsigned char number,
const unsigned int n);
748 set_bit(
unsigned char &number,
const unsigned int n,
const bool x);
808 template <
typename To,
typename From>
815 template <
typename T>
822 template <
typename T>
829 template <
typename T>
836 template <
typename T>
843 template <
typename T>
972 posix_memalign(
void **memptr, std::size_t alignment, std::size_t size);
981 template <
int N,
typename T>
985 Assert(((std::is_integral<T>::value ==
true) && (N >= 0)) ||
986 (std::is_integral<T>::value ==
false),
987 ExcMessage(
"If the type of the argument, T, is an integer type, "
988 "then the exponent N must be a non-negative integer "
989 "because the result would otherwise not be an integer."));
999 return ((N % 2 == 1) ? x *
fixed_power<N / 2>(x * x) :
1009 return boost::core::demangle(
typeid(t).name());
1014 template <
typename Iterator,
typename T>
1023 template <
typename Iterator,
typename T,
typename Comp>
1032 "The given iterators do not satisfy the proper ordering."));
1034 unsigned int len =
static_cast<unsigned int>(last -
first);
1051 if (!comp(*
first, val))
1056 if (!comp(*
first, val))
1061 if (!comp(*
first, val))
1066 if (!comp(*
first, val))
1071 if (!comp(*
first, val))
1076 if (!comp(*
first, val))
1081 if (!comp(*
first, val))
1100 const unsigned int half = len >> 1;
1101 const Iterator middle =
first + half;
1108 if (comp(*middle, val))
1128 template <
typename T>
1136 template <
typename T>
1140 std::is_trivially_copyable<T>::value && !std::is_same<T, bool>::value;
1145 template <
typename T>
1149 std::is_trivially_copyable<T>::value && !std::is_same<T, bool>::value;
1162 template <
typename T>
1165 std::vector<char> &)
1173 template <
typename T,
1174 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1175 std::is_trivially_copyable<T>::value>>
1178 const std::vector<T> &
object,
1179 std::vector<char> & dest_buffer)
1181 const typename std::vector<T>::size_type vector_size =
object.size();
1185 dest_buffer.reserve(dest_buffer.size() +
sizeof(vector_size) +
1186 vector_size *
sizeof(T));
1189 dest_buffer.insert(dest_buffer.end(),
1190 reinterpret_cast<const char *
>(&vector_size),
1191 reinterpret_cast<const char *
>(&vector_size + 1));
1194 if (vector_size > 0)
1195 dest_buffer.insert(dest_buffer.end(),
1196 reinterpret_cast<const char *
>(
object.data()),
1197 reinterpret_cast<const char *
>(
object.data() +
1203 template <
typename T,
1204 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1205 std::is_trivially_copyable<T>::value>>
1208 const std::vector<std::vector<T>> &
object,
1209 std::vector<char> & dest_buffer)
1211 using size_type =
typename std::vector<T>::size_type;
1212 const size_type vector_size =
object.size();
1214 typename std::vector<T>::size_type aggregated_size = 0;
1215 std::vector<size_type> sizes;
1216 sizes.reserve(vector_size);
1217 for (
const auto &a : object)
1219 aggregated_size += a.size();
1220 sizes.push_back(a.size());
1225 dest_buffer.reserve(dest_buffer.size() +
1226 sizeof(vector_size) * (1 + vector_size) +
1227 aggregated_size *
sizeof(T));
1230 dest_buffer.insert(dest_buffer.end(),
1231 reinterpret_cast<const char *
>(&vector_size),
1232 reinterpret_cast<const char *
>(&vector_size + 1));
1235 if (vector_size > 0)
1236 dest_buffer.insert(dest_buffer.end(),
1237 reinterpret_cast<const char *
>(sizes.data()),
1238 reinterpret_cast<const char *
>(sizes.data() +
1242 for (
const auto &a : object)
1243 dest_buffer.insert(dest_buffer.end(),
1244 reinterpret_cast<const char *
>(a.data()),
1245 reinterpret_cast<const char *
>(a.data() + a.size()));
1250 template <
typename T>
1253 const std::vector<char>::const_iterator &,
1254 const std::vector<char>::const_iterator &,
1263 template <
typename T,
1264 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1265 std::is_trivially_copyable<T>::value>>
1268 const std::vector<char>::const_iterator &cbegin,
1269 const std::vector<char>::const_iterator &cend,
1270 std::vector<T> &
object)
1276 typename std::vector<T>::size_type vector_size;
1277 memcpy(&vector_size, &*cbegin,
sizeof(vector_size));
1279 Assert(
static_cast<std::ptrdiff_t
>(cend - cbegin) ==
1280 static_cast<std::ptrdiff_t
>(
sizeof(vector_size) +
1281 vector_size *
sizeof(T)),
1282 ExcMessage(
"The given buffer has the wrong size."));
1287 if (vector_size > 0)
1289 const auto buffer_data_begin =
1290 reinterpret_cast<const T *
>(&*cbegin +
sizeof(vector_size));
1291 const auto buffer_data_end = buffer_data_begin + vector_size;
1292 object.insert(
object.end(), buffer_data_begin, buffer_data_end);
1298 template <
typename T,
1299 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1300 std::is_trivially_copyable<T>::value>>
1303 const std::vector<char>::const_iterator &cbegin,
1304 const std::vector<char>::const_iterator &cend,
1305 std::vector<std::vector<T>> &
object)
1308 using size_type =
typename std::vector<T>::size_type;
1309 std::vector<char>::const_iterator iterator = cbegin;
1310 size_type vector_size;
1311 memcpy(&vector_size, &*iterator,
sizeof(vector_size));
1313 object.resize(vector_size);
1314 std::vector<size_type> sizes(vector_size);
1315 if (vector_size > 0)
1316 memcpy(sizes.data(),
1317 &*iterator +
sizeof(vector_size),
1318 vector_size *
sizeof(size_type));
1320 iterator +=
sizeof(vector_size) * (1 + vector_size);
1321 size_type aggregated_size = 0;
1322 for (
const auto a : sizes)
1323 aggregated_size += a;
1325 Assert(
static_cast<std::ptrdiff_t
>(cend - iterator) ==
1326 static_cast<std::ptrdiff_t
>(aggregated_size *
sizeof(T)),
1327 ExcMessage(
"The given buffer has the wrong size."));
1331 for (
unsigned int i = 0; i < vector_size; ++i)
1334 object[i].insert(
object[i].end(),
1335 reinterpret_cast<const T *
>(&*iterator),
1336 reinterpret_cast<const T *
>(&*iterator +
1337 sizeof(T) * sizes[i]));
1338 iterator +=
sizeof(T) * sizes[i];
1342 ExcMessage(
"The given buffer has the wrong size."));
1349 template <
typename T>
1352 std::vector<char> &dest_buffer,
1353 const bool allow_compression)
1355 std::size_t size = 0;
1370 size = (std::is_same<T, std::tuple<>>::value ? 0 :
sizeof(T));
1372 (void)allow_compression;
1373 const std::size_t previous_size = dest_buffer.size();
1374 dest_buffer.resize(previous_size + size);
1377 std::memcpy(dest_buffer.data() + previous_size, &
object, size);
1385 (allow_compression ==
false))
1387 const std::size_t previous_size = dest_buffer.size();
1396 size = dest_buffer.size() - previous_size;
1402 const std::size_t previous_size = dest_buffer.size();
1404 boost::iostreams::filtering_ostreambuf fosb;
1405#ifdef DEAL_II_WITH_ZLIB
1406 if (allow_compression)
1407 fosb.push(boost::iostreams::gzip_compressor());
1409 (void)allow_compression;
1411 fosb.push(boost::iostreams::back_inserter(dest_buffer));
1413 boost::archive::binary_oarchive boa(fosb);
1418 size = dest_buffer.size() - previous_size;
1425 template <
typename T>
1427 pack(
const T &
object,
const bool allow_compression)
1429 std::vector<char> buffer;
1430 pack<T>(
object, buffer, allow_compression);
1436 template <
typename T>
1438 unpack(
const std::vector<char>::const_iterator &cbegin,
1439 const std::vector<char>::const_iterator &cend,
1440 const bool allow_compression)
1454 const std::size_t size =
1455 (std::is_same<T, std::tuple<>>::value ? 0 :
sizeof(T));
1459 (void)allow_compression;
1463 std::memcpy(&
object, &*cbegin, size);
1473 (allow_compression ==
false))
1488 boost::iostreams::filtering_istreambuf fisb;
1489#ifdef DEAL_II_WITH_ZLIB
1490 if (allow_compression)
1491 fisb.push(boost::iostreams::gzip_decompressor());
1493 (void)allow_compression;
1495 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1497 boost::archive::binary_iarchive bia(fisb);
1508 template <
typename T>
1510 unpack(
const std::vector<char> &buffer,
const bool allow_compression)
1512 return unpack<T>(buffer.cbegin(), buffer.cend(), allow_compression);
1516 template <
typename T,
int N>
1518 unpack(
const std::vector<char>::const_iterator &cbegin,
1519 const std::vector<char>::const_iterator &cend,
1520 T (&unpacked_object)[N],
1521 const bool allow_compression)
1527 sizeof(T) * N < 256)
1529 Assert(std::distance(cbegin, cend) ==
sizeof(T) * N,
1531 std::memcpy(unpacked_object, &*cbegin,
sizeof(T) * N);
1536 boost::iostreams::filtering_istreambuf fisb;
1537#ifdef DEAL_II_WITH_ZLIB
1538 if (allow_compression)
1539 fisb.push(boost::iostreams::gzip_decompressor());
1541 (void)allow_compression;
1543 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1545 boost::archive::binary_iarchive bia(fisb);
1546 bia >> unpacked_object;
1551 template <
typename T,
int N>
1554 T (&unpacked_object)[N],
1555 const bool allow_compression)
1557 unpack<T, N>(buffer.cbegin(),
1566 get_bit(
const unsigned char number,
const unsigned int n)
1573 return ((number >> n) & 1U) != 0u;
1579 set_bit(
unsigned char &number,
const unsigned int n,
const bool x)
1586 number ^= (-
static_cast<unsigned char>(x) ^ number) & (1U << n);
1591 template <
typename To,
typename From>
1592 inline std::unique_ptr<To>
1598 if (To *cast =
dynamic_cast<To *
>(p.get()))
1600 std::unique_ptr<To> result(cast);
1605 throw std::bad_cast();
1610 template <
typename T>
1619 template <
typename T>
1628 template <
typename T>
1637 template <
typename T>
1646 template <
typename T>
1655 template <
typename Integer>
1656 std::vector<Integer>
1659 const std::size_t n = permutation.size();
1661 std::vector<Integer> out(n);
1662 for (std::size_t i = 0; i < n; ++i)
1663 out[i] = n - 1 - permutation[i];
1670 template <
typename Integer>
1671 std::vector<Integer>
1674 const std::size_t n = permutation.size();
1678 for (std::size_t i = 0; i < n; ++i)
1681 out[permutation[i]] = i;
1686 for (std::size_t i = 0; i < n; ++i)
1688 ExcMessage(
"The given input permutation had duplicate entries!"));
1700 namespace serialization
1707 template <
class Archive,
typename... Args>
1709 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1711 ar &std::get<N - 1>(t);
1712 Serialize<N - 1>::serialize(ar, t, version);
1719 template <
class Archive,
typename... Args>
1721 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1729 template <
class Archive,
typename... Args>
1731 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1733 Serialize<
sizeof...(Args)>::serialize(ar, t, version);
#define DEAL_II_CONSTEXPR_IN_CONDITIONAL
#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)
void get_memory_stats(MemoryStats &stats)
std::string get_hostname()
void posix_memalign(void **memptr, std::size_t alignment, std::size_t size)
const 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 > &)
constexpr T pow(const T base, const int iexp)
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)
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)
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