From a1cbf6e0a1bb5b553a3bd9d06592c239ffb23f19 Mon Sep 17 00:00:00 2001 From: Peter Munch Date: Wed, 12 May 2021 18:54:06 +0200 Subject: [PATCH] Let CA return the requesting processes --- .../deal.II/base/mpi_consensus_algorithms.h | 18 +++---- .../base/mpi_consensus_algorithms.templates.h | 48 ++++++++++++------- tests/base/consensus_algorithm_01.cc | 10 +++- .../consensus_algorithm_01.mpirun=4.output | 4 ++ 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/include/deal.II/base/mpi_consensus_algorithms.h b/include/deal.II/base/mpi_consensus_algorithms.h index 05ad9c32ba..45fb9388a2 100644 --- a/include/deal.II/base/mpi_consensus_algorithms.h +++ b/include/deal.II/base/mpi_consensus_algorithms.h @@ -174,9 +174,9 @@ namespace Utilities virtual ~Interface() = default; /** - * Run consensus algorithm. + * Run consensus algorithm and return the requesting processes. */ - virtual void + virtual std::vector run() = 0; protected: @@ -241,7 +241,7 @@ namespace Utilities /** * @copydoc Interface::run() */ - virtual void + virtual std::vector run() override; private: @@ -286,12 +286,10 @@ namespace Utilities MPI_Request barrier_request; #endif -#ifdef DEBUG /** * List of processes who have made a request to this process. */ std::set requesting_processes; -#endif /** * Check if all request answers have been received by this rank. @@ -377,7 +375,7 @@ namespace Utilities /** * @copydoc Interface::run() */ - virtual void + virtual std::vector run() override; private: @@ -419,6 +417,10 @@ namespace Utilities */ std::vector requests_answers; #endif + /** + * List of processes who have made a request to this process. + */ + std::set requesting_processes; /** * The ith request message from another rank has been received: process @@ -461,7 +463,7 @@ namespace Utilities /** * @copydoc Interface::run() */ - virtual void + virtual std::vector run() override; }; @@ -497,7 +499,7 @@ namespace Utilities * * @note The function call is delegated to another ConsensusAlgorithms::Interface implementation. */ - virtual void + virtual std::vector run() override; private: diff --git a/include/deal.II/base/mpi_consensus_algorithms.templates.h b/include/deal.II/base/mpi_consensus_algorithms.templates.h index 3037ef78fc..eb6945da09 100644 --- a/include/deal.II/base/mpi_consensus_algorithms.templates.h +++ b/include/deal.II/base/mpi_consensus_algorithms.templates.h @@ -98,7 +98,7 @@ namespace Utilities template - void + std::vector NBX::run() { static CollectiveMutex mutex; @@ -124,6 +124,10 @@ namespace Utilities // 5) process the answer to all requests clean_up_and_end_communication(); + + return std::vector(requesting_processes.begin(), + requesting_processes.end()); + ; } @@ -211,12 +215,10 @@ namespace Utilities // get rank of requesting process const auto other_rank = status.MPI_SOURCE; -# ifdef DEBUG Assert(requesting_processes.find(other_rank) == requesting_processes.end(), ExcMessage("Process is requesting a second time!")); requesting_processes.insert(other_rank); -# endif std::vector buffer_recv; // get size of incoming message @@ -378,7 +380,7 @@ namespace Utilities template - void + std::vector PEX::run() { static CollectiveMutex mutex; @@ -394,6 +396,9 @@ namespace Utilities // 3) process answers clean_up_and_end_communication(); + + return std::vector(requesting_processes.begin(), + requesting_processes.end()); } @@ -415,6 +420,11 @@ namespace Utilities // get rank of incoming message const auto other_rank = status.MPI_SOURCE; + Assert(requesting_processes.find(other_rank) == + requesting_processes.end(), + ExcMessage("Process is requesting a second time!")); + requesting_processes.insert(other_rank); + std::vector buffer_recv; // get size of incoming message @@ -560,25 +570,27 @@ namespace Utilities template - void + std::vector Serial::run() { const auto targets = this->process.compute_targets(); - if (targets.size() == 0) - return; // nothing to do + if (targets.size() != 0) + { + AssertDimension(targets[0], 0); - AssertDimension(targets[0], 0); + std::vector send_buffer; + std::vector recv_buffer; + std::vector request_buffer; - std::vector send_buffer; - std::vector recv_buffer; - std::vector request_buffer; + this->process.create_request(0, send_buffer); + this->process.prepare_buffer_for_answer(0, recv_buffer); + this->process.answer_request(0, send_buffer, request_buffer); + recv_buffer = request_buffer; + this->process.read_answer(0, recv_buffer); + } - this->process.create_request(0, send_buffer); - this->process.prepare_buffer_for_answer(0, recv_buffer); - this->process.answer_request(0, send_buffer, request_buffer); - recv_buffer = request_buffer; - this->process.read_answer(0, recv_buffer); + return targets; // nothing to do } @@ -612,10 +624,10 @@ namespace Utilities template - void + std::vector Selector::run() { - consensus_algo->run(); + return consensus_algo->run(); } diff --git a/tests/base/consensus_algorithm_01.cc b/tests/base/consensus_algorithm_01.cc index 0d2204825b..bdbe93f616 100644 --- a/tests/base/consensus_algorithm_01.cc +++ b/tests/base/consensus_algorithm_01.cc @@ -54,8 +54,14 @@ test(const MPI_Comm &comm) deallog << "ConsensusAlgorithmProcess::function_read_answer() passed!" << std::endl; }); - dealii::Utilities::MPI::ConsensusAlgorithms::Selector(process, comm) - .run(); + + const auto sources = + dealii::Utilities::MPI::ConsensusAlgorithms::Selector(process, comm) + .run(); + + for (const auto &i : sources) + deallog << i << " "; + deallog << std::endl; } int diff --git a/tests/base/consensus_algorithm_01.mpirun=4.output b/tests/base/consensus_algorithm_01.mpirun=4.output index 8faf9d944b..f2df94cb28 100644 --- a/tests/base/consensus_algorithm_01.mpirun=4.output +++ b/tests/base/consensus_algorithm_01.mpirun=4.output @@ -1,15 +1,19 @@ DEAL:0::ConsensusAlgorithmProcess::answer_request() passed! DEAL:0::ConsensusAlgorithmProcess::function_read_answer() passed! +DEAL:0::3 DEAL:1::ConsensusAlgorithmProcess::answer_request() passed! DEAL:1::ConsensusAlgorithmProcess::function_read_answer() passed! +DEAL:1::0 DEAL:2::ConsensusAlgorithmProcess::answer_request() passed! DEAL:2::ConsensusAlgorithmProcess::function_read_answer() passed! +DEAL:2::1 DEAL:3::ConsensusAlgorithmProcess::answer_request() passed! DEAL:3::ConsensusAlgorithmProcess::function_read_answer() passed! +DEAL:3::2 -- 2.39.5