16 #ifndef dealii_utilities_h 17 #define dealii_utilities_h 19 #include <deal.II/base/config.h> 20 #include <deal.II/base/exceptions.h> 27 #include <type_traits> 29 #ifdef DEAL_II_WITH_TRILINOS 30 # include <Epetra_Comm.h> 31 # include <Epetra_Map.h> 32 # ifdef DEAL_II_WITH_MPI 33 # include <Epetra_MpiComm.h> 35 # include <Epetra_SerialComm.h> 39 #include <boost/archive/binary_oarchive.hpp> 40 #include <boost/archive/binary_iarchive.hpp> 41 #include <boost/serialization/vector.hpp> 42 #include <boost/serialization/array.hpp> 44 #ifdef DEAL_II_WITH_ZLIB 45 # include <boost/iostreams/stream.hpp> 46 # include <boost/iostreams/filtering_stream.hpp> 47 # include <boost/iostreams/device/back_inserter.hpp> 48 # include <boost/iostreams/filter/gzip.hpp> 51 DEAL_II_NAMESPACE_OPEN
102 template <
typename number>
132 std::string
dim_string(
const int dim,
const int spacedim);
199 std::vector<std::string>
201 const std::string &delimiter =
",");
208 std::vector<std::string>
210 const char delimiter);
222 std::vector<std::string>
224 const unsigned int width,
225 const char delimiter =
' ');
233 const std::string &pattern);
243 std::pair<int, unsigned int>
245 const unsigned int position);
252 const std::string &from,
253 const std::string &to);
261 trim(
const std::string &input);
301 template <
int N,
typename T>
329 template <
int a,
int N>
345 static const int value = 1;
354 pow(
const unsigned int base,
const unsigned int iexp)
356 return iexp == 0 ? 1 : base*::Utilities::pow(base, iexp - 1);
380 template <
typename Iterator,
typename T>
392 template <
typename Iterator,
typename T,
typename Comp>
404 std::vector<unsigned int>
412 std::vector<unsigned int>
420 std::vector<unsigned long long int>
428 std::vector<unsigned long long int>
447 template <
typename T>
448 size_t pack (
const T &
object, std::vector<char> &dest_buffer);
456 template <
typename T>
457 std::vector<char>
pack (
const T &
object);
487 template <
typename T>
488 T
unpack (
const std::vector<char> &buffer);
496 template <
typename T>
497 T
unpack (
const std::vector<char>::iterator &begin,
498 const std::vector<char>::iterator &end);
530 template <
typename T,
int N>
531 void unpack (
const std::vector<char> &buffer,
532 T (&unpacked_object)[N]);
540 template <
typename T,
int N>
541 void unpack (
const std::vector<char>::iterator &begin,
542 const std::vector<char>::iterator &end,
543 T (&unpacked_object)[N]);
590 template <
typename To,
typename From>
597 if (To *cast = dynamic_cast<To *>(p.get()))
599 std::unique_ptr<To> result(cast);
604 throw std::bad_cast();
724 void posix_memalign (
void **memptr,
size_t alignment,
size_t size);
728 #ifdef DEAL_II_WITH_TRILINOS 848 const Epetra_Comm &comm);
861 template <
int N,
typename T>
869 return ::internal::NumberType<T>::value(1);
880 for (
int d=1; d<N; ++d)
888 template <
typename Iterator,
typename T>
901 template <
typename Iterator,
typename T,
typename Comp>
912 Assert (last - first >= 0,
913 ExcMessage (
"The given iterators do not satisfy the proper ordering."));
915 unsigned int len =
static_cast<unsigned int>(last-first);
932 if (!comp(*first, val))
937 if (!comp(*first, val))
942 if (!comp(*first, val))
947 if (!comp(*first, val))
952 if (!comp(*first, val))
957 if (!comp(*first, val))
962 if (!comp(*first, val))
981 const unsigned int half = len >> 1;
982 const Iterator middle = first + half;
989 if (comp(*middle, val))
1002 template <
typename T>
1003 size_t pack (
const T &
object, std::vector<char> &dest_buffer)
1012 #if __GNUG__ && __GNUC__ < 5 1013 if ( __has_trivial_copy(T) &&
sizeof(T)<256)
1015 # ifdef DEAL_II_WITH_CXX17 1016 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T)<256)
1018 if (std::is_trivially_copyable<T>() &&
sizeof(T)<256)
1022 const size_t previous_size = dest_buffer.size();
1023 dest_buffer.resize (previous_size +
sizeof(T));
1025 std::memcpy (dest_buffer.data() + previous_size, &object,
sizeof(T));
1033 const size_t previous_size = dest_buffer.size();
1035 #ifdef DEAL_II_WITH_ZLIB 1036 boost::iostreams::filtering_ostream out;
1037 out.push(boost::iostreams::gzip_compressor
1038 (boost::iostreams::gzip_params
1039 (boost::iostreams::gzip::best_compression)));
1040 out.push(boost::iostreams::back_inserter(dest_buffer));
1042 boost::archive::binary_oarchive archive(out);
1046 std::ostringstream out;
1047 boost::archive::binary_oarchive archive(out);
1050 const std::string &s = out.str();
1051 dest_buffer.reserve (dest_buffer.size() + s.size());
1052 std::move (s.begin(), s.end(), std::back_inserter(dest_buffer));
1055 return (dest_buffer.size() - previous_size);
1060 template <
typename T>
1061 std::vector<char>
pack (
const T &
object)
1063 std::vector<char> buffer;
1064 pack<T> (object, buffer);
1069 template <
typename T>
1070 T
unpack (
const std::vector<char>::const_iterator &cbegin,
1071 const std::vector<char>::const_iterator &cend)
1080 #if __GNUG__ && __GNUC__ < 5 1081 if ( __has_trivial_copy(T) &&
sizeof(T)<256)
1083 # ifdef DEAL_II_WITH_CXX17 1084 if constexpr (std::is_trivially_copyable<T>() &&
sizeof(T)<256)
1086 if (std::is_trivially_copyable<T>() &&
sizeof(T)<256)
1092 std::memcpy (&
object, &*cbegin,
sizeof(T));
1097 std::string decompressed_buffer;
1102 #ifdef DEAL_II_WITH_ZLIB 1103 boost::iostreams::filtering_ostream decompressing_stream;
1104 decompressing_stream.push(boost::iostreams::gzip_decompressor());
1105 decompressing_stream.push(boost::iostreams::back_inserter(decompressed_buffer));
1106 decompressing_stream.write (&*cbegin, std::distance(cbegin, cend));
1108 decompressed_buffer.assign (cbegin, cend);
1113 std::istringstream in(decompressed_buffer);
1114 boost::archive::binary_iarchive archive(in);
1122 template <
typename T>
1125 return unpack<T> (buffer.cbegin(), buffer.cend());
1129 template <
typename T,
int N>
1130 void unpack (
const std::vector<char>::const_iterator &cbegin,
1131 const std::vector<char>::const_iterator &cend,
1132 T (&unpacked_object)[N])
1142 #
if __GNUG__ && __GNUC__ < 5
1143 __has_trivial_copy(T)
1145 std::is_trivially_copyable<T>()
1151 std::memcpy (unpacked_object, &*cbegin,
sizeof(T)*N);
1155 std::string decompressed_buffer;
1159 #ifdef DEAL_II_WITH_ZLIB 1160 boost::iostreams::filtering_ostream decompressing_stream;
1161 decompressing_stream.push(boost::iostreams::gzip_decompressor());
1162 decompressing_stream.push(boost::iostreams::back_inserter(decompressed_buffer));
1163 decompressing_stream.write (&*cbegin, std::distance(cbegin, cend));
1165 decompressed_buffer.assign (cbegin, cend);
1170 std::istringstream in(decompressed_buffer);
1171 boost::archive::binary_iarchive archive(in);
1173 archive >> unpacked_object;
1178 template <
typename T,
int N>
1179 void unpack (
const std::vector<char> &buffer,
1180 T (&unpacked_object)[N])
1182 unpack<T,N> (buffer.cbegin(), buffer.cend(), unpacked_object);
1188 DEAL_II_NAMESPACE_CLOSE
1193 namespace serialization
1200 template<
class Archive,
typename... Args>
1201 static void serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1203 ar &std::get<N-1>(t);
1204 Serialize<N-1>::serialize(ar, t, version);
1211 template<
class Archive,
typename... Args>
1212 static void serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1220 template<
class Archive,
typename... Args>
1221 void serialize(Archive &ar, std::tuple<Args...> &t,
const unsigned int version)
1223 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=",")
static const unsigned int invalid_unsigned_int
constexpr unsigned int pow(const unsigned int base, const unsigned int iexp)
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=' ')
T unpack(const std::vector< char > &buffer)
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)
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)
bool match_at_string_start(const std::string &name, const std::string &pattern)
void posix_memalign(void **memptr, size_t alignment, size_t size)
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)
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)
void destroy_communicator(Epetra_Comm &communicator)
std::string get_hostname()
size_t pack(const T &object, std::vector< char > &dest_buffer)
const Epetra_Comm & comm_world()
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()