include/deal.II/base/utilities.h

00001 //---------------------------------------------------------------------------
00002 //    @f$Id: utilities.h 25493 2012-05-05 18:36:25Z kormann @f$
00003 //
00004 //    Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by the deal.II authors
00005 //
00006 //    This file is subject to QPL and may not be  distributed
00007 //    without copyright and license information. Please refer
00008 //    to the file deal.II/doc/license.html for the  text  and
00009 //    further information on this license.
00010 //
00011 //---------------------------------------------------------------------------
00012 #ifndef __deal2__utilities_h
00013 #define __deal2__utilities_h
00014 
00015 #include <deal.II/base/config.h>
00016 #include <deal.II/base/exceptions.h>
00017 #include <deal.II/base/mpi.h>
00018 
00019 #include <vector>
00020 #include <utility>
00021 #include <functional>
00022 #include <string>
00023 
00024 #ifdef DEAL_II_USE_TRILINOS
00025 #  include <Epetra_Comm.h>
00026 #  include <Epetra_Map.h>
00027 #  ifdef DEAL_II_COMPILER_SUPPORTS_MPI
00028 #    include <Epetra_MpiComm.h>
00029 #  else
00030 #    include <Epetra_SerialComm.h>
00031 #  endif
00032 #endif
00033 
00034 DEAL_II_NAMESPACE_OPEN
00035 
00036 
00045 namespace Utilities
00046 {
00047 
00059   std::string
00060   int_to_string (const unsigned int i,
00061                  const unsigned int digits = numbers::invalid_unsigned_int);
00062 
00068   unsigned int
00069   needed_digits (const unsigned int max_number);
00070 
00076   int
00077   string_to_int (const std::string &s);
00078 
00079 
00085   std::vector<int>
00086   string_to_int (const std::vector<std::string> &s);
00087 
00093   double
00094   string_to_double (const std::string &s);
00095 
00096 
00102   std::vector<double>
00103   string_to_double (const std::vector<std::string> &s);
00104 
00115   std::vector<std::string>
00116   split_string_list (const std::string &s,
00117                      const char         delimiter = ',');
00118 
00133   std::vector<std::string>
00134   break_text_into_lines (const std::string &original_text,
00135                          const unsigned int width,
00136                          const char delimiter = ' ');
00137 
00143   bool
00144   match_at_string_start (const std::string &name,
00145                          const std::string &pattern);
00146 
00160   std::pair<int, unsigned int>
00161   get_integer_at_position (const std::string &name,
00162                            const unsigned int position);
00163 
00170   double
00171   generate_normal_random_number (const double a,
00172                                  const double sigma);
00173 
00174 
00187   template <int N, typename T>
00188   T
00189   fixed_power (const T t);
00190 
00203   template <int a, int N>
00204   struct fixed_int_power
00205   {
00206     static const int value = a * fixed_int_power<a,N-1>::value;
00207   };
00208 
00214   template <int a>
00215   struct fixed_int_power<a,0>
00216   {
00217     static const int value = 1;
00218   };
00219 
00262   template<typename Iterator, typename T>
00263   Iterator
00264   lower_bound (Iterator  first,
00265                Iterator  last,
00266                const T  &val);
00267 
00268 
00275   template<typename Iterator, typename T, typename Comp>
00276   Iterator
00277   lower_bound (Iterator   first,
00278                Iterator   last,
00279                const T   &val,
00280                const Comp comp);
00281 
00289   std::vector<unsigned int>
00290   reverse_permutation (const std::vector<unsigned int> &permutation);
00291 
00300   std::vector<unsigned int>
00301   invert_permutation (const std::vector<unsigned int> &permutation);
00302 
00309   namespace System
00310   {
00311 
00322     double get_cpu_load ();
00323 
00330     struct MemoryStats
00331     {
00332         unsigned long int VmPeak; 
00333         unsigned long int VmSize; 
00334         unsigned long int VmHWM; 
00335         unsigned long int VmRSS; 
00336     };
00337 
00338 
00345     void get_memory_stats (MemoryStats & stats);
00346 
00347 
00352     std::string get_hostname ();
00353 
00354 
00358     std::string get_time ();
00359 
00382     bool job_supports_mpi ();
00383 
00389     bool program_uses_mpi ();
00390 
00407     unsigned int get_n_mpi_processes (const MPI_Comm &mpi_communicator);
00408 
00415     unsigned int get_this_mpi_process (const MPI_Comm &mpi_communicator);
00416 
00417 
00424     using
00425     Utilities::MPI::compute_point_to_point_communication_pattern;
00426 
00427 
00434     using Utilities::MPI::duplicate_communicator;
00435 
00441     using Utilities::MPI::MinMaxAvg;
00442 
00448     void
00449     calculate_collective_mpi_min_max_avg (const MPI_Comm &mpi_communicator,
00450                                           const double my_value,
00451                                           MinMaxAvg & result);
00452 
00458     using Utilities::MPI::MPI_InitFinalize;
00459 
00461   }
00462 
00463 
00464 #ifdef DEAL_II_USE_TRILINOS
00465 
00470   namespace Trilinos
00471   {
00486     const Epetra_Comm& comm_world();
00487 
00502     const Epetra_Comm& comm_self();
00503 
00561     Epetra_Comm *
00562     duplicate_communicator (const Epetra_Comm &communicator);
00563 
00600     void
00601     destroy_communicator (Epetra_Comm &communicator);
00602 
00609     unsigned int get_n_mpi_processes (const Epetra_Comm &mpi_communicator);
00610 
00621     unsigned int get_this_mpi_process (const Epetra_Comm &mpi_communicator);
00622 
00638     Epetra_Map
00639     duplicate_map (const Epetra_BlockMap  &map,
00640                    const Epetra_Comm &comm);
00641   }
00642 
00643 #endif
00644 
00645 }
00646 
00647 
00648 // --------------------- inline functions
00649 
00650 namespace Utilities
00651 {
00652   template <int N, typename T>
00653   inline
00654   T fixed_power (const T n)
00655   {
00656     Assert (N>0, ExcNotImplemented());
00657     switch (N)
00658       {
00659         case 0:
00660               return 1;
00661         case 1:
00662               return n;
00663         case 2:
00664               return n*n;
00665         case 3:
00666               return n*n*n;
00667         case 4:
00668               return n*n*n*n;
00669         default:
00670               T result = 1;
00671               for (int d=0;d<N;++d)
00672                 result *= n;
00673               return result;
00674       }
00675   }
00676 
00677 
00678 
00679   template<typename Iterator, typename T>
00680   inline
00681   Iterator
00682   lower_bound (Iterator  first,
00683                Iterator  last,
00684                const T  &val)
00685   {
00686     return Utilities::lower_bound (first, last, val,
00687                                    std::less<T>());
00688   }
00689 
00690 
00691 
00692   template<typename Iterator, typename T, typename Comp>
00693   inline
00694   Iterator
00695   lower_bound (Iterator    first,
00696                Iterator    last,
00697                const T    &val,
00698                const Comp  comp)
00699   {
00700     unsigned int len = last-first;
00701 
00702     if (len==0)
00703       return first;
00704 
00705     while (true)
00706       {
00707                                          // if length equals 8 or less,
00708                                          // then do a rolled out
00709                                          // search. use a switch without
00710                                          // breaks for that and roll-out
00711                                          // the loop somehow
00712         if (len < 8)
00713           {
00714             switch (len)
00715               {
00716                 case 7:
00717                       if (!comp(*first, val))
00718                         return first;
00719                       ++first;
00720                 case 6:
00721                       if (!comp(*first, val))
00722                         return first;
00723                       ++first;
00724                 case 5:
00725                       if (!comp(*first, val))
00726                         return first;
00727                       ++first;
00728                 case 4:
00729                       if (!comp(*first, val))
00730                         return first;
00731                       ++first;
00732                 case 3:
00733                       if (!comp(*first, val))
00734                         return first;
00735                       ++first;
00736                 case 2:
00737                       if (!comp(*first, val))
00738                         return first;
00739                       ++first;
00740                 case 1:
00741                       if (!comp(*first, val))
00742                         return first;
00743                       return first+1;
00744                 default:
00745                                                        // indices seem
00746                                                        // to not be
00747                                                        // sorted
00748                                                        // correctly!? or
00749                                                        // did len
00750                                                        // become==0
00751                                                        // somehow? that
00752                                                        // shouln't have
00753                                                        // happened
00754                       Assert (false, ExcInternalError());
00755               }
00756           }
00757 
00758 
00759 
00760         const unsigned int half   = len >> 1;
00761         const Iterator     middle = first + half;
00762 
00763                                          // if the value is larger than
00764                                          // that pointed to by the
00765                                          // middle pointer, then the
00766                                          // insertion point must be
00767                                          // right of it
00768         if (comp(*middle, val))
00769           {
00770             first = middle + 1;
00771             len  -= half + 1;
00772           }
00773         else
00774           len = half;
00775       }
00776   }
00777 }
00778 
00779 
00780 DEAL_II_NAMESPACE_CLOSE
00781 
00782 #endif
00783 
00784 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

deal.II documentation generated on Tue May 22 2012 12:06:02 by doxygen 1.7.3