16#ifndef dealii_utilities_h
17#define dealii_utilities_h
31#ifdef DEAL_II_WITH_TRILINOS
32# include <Epetra_Comm.h>
33# include <Epetra_Map.h>
34# include <Teuchos_Comm.hpp>
35# include <Teuchos_RCP.hpp>
36# ifdef DEAL_II_WITH_MPI
37# include <Epetra_MpiComm.h>
39# include <Epetra_SerialComm.h>
45#include <boost/archive/binary_iarchive.hpp>
46#include <boost/archive/binary_oarchive.hpp>
47#include <boost/core/demangle.hpp>
48#include <boost/iostreams/device/array.hpp>
49#include <boost/iostreams/device/back_inserter.hpp>
50#include <boost/iostreams/filtering_streambuf.hpp>
51#include <boost/serialization/array.hpp>
52#include <boost/serialization/complex.hpp>
53#include <boost/serialization/vector.hpp>
55#ifdef DEAL_II_WITH_ZLIB
56# include <boost/iostreams/filter/gzip.hpp>
65template <
int dim,
typename Number>
103 template <
int dim,
typename Number>
104 std::vector<std::array<std::uint64_t, dim>>
107 const int bits_per_dim = 64);
113 std::vector<std::array<std::uint64_t, dim>>
115 const std::vector<std::array<std::uint64_t, dim>> &points,
116 const int bits_per_dim = 64);
136 const int bits_per_dim);
167 decompress(
const std::string &compressed_input);
183 encode_base64(
const std::vector<unsigned char> &binary_input);
193 std::vector<unsigned char>
232 template <
typename number>
252 template <
typename Number>
275 dim_string(
const int dim,
const int spacedim);
342 std::vector<std::string>
350 std::vector<std::string>
363 std::vector<std::string>
365 const unsigned int width,
366 const char delimiter =
' ');
383 std::pair<int, unsigned int>
392 const std::string &from,
393 const std::string &to);
401 trim(
const std::string &input);
450 template <
int N,
typename T>
459 template <
typename T>
461 pow(
const T base,
const int iexp)
463#if defined(DEBUG) && !defined(DEAL_II_CXX14_CONSTEXPR_BUG)
473 "ExcMessage(\"The exponent must not be negative!\")",
474 ExcMessage(
"The exponent must not be negative!"));
497 static_assert(std::is_integral<T>::value,
"Only integral types supported");
502 (((iexp % 2 == 1) ? base : 1) *
527 template <
typename Iterator,
typename T>
537 template <
typename Iterator,
typename T,
typename Comp>
546 template <
typename Integer>
555 template <
typename Integer>
574 template <
typename T>
576 pack(
const T &
object,
577 std::vector<char> &dest_buffer,
578 const bool allow_compression =
true);
588 template <
typename T>
590 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);
932#ifdef DEAL_II_WITH_TRILINOS
972 const Teuchos::RCP<const Teuchos::Comm<int>> &
1079 template <
int N,
typename T>
1084 !std::is_integral<T>::value || (
N >= 0),
1086 "The non-type template parameter N must be a non-negative integer for integral type T"));
1095 fixed_power<N / 2>(x * x));
1104 return boost::core::demangle(
typeid(t).name());
1109 template <
typename Iterator,
typename T>
1118 template <
typename Iterator,
typename T,
typename Comp>
1127 "The given iterators do not satisfy the proper ordering."));
1129 unsigned int len =
static_cast<unsigned int>(last -
first);
1146 if (!comp(*
first, val))
1151 if (!comp(*
first, val))
1156 if (!comp(*
first, val))
1161 if (!comp(*
first, val))
1166 if (!comp(*
first, val))
1171 if (!comp(*
first, val))
1176 if (!comp(*
first, val))
1195 const unsigned int half = len >> 1;
1196 const Iterator middle =
first + half;
1203 if (comp(*middle, val))
1216 template <
typename T>
1219 std::vector<char> &dest_buffer,
1220 const bool allow_compression)
1222 std::size_t size = 0;
1227#ifdef DEAL_II_HAVE_CXX17
1228 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(
T) < 256)
1230 if (std::is_trivially_copyable<T>() &&
sizeof(
T) < 256)
1233 (void)allow_compression;
1234 const std::size_t previous_size = dest_buffer.size();
1235 dest_buffer.resize(previous_size +
sizeof(
T));
1237 std::memcpy(dest_buffer.data() + previous_size, &
object,
sizeof(
T));
1245 const std::size_t previous_size = dest_buffer.size();
1247 boost::iostreams::filtering_ostreambuf fosb;
1248#ifdef DEAL_II_WITH_ZLIB
1249 if (allow_compression)
1250 fosb.push(boost::iostreams::gzip_compressor());
1252 (void)allow_compression;
1254 fosb.push(boost::iostreams::back_inserter(dest_buffer));
1256 boost::archive::binary_oarchive boa(fosb);
1261 size = dest_buffer.size() - previous_size;
1268 template <
typename T>
1270 pack(
const T &
object,
const bool allow_compression)
1272 std::vector<char> buffer;
1273 pack<T>(
object, buffer, allow_compression);
1278 template <
typename T>
1280 unpack(
const std::vector<char>::const_iterator &cbegin,
1281 const std::vector<char>::const_iterator &cend,
1282 const bool allow_compression)
1289#ifdef DEAL_II_HAVE_CXX17
1290 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(
T) < 256)
1292 if (std::is_trivially_copyable<T>() &&
sizeof(
T) < 256)
1295 (void)allow_compression;
1297 std::memcpy(&
object, &*cbegin,
sizeof(
T));
1302 boost::iostreams::filtering_istreambuf fisb;
1303#ifdef DEAL_II_WITH_ZLIB
1304 if (allow_compression)
1305 fisb.push(boost::iostreams::gzip_decompressor());
1307 (void)allow_compression;
1309 fisb.push(boost::iostreams::array_source(&*cbegin, &*cend));
1311 boost::archive::binary_iarchive bia(fisb);
1319 template <
typename T>
1321 unpack(
const std::vector<char> &buffer,
const bool allow_compression)
1323 return unpack<T>(buffer.cbegin(), buffer.cend(), allow_compression);
1327 template <
typename T,
int N>
1329 unpack(
const std::vector<char>::const_iterator &cbegin,
1330 const std::vector<char>::const_iterator &cend,
1331 T (&unpacked_object)[
N],
1332 const bool allow_compression)
1337 if (std::is_trivially_copyable<T>() &&
sizeof(
T) *
N < 256)
1339 Assert(std::distance(cbegin, cend) ==
sizeof(
T) *
N,
1341 std::memcpy(unpacked_object, &*cbegin,
sizeof(
T) *
N);
1346 boost::iostreams::filtering_istreambuf fisb;
1347#ifdef DEAL_II_WITH_ZLIB
1348 if (allow_compression)
1349 fisb.push(boost::iostreams::gzip_decompressor());
1351 (void)allow_compression;
1353 fisb.push(boost::iostreams::array_source(&*cbegin, &*cend));
1355 boost::archive::binary_iarchive bia(fisb);
1356 bia >> unpacked_object;
1361 template <
typename T,
int N>
1364 T (&unpacked_object)[
N],
1365 const bool allow_compression)
1367 unpack<T, N>(buffer.cbegin(),
1376 get_bit(
const unsigned char number,
const unsigned int n)
1383 return (number >> n) & 1U;
1389 set_bit(
unsigned char &number,
const unsigned int n,
const bool x)
1396 number ^= (-
static_cast<unsigned char>(x) ^ number) & (1U << n);
1401 template <
typename To,
typename From>
1402 inline std::unique_ptr<To>
1408 if (To *cast =
dynamic_cast<To *
>(p.get()))
1410 std::unique_ptr<To> result(cast);
1415 throw std::bad_cast();
1420 template <
typename T>
1429 template <
typename T>
1438 template <
typename T>
1447 template <
typename T>
1456 template <
typename T>
1465 template <
typename Integer>
1466 std::vector<Integer>
1469 const std::size_t n = permutation.size();
1471 std::vector<Integer> out(n);
1472 for (std::size_t i = 0; i < n; ++i)
1473 out[i] = n - 1 - permutation[i];
1480 template <
typename Integer>
1481 std::vector<Integer>
1484 const std::size_t n = permutation.size();
1488 for (std::size_t i = 0; i < n; ++i)
1491 out[permutation[i]] = i;
1496 for (std::size_t i = 0; i < n; ++i)
1498 ExcMessage(
"The given input permutation had duplicate entries!"));
1510 namespace serialization
1517 template <
class Archive,
typename... Args>
1519 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1521 ar &std::get<
N - 1>(t);
1522 Serialize<N - 1>::serialize(ar, t, version);
1529 template <
class Archive,
typename... Args>
1531 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1539 template <
class Archive,
typename... Args>
1541 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1543 Serialize<
sizeof...(Args)>::serialize(ar, t, version);
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_FALLTHROUGH
#define DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
#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 destroy_communicator(Epetra_Comm &communicator)
Epetra_Map duplicate_map(const Epetra_BlockMap &map, const Epetra_Comm &comm)
unsigned int get_this_mpi_process(const Epetra_Comm &mpi_communicator)
unsigned int get_n_mpi_processes(const Epetra_Comm &mpi_communicator)
const Epetra_Comm & comm_self()
const Teuchos::RCP< const Teuchos::Comm< int > > & tpetra_comm_self()
Epetra_Comm * duplicate_communicator(const Epetra_Comm &communicator)
const Epetra_Comm & comm_world()
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)
@ abort_or_throw_on_exception
static const unsigned int invalid_unsigned_int
*braid_SplitCommworld & comm