16 #ifndef dealii_utilities_h
17 #define dealii_utilities_h
27 #include <type_traits>
34 #ifdef DEAL_II_WITH_TRILINOS
35 # include <Epetra_Comm.h>
36 # include <Epetra_Map.h>
37 # include <Teuchos_Comm.hpp>
38 # include <Teuchos_RCP.hpp>
39 # ifdef DEAL_II_WITH_MPI
40 # include <Epetra_MpiComm.h>
42 # include <Epetra_SerialComm.h>
46 #include <boost/archive/binary_iarchive.hpp>
47 #include <boost/archive/binary_oarchive.hpp>
48 #include <boost/core/demangle.hpp>
49 #include <boost/iostreams/device/array.hpp>
50 #include <boost/iostreams/device/back_inserter.hpp>
51 #include <boost/iostreams/filtering_streambuf.hpp>
52 #include <boost/serialization/array.hpp>
53 #include <boost/serialization/complex.hpp>
54 #include <boost/serialization/vector.hpp>
56 #ifdef DEAL_II_WITH_ZLIB
57 # include <boost/iostreams/filter/gzip.hpp>
66 template <
int dim,
typename Number>
104 template <
int dim,
typename Number>
105 std::vector<std::array<std::uint64_t, dim>>
108 const int bits_per_dim = 64);
114 std::vector<std::array<std::uint64_t, dim>>
116 const std::vector<std::array<std::uint64_t, dim>> &points,
117 const int bits_per_dim = 64);
137 const int bits_per_dim);
168 decompress(
const std::string &compressed_input);
184 encode_base64(
const std::vector<unsigned char> &binary_input);
194 std::vector<unsigned char>
233 template <
typename number>
253 template <
typename Number>
276 dim_string(
const int dim,
const int spacedim);
343 std::vector<std::string>
351 std::vector<std::string>
364 std::vector<std::string>
366 const unsigned int width,
367 const char delimiter =
' ');
384 std::pair<int, unsigned int>
393 const std::string &from,
394 const std::string &to);
402 trim(
const std::string &input);
451 template <
int N,
typename T>
460 template <
typename T>
462 pow(
const T base,
const int iexp)
464 #if defined(DEBUG) && !defined(DEAL_II_CXX14_CONSTEXPR_BUG)
470 abort_or_throw_on_exception,
475 "ExcMessage(\"The exponent must not be negative!\")",
476 ExcMessage(
"The exponent must not be negative!"));
499 static_assert(std::is_integral<T>::value,
"Only integral types supported");
504 (((iexp % 2 == 1) ? base : 1) *
529 template <
typename Iterator,
typename T>
539 template <
typename Iterator,
typename T,
typename Comp>
548 template <
typename Integer>
557 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);
624 template <
typename T>
626 unpack(
const std::vector<char> &buffer,
const bool allow_compression =
true);
636 template <
typename T>
638 unpack(
const std::vector<char>::const_iterator &cbegin,
639 const std::vector<char>::const_iterator &cend,
640 const bool allow_compression =
true);
674 template <
typename T,
int N>
676 unpack(
const std::vector<char> &buffer,
677 T (&unpacked_object)[
N],
678 const bool allow_compression =
true);
688 template <
typename T,
int N>
690 unpack(
const std::vector<char>::const_iterator &cbegin,
691 const std::vector<char>::const_iterator &cend,
692 T (&unpacked_object)[
N],
693 const bool allow_compression =
true);
699 get_bit(
const unsigned char number,
const unsigned int n);
706 set_bit(
unsigned char &number,
const unsigned int n,
const bool x);
766 template <
typename To,
typename From>
773 template <
typename T>
780 template <
typename T>
787 template <
typename T>
794 template <
typename T>
801 template <
typename T>
930 posix_memalign(
void **memptr, std::size_t alignment, std::size_t size);
934 #ifdef DEAL_II_WITH_TRILINOS
974 const Teuchos::RCP<const Teuchos::Comm<int>> &
1081 template <
int N,
typename T>
1086 !std::is_integral<T>::value || (
N >= 0),
1088 "The non-type template parameter N must be a non-negative integer for integral type T"));
1097 fixed_power<N / 2>(x * x));
1106 return boost::core::demangle(
typeid(t).name());
1111 template <
typename Iterator,
typename T>
1120 template <
typename Iterator,
typename T,
typename Comp>
1129 "The given iterators do not satisfy the proper ordering."));
1131 unsigned int len =
static_cast<unsigned int>(last -
first);
1148 if (!comp(*
first, val))
1153 if (!comp(*
first, val))
1158 if (!comp(*
first, val))
1163 if (!comp(*
first, val))
1168 if (!comp(*
first, val))
1173 if (!comp(*
first, val))
1178 if (!comp(*
first, val))
1197 const unsigned int half = len >> 1;
1198 const Iterator middle =
first + half;
1205 if (comp(*middle, val))
1225 template <
typename T>
1233 template <
typename T>
1237 std::is_trivially_copyable<T>::value && !std::is_same<T, bool>::value;
1242 template <
typename T>
1246 std::is_trivially_copyable<T>::value && !std::is_same<T, bool>::value;
1259 template <
typename T>
1262 std::vector<char> &)
1270 template <
typename T,
1271 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1272 std::is_trivially_copyable<T>::value>>
1275 const std::vector<T> &
object,
1276 std::vector<char> & dest_buffer)
1282 dest_buffer.reserve(dest_buffer.size() +
sizeof(vector_size) +
1283 vector_size *
sizeof(
T));
1286 dest_buffer.insert(dest_buffer.end(),
1287 reinterpret_cast<const char *
>(&vector_size),
1288 reinterpret_cast<const char *
>(&vector_size + 1));
1291 if (vector_size > 0)
1292 dest_buffer.insert(dest_buffer.end(),
1293 reinterpret_cast<const char *
>(
object.data()),
1294 reinterpret_cast<const char *
>(
object.data() +
1300 template <
typename T,
1301 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1302 std::is_trivially_copyable<T>::value>>
1305 const std::vector<std::vector<T>> &
object,
1306 std::vector<char> & dest_buffer)
1309 const size_type vector_size =
object.size();
1312 std::vector<size_type> sizes;
1313 sizes.reserve(vector_size);
1314 for (
const auto &a :
object)
1316 aggregated_size += a.size();
1317 sizes.push_back(a.size());
1322 dest_buffer.reserve(dest_buffer.size() +
1323 sizeof(vector_size) * (1 + vector_size) +
1324 aggregated_size *
sizeof(
T));
1327 dest_buffer.insert(dest_buffer.end(),
1328 reinterpret_cast<const char *
>(&vector_size),
1329 reinterpret_cast<const char *
>(&vector_size + 1));
1332 if (vector_size > 0)
1333 dest_buffer.insert(dest_buffer.end(),
1334 reinterpret_cast<const char *
>(sizes.data()),
1335 reinterpret_cast<const char *
>(sizes.data() +
1339 for (
const auto &a :
object)
1340 dest_buffer.insert(dest_buffer.end(),
1341 reinterpret_cast<const char *
>(a.data()),
1342 reinterpret_cast<const char *
>(a.data() + a.size()));
1347 template <
typename T>
1350 const std::vector<char>::const_iterator &,
1351 const std::vector<char>::const_iterator &,
1360 template <
typename T,
1361 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1362 std::is_trivially_copyable<T>::value>>
1365 const std::vector<char>::const_iterator &cbegin,
1366 const std::vector<char>::const_iterator &cend,
1367 std::vector<T> &
object)
1371 memcpy(&vector_size, &*cbegin,
sizeof(vector_size));
1373 Assert(
static_cast<std::ptrdiff_t
>(cend - cbegin) ==
1374 static_cast<std::ptrdiff_t
>(
sizeof(vector_size) +
1375 vector_size *
sizeof(
T)),
1376 ExcMessage(
"The given buffer has the wrong size."));
1381 if (vector_size > 0)
1382 object.insert(
object.
end(),
1383 reinterpret_cast<const T *
>(&*cbegin +
1384 sizeof(vector_size)),
1385 reinterpret_cast<const T *
>(&*cend));
1390 template <
typename T,
1391 typename = std::enable_if_t<!std::is_same<T, bool>::value &&
1392 std::is_trivially_copyable<T>::value>>
1395 const std::vector<char>::const_iterator &cbegin,
1396 const std::vector<char>::const_iterator &cend,
1397 std::vector<std::vector<T>> &
object)
1401 std::vector<char>::const_iterator iterator = cbegin;
1403 memcpy(&vector_size, &*iterator,
sizeof(vector_size));
1405 object.resize(vector_size);
1406 std::vector<size_type> sizes(vector_size);
1407 if (vector_size > 0)
1408 memcpy(sizes.data(),
1409 &*iterator +
sizeof(vector_size),
1412 iterator +=
sizeof(vector_size) * (1 + vector_size);
1414 for (
const auto a : sizes)
1415 aggregated_size += a;
1417 Assert(
static_cast<std::ptrdiff_t
>(cend - iterator) ==
1418 static_cast<std::ptrdiff_t
>(aggregated_size *
sizeof(
T)),
1419 ExcMessage(
"The given buffer has the wrong size."));
1423 for (
unsigned int i = 0; i < vector_size; ++i)
1426 object[i].insert(
object[i].
end(),
1427 reinterpret_cast<const T *
>(&*iterator),
1428 reinterpret_cast<const T *
>(&*iterator +
1429 sizeof(
T) * sizes[i]));
1430 iterator +=
sizeof(
T) * sizes[i];
1434 ExcMessage(
"The given buffer has the wrong size."));
1441 template <
typename T>
1444 std::vector<char> &dest_buffer,
1445 const bool allow_compression)
1447 std::size_t size = 0;
1453 #ifdef DEAL_II_HAVE_CXX17
1454 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(
T) < 256)
1456 if (std::is_trivially_copyable<T>() &&
sizeof(
T) < 256)
1459 (void)allow_compression;
1460 const std::size_t previous_size = dest_buffer.size();
1461 dest_buffer.resize(previous_size +
sizeof(
T));
1463 std::memcpy(dest_buffer.data() + previous_size, &
object,
sizeof(
T));
1473 (allow_compression ==
false))
1475 const std::size_t previous_size = dest_buffer.size();
1484 size = dest_buffer.size() - previous_size;
1490 const std::size_t previous_size = dest_buffer.size();
1492 boost::iostreams::filtering_ostreambuf fosb;
1493 #ifdef DEAL_II_WITH_ZLIB
1494 if (allow_compression)
1495 fosb.push(boost::iostreams::gzip_compressor());
1497 (void)allow_compression;
1499 fosb.push(boost::iostreams::back_inserter(dest_buffer));
1501 boost::archive::binary_oarchive boa(fosb);
1506 size = dest_buffer.size() - previous_size;
1513 template <
typename T>
1515 pack(
const T &
object,
const bool allow_compression)
1517 std::vector<char> buffer;
1518 pack<T>(
object, buffer, allow_compression);
1524 template <
typename T>
1526 unpack(
const std::vector<char>::const_iterator &cbegin,
1527 const std::vector<char>::const_iterator &cend,
1528 const bool allow_compression)
1533 #ifdef DEAL_II_HAVE_CXX17
1534 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(
T) < 256)
1536 if (std::is_trivially_copyable<T>() &&
sizeof(
T) < 256)
1541 (void)allow_compression;
1543 std::memcpy(&
object, &*cbegin,
sizeof(
T));
1553 (allow_compression ==
false))
1568 boost::iostreams::filtering_istreambuf fisb;
1569 #ifdef DEAL_II_WITH_ZLIB
1570 if (allow_compression)
1571 fisb.push(boost::iostreams::gzip_decompressor());
1573 (void)allow_compression;
1575 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1577 boost::archive::binary_iarchive bia(fisb);
1588 template <
typename T>
1590 unpack(
const std::vector<char> &buffer,
const bool allow_compression)
1592 return unpack<T>(buffer.cbegin(), buffer.cend(), allow_compression);
1596 template <
typename T,
int N>
1598 unpack(
const std::vector<char>::const_iterator &cbegin,
1599 const std::vector<char>::const_iterator &cend,
1600 T (&unpacked_object)[
N],
1601 const bool allow_compression)
1606 #ifdef DEAL_II_HAVE_CXX17
1607 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(
T) *
N < 256)
1609 if (std::is_trivially_copyable<T>() &&
sizeof(
T) *
N < 256)
1612 Assert(std::distance(cbegin, cend) ==
sizeof(
T) *
N,
1614 std::memcpy(unpacked_object, &*cbegin,
sizeof(
T) *
N);
1619 boost::iostreams::filtering_istreambuf fisb;
1620 #ifdef DEAL_II_WITH_ZLIB
1621 if (allow_compression)
1622 fisb.push(boost::iostreams::gzip_decompressor());
1624 (void)allow_compression;
1626 fisb.push(boost::iostreams::array_source(&*cbegin, cend - cbegin));
1628 boost::archive::binary_iarchive bia(fisb);
1629 bia >> unpacked_object;
1634 template <
typename T,
int N>
1637 T (&unpacked_object)[
N],
1638 const bool allow_compression)
1640 unpack<T, N>(buffer.cbegin(),
1649 get_bit(
const unsigned char number,
const unsigned int n)
1656 return ((number >> n) & 1U) != 0u;
1662 set_bit(
unsigned char &number,
const unsigned int n,
const bool x)
1669 number ^= (-
static_cast<unsigned char>(x) ^ number) & (1U << n);
1674 template <
typename To,
typename From>
1675 inline std::unique_ptr<To>
1681 if (To *cast =
dynamic_cast<To *
>(p.get()))
1683 std::unique_ptr<To> result(cast);
1688 throw std::bad_cast();
1693 template <
typename T>
1702 template <
typename T>
1711 template <
typename T>
1720 template <
typename T>
1729 template <
typename T>
1738 template <
typename Integer>
1739 std::vector<Integer>
1742 const std::size_t n = permutation.size();
1744 std::vector<Integer> out(n);
1745 for (std::size_t i = 0; i < n; ++i)
1746 out[i] = n - 1 - permutation[i];
1753 template <
typename Integer>
1754 std::vector<Integer>
1757 const std::size_t n = permutation.size();
1761 for (std::size_t i = 0; i < n; ++i)
1764 out[permutation[i]] = i;
1769 for (std::size_t i = 0; i < n; ++i)
1771 ExcMessage(
"The given input permutation had duplicate entries!"));
1783 namespace serialization
1790 template <
class Archive,
typename... Args>
1792 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1794 ar &std::get<
N - 1>(t);
1795 Serialize<N - 1>::serialize(ar, t, version);
1802 template <
class Archive,
typename... Args>
1804 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1812 template <
class Archive,
typename... Args>
1814 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1816 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
static ::ExceptionBase & ExcInternalError()
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
types::global_dof_index size_type
VectorType::value_type * end(VectorType &V)
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()
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::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::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::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
static constexpr bool value