From: Peter Munch <peterrmuench@gmail.com>
Date: Tue, 7 Apr 2020 08:33:06 +0000 (+0200)
Subject: Add Utilities::MPI::compute_set_union
X-Git-Tag: v9.2.0-rc1~275^2
X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=71cba4d8c1e886c89d16930759704ea7d52a3ba9;p=dealii.git

Add Utilities::MPI::compute_set_union
---

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.
 <br>
 (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 <typename T>
     std::vector<T>
-    compute_union(const std::vector<T> &vec, const MPI_Comm &comm);
+    compute_set_union(const std::vector<T> &vec, const MPI_Comm &comm);
+
+    /**
+     * The same as above but for std::set.
+     */
+    template <typename T>
+    std::set<T>
+    compute_set_union(const std::set<T> &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 <typename T>
     std::vector<T>
-    compute_union(const std::vector<T> &vec, const MPI_Comm &comm)
+    compute_set_union(const std::vector<T> &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 <typename T>
+    std::set<T>
+    compute_set_union(const std::set<T> &set_in, const MPI_Comm &comm)
+    {
+      // convert vector to set
+      std::vector<T> vector_in(set_in.begin(), set_in.end());
+
+      // perform operation to vector
+      const std::vector<T> vector_out = compute_set_union(vector_in, comm);
+
+      // convert vector to set
+      return std::set<T>(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<unsigned int>
-    compute_union(const std::vector<unsigned int> &vec, const MPI_Comm &comm);
+    compute_set_union(const std::vector<unsigned int> &vec,
+                      const MPI_Comm &                 comm);
+
+
+    template std::set<unsigned int>
+    compute_set_union(const std::set<unsigned int> &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 <deal.II/base/mpi.h>
 
@@ -32,11 +32,24 @@ test(const MPI_Comm comm)
   else
     vector = std::vector<unsigned int>{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<unsigned int>(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