16 #ifndef dealii_utilities_h 17 #define dealii_utilities_h 19 #include <deal.II/base/config.h> 21 #include <deal.II/base/exceptions.h> 26 #include <type_traits> 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> 43 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
45 #include <boost/archive/binary_iarchive.hpp> 46 #include <boost/archive/binary_oarchive.hpp> 47 #include <boost/core/demangle.hpp> 48 #include <boost/serialization/array.hpp> 49 #include <boost/serialization/complex.hpp> 50 #include <boost/serialization/vector.hpp> 52 #ifdef DEAL_II_WITH_ZLIB 53 # include <boost/iostreams/device/back_inserter.hpp> 54 # include <boost/iostreams/filter/gzip.hpp> 55 # include <boost/iostreams/filtering_stream.hpp> 56 # include <boost/iostreams/stream.hpp> 59 DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
61 DEAL_II_NAMESPACE_OPEN
64 template <
int dim,
typename Number>
102 template <
int dim,
typename Number>
103 std::vector<std::array<std::uint64_t, dim>>
106 const int bits_per_dim = 64);
112 std::vector<std::array<std::uint64_t, dim>>
114 const std::vector<std::array<std::uint64_t, dim>> &points,
115 const int bits_per_dim = 64);
135 const int bits_per_dim);
167 template <
typename number>
198 dim_string(
const int dim,
const int spacedim);
265 std::vector<std::string>
273 std::vector<std::string>
286 std::vector<std::string>
288 const unsigned int width,
289 const char delimiter =
' ');
306 std::pair<int, unsigned int>
315 const std::string &from,
316 const std::string &to);
324 trim(
const std::string &input);
375 template <
int N,
typename T>
403 template <
int a,
int N>
419 static const int value = 1;
427 constexpr
unsigned int 428 pow(
const unsigned int base,
const int iexp)
430 #if defined(DEBUG) && defined(DEAL_II_WITH_CXX14) && \ 431 defined(DEAL_II_HAVE_CXX14_CONSTEXPR_CAN_CALL_NONCONSTEXPR) 435 ::deal_II_exceptions::internals::issue_error_noreturn(
441 "ExcMessage(\"The exponent must not be negative!\")",
442 ExcMessage(
"The exponent must not be negative!"));
461 return iexp <= 0 ? 1 :
462 (((iexp % 2 == 1) ? base : 1) *
463 ::Utilities::pow(base * base, iexp / 2));
487 template <
typename Iterator,
typename T>
489 lower_bound(Iterator first, Iterator last,
const T &val);
497 template <
typename Iterator,
typename T,
typename Comp>
499 lower_bound(Iterator first, Iterator last,
const T &val,
const Comp comp);
506 std::vector<unsigned int>
514 std::vector<unsigned int>
522 std::vector<unsigned long long int>
530 std::vector<unsigned long long int>
550 template <
typename T>
552 pack(
const T &
object,
553 std::vector<char> &dest_buffer,
554 const bool allow_compression =
true);
566 template <
typename T>
568 pack(
const T &
object,
const bool allow_compression =
true);
602 template <
typename T>
604 unpack(
const std::vector<char> &buffer,
const bool allow_compression =
true);
616 template <
typename T>
618 unpack(
const std::vector<char>::const_iterator &cbegin,
619 const std::vector<char>::const_iterator &cend,
620 const bool allow_compression =
true);
656 template <
typename T,
int N>
658 unpack(
const std::vector<char> &buffer,
659 T (&unpacked_object)[N],
660 const bool allow_compression =
true);
672 template <
typename T,
int N>
674 unpack(
const std::vector<char>::const_iterator &cbegin,
675 const std::vector<char>::const_iterator &cend,
676 T (&unpacked_object)[N],
677 const bool allow_compression =
true);
736 template <
typename To,
typename From>
743 if (To *cast = dynamic_cast<To *>(p.get()))
745 std::unique_ptr<To> result(cast);
750 throw std::bad_cast();
878 posix_memalign(
void **memptr, std::size_t alignment, std::size_t size);
882 #ifdef DEAL_II_WITH_TRILINOS 922 const Teuchos::RCP<const Teuchos::Comm<int>> &
1016 duplicate_map(
const Epetra_BlockMap &map,
const Epetra_Comm &comm);
1029 template <
int N,
typename T>
1037 return ::internal::NumberType<T>::value(1);
1045 return n * n * n * n;
1048 for (
int d = 1; d < N; ++d)
1060 return boost::core::demangle(
typeid(t).name());
1065 template <
typename Iterator,
typename T>
1074 template <
typename Iterator,
typename T,
typename Comp>
1076 lower_bound(Iterator first, Iterator last,
const T &val,
const Comp comp)
1081 Assert(last - first >= 0,
1083 "The given iterators do not satisfy the proper ordering."));
1085 unsigned int len =
static_cast<unsigned int>(last - first);
1102 if (!comp(*first, val))
1105 DEAL_II_FALLTHROUGH;
1107 if (!comp(*first, val))
1110 DEAL_II_FALLTHROUGH;
1112 if (!comp(*first, val))
1115 DEAL_II_FALLTHROUGH;
1117 if (!comp(*first, val))
1120 DEAL_II_FALLTHROUGH;
1122 if (!comp(*first, val))
1125 DEAL_II_FALLTHROUGH;
1127 if (!comp(*first, val))
1130 DEAL_II_FALLTHROUGH;
1132 if (!comp(*first, val))
1151 const unsigned int half = len >> 1;
1152 const Iterator middle = first + half;
1159 if (comp(*middle, val))
1172 template <
typename T>
1175 std::vector<char> &dest_buffer,
1176 const bool allow_compression)
1179 (void)allow_compression;
1181 std::size_t size = 0;
1190 #if __GNUG__ && __GNUC__ < 5 1191 if (__has_trivial_copy(T) &&
sizeof(T) < 256)
1193 # ifdef DEAL_II_WITH_CXX17 1194 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1196 if (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1200 const std::size_t previous_size = dest_buffer.size();
1201 dest_buffer.resize(previous_size +
sizeof(T));
1203 std::memcpy(dest_buffer.data() + previous_size, &object,
sizeof(T));
1211 const std::size_t previous_size = dest_buffer.size();
1212 #ifdef DEAL_II_WITH_ZLIB 1213 if (allow_compression)
1215 boost::iostreams::filtering_ostream out;
1217 boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(
1218 boost::iostreams::gzip::best_compression)));
1219 out.push(boost::iostreams::back_inserter(dest_buffer));
1221 boost::archive::binary_oarchive archive(out);
1228 std::ostringstream out;
1229 boost::archive::binary_oarchive archive(out);
1232 const std::string &s = out.str();
1233 dest_buffer.reserve(dest_buffer.size() + s.size());
1234 std::move(s.begin(), s.end(), std::back_inserter(dest_buffer));
1237 size = dest_buffer.size() - previous_size;
1244 template <
typename T>
1246 pack(
const T &
object,
const bool allow_compression)
1248 std::vector<char> buffer;
1249 pack<T>(object, buffer, allow_compression);
1254 template <
typename T>
1256 unpack(
const std::vector<char>::const_iterator &cbegin,
1257 const std::vector<char>::const_iterator &cend,
1258 const bool allow_compression)
1263 (void)allow_compression;
1272 #if __GNUG__ && __GNUC__ < 5 1273 if (__has_trivial_copy(T) &&
sizeof(T) < 256)
1275 # ifdef DEAL_II_WITH_CXX17 1276 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1278 if (std::is_trivially_copyable<T>() &&
sizeof(T) < 256)
1283 std::memcpy(&
object, &*cbegin,
sizeof(T));
1287 std::string decompressed_buffer;
1290 #ifdef DEAL_II_WITH_ZLIB 1291 if (allow_compression)
1293 boost::iostreams::filtering_ostream decompressing_stream;
1294 decompressing_stream.push(boost::iostreams::gzip_decompressor());
1295 decompressing_stream.push(
1296 boost::iostreams::back_inserter(decompressed_buffer));
1297 decompressing_stream.write(&*cbegin, std::distance(cbegin, cend));
1302 decompressed_buffer.assign(cbegin, cend);
1306 std::istringstream in(decompressed_buffer);
1307 boost::archive::binary_iarchive archive(in);
1316 template <
typename T>
1318 unpack(
const std::vector<char> &buffer,
const bool allow_compression)
1320 return unpack<T>(buffer.cbegin(), buffer.cend(), allow_compression);
1324 template <
typename T,
int N>
1326 unpack(
const std::vector<char>::const_iterator &cbegin,
1327 const std::vector<char>::const_iterator &cend,
1328 T (&unpacked_object)[N],
1329 const bool allow_compression)
1339 #
if __GNUG__ && __GNUC__ < 5
1340 __has_trivial_copy(T)
1342 std::is_trivially_copyable<T>()
1344 &&
sizeof(T) * N < 256)
1346 Assert(std::distance(cbegin, cend) ==
sizeof(T) * N,
1348 std::memcpy(unpacked_object, &*cbegin,
sizeof(T) * N);
1352 std::string decompressed_buffer;
1355 (void)allow_compression;
1356 #ifdef DEAL_II_WITH_ZLIB 1357 if (allow_compression)
1359 boost::iostreams::filtering_ostream decompressing_stream;
1360 decompressing_stream.push(boost::iostreams::gzip_decompressor());
1361 decompressing_stream.push(
1362 boost::iostreams::back_inserter(decompressed_buffer));
1363 decompressing_stream.write(&*cbegin, std::distance(cbegin, cend));
1368 decompressed_buffer.assign(cbegin, cend);
1372 std::istringstream in(decompressed_buffer);
1373 boost::archive::binary_iarchive archive(in);
1375 archive >> unpacked_object;
1380 template <
typename T,
int N>
1383 T (&unpacked_object)[N],
1384 const bool allow_compression)
1386 unpack<T, N>(buffer.cbegin(),
1395 DEAL_II_NAMESPACE_CLOSE
1400 namespace serialization
1407 template <
class Archive,
typename... Args>
1409 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1411 ar &std::get<N - 1>(t);
1412 Serialize<N - 1>::serialize(ar, t, version);
1419 template <
class Archive,
typename... Args>
1421 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1429 template <
class Archive,
typename... Args>
1431 serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1433 Serialize<
sizeof...(Args)>::serialize(ar, t, version);
Iterator lower_bound(Iterator first, Iterator last, const T &val)
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
void posix_memalign(void **memptr, std::size_t alignment, std::size_t size)
static const unsigned int invalid_unsigned_int
std::uint64_t pack_integers(const std::array< std::uint64_t, dim > &index, const int bits_per_dim)
std::string type_to_string(const T &t)
std::string dealii_version_string()
unsigned int get_n_mpi_processes(const Epetra_Comm &mpi_communicator)
std::string trim(const std::string &input)
std::pair< int, unsigned int > get_integer_at_position(const std::string &name, const unsigned int position)
std::vector< std::string > break_text_into_lines(const std::string &original_text, const unsigned int width, const char delimiter=' ')
const Epetra_Comm & comm_self()
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
void get_memory_stats(MemoryStats &stats)
std::unique_ptr< To > dynamic_unique_cast(std::unique_ptr< From > &&p)
double string_to_double(const std::string &s)
const Teuchos::RCP< const Teuchos::Comm< int > > & tpetra_comm_self()
static ::ExceptionBase & ExcMessage(std::string arg1)
Epetra_Comm * duplicate_communicator(const Epetra_Comm &communicator)
double generate_normal_random_number(const double a, const double sigma)
#define Assert(cond, exc)
constexpr unsigned int pow(const unsigned int base, const int iexp)
bool match_at_string_start(const std::string &name, const std::string &pattern)
std::vector< unsigned int > invert_permutation(const std::vector< unsigned int > &permutation)
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
size_t pack(const T &object, std::vector< char > &dest_buffer, const bool allow_compression=true)
std::string replace_in_string(const std::string &input, const std::string &from, const std::string &to)
std::string dim_string(const int dim, const int spacedim)
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)
void destroy_communicator(Epetra_Comm &communicator)
std::string get_hostname()
const Epetra_Comm & comm_world()
T unpack(const std::vector< char > &buffer, const bool allow_compression=true)
int string_to_int(const std::string &s)
std::vector< unsigned int > reverse_permutation(const std::vector< unsigned int > &permutation)
static ::ExceptionBase & ExcNotImplemented()
unsigned int get_this_mpi_process(const Epetra_Comm &mpi_communicator)
const std::string get_current_vectorization_level()
Epetra_Map duplicate_map(const Epetra_BlockMap &map, const Epetra_Comm &comm)
unsigned int needed_digits(const unsigned int max_number)
static ::ExceptionBase & ExcInternalError()