Reference documentation for deal.II version 8.4.1
mpi.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2011 - 2016 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 #include <vector>
21 
22 #if !defined(DEAL_II_WITH_MPI) && !defined(DEAL_II_WITH_PETSC)
23 // without MPI, we would still like to use
24 // some constructs with MPI data
25 // types. Therefore, create some dummies
26 typedef int MPI_Comm;
27 const int MPI_COMM_SELF = 0;
28 typedef int MPI_Datatype;
29 typedef int MPI_Op;
30 namespace MPI
31 {
32  static const unsigned int UNSIGNED = 0;
33  static const unsigned int LONG_DOUBLE = 0;
34  static const unsigned int LONG_DOUBLE_COMPLEX = 0;
35  static const unsigned int MAX = 0;
36  static const unsigned int MIN = 0;
37  static const unsigned int SUM = 0;
38 }
39 static const int MPI_MIN = 0;
40 static const int MPI_MAX = 0;
41 static const int MPI_SUM = 0;
42 #endif
43 
44 DEAL_II_NAMESPACE_OPEN
45 
46 
47 //Forward type declarations to allow MPI sums over tensorial types
48 template <int rank, int dim, typename Number> class Tensor;
49 template <int rank, int dim, typename Number> class SymmetricTensor;
50 
51 //Forward type declaration to allow MPI sums over Vector<number> type
52 template <typename Number> class Vector;
53 
54 
55 namespace Utilities
56 {
64  namespace MPI
65  {
71  unsigned int n_mpi_processes (const MPI_Comm &mpi_communicator);
72 
81  unsigned int this_mpi_process (const MPI_Comm &mpi_communicator);
82 
104  std::vector<unsigned int>
105  compute_point_to_point_communication_pattern (const MPI_Comm &mpi_comm,
106  const std::vector<unsigned int> &destinations);
107 
121  MPI_Comm duplicate_communicator (const MPI_Comm &mpi_communicator);
122 
142  template <typename T>
143  T sum (const T &t,
144  const MPI_Comm &mpi_communicator);
145 
154  template <typename T, unsigned int N>
155  inline
156  void sum (const T (&values)[N],
157  const MPI_Comm &mpi_communicator,
158  T (&sums)[N]);
159 
167  template <typename T>
168  inline
169  void sum (const std::vector<T> &values,
170  const MPI_Comm &mpi_communicator,
171  std::vector<T> &sums);
172 
179  template <typename T>
180  inline
181  void sum (const Vector<T> &values,
182  const MPI_Comm &mpi_communicator,
183  Vector<T> &sums);
184 
185 
191  template <int rank, int dim, typename Number>
192  inline
194  sum (const SymmetricTensor<rank,dim,Number> &local,
195  const MPI_Comm &mpi_communicator);
196 
202  template <int rank, int dim, typename Number>
203  inline
205  sum (const Tensor<rank,dim,Number> &local,
206  const MPI_Comm &mpi_communicator);
207 
227  template <typename T>
228  T max (const T &t,
229  const MPI_Comm &mpi_communicator);
230 
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 
253  template <typename T>
254  inline
255  void max (const std::vector<T> &values,
256  const MPI_Comm &mpi_communicator,
257  std::vector<T> &maxima);
258 
278  template <typename T>
279  T min (const T &t,
280  const MPI_Comm &mpi_communicator);
281 
290  template <typename T, unsigned int N>
291  inline
292  void min (const T (&values)[N],
293  const MPI_Comm &mpi_communicator,
294  T (&minima)[N]);
295 
304  template <typename T>
305  inline
306  void min (const std::vector<T> &values,
307  const MPI_Comm &mpi_communicator,
308  std::vector<T> &minima);
309 
310 
314  struct MinMaxAvg
315  {
316  // Note: We assume a POD property of this struct in the MPI calls in
317  // min_max_avg
318  double sum;
319  double min;
320  double max;
321  unsigned int min_index;
322  unsigned int max_index;
323  double avg;
324  };
325 
340  MinMaxAvg
341  min_max_avg (const double my_value,
342  const MPI_Comm &mpi_communicator);
343 
344 
345 
365  {
366  public:
412  MPI_InitFinalize (int &argc,
413  char ** &argv,
414  const unsigned int max_num_threads = numbers::invalid_unsigned_int);
415 
420  ~MPI_InitFinalize();
421  };
422 
434  bool job_supports_mpi ();
435 
436  namespace internal
437  {
438 #ifdef DEAL_II_WITH_MPI
439 
442  inline MPI_Datatype mpi_type_id (const int *)
443  {
444  return MPI_INT;
445  }
446 
447 
448  inline MPI_Datatype mpi_type_id (const long int *)
449  {
450  return MPI_LONG;
451  }
452 
453 
454  inline MPI_Datatype mpi_type_id (const unsigned int *)
455  {
456  return MPI_UNSIGNED;
457  }
458 
459 
460  inline MPI_Datatype mpi_type_id (const unsigned long int *)
461  {
462  return MPI_UNSIGNED_LONG;
463  }
464 
465 
466  inline MPI_Datatype mpi_type_id (const unsigned long long int *)
467  {
468  return MPI_UNSIGNED_LONG_LONG;
469  }
470 
471 
472  inline MPI_Datatype mpi_type_id (const float *)
473  {
474  return MPI_FLOAT;
475  }
476 
477 
478  inline MPI_Datatype mpi_type_id (const double *)
479  {
480  return MPI_DOUBLE;
481  }
482 
483 
484  inline MPI_Datatype mpi_type_id (const long double *)
485  {
486  return MPI_LONG_DOUBLE;
487  }
488 #endif
489 
490  template <typename T>
491  inline
492  T all_reduce (const MPI_Op &mpi_op,
493  const T &t,
494  const MPI_Comm &mpi_communicator)
495  {
496 #ifdef DEAL_II_WITH_MPI
497  if (job_supports_mpi())
498  {
499  T output;
500  MPI_Allreduce (const_cast<void *>(static_cast<const void *>(&t)),
501  &output, 1, internal::mpi_type_id(&t), mpi_op,
502  mpi_communicator);
503  return output;
504  }
505  else
506 #endif
507  {
508  (void)mpi_op;
509  (void)mpi_communicator;
510  return t;
511  }
512  }
513 
514  template <typename T, unsigned int N>
515  inline
516  void all_reduce (const MPI_Op &mpi_op,
517  const T (&values)[N],
518  const MPI_Comm &mpi_communicator,
519  T (&output)[N])
520  {
521 #ifdef DEAL_II_WITH_MPI
522  if (job_supports_mpi())
523  {
524  MPI_Allreduce ((&values[0] != &output[0]
525  ?
526  const_cast<void *>(static_cast<const void *>(&values[0]))
527  :
528  MPI_IN_PLACE),
529  &output[0], N, internal::mpi_type_id(values), mpi_op,
530  mpi_communicator);
531  }
532  else
533 #endif
534  {
535  (void)mpi_op;
536  (void)mpi_communicator;
537  for (unsigned int i=0; i<N; ++i)
538  output[i] = values[i];
539  }
540  }
541 
542  template <typename T>
543  inline
544  void all_reduce (const MPI_Op &mpi_op,
545  const std::vector<T> &values,
546  const MPI_Comm &mpi_communicator,
547  std::vector<T> &output)
548  {
549 #ifdef DEAL_II_WITH_MPI
550  if (job_supports_mpi())
551  {
552  output.resize (values.size());
553  MPI_Allreduce ((&values[0] != &output[0]
554  ?
555  const_cast<void *>(static_cast<const void *>(&values[0]))
556  :
557  MPI_IN_PLACE),
558  &output[0], values.size(), internal::mpi_type_id((T *)0), mpi_op,
559  mpi_communicator);
560  }
561  else
562 #endif
563  {
564  (void)mpi_op;
565  (void)mpi_communicator;
566  output = values;
567  }
568  }
569 
570  template <typename T>
571  inline
572  void all_reduce (const MPI_Op &mpi_op,
573  const Vector<T> &values,
574  const MPI_Comm &mpi_communicator,
575  Vector<T> &output)
576  {
577 #ifdef DEAL_II_WITH_MPI
578  if (job_supports_mpi())
579  {
580  if (values.begin() != output.begin())
581  output.reinit (values.size());
582 
583  MPI_Allreduce ((values.begin() != output.begin()
584  ?
585  const_cast<void *>(static_cast<const void *>(values.begin()))
586  :
587  MPI_IN_PLACE),
588  output.begin(), values.size(), internal::mpi_type_id((T *)0), mpi_op,
589  mpi_communicator);
590  }
591  else
592 #endif
593  {
594  (void)mpi_op;
595  (void)mpi_communicator;
596  output = values;
597  }
598  }
599 
600 
601 
602  }
603 
604 
605  template <typename T>
606  inline
607  T sum (const T &t,
608  const MPI_Comm &mpi_communicator)
609  {
610  return internal::all_reduce(MPI_SUM, t, mpi_communicator);
611  }
612 
613 
614  template <typename T, unsigned int N>
615  inline
616  void sum (const T (&values)[N],
617  const MPI_Comm &mpi_communicator,
618  T (&sums)[N])
619  {
620  internal::all_reduce(MPI_SUM, values, mpi_communicator, sums);
621  }
622 
623 
624  template <typename T>
625  inline
626  void sum (const std::vector<T> &values,
627  const MPI_Comm &mpi_communicator,
628  std::vector<T> &sums)
629  {
630  internal::all_reduce(MPI_SUM, values, mpi_communicator, sums);
631  }
632 
633  template <typename T>
634  inline
635  void sum (const Vector<T> &values,
636  const MPI_Comm &mpi_communicator,
637  Vector<T> &sums)
638  {
639  internal::all_reduce(MPI_SUM, values, mpi_communicator, sums);
640  }
641 
642 
643  template <int rank, int dim, typename Number>
644  inline
646  sum (const Tensor<rank,dim,Number> &local,
647  const MPI_Comm &mpi_communicator)
648  {
649  const unsigned int n_entries = Tensor<rank,dim,Number>::n_independent_components;
651 
652  for (unsigned int i=0; i< n_entries; ++i)
653  entries[i] = local[ local.unrolled_to_component_indices(i) ];
654 
655  Number global_entries[ Tensor<rank,dim,Number>::n_independent_components ];
656  ::Utilities::MPI::sum( entries, mpi_communicator, global_entries );
657 
659  for (unsigned int i=0; i< n_entries; ++i)
660  global[ global.unrolled_to_component_indices(i) ] = global_entries[i];
661 
662  return global;
663  }
664 
665  template <int rank, int dim, typename Number>
666  inline
669  const MPI_Comm &mpi_communicator)
670  {
671  const unsigned int n_entries = SymmetricTensor<rank,dim,Number>::n_independent_components;
673 
674  for (unsigned int i=0; i< n_entries; ++i)
675  entries[i] = local[ local.unrolled_to_component_indices(i) ];
676 
678  ::Utilities::MPI::sum( entries, mpi_communicator, global_entries );
679 
681  for (unsigned int i=0; i< n_entries; ++i)
682  global[ global.unrolled_to_component_indices(i) ] = global_entries[i];
683 
684  return global;
685  }
686 
687  template <typename T>
688  inline
689  T max (const T &t,
690  const MPI_Comm &mpi_communicator)
691  {
692  return internal::all_reduce(MPI_MAX, t, mpi_communicator);
693  }
694 
695 
696  template <typename T, unsigned int N>
697  inline
698  void max (const T (&values)[N],
699  const MPI_Comm &mpi_communicator,
700  T (&maxima)[N])
701  {
702  internal::all_reduce(MPI_MAX, values, mpi_communicator, maxima);
703  }
704 
705 
706  template <typename T>
707  inline
708  void max (const std::vector<T> &values,
709  const MPI_Comm &mpi_communicator,
710  std::vector<T> &maxima)
711  {
712  internal::all_reduce(MPI_MAX, values, mpi_communicator, maxima);
713  }
714 
715 
716  template <typename T>
717  inline
718  T min (const T &t,
719  const MPI_Comm &mpi_communicator)
720  {
721  return internal::all_reduce(MPI_MIN, t, mpi_communicator);
722  }
723 
724 
725  template <typename T, unsigned int N>
726  inline
727  void min (const T (&values)[N],
728  const MPI_Comm &mpi_communicator,
729  T (&minima)[N])
730  {
731  internal::all_reduce(MPI_MIN, values, mpi_communicator, minima);
732  }
733 
734 
735  template <typename T>
736  inline
737  void min (const std::vector<T> &values,
738  const MPI_Comm &mpi_communicator,
739  std::vector<T> &minima)
740  {
741  internal::all_reduce(MPI_MIN, values, mpi_communicator, minima);
742  }
743 
744 
745  inline
746  bool job_supports_mpi ()
747  {
748 #ifdef DEAL_II_WITH_MPI
749  int MPI_has_been_started = 0;
750  MPI_Initialized(&MPI_has_been_started);
751 
752  return (MPI_has_been_started > 0);
753 #else
754  return false;
755 #endif
756  }
757  } // end of namespace MPI
758 } // end of namespace Utilities
759 
760 
761 DEAL_II_NAMESPACE_CLOSE
762 
763 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:164
static TableIndices< rank > unrolled_to_component_indices(const unsigned int i)
T sum(const T &t, const MPI_Comm &mpi_communicator)
Definition: mpi.h:607
std::size_t size() const
iterator begin()
static TableIndices< rank_ > unrolled_to_component_indices(const unsigned int i)
Definition: tensor.h:1107
unsigned int n_mpi_processes(const MPI_Comm &mpi_communicator)
Definition: mpi.cc:95
Definition: mpi.h:55
MPI_Comm duplicate_communicator(const MPI_Comm &mpi_communicator)
Definition: mpi.cc:113
Definition: mpi.h:48
T min(const T &t, const MPI_Comm &mpi_communicator)
Definition: mpi.h:718
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
unsigned int this_mpi_process(const MPI_Comm &mpi_communicator)
Definition: mpi.cc:104
MinMaxAvg min_max_avg(const double my_value, const MPI_Comm &mpi_communicator)
Definition: mpi.cc:235
std::vector< unsigned int > compute_point_to_point_communication_pattern(const MPI_Comm &mpi_comm, const std::vector< unsigned int > &destinations)
Definition: mpi.cc:122
bool job_supports_mpi()
Definition: mpi.h:746
T max(const T &t, const MPI_Comm &mpi_communicator)
Definition: mpi.h:689