Reference documentation for deal.II version 8.1.0
utilities.h
1 // ---------------------------------------------------------------------
2 // @f$Id: utilities.h 30154 2013-07-25 10:53:32Z bangerth @f$
3 //
4 // Copyright (C) 2005 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__utilities_h
18 #define __deal2__utilities_h
19 
20 #include <deal.II/base/config.h>
22 #include <deal.II/base/mpi.h>
23 
24 #include <vector>
25 #include <utility>
26 #include <functional>
27 #include <string>
28 
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>
34 # else
35 # include <Epetra_SerialComm.h>
36 # endif
37 #endif
38 
40 
41 
50 namespace Utilities
51 {
52 
64  std::string
65  int_to_string (const unsigned int i,
66  const unsigned int digits = numbers::invalid_unsigned_int);
67 
73  unsigned int
74  needed_digits (const unsigned int max_number);
75 
81  int
82  string_to_int (const std::string &s);
83 
84 
90  std::vector<int>
91  string_to_int (const std::vector<std::string> &s);
92 
98  double
99  string_to_double (const std::string &s);
100 
101 
107  std::vector<double>
108  string_to_double (const std::vector<std::string> &s);
109 
120  std::vector<std::string>
121  split_string_list (const std::string &s,
122  const char delimiter = ',');
123 
138  std::vector<std::string>
139  break_text_into_lines (const std::string &original_text,
140  const unsigned int width,
141  const char delimiter = ' ');
142 
148  bool
149  match_at_string_start (const std::string &name,
150  const std::string &pattern);
151 
165  std::pair<int, unsigned int>
166  get_integer_at_position (const std::string &name,
167  const unsigned int position);
168 
197  double
198  generate_normal_random_number (const double a,
199  const double sigma);
200 
201 
214  template <int N, typename T>
215  T
216  fixed_power (const T t);
217 
232  template <int a, int N>
234  {
235  static const int value = a *fixed_int_power<a,N-1>::value;
236  };
237 
243  template <int a>
244  struct fixed_int_power<a,0>
245  {
246  static const int value = 1;
247  };
248 
291  template<typename Iterator, typename T>
292  Iterator
293  lower_bound (Iterator first,
294  Iterator last,
295  const T &val);
296 
297 
304  template<typename Iterator, typename T, typename Comp>
305  Iterator
306  lower_bound (Iterator first,
307  Iterator last,
308  const T &val,
309  const Comp comp);
310 
318  std::vector<unsigned int>
319  reverse_permutation (const std::vector<unsigned int> &permutation);
320 
329  std::vector<unsigned int>
330  invert_permutation (const std::vector<unsigned int> &permutation);
331 
339  std::vector<unsigned long long int>
340  reverse_permutation (const std::vector<unsigned long long int> &permutation);
341 
350  std::vector<unsigned long long int>
351  invert_permutation (const std::vector<unsigned long long int> &permutation);
352 
359  namespace System
360  {
361 
372  double get_cpu_load ();
373 
380  struct MemoryStats
381  {
382  unsigned long int VmPeak;
383  unsigned long int VmSize;
384  unsigned long int VmHWM;
385  unsigned long int VmRSS;
386  };
387 
388 
395  void get_memory_stats (MemoryStats &stats);
396 
397 
402  std::string get_hostname ();
403 
404 
408  std::string get_time ();
409 
432  bool job_supports_mpi ();
433 
440 
457  unsigned int get_n_mpi_processes (const MPI_Comm &mpi_communicator) DEAL_II_DEPRECATED;
458 
465  unsigned int get_this_mpi_process (const MPI_Comm &mpi_communicator) DEAL_II_DEPRECATED;
466 
467 
474  using
475  Utilities::MPI::compute_point_to_point_communication_pattern;
476 
477 
484  using Utilities::MPI::duplicate_communicator;
485 
491  using Utilities::MPI::MinMaxAvg;
492 
498  void
499  calculate_collective_mpi_min_max_avg (const MPI_Comm &mpi_communicator,
500  const double my_value,
501  MinMaxAvg &result) DEAL_II_DEPRECATED;
502 
508  using Utilities::MPI::MPI_InitFinalize;
509 
511  }
512 
513 
514 #ifdef DEAL_II_WITH_TRILINOS
515 
520  namespace Trilinos
521  {
536  const Epetra_Comm &comm_world();
537 
552  const Epetra_Comm &comm_self();
553 
611  Epetra_Comm *
612  duplicate_communicator (const Epetra_Comm &communicator);
613 
650  void
651  destroy_communicator (Epetra_Comm &communicator);
652 
659  unsigned int get_n_mpi_processes (const Epetra_Comm &mpi_communicator);
660 
671  unsigned int get_this_mpi_process (const Epetra_Comm &mpi_communicator);
672 
688  Epetra_Map
689  duplicate_map (const Epetra_BlockMap &map,
690  const Epetra_Comm &comm);
691  }
692 
693 #endif
694 
695 
696 }
697 
698 
699 // --------------------- inline functions
700 
701 namespace Utilities
702 {
703  template <int N, typename T>
704  inline
705  T fixed_power (const T n)
706  {
707  Assert (N>0, ExcNotImplemented());
708  switch (N)
709  {
710  case 1:
711  return n;
712  case 2:
713  return n*n;
714  case 3:
715  return n*n*n;
716  case 4:
717  return n*n*n*n;
718  default:
719  T result = n;
720  for (int d=1; d<N; ++d)
721  result *= n;
722  return result;
723  }
724  }
725 
726 
727 
728  template<typename Iterator, typename T>
729  inline
730  Iterator
731  lower_bound (Iterator first,
732  Iterator last,
733  const T &val)
734  {
735  return Utilities::lower_bound (first, last, val,
736  std::less<T>());
737  }
738 
739 
740 
741  template<typename Iterator, typename T, typename Comp>
742  inline
743  Iterator
744  lower_bound (Iterator first,
745  Iterator last,
746  const T &val,
747  const Comp comp)
748  {
749  // verify that the two iterators are properly ordered. since
750  // we need operator- for the iterator type anyway, do the
751  // test as follows, rather than via 'last >= first'
752  Assert (last - first >= 0,
753  ExcMessage ("The given iterators do not satisfy the proper ordering."));
754 
755  unsigned int len = static_cast<unsigned int>(last-first);
756 
757  if (len==0)
758  return first;
759 
760  while (true)
761  {
762  // if length equals 8 or less,
763  // then do a rolled out
764  // search. use a switch without
765  // breaks for that and roll-out
766  // the loop somehow
767  if (len < 8)
768  {
769  switch (len)
770  {
771  case 7:
772  if (!comp(*first, val))
773  return first;
774  ++first;
775  case 6:
776  if (!comp(*first, val))
777  return first;
778  ++first;
779  case 5:
780  if (!comp(*first, val))
781  return first;
782  ++first;
783  case 4:
784  if (!comp(*first, val))
785  return first;
786  ++first;
787  case 3:
788  if (!comp(*first, val))
789  return first;
790  ++first;
791  case 2:
792  if (!comp(*first, val))
793  return first;
794  ++first;
795  case 1:
796  if (!comp(*first, val))
797  return first;
798  return first+1;
799  default:
800  // indices seem
801  // to not be
802  // sorted
803  // correctly!? or
804  // did len
805  // become==0
806  // somehow? that
807  // shouln't have
808  // happened
809  Assert (false, ExcInternalError());
810  }
811  }
812 
813 
814 
815  const unsigned int half = len >> 1;
816  const Iterator middle = first + half;
817 
818  // if the value is larger than
819  // that pointed to by the
820  // middle pointer, then the
821  // insertion point must be
822  // right of it
823  if (comp(*middle, val))
824  {
825  first = middle + 1;
826  len -= half + 1;
827  }
828  else
829  len = half;
830  }
831  }
832 }
833 
834 
835 DEAL_II_NAMESPACE_CLOSE
836 
837 #endif
Iterator lower_bound(Iterator first, Iterator last, const T &val)
Definition: utilities.h:731
void calculate_collective_mpi_min_max_avg(const MPI_Comm &mpi_communicator, const double my_value, MinMaxAvg &result) DEAL_II_DEPRECATED
static const unsigned int invalid_unsigned_int
Definition: types.h:191
std::vector< unsigned int > reverse_permutation(const std::vector< unsigned int > &permutation)
const Epetra_Comm & comm_world()
::ExceptionBase & ExcMessage(std::string arg1)
void get_memory_stats(MemoryStats &stats)
double string_to_double(const std::string &s)
double get_cpu_load()
T fixed_power(const T t)
Definition: utilities.h:705
double generate_normal_random_number(const double a, const double sigma)
#define Assert(cond, exc)
Definition: exceptions.h:299
std::vector< std::string > break_text_into_lines(const std::string &original_text, const unsigned int width, const char delimiter= ' ')
unsigned long int VmSize
Definition: utilities.h:383
bool match_at_string_start(const std::string &name, const std::string &pattern)
std::string int_to_string(const unsigned int i, const unsigned int digits=numbers::invalid_unsigned_int)
unsigned int get_n_mpi_processes(const MPI_Comm &mpi_communicator) DEAL_II_DEPRECATED
std::pair< int, unsigned int > get_integer_at_position(const std::string &name, const unsigned int position)
const Epetra_Comm & comm_self()
BlockCompressedSparsityPattern CompressedBlockSparsityPattern DEAL_II_DEPRECATED
std::vector< unsigned int > invert_permutation(const std::vector< unsigned int > &permutation)
bool program_uses_mpi() DEAL_II_DEPRECATED
std::vector< std::string > split_string_list(const std::string &s, const char delimiter= ',')
void destroy_communicator(Epetra_Comm &communicator)
std::string get_hostname()
unsigned long int VmHWM
Definition: utilities.h:384
int string_to_int(const std::string &s)
::ExceptionBase & ExcNotImplemented()
unsigned long int VmRSS
Definition: utilities.h:385
Epetra_Map duplicate_map(const Epetra_BlockMap &map, const Epetra_Comm &comm)
::ExceptionBase & ExcInternalError()
std::string get_time()
unsigned int needed_digits(const unsigned int max_number)
unsigned int get_this_mpi_process(const MPI_Comm &mpi_communicator) DEAL_II_DEPRECATED