From 93689c99ead52232a7e578ba2d72312b680c6e91 Mon Sep 17 00:00:00 2001 From: Peter Munch Date: Mon, 23 Nov 2020 08:34:54 +0100 Subject: [PATCH] Provide a serial implementation of ConsensusAlgorithms::Interface --- .../deal.II/base/mpi_consensus_algorithms.h | 28 ++++++++++++ .../base/mpi_consensus_algorithms.templates.h | 43 ++++++++++++++++--- source/base/mpi_consensus_algorithms.cc | 8 ++++ 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/include/deal.II/base/mpi_consensus_algorithms.h b/include/deal.II/base/mpi_consensus_algorithms.h index eb5c98e532..707644ad00 100644 --- a/include/deal.II/base/mpi_consensus_algorithms.h +++ b/include/deal.II/base/mpi_consensus_algorithms.h @@ -190,6 +190,11 @@ namespace Utilities */ const MPI_Comm &comm; + /** + * Cache if job supports MPI. + */ + const bool job_supports_mpi; + /** * Rank of this process. */ @@ -440,6 +445,29 @@ namespace Utilities clean_up_and_end_communication(); }; + /** + * A serial fall back for the above classes to allow programming + * independently of whether MPI is used or not. + */ + template + class Serial : public Interface + { + public: + /** + * Constructor. + * + * @param process Process to be run during consensus algorithm. + * @param comm MPI Communicator (ignored) + */ + Serial(Process &process, const MPI_Comm &comm); + + /** + * @copydoc Interface::run() + */ + virtual void + run() override; + }; + /** * A class which delegates its task to other * ConsensusAlgorithms::Interface implementations depending on the number diff --git a/include/deal.II/base/mpi_consensus_algorithms.templates.h b/include/deal.II/base/mpi_consensus_algorithms.templates.h index 010f069785..725c611ea0 100644 --- a/include/deal.II/base/mpi_consensus_algorithms.templates.h +++ b/include/deal.II/base/mpi_consensus_algorithms.templates.h @@ -83,10 +83,9 @@ namespace Utilities const MPI_Comm & comm) : process(process) , comm(comm) - , my_rank(Utilities::MPI::job_supports_mpi() ? this_mpi_process(comm) : - 0) - , n_procs(Utilities::MPI::job_supports_mpi() ? n_mpi_processes(comm) : - 1) + , job_supports_mpi(Utilities::MPI::job_supports_mpi()) + , my_rank(job_supports_mpi ? this_mpi_process(comm) : 0) + , n_procs(job_supports_mpi ? n_mpi_processes(comm) : 1) {} @@ -555,6 +554,37 @@ namespace Utilities + template + Serial::Serial(Process &process, const MPI_Comm &comm) + : Interface(process, comm) + {} + + + + template + void + Serial::run() + { + const auto targets = this->process.compute_targets(); + + if (targets.size() == 0) + return; // nothing to do + + AssertDimension(targets[0], 0); + + std::vector send_buffer; + std::vector recv_buffer; + std::vector request_buffer; + std::vector buffer_recv; + + this->process.create_request(0, send_buffer); + this->process.prepare_buffer_for_answer(0, recv_buffer); + this->process.answer_request(0, buffer_recv, request_buffer); + this->process.read_answer(0, recv_buffer); + } + + + template Selector::Selector(Process &process, const MPI_Comm &comm) : Interface(process, comm) @@ -577,6 +607,8 @@ namespace Utilities #endif if (this->n_procs > 1) consensus_algo.reset(new PEX(process, comm)); + else + consensus_algo.reset(new Serial(process, comm)); } @@ -585,8 +617,7 @@ namespace Utilities void Selector::run() { - if (consensus_algo) - consensus_algo->run(); + consensus_algo->run(); } diff --git a/source/base/mpi_consensus_algorithms.cc b/source/base/mpi_consensus_algorithms.cc index 1eebaa29ea..0f1b560198 100644 --- a/source/base/mpi_consensus_algorithms.cc +++ b/source/base/mpi_consensus_algorithms.cc @@ -29,6 +29,8 @@ namespace Utilities template class PEX; + template class Serial; + template class Selector; @@ -44,6 +46,10 @@ namespace Utilities std::pair, unsigned int>; + template class Serial< + std::pair, + unsigned int>; + template class PEX< std::pair, unsigned int>; @@ -53,6 +59,8 @@ namespace Utilities template class NBX; + template class Serial; + template class PEX; template class Selector; -- 2.39.5