Reference documentation for deal.II version Git 7f58f27 2017-09-20 15:35:49 -0600
mpi.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2011 - 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_mpi_h
17 #define dealii_mpi_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <vector>
22 
23 #if !defined(DEAL_II_WITH_MPI) && !defined(DEAL_II_WITH_PETSC)
24 // without MPI, we would still like to use
25 // some constructs with MPI data
26 // types. Therefore, create some dummies
27 typedef int MPI_Comm;
28 typedef int MPI_Datatype;
29 typedef int MPI_Op;
30 # ifndef MPI_COMM_WORLD
31 # define MPI_COMM_WORLD 0
32 # endif
33 # ifndef MPI_COMM_SELF
34 # define MPI_COMM_SELF 0
35 # endif
36 # ifndef MPI_MIN
37 # define MPI_MIN 0
38 # endif
39 # ifndef MPI_MAX
40 # define MPI_MAX 0
41 # endif
42 # ifndef MPI_SUM
43 # define MPI_SUM 0
44 # endif
45 #endif
46 
47 DEAL_II_NAMESPACE_OPEN
48 
49 
50 //Forward type declarations to allow MPI sums over tensorial types
51 template <int rank, int dim, typename Number> class Tensor;
52 template <int rank, int dim, typename Number> class SymmetricTensor;
53 
54 //Forward type declaration to allow MPI sums over Vector<number> type
55 template <typename Number> class Vector;
56 //Forward type declaration to allow MPI sums over FullMatrix<number> type
57 template <typename Number> class FullMatrix;
58 
59 
60 namespace Utilities
61 {
69  namespace MPI
70  {
76  unsigned int n_mpi_processes (const MPI_Comm &mpi_communicator);
77 
86  unsigned int this_mpi_process (const MPI_Comm &mpi_communicator);
87 
109  std::vector<unsigned int>
110  compute_point_to_point_communication_pattern (const MPI_Comm &mpi_comm,
111  const std::vector<unsigned int> &destinations);
112 
126  MPI_Comm duplicate_communicator (const MPI_Comm &mpi_communicator);
127 
147  template <typename T>
148  T sum (const T &t,
149  const MPI_Comm &mpi_communicator);
150 
159  template <typename T, unsigned int N>
160  void sum (const T (&values)[N],
161  const MPI_Comm &mpi_communicator,
162  T (&sums)[N]);
163 
171  template <typename T>
172  void sum (const std::vector<T> &values,
173  const MPI_Comm &mpi_communicator,
174  std::vector<T> &sums);
175 
182  template <typename T>
183  void sum (const Vector<T> &values,
184  const MPI_Comm &mpi_communicator,
185  Vector<T> &sums);
186 
193  template <typename T>
194  void sum (const FullMatrix<T> &values,
195  const MPI_Comm &mpi_communicator,
196  FullMatrix<T> &sums);
197 
203  template <int rank, int dim, typename Number>
206  const MPI_Comm &mpi_communicator);
207 
213  template <int rank, int dim, typename Number>
215  sum (const Tensor<rank,dim,Number> &local,
216  const MPI_Comm &mpi_communicator);
217 
237  template <typename T>
238  T max (const T &t,
239  const MPI_Comm &mpi_communicator);
240 
249  template <typename T, unsigned int N>
250  void max (const T (&values)[N],
251  const MPI_Comm &mpi_communicator,
252  T (&maxima)[N]);
253 
262  template <typename T>
263  void max (const std::vector<T> &values,
264  const MPI_Comm &mpi_communicator,
265  std::vector<T> &maxima);
266 
286  template <typename T>
287  T min (const T &t,
288  const MPI_Comm &mpi_communicator);
289 
298  template <typename T, unsigned int N>
299  void min (const T (&values)[N],
300  const MPI_Comm &mpi_communicator,
301  T (&minima)[N]);
302 
311  template <typename T>
312  void min (const std::vector<T> &values,
313  const MPI_Comm &mpi_communicator,
314  std::vector<T> &minima);
315 
316 
329  struct MinMaxAvg
330  {
336  double sum;
337  double min;
338  double max;
339 
345  unsigned int min_index;
346  unsigned int max_index;
347 
352  double avg;
353  };
354 
369  MinMaxAvg
370  min_max_avg (const double my_value,
371  const MPI_Comm &mpi_communicator);
372 
373 
374 
398  {
399  public:
445  MPI_InitFinalize (int &argc,
446  char ** &argv,
447  const unsigned int max_num_threads = numbers::invalid_unsigned_int);
448 
454  };
455 
467  bool job_supports_mpi ();
468 
469 #ifndef DOXYGEN
470  // declaration for an internal function that lives in mpi.templates.h
471  namespace internal
472  {
473  template <typename T>
474  void all_reduce (const MPI_Op &mpi_op,
475  const T *const values,
476  const MPI_Comm &mpi_communicator,
477  T *output,
478  const std::size_t size);
479  }
480 
481  // Since these depend on N they must live in the header file
482  template <typename T, unsigned int N>
483  void sum (const T (&values)[N],
484  const MPI_Comm &mpi_communicator,
485  T (&sums)[N])
486  {
487  internal::all_reduce(MPI_SUM, values, mpi_communicator, sums, N);
488  }
489 
490  template <typename T, unsigned int N>
491  void max (const T (&values)[N],
492  const MPI_Comm &mpi_communicator,
493  T (&maxima)[N])
494  {
495  internal::all_reduce(MPI_MAX, values, mpi_communicator, maxima, N);
496  }
497 
498  template <typename T, unsigned int N>
499  void min (const T (&values)[N],
500  const MPI_Comm &mpi_communicator,
501  T (&minima)[N])
502  {
503  internal::all_reduce(MPI_MIN, values, mpi_communicator, minima, N);
504  }
505 #endif
506  } // end of namespace MPI
507 } // end of namespace Utilities
508 
509 
510 DEAL_II_NAMESPACE_CLOSE
511 
512 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:173
MPI_InitFinalize(int &argc, char **&argv, const unsigned int max_num_threads=numbers::invalid_unsigned_int)
Definition: mpi.cc:295
Definition: mpi.h:57
T sum(const T &t, const MPI_Comm &mpi_communicator)
unsigned int n_mpi_processes(const MPI_Comm &mpi_communicator)
Definition: mpi.cc:61
Definition: mpi.h:60
MPI_Comm duplicate_communicator(const MPI_Comm &mpi_communicator)
Definition: mpi.cc:81
Definition: mpi.h:51
T min(const T &t, const MPI_Comm &mpi_communicator)
unsigned int this_mpi_process(const MPI_Comm &mpi_communicator)
Definition: mpi.cc:71
MinMaxAvg min_max_avg(const double my_value, const MPI_Comm &mpi_communicator)
Definition: mpi.cc:192
std::vector< unsigned int > compute_point_to_point_communication_pattern(const MPI_Comm &mpi_comm, const std::vector< unsigned int > &destinations)
Definition: mpi.cc:91
bool job_supports_mpi()
Definition: mpi.cc:517
unsigned int min_index
Definition: mpi.h:345
T max(const T &t, const MPI_Comm &mpi_communicator)