Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
mpi.h
1 // ---------------------------------------------------------------------
2 // @f$Id: mpi.h 30040 2013-07-18 17:06:48Z maier @f$
3 //
4 // Copyright (C) 2011 - 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__mpi_h
18 #define __deal2__mpi_h
19 
20 #include <deal.II/base/config.h>
21 #include <vector>
22 
23 #if defined(DEAL_II_WITH_MPI) || defined(DEAL_II_WITH_PETSC)
24 # include <mpi.h>
25 // Check whether <mpi.h> is a suitable
26 // include for us (if MPI_SEEK_SET is not
27 // defined, we'll die anyway):
28 # ifndef MPI_SEEK_SET
29 # error "The buildsystem included an insufficient mpi.h header that does not export MPI_SEEK_SET"
30 # endif
31 
32 #else
33 // without MPI, we would still like to use
34 // some constructs with MPI data
35 // types. Therefore, create some dummies
36 typedef int MPI_Comm;
37 const int MPI_COMM_SELF = 0;
38 typedef int MPI_Datatype;
39 typedef int MPI_Op;
40 namespace MPI
41 {
42  static const unsigned int UNSIGNED = 0;
43  static const unsigned int LONG_DOUBLE = 0;
44  static const unsigned int LONG_DOUBLE_COMPLEX = 0;
45  static const unsigned int MAX = 0;
46  static const unsigned int MIN = 0;
47  static const unsigned int SUM = 0;
48 }
49 #endif
50 
52 
53 
54 namespace Utilities
55 {
66  namespace MPI
67  {
74  unsigned int n_mpi_processes (const MPI_Comm &mpi_communicator);
75 
86  unsigned int this_mpi_process (const MPI_Comm &mpi_communicator);
87 
126  std::vector<unsigned int>
127  compute_point_to_point_communication_pattern (const MPI_Comm &mpi_comm,
128  const std::vector<unsigned int> &destinations);
129 
148  MPI_Comm duplicate_communicator (const MPI_Comm &mpi_communicator);
149 
170  template <typename T>
171  T sum (const T &t,
172  const MPI_Comm &mpi_communicator);
173 
184  template <typename T, unsigned int N>
185  inline
186  void sum (const T (&values)[N],
187  const MPI_Comm &mpi_communicator,
188  T (&sums)[N]);
189 
199  template <typename T>
200  inline
201  void sum (const std::vector<T> &values,
202  const MPI_Comm &mpi_communicator,
203  std::vector<T> &sums);
204 
225  template <typename T>
226  T max (const T &t,
227  const MPI_Comm &mpi_communicator);
228 
239  template <typename T, unsigned int N>
240  inline
241  void max (const T (&values)[N],
242  const MPI_Comm &mpi_communicator,
243  T (&maxima)[N]);
244 
254  template <typename T>
255  inline
256  void max (const std::vector<T> &values,
257  const MPI_Comm &mpi_communicator,
258  std::vector<T> &maxima);
259 
264  struct MinMaxAvg
265  {
266  double sum;
267  double min;
268  double max;
269  unsigned int min_index;
270  unsigned int max_index;
271  double avg;
272  };
273 
293  MinMaxAvg
294  min_max_avg (const double my_value,
295  const MPI_Comm &mpi_communicator);
296 
297 
298 
328  {
329  public:
344  MPI_InitFinalize (int &argc,
345  char ** &argv) /*DEAL_II_DEPRECATED*/;
346 
354  MPI_InitFinalize (int &argc,
355  char ** &argv,
356  unsigned int max_num_threads);
364 
365  private:
377  const bool owns_mpi;
378 
379 
383  void do_init(int &argc,
384  char ** &argv,
385  unsigned int max_num_threads);
386  };
387 
388  namespace internal
389  {
390 #ifdef DEAL_II_WITH_MPI
391 
395  inline MPI_Datatype mpi_type_id (const int *)
396  {
397  return MPI_INT;
398  }
399 
400 
401  inline MPI_Datatype mpi_type_id (const long int *)
402  {
403  return MPI_LONG;
404  }
405 
406 
407  inline MPI_Datatype mpi_type_id (const unsigned int *)
408  {
409  return MPI_UNSIGNED;
410  }
411 
412 
413  inline MPI_Datatype mpi_type_id (const unsigned long int *)
414  {
415  return MPI_UNSIGNED_LONG;
416  }
417 
418 
419  inline MPI_Datatype mpi_type_id (const unsigned long long int *)
420  {
421  return MPI_UNSIGNED_LONG_LONG;
422  }
423 
424 
425  inline MPI_Datatype mpi_type_id (const float *)
426  {
427  return MPI_FLOAT;
428  }
429 
430 
431  inline MPI_Datatype mpi_type_id (const double *)
432  {
433  return MPI_DOUBLE;
434  }
435 
436 
437  inline MPI_Datatype mpi_type_id (const long double *)
438  {
439  return MPI_LONG_DOUBLE;
440  }
441 #endif
442  }
443 
444 
445  template <typename T>
446  inline
447  T sum (const T &t,
448  const MPI_Comm &mpi_communicator)
449  {
450 #ifdef DEAL_II_WITH_MPI
451  T sum;
452  MPI_Allreduce (const_cast<void *>(static_cast<const void *>(&t)),
453  &sum, 1, internal::mpi_type_id(&t), MPI_SUM,
454  mpi_communicator);
455  return sum;
456 #else
457  (void)mpi_communicator;
458  return t;
459 #endif
460  }
461 
462 
463  template <typename T, unsigned int N>
464  inline
465  void sum (const T (&values)[N],
466  const MPI_Comm &mpi_communicator,
467  T (&sums)[N])
468  {
469 #ifdef DEAL_II_WITH_MPI
470  MPI_Allreduce (const_cast<void *>(static_cast<const void *>(&values[0])),
471  &sums[0], N, internal::mpi_type_id(values), MPI_SUM,
472  mpi_communicator);
473 #else
474  (void)mpi_communicator;
475  for (unsigned int i=0; i<N; ++i)
476  sums[i] = values[i];
477 #endif
478  }
479 
480 
481  template <typename T>
482  inline
483  void sum (const std::vector<T> &values,
484  const MPI_Comm &mpi_communicator,
485  std::vector<T> &sums)
486  {
487 #ifdef DEAL_II_WITH_MPI
488  sums.resize (values.size());
489  MPI_Allreduce (const_cast<void *>(static_cast<const void *>(&values[0])),
490  &sums[0], values.size(), internal::mpi_type_id((T *)0), MPI_SUM,
491  mpi_communicator);
492 #else
493  (void)mpi_communicator;
494  sums = values;
495 #endif
496  }
497 
498 
499  template <typename T>
500  inline
501  T max (const T &t,
502  const MPI_Comm &mpi_communicator)
503  {
504 #ifdef DEAL_II_WITH_MPI
505  T sum;
506  MPI_Allreduce (const_cast<void *>(static_cast<const void *>(&t)),
507  &sum, 1, internal::mpi_type_id(&t), MPI_MAX,
508  mpi_communicator);
509  return sum;
510 #else
511  (void)mpi_communicator;
512  return t;
513 #endif
514  }
515 
516 
517  template <typename T, unsigned int N>
518  inline
519  void max (const T (&values)[N],
520  const MPI_Comm &mpi_communicator,
521  T (&maxima)[N])
522  {
523 #ifdef DEAL_II_WITH_MPI
524  MPI_Allreduce (const_cast<void *>(static_cast<const void *>(&values[0])),
525  &maxima[0], N, internal::mpi_type_id(values), MPI_MAX,
526  mpi_communicator);
527 #else
528  (void)mpi_communicator;
529  for (unsigned int i=0; i<N; ++i)
530  maxima[i] = values[i];
531 #endif
532  }
533 
534 
535  template <typename T>
536  inline
537  void max (const std::vector<T> &values,
538  const MPI_Comm &mpi_communicator,
539  std::vector<T> &maxima)
540  {
541 #ifdef DEAL_II_WITH_MPI
542  maxima.resize (values.size());
543  MPI_Allreduce (const_cast<void *>(static_cast<const void *>(&values[0])),
544  &maxima[0], values.size(), internal::mpi_type_id((T *)0), MPI_MAX,
545  mpi_communicator);
546 #else
547  (void)mpi_communicator;
548  maxima = values;
549 #endif
550  }
551  } // end of namespace MPI
552 } // end of namespace Utilities
553 
554 
555 DEAL_II_NAMESPACE_CLOSE
556 
557 #endif
void do_init(int &argc, char **&argv, unsigned int max_num_threads)
MPI_InitFinalize(int &argc, char **&argv)
std::vector< unsigned int > compute_point_to_point_communication_pattern(const MPI_Comm &mpi_comm, const std::vector< unsigned int > &destinations)
T sum(const T &t, const MPI_Comm &mpi_communicator)
Definition: mpi.h:447
unsigned int n_mpi_processes(const MPI_Comm &mpi_communicator)
MPI_Comm duplicate_communicator(const MPI_Comm &mpi_communicator)
unsigned int this_mpi_process(const MPI_Comm &mpi_communicator)
MinMaxAvg min_max_avg(const double my_value, const MPI_Comm &mpi_communicator)
T max(const T &t, const MPI_Comm &mpi_communicator)
Definition: mpi.h:501