/usr/include/deal.II/base/utilities.h is in libdeal.ii-dev 6.3.1-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 | //---------------------------------------------------------------------------
// $Id: utilities.h 21358 2010-06-24 23:38:14Z bangerth $
// Version: $Name$
//
// Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 by the deal.II authors
//
// This file is subject to QPL and may not be distributed
// without copyright and license information. Please refer
// to the file deal.II/doc/license.html for the text and
// further information on this license.
//
//---------------------------------------------------------------------------
#ifndef __deal2__utilities_h
#define __deal2__utilities_h
#include <base/config.h>
#include <base/exceptions.h>
#include <vector>
#include <utility>
#include <string>
#if defined(DEAL_II_COMPILER_SUPPORTS_MPI) || defined(DEAL_II_USE_PETSC)
#include <mpi.h>
#else
typedef int MPI_Comm;
#endif
#ifdef DEAL_II_USE_TRILINOS
# include <Teuchos_RCP.hpp>
# include <Epetra_Comm.h>
# include <Epetra_Map.h>
# ifdef DEAL_II_COMPILER_SUPPORTS_MPI
# include <Epetra_MpiComm.h>
# else
# include <Epetra_SerialComm.h>
# endif
#endif
DEAL_II_NAMESPACE_OPEN
/**
* A namespace for utility functions that are not particularly specific to
* finite element computing or numerical programs, but nevertheless are needed
* in various contexts when writing applications.
*
* @ingroup utilities
* @author Wolfgang Bangerth, 2005
*/
namespace Utilities
{
/**
* Convert a number @p i to a string, with
* as many digits as given to fill with
* leading zeros.
*
* If the second parameter is left at its
* default value, the number is not padded
* with leading zeros. The result is then
* the same as of the standard C function
* <code>itoa()</code> had been called.
*/
std::string
int_to_string (const unsigned int i,
const unsigned int digits = numbers::invalid_unsigned_int);
/**
* Determine how many digits are needed to
* represent numbers at most as large as
* the given number.
*/
unsigned int
needed_digits (const unsigned int max_number);
/**
* Given a string, convert it to an
* integer. Throw an assertion if that is
* not possible.
*/
int
string_to_int (const std::string &s);
/**
* Given a list of strings, convert it to a
* list of integers. Throw an assertion if
* that is not possible.
*/
std::vector<int>
string_to_int (const std::vector<std::string> &s);
/**
* Given a string that contains text
* separated by a @p delimiter, split it into
* its components; for each component,
* remove leading and trailing spaces.
*
* The default value of the delimiter is a
* comma, so that the function splits comma
* separated lists of strings.
*/
std::vector<std::string>
split_string_list (const std::string &s,
const char delimiter = ',');
/**
* Take a text, usually a documentation or
* something, and try to break it into
* individual lines of text at most @p
* width characters wide, by breaking at
* positions marked by @p delimiter in the text.
* If this is not possible, return the shortest
* lines than are longer than @p width.
* The default value of the delimiter is a
* space character.
*/
std::vector<std::string>
break_text_into_lines (const std::string &original_text,
const unsigned int width,
const char delimiter = ' ');
/**
* Return true if the given pattern
* string appears in the first
* position of the string.
*/
bool
match_at_string_start (const std::string &name,
const std::string &pattern);
/**
* Read a (signed) integer starting
* at the position in @p name
* indicated by the second
* argument, and retun this integer
* as a pair together with how many
* characters it takes up in the
* string.
*
* If no integer can be read at the
* indicated position, return
* (-1,numbers::invalid_unsigned_int)
*/
std::pair<int, unsigned int>
get_integer_at_position (const std::string &name,
const unsigned int position);
/**
* Generate a random number from a
* normalized Gaussian probability
* distribution centered around @p a and
* with standard deviation @p sigma.
*/
double
generate_normal_random_number (const double a,
const double sigma);
/**
* Calculate a fixed power, provided as a
* template argument, of a number.
*
* This function provides an efficient way
* to calculate things like
* <code>t^N</code> where <code>N</code> is
* a known number at compile time.
*
* Use this function as in
* <code>fixed_power@<dim@> (n)</code>.
*/
template <int N, typename T>
T
fixed_power (const T t);
/**
* Given a permutation vector (i.e. a
* vector $p_0\ldots p_{N-1}$ where each
* $p_i\in [0,N)$ and $p_i\neq p_j$ for
* $i\neq j$), produce the reverse
* permutation $q_i=N-1-p_i$.
*/
std::vector<unsigned int>
reverse_permutation (const std::vector<unsigned int> &permutation);
/**
* Given a permutation vector (i.e. a
* vector $p_0\ldots p_{N-1}$ where each
* $p_i\in [0,N)$ and $p_i\neq p_j$ for
* $i\neq j$), produce the inverse
* permutation $q_0\ldots q_{N-1}$ so that
* $q_{p_i}=p_{q_i}=i$.
*/
std::vector<unsigned int>
invert_permutation (const std::vector<unsigned int> &permutation);
/**
* A namespace for utility functions that
* probe system properties.
*
* @ingroup utilities
*/
namespace System
{
/**
* Return the CPU load as returned by
* "uptime". Note that the interpretation
* of this number depends on the actual
* number of processors in the
* machine. This is presently only
* implemented on Linux, using the
* /proc/loadavg pseudo-file, on other
* systems we simply return zero.
*/
double get_cpu_load ();
/**
* Return the name of the host this
* process runs on.
*/
std::string get_hostname ();
/**
* Return the present time as HH:MM:SS.
*/
std::string get_time ();
/**
* Return whether (i) deal.II has
* been compiled to support MPI
* (for example by compiling with
* <code>CXX=mpiCC</code>) and if
* so whether (ii)
* <code>MPI_Init()</code> has
* been called (for example using
* the
* Utilities::System::MPI_InitFinalize
* class). In other words, the
* result indicates whether the
* current job is running under
* MPI.
*/
bool job_supports_mpi ();
/**
* Return the number of MPI processes
* there exist in the given communicator
* object. If this is a sequential job,
* it returns 1.
*/
unsigned int get_n_mpi_processes (const MPI_Comm &mpi_communicator);
/**
* Return the number of the present MPI
* process in the space of processes
* described by the given
* communicator. This will be a unique
* value for each process between zero
* and (less than) the number of all
* processes (given by
* get_n_mpi_processes()).
*/
unsigned int get_this_mpi_process (const MPI_Comm &mpi_communicator);
/**
* Consider an unstructured
* communication pattern where
* every process in an MPI
* universe wants to send some
* data to a subset of the other
* processors. To do that, the
* other processors need to know
* who to expect messages
* from. This function computes
* this information.
*
* @param mpi_comm A communicator
* that describes the processors
* that are going to communicate
* with each other.
*
* @param destinations The list
* of processors the current
* process wants to send
* information to. This list need
* not be sorted in any way. If
* it contains duplicate entries
* that means that multiple
* messages are intended for a
* given destination.
*
* @return A list of processors
* that have indicated that they
* want to send something to the
* current processor. The
* resulting list is not
* sorted. It may contain
* duplicate entries if
* processors enter the same
* destination more than once in
* their destinations list.
*/
std::vector<unsigned int>
compute_point_to_point_communication_pattern (const MPI_Comm & mpi_comm,
const std::vector<unsigned int> & destinations);
/**
* Given a communicator, generate a new
* communicator that contains the same
* set of processors but that has a
* different, unique identifier.
*
* This functionality can be used to
* ensure that different objects, such as
* distributed matrices, each have unique
* communicators over which they can
* interact without interfering with each
* other.
*
* When no longer needed, the
* communicator created here needs to
* be destroyed using
* <code>MPI_Comm_free</code>.
*/
MPI_Comm duplicate_communicator (const MPI_Comm &mpi_communicator);
/**
* A class that is used to initialize the
* MPI system at the beginning of a
* program and to shut it down again at
* the end.
*
* If a program uses MPI one would
* typically just create an object of
* this type at the beginning of
* <code>main()</code>. The constructor
* of this class then runs
* <code>MPI_Init()</code> with the given
* arguments. At the end of the program,
* the compiler will invoke the
* destructor of this object which in
* turns calls <code>MPI_Finalize</code>
* to shut down the MPI system.
*/
class MPI_InitFinalize
{
public:
/**
* Constructor. Takes the arguments
* from the command line (in case of
* MPI, the number of processes is
* specified there), and sets up a
* respective communicator by calling
* <tt>MPI_Init()</tt>. This
* constructor can only be called once
* in a program, since MPI cannot be
* initialized twice.
*/
MPI_InitFinalize (int &argc,
char** &argv);
/**
* Destructor. Calls
* <tt>MPI_Finalize()</tt> in
* case this class owns the MPI
* process.
*/
~MPI_InitFinalize();
private:
/**
* This flag tells the class
* whether it owns the MPI
* process (i.e., it has been
* constructed using the
* argc/argv input, or it has
* been copied). In the former
* case, the command
* <tt>MPI_Finalize()</tt> will
* be called at destruction.
*/
const bool owns_mpi;
};
/**
* Returns whether deal.II has been
* configured to use MPI and if so
* whether MPI has already been
* initialized using MPI_Init().
*/
bool program_uses_mpi();
}
/**
* This class provides the basic structures for the use of the Trilinos
* classes such as matrices, vectors, and preconditioners. The most
* important function in this class is <tt>comm()</tt>, which is needed
* for the initialization of Trilinos Epetra_Maps, which design the
* %parallel distribution of vectors and matrices. Moreover, this class
* provides a unified interface to both serial and %parallel
* implementations of Trilinos, sets up the MPI communicator in case the
* programs are run in %parallel, and correctly terminates all processes
* when the destructor is called.
*/
#ifdef DEAL_II_USE_TRILINOS
namespace Trilinos
{
/**
* Returns a Trilinos Epetra_Comm
* object needed for creation of
* Epetra_Maps.
*
* If deal.II has been configured to use
* a compiler that does not support MPI
* then the resulting communicator will
* be a serial one. Otherwise, the
* communicator will correspond to
* MPI_COMM_WORLD, i.e. a communicator
* that encompasses all processes within
* this MPI universe.
*/
const Epetra_Comm& comm_world();
/**
* Returns a Trilinos Epetra_Comm
* object needed for creation of
* Epetra_Maps.
*
* If deal.II has been configured to use
* a compiler that does not support MPI
* then the resulting communicator will
* be a serial one. Otherwise, the
* communicator will correspond to
* MPI_COMM_SELF, i.e. a communicator
* that comprises only this one
* processor.
*/
const Epetra_Comm& comm_self();
/**
* Given a communicator, duplicate it. If
* the given communicator is serial, that
* means to just return a copy of
* itself. On the other hand, if it is
* %parallel, we duplicate the underlying
* MPI_Comm object: we create a separate
* MPI communicator that contains the
* same processors and in the same order
* but has a separate identifier distinct
* from the given communicator. The
* function returns a pointer to a new
* object of a class derived from
* Epetra_Comm. The caller of this
* function needs to assume ownership of
* this function. The returned object
* should be destroyed using the
* destroy_communicator() function.
*
* This facility is used to separate
* streams of communication. For example,
* a program could simply use
* MPI_Comm_World for everything. But it
* is easy to come up with scenarios
* where sometimes not all processors
* participate in a communication that is
* intended to be global -- for example
* if we assemble a matrix on a coarse
* mesh with fewer cells than there are
* processors, some processors may not
* sync their matrices with the rest
* because they haven't written into it
* because they own no cells. That's
* clearly a bug. However, if these
* processors just continue their work,
* and the next %parallel operation
* happens to be a sync on a different
* matrix, then the sync could succeed --
* by accident, since different
* processors are talking about different
* matrices.
*
* This kind of situation can be avoided
* if we use different communicators for
* different matrices which reduces the
* likelihood that communications meant
* to be separate aren't recognized as
* such just because they happen on the
* same communicator. In addition, it is
* conceivable that some MPI operations
* can be parallelized using multiple
* threads because their communicators
* identifies the communication in
* question, not their relative timing as
* is the case in a sequential program
* that just uses a single communicator.
*/
Epetra_Comm *
duplicate_communicator (const Epetra_Comm &communicator);
/**
* Given an Epetra communicator that was
* created by the
* duplicate_communicator() function,
* destroy the underlying MPI
* communicator object and reset the
* Epetra_Comm object to a the result of
* comm_self().
*
* It is necessary to call this function
* at the time when the result of
* duplicate_communicator() is no longer
* needed. The reason is that in that
* function, we first create a new
* MPI_Comm object and then create an
* Epetra_Comm around it. While we can
* take care of destroying the latter, it
* doesn't destroy the communicator since
* it can only assume that it may also be
* still used by other objects in the
* program. Consequently, we have to take
* care of destroying it ourselves,
* explicitly.
*
* This function does exactly
* that. Because this has to happen while
* the Epetra_Comm object is still
* around, it first resets the latter and
* then destroys the communicator object.
*
* @note If you call this function on an
* Epetra_Comm object that is not created
* by duplicate_communicator(), you are
* likely doing something quite
* wrong. Don't do this.
*/
void
destroy_communicator (Epetra_Comm &communicator);
/**
* Return the number of MPI processes
* there exist in the given communicator
* object. If this is a sequential job,
* it returns 1.
*/
unsigned int get_n_mpi_processes (const Epetra_Comm &mpi_communicator);
/**
* Return the number of the present MPI
* process in the space of processes
* described by the given
* communicator. This will be a unique
* value for each process between zero
* and (less than) the number of all
* processes (given by
* get_n_mpi_processes()).
*/
unsigned int get_this_mpi_process (const Epetra_Comm &mpi_communicator);
/**
* Given a Trilinos Epetra map, create a
* new map that has the same subdivision
* of elements to processors but uses the
* given communicator object instead of
* the one stored in the first
* argument. In essence, this means that
* we create a map that communicates
* among the same processors in the same
* way, but using a separate channel.
*
* This function is typically used with a
* communicator that has been obtained by
* the duplicate_communicator() function.
*/
Epetra_Map
duplicate_map (const Epetra_BlockMap &map,
const Epetra_Comm &comm);
}
#endif
}
// --------------------- inline functions
namespace Utilities
{
template <int N, typename T>
inline
T fixed_power (const T n)
{
Assert (N>0, ExcNotImplemented());
switch (N)
{
case 1:
return n;
case 2:
return n*n;
case 3:
return n*n*n;
case 4:
return n*n*n*n;
default:
T result = n;
for (unsigned int d=1;d<N;++d)
result *= n;
return result;
}
}
}
DEAL_II_NAMESPACE_CLOSE
#endif
|