Reference documentation for deal.II version 9.2.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
mpi_consensus_algorithms.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2020 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.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_mpi_consensus_algorithm_h
17 #define dealii_mpi_consensus_algorithm_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <deal.II/base/mpi.h>
22 #include <deal.II/base/mpi.templates.h>
23 
25 
26 
27 namespace Utilities
28 {
29  namespace MPI
30  {
37  namespace ConsensusAlgorithms
38  {
63  template <typename T1, typename T2>
64  class Process
65  {
66  public:
70  virtual ~Process() = default;
71 
78  virtual std::vector<unsigned int>
79  compute_targets() = 0;
80 
90  virtual void
91  create_request(const unsigned int other_rank,
92  std::vector<T1> & send_buffer);
93 
104  virtual void
105  prepare_buffer_for_answer(const unsigned int other_rank,
106  std::vector<T2> & recv_buffer);
107 
120  virtual void
121  answer_request(const unsigned int other_rank,
122  const std::vector<T1> &buffer_recv,
123  std::vector<T2> & request_buffer);
124 
132  virtual void
133  read_answer(const unsigned int other_rank,
134  const std::vector<T2> &recv_buffer);
135  };
136 
137 
138 
169  template <typename T1, typename T2>
170  class Interface
171  {
172  public:
174 
178  virtual ~Interface() = default;
179 
183  virtual void
184  run() = 0;
185 
186  protected:
191 
195  const MPI_Comm &comm;
196 
200  const unsigned int my_rank;
201 
205  const unsigned int n_procs;
206  };
207 
208 
227  template <typename T1, typename T2>
228  class NBX : public Interface<T1, T2>
229  {
230  public:
238 
242  virtual ~NBX() = default;
243 
247  virtual void
248  run() override;
249 
250  private:
251 #ifdef DEAL_II_WITH_MPI
252 
255  std::vector<unsigned int> targets;
256 
260  std::vector<std::vector<T1>> send_buffers;
261 
265  std::vector<MPI_Request> send_requests;
266 
270  std::vector<std::vector<T2>> recv_buffers;
271 
272 
276  std::vector<MPI_Request> recv_requests;
277 
281  std::vector<std::unique_ptr<std::vector<T2>>> request_buffers;
282 
286  std::vector<std::unique_ptr<MPI_Request>> request_requests;
287 
288  // request for barrier
290 #endif
291 
292 #ifdef DEBUG
293 
296  std::set<unsigned int> requesting_processes;
297 #endif
298 
302  bool
303  check_own_state();
304 
309  void
310  signal_finish();
311 
316  bool
318 
323  void
324  answer_requests();
325 
330  void
332 
337  void
339  };
340 
366  template <typename T1, typename T2>
367  class PEX : public Interface<T1, T2>
368  {
369  public:
377 
381  virtual ~PEX() = default;
382 
386  virtual void
387  run() override;
388 
389  private:
390 #ifdef DEAL_II_WITH_MPI
391 
394  std::vector<unsigned int> targets;
395 
399  std::vector<unsigned int> sources;
400 
401  // data structures to send and receive requests
402 
406  std::vector<std::vector<T1>> send_buffers;
407 
411  std::vector<std::vector<T2>> recv_buffers;
412 
416  std::vector<MPI_Request> send_and_recv_buffers;
417 
421  std::vector<std::vector<T2>> requests_buffers;
422 
426  std::vector<MPI_Request> requests_answers;
427 #endif
428 
433  void
434  answer_requests(int index);
435 
440  unsigned int
442 
447  void
449  };
450 
463  template <typename T1, typename T2>
464  class Selector : public Interface<T1, T2>
465  {
466  public:
474 
478  virtual ~Selector() = default;
479 
485  virtual void
486  run() override;
487 
488  private:
489  // Pointer to the actual ConsensusAlgorithms::Interface implementation.
490  std::shared_ptr<Interface<T1, T2>> consensus_algo;
491  };
492 
499  template <typename T1, typename T2>
500  class AnonymousProcess : public Process<T1, T2>
501  {
502  public:
515  const std::function<std::vector<unsigned int>()>
517  const std::function<void(const unsigned int, std::vector<T1> &)>
519  [](const unsigned int, std::vector<T1> &) {},
520  const std::function<void(const unsigned int,
521  const std::vector<T1> &,
522  std::vector<T2> &)>
523  &function_answer_request = [](const unsigned int,
524  const std::vector<T1> &,
525  std::vector<T2> &) {},
526  const std::function<void(const unsigned int, std::vector<T2> &)>
528  [](const unsigned int, std::vector<T2> &) {},
529  const std::function<void(const unsigned int, const std::vector<T2> &)>
531  [](const unsigned int, const std::vector<T2> &) {});
532 
536  std::vector<unsigned int>
537  compute_targets() override;
538 
542  void
543  create_request(const unsigned int other_rank,
544  std::vector<T1> & send_buffer) override;
545 
549  void
550  answer_request(const unsigned int other_rank,
551  const std::vector<T1> &buffer_recv,
552  std::vector<T2> & request_buffer) override;
553 
557  void
558  prepare_buffer_for_answer(const unsigned int other_rank,
559  std::vector<T2> & recv_buffer) override;
560 
564  void
565  read_answer(const unsigned int other_rank,
566  const std::vector<T2> &recv_buffer) override;
567 
568  private:
569  const std::function<std::vector<unsigned int>()>
571  const std::function<void(const int, std::vector<T1> &)>
573  const std::function<
574  void(const unsigned int, const std::vector<T1> &, std::vector<T2> &)>
576  const std::function<void(const int, std::vector<T2> &)>
578  const std::function<void(const int, const std::vector<T2> &)>
580  };
581 
582 
583 
584  template <typename T1, typename T2>
586  const std::function<std::vector<unsigned int>()>
587  &function_compute_targets,
588  const std::function<void(const unsigned int, std::vector<T1> &)>
589  & function_create_request,
590  const std::function<void(const unsigned int,
591  const std::vector<T1> &,
592  std::vector<T2> &)> &function_answer_request,
593  const std::function<void(const unsigned int, std::vector<T2> &)>
594  &function_prepare_buffer_for_answer,
595  const std::function<void(const unsigned int, const std::vector<T2> &)>
596  &function_read_answer)
597  : function_compute_targets(function_compute_targets)
598  , function_create_request(function_create_request)
599  , function_answer_request(function_answer_request)
600  , function_prepare_buffer_for_answer(function_prepare_buffer_for_answer)
601  , function_read_answer(function_read_answer)
602  {}
603 
604 
605 
606  template <typename T1, typename T2>
607  std::vector<unsigned int>
609  {
610  return function_compute_targets();
611  }
612 
613 
614 
615  template <typename T1, typename T2>
616  void
617  AnonymousProcess<T1, T2>::create_request(const unsigned int other_rank,
618  std::vector<T1> & send_buffer)
619  {
620  function_create_request(other_rank, send_buffer);
621  }
622 
623 
624 
625  template <typename T1, typename T2>
626  void
628  const unsigned int other_rank,
629  const std::vector<T1> &buffer_recv,
630  std::vector<T2> & request_buffer)
631  {
632  function_answer_request(other_rank, buffer_recv, request_buffer);
633  }
634 
635 
636 
637  template <typename T1, typename T2>
638  void
640  const unsigned int other_rank,
641  std::vector<T2> & recv_buffer)
642  {
643  function_prepare_buffer_for_answer(other_rank, recv_buffer);
644  }
645 
646 
647 
648  template <typename T1, typename T2>
649  void
650  AnonymousProcess<T1, T2>::read_answer(const unsigned int other_rank,
651  const std::vector<T2> &recv_buffer)
652  {
653  function_read_answer(other_rank, recv_buffer);
654  }
655 
656 
657 
658  } // namespace ConsensusAlgorithms
659  } // end of namespace MPI
660 } // end of namespace Utilities
661 
662 
664 
665 #endif
Utilities::MPI::ConsensusAlgorithms::Process::compute_targets
virtual std::vector< unsigned int > compute_targets()=0
Utilities::MPI::ConsensusAlgorithms::NBX::~NBX
virtual ~NBX()=default
Utilities::MPI::ConsensusAlgorithms::Interface::~Interface
virtual ~Interface()=default
Utilities::MPI::ConsensusAlgorithms::PEX::clean_up_and_end_communication
void clean_up_and_end_communication()
Utilities::MPI::ConsensusAlgorithms::Process::~Process
virtual ~Process()=default
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess::function_answer_request
const std::function< void(const unsigned int, const std::vector< T1 > &, std::vector< T2 > &)> function_answer_request
Definition: mpi_consensus_algorithms.h:575
Utilities::MPI::ConsensusAlgorithms::Selector::consensus_algo
std::shared_ptr< Interface< T1, T2 > > consensus_algo
Definition: mpi_consensus_algorithms.h:490
Utilities::MPI::ConsensusAlgorithms::PEX::send_and_recv_buffers
std::vector< MPI_Request > send_and_recv_buffers
Definition: mpi_consensus_algorithms.h:416
Utilities::MPI::ConsensusAlgorithms::NBX::targets
std::vector< unsigned int > targets
Definition: mpi_consensus_algorithms.h:255
Utilities::MPI::ConsensusAlgorithms::Interface::run
virtual void run()=0
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess::function_create_request
const std::function< void(const int, std::vector< T1 > &)> function_create_request
Definition: mpi_consensus_algorithms.h:572
Utilities::MPI::ConsensusAlgorithms::PEX::~PEX
virtual ~PEX()=default
Utilities::MPI::ConsensusAlgorithms::Interface::Interface
Interface(Process< T1, T2 > &process, const MPI_Comm &comm)
Utilities::MPI::ConsensusAlgorithms::PEX
Definition: mpi_consensus_algorithms.h:367
Utilities::MPI::ConsensusAlgorithms::NBX::send_requests
std::vector< MPI_Request > send_requests
Definition: mpi_consensus_algorithms.h:265
MPI_Comm
Utilities::MPI::ConsensusAlgorithms::NBX::check_own_state
bool check_own_state()
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess::read_answer
void read_answer(const unsigned int other_rank, const std::vector< T2 > &recv_buffer) override
Definition: mpi_consensus_algorithms.h:650
Utilities::MPI::ConsensusAlgorithms::NBX::request_buffers
std::vector< std::unique_ptr< std::vector< T2 > > > request_buffers
Definition: mpi_consensus_algorithms.h:281
Utilities::MPI::ConsensusAlgorithms::Process::read_answer
virtual void read_answer(const unsigned int other_rank, const std::vector< T2 > &recv_buffer)
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess::prepare_buffer_for_answer
void prepare_buffer_for_answer(const unsigned int other_rank, std::vector< T2 > &recv_buffer) override
Definition: mpi_consensus_algorithms.h:639
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess::AnonymousProcess
AnonymousProcess(const std::function< std::vector< unsigned int >()> &function_compute_targets, const std::function< void(const unsigned int, std::vector< T1 > &)> &function_create_request=[](const unsigned int, std::vector< T1 > &) {}, const std::function< void(const unsigned int, const std::vector< T1 > &, std::vector< T2 > &)> &function_answer_request=[](const unsigned int, const std::vector< T1 > &, std::vector< T2 > &) {}, const std::function< void(const unsigned int, std::vector< T2 > &)> &function_prepare_buffer_for_answer=[](const unsigned int, std::vector< T2 > &) {}, const std::function< void(const unsigned int, const std::vector< T2 > &)> &function_read_answer=[](const unsigned int, const std::vector< T2 > &) {})
Definition: mpi_consensus_algorithms.h:585
Utilities::MPI::ConsensusAlgorithms::Selector
Definition: mpi_consensus_algorithms.h:464
Utilities::MPI::ConsensusAlgorithms::NBX::signal_finish
void signal_finish()
Utilities::MPI::ConsensusAlgorithms::Process::prepare_buffer_for_answer
virtual void prepare_buffer_for_answer(const unsigned int other_rank, std::vector< T2 > &recv_buffer)
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess
Definition: mpi_consensus_algorithms.h:500
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess::answer_request
void answer_request(const unsigned int other_rank, const std::vector< T1 > &buffer_recv, std::vector< T2 > &request_buffer) override
Definition: mpi_consensus_algorithms.h:627
mpi.h
Utilities::MPI::ConsensusAlgorithms::Interface::process
Process< T1, T2 > & process
Definition: mpi_consensus_algorithms.h:190
Utilities::MPI::ConsensusAlgorithms::NBX::recv_requests
std::vector< MPI_Request > recv_requests
Definition: mpi_consensus_algorithms.h:276
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
Utilities::MPI::ConsensusAlgorithms::NBX::answer_requests
void answer_requests()
Utilities::MPI::ConsensusAlgorithms::Selector::~Selector
virtual ~Selector()=default
Utilities::MPI::ConsensusAlgorithms::PEX::start_communication
unsigned int start_communication()
Utilities::MPI::ConsensusAlgorithms::PEX::sources
std::vector< unsigned int > sources
Definition: mpi_consensus_algorithms.h:399
Utilities::MPI::ConsensusAlgorithms::NBX::barrier_request
MPI_Request barrier_request
Definition: mpi_consensus_algorithms.h:289
Utilities::MPI::ConsensusAlgorithms::NBX::run
virtual void run() override
Utilities::MPI::ConsensusAlgorithms::NBX::send_buffers
std::vector< std::vector< T1 > > send_buffers
Definition: mpi_consensus_algorithms.h:260
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess::function_prepare_buffer_for_answer
const std::function< void(const int, std::vector< T2 > &)> function_prepare_buffer_for_answer
Definition: mpi_consensus_algorithms.h:577
Utilities::MPI::ConsensusAlgorithms::PEX::send_buffers
std::vector< std::vector< T1 > > send_buffers
Definition: mpi_consensus_algorithms.h:406
Utilities::MPI::ConsensusAlgorithms::Process::create_request
virtual void create_request(const unsigned int other_rank, std::vector< T1 > &send_buffer)
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess::create_request
void create_request(const unsigned int other_rank, std::vector< T1 > &send_buffer) override
Definition: mpi_consensus_algorithms.h:617
Utilities::MPI::ConsensusAlgorithms::Interface::comm
const MPI_Comm & comm
Definition: mpi_consensus_algorithms.h:195
Utilities::MPI::ConsensusAlgorithms::PEX::run
virtual void run() override
Utilities::MPI::ConsensusAlgorithms::NBX
Definition: mpi_consensus_algorithms.h:228
Utilities::MPI::ConsensusAlgorithms::PEX::PEX
PEX(Process< T1, T2 > &process, const MPI_Comm &comm)
Utilities::MPI::ConsensusAlgorithms::Interface
Definition: mpi_consensus_algorithms.h:170
Utilities::MPI::ConsensusAlgorithms::NBX::NBX
NBX(Process< T1, T2 > &process, const MPI_Comm &comm)
Utilities::MPI::ConsensusAlgorithms::NBX::recv_buffers
std::vector< std::vector< T2 > > recv_buffers
Definition: mpi_consensus_algorithms.h:270
Utilities::MPI::ConsensusAlgorithms::PEX::requests_answers
std::vector< MPI_Request > requests_answers
Definition: mpi_consensus_algorithms.h:426
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess::compute_targets
std::vector< unsigned int > compute_targets() override
Definition: mpi_consensus_algorithms.h:608
Utilities::MPI::ConsensusAlgorithms::NBX::check_global_state
bool check_global_state()
Utilities::MPI::ConsensusAlgorithms::Selector::run
virtual void run() override
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess::function_compute_targets
const std::function< std::vector< unsigned int >)> function_compute_targets
Definition: mpi_consensus_algorithms.h:570
Utilities::MPI::ConsensusAlgorithms::Selector::Selector
Selector(Process< T1, T2 > &process, const MPI_Comm &comm)
Utilities::MPI::ConsensusAlgorithms::NBX::requesting_processes
std::set< unsigned int > requesting_processes
Definition: mpi_consensus_algorithms.h:296
config.h
Utilities::MPI::ConsensusAlgorithms::PEX::requests_buffers
std::vector< std::vector< T2 > > requests_buffers
Definition: mpi_consensus_algorithms.h:421
Utilities::MPI::ConsensusAlgorithms::PEX::recv_buffers
std::vector< std::vector< T2 > > recv_buffers
Definition: mpi_consensus_algorithms.h:411
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
Utilities::MPI::ConsensusAlgorithms::NBX::clean_up_and_end_communication
void clean_up_and_end_communication()
Utilities::MPI::ConsensusAlgorithms::NBX::request_requests
std::vector< std::unique_ptr< MPI_Request > > request_requests
Definition: mpi_consensus_algorithms.h:286
Utilities::MPI::ConsensusAlgorithms::NBX::start_communication
void start_communication()
MPI_Request
Utilities::MPI::ConsensusAlgorithms::Interface::n_procs
const unsigned int n_procs
Definition: mpi_consensus_algorithms.h:205
Utilities
Definition: cuda.h:31
Utilities::MPI::ConsensusAlgorithms::PEX::answer_requests
void answer_requests(int index)
Utilities::MPI::ConsensusAlgorithms::Process::answer_request
virtual void answer_request(const unsigned int other_rank, const std::vector< T1 > &buffer_recv, std::vector< T2 > &request_buffer)
Utilities::MPI::ConsensusAlgorithms::AnonymousProcess::function_read_answer
const std::function< void(const int, const std::vector< T2 > &)> function_read_answer
Definition: mpi_consensus_algorithms.h:579
Utilities::MPI::ConsensusAlgorithms::PEX::targets
std::vector< unsigned int > targets
Definition: mpi_consensus_algorithms.h:394
int
Utilities::MPI::ConsensusAlgorithms::Interface::my_rank
const unsigned int my_rank
Definition: mpi_consensus_algorithms.h:200
Utilities::MPI::ConsensusAlgorithms::Process
Definition: mpi_consensus_algorithms.h:64