From 71cba4d8c1e886c89d16930759704ea7d52a3ba9 Mon Sep 17 00:00:00 2001 From: Peter Munch Date: Tue, 7 Apr 2020 10:33:06 +0200 Subject: [PATCH] Add Utilities::MPI::compute_set_union --- doc/news/changes/minor/20200404PeterMunch | 4 ++-- include/deal.II/base/mpi.h | 9 ++++++++- include/deal.II/base/mpi.templates.h | 20 ++++++++++++++++++-- source/base/mpi.cc | 7 ++++++- tests/mpi/union_01.cc | 23 ++++++++++++++++++----- tests/mpi/union_01.mpirun=2.output | 2 ++ 6 files changed, 54 insertions(+), 11 deletions(-) diff --git a/doc/news/changes/minor/20200404PeterMunch b/doc/news/changes/minor/20200404PeterMunch index 20b83cd62a..4cda0fa562 100644 --- a/doc/news/changes/minor/20200404PeterMunch +++ b/doc/news/changes/minor/20200404PeterMunch @@ -1,4 +1,4 @@ -New: Add new function Utilities::MPI::compute_union(), which computes the union of given input -vectors of all processes in a given MPI communicator. +New: Add new function Utilities::MPI::compute_set_union(), which computes the union of given input +sets and vectors of all processes in a given MPI communicator.
(Peter Munch, 2020/04/04) diff --git a/include/deal.II/base/mpi.h b/include/deal.II/base/mpi.h index 8c118a978b..589260d15c 100644 --- a/include/deal.II/base/mpi.h +++ b/include/deal.II/base/mpi.h @@ -1033,7 +1033,14 @@ namespace Utilities */ template std::vector - compute_union(const std::vector &vec, const MPI_Comm &comm); + compute_set_union(const std::vector &vec, const MPI_Comm &comm); + + /** + * The same as above but for std::set. + */ + template + std::set + compute_set_union(const std::set &set, const MPI_Comm &comm); #ifndef DOXYGEN // declaration for an internal function that lives in mpi.templates.h diff --git a/include/deal.II/base/mpi.templates.h b/include/deal.II/base/mpi.templates.h index d99d6c4841..a71ab36f49 100644 --- a/include/deal.II/base/mpi.templates.h +++ b/include/deal.II/base/mpi.templates.h @@ -418,7 +418,7 @@ namespace Utilities template std::vector - compute_union(const std::vector &vec, const MPI_Comm &comm) + compute_set_union(const std::vector &vec, const MPI_Comm &comm) { #ifdef DEAL_II_WITH_MPI // 1) collect vector entries and create union @@ -447,7 +447,7 @@ namespace Utilities const auto ierr_3 = MPI_Recv(result.data() + old_size, amount, - MPI_INT, + internal::mpi_type_id(vec.data()), status.MPI_SOURCE, status.MPI_TAG, comm, @@ -484,6 +484,22 @@ namespace Utilities #endif } + + + template + std::set + compute_set_union(const std::set &set_in, const MPI_Comm &comm) + { + // convert vector to set + std::vector vector_in(set_in.begin(), set_in.end()); + + // perform operation to vector + const std::vector vector_out = compute_set_union(vector_in, comm); + + // convert vector to set + return std::set(vector_out.begin(), vector_out.end()); + } + } // end of namespace MPI } // end of namespace Utilities diff --git a/source/base/mpi.cc b/source/base/mpi.cc index d6784fe361..f8cddf8b7f 100644 --- a/source/base/mpi.cc +++ b/source/base/mpi.cc @@ -1128,7 +1128,12 @@ namespace Utilities template std::vector - compute_union(const std::vector &vec, const MPI_Comm &comm); + compute_set_union(const std::vector &vec, + const MPI_Comm & comm); + + + template std::set + compute_set_union(const std::set &set, const MPI_Comm &comm); #include "mpi.inst" } // end of namespace MPI diff --git a/tests/mpi/union_01.cc b/tests/mpi/union_01.cc index 884e113499..6fa5c3ae7d 100644 --- a/tests/mpi/union_01.cc +++ b/tests/mpi/union_01.cc @@ -14,7 +14,7 @@ // --------------------------------------------------------------------- -// Create a distributed triangulation with multigrid levels and copy it. +// Test the function compute_set_union() for set::vector and std::set. #include @@ -32,11 +32,24 @@ test(const MPI_Comm comm) else vector = std::vector{5, 8, 9, 10}; - const auto result = Utilities::MPI::compute_union(vector, comm); + // test function for vector + { + const auto result = Utilities::MPI::compute_set_union(vector, comm); - for (auto i : result) - deallog << i << " "; - deallog << std::endl; + for (auto i : result) + deallog << i << " "; + deallog << std::endl; + } + + // test function for set + { + const auto result = Utilities::MPI::compute_set_union( + std::set(vector.begin(), vector.end()), comm); + + for (auto i : result) + deallog << i << " "; + deallog << std::endl; + } } int diff --git a/tests/mpi/union_01.mpirun=2.output b/tests/mpi/union_01.mpirun=2.output index ba6eac9eb4..c0d718074a 100644 --- a/tests/mpi/union_01.mpirun=2.output +++ b/tests/mpi/union_01.mpirun=2.output @@ -1,5 +1,7 @@ +DEAL:0::2 5 7 8 9 10 DEAL:0::2 5 7 8 9 10 DEAL:1::2 5 7 8 9 10 +DEAL:1::2 5 7 8 9 10 -- 2.39.5