]> https://gitweb.dealii.org/ - dealii.git/commitdiff
implement Utilities::MPI::sum for Vector<number> 1093/head
authorDenis Davydov <davydden@gmail.com>
Thu, 9 Jul 2015 11:10:54 +0000 (13:10 +0200)
committerDenis Davydov <davydden@gmail.com>
Thu, 9 Jul 2015 12:58:19 +0000 (14:58 +0200)
include/deal.II/base/mpi.h
tests/mpi/collective_02_dealii_vector.cc [new file with mode: 0644]
tests/mpi/collective_02_dealii_vector.mpirun=1.output [new file with mode: 0644]
tests/mpi/collective_02_dealii_vector.mpirun=4.output [new file with mode: 0644]
tests/mpi/collective_02_dealii_vector_in_place.cc [new file with mode: 0644]
tests/mpi/collective_02_dealii_vector_in_place.mpirun=1.output [new file with mode: 0644]
tests/mpi/collective_02_dealii_vector_in_place.mpirun=4.output [new file with mode: 0644]

index 54ed4017cf38a819741bf7f08100b96c174db9c2..5ffd0b4a7140207eb4103fd27b637e6b7b502518 100644 (file)
@@ -57,6 +57,10 @@ DEAL_II_NAMESPACE_OPEN
 template <int rank, int dim, typename Number> class Tensor;
 template <int rank, int dim, typename Number> class SymmetricTensor;
 
+//Forward type declaration to allow MPI sums over Vector<number> type
+template <typename Number> class Vector;
+
+
 namespace Utilities
 {
   /**
@@ -166,6 +170,19 @@ namespace Utilities
               const MPI_Comm &mpi_communicator,
               std::vector<T> &sums);
 
+    /**
+     * Like the previous function, but take the sums over the elements of a
+     * Vector<T>.
+     *
+     * Input and output vectors may be the same.
+     */
+    template <typename T>
+    inline
+    void sum (const Vector<T> &values,
+              const MPI_Comm &mpi_communicator,
+              Vector<T> &sums);
+
+
     /**
      * Perform an MPI sum of the entries of a symmetric tensor.
      *
@@ -547,6 +564,37 @@ namespace Utilities
           }
       }
 
+      template <typename T>
+      inline
+      void all_reduce (const MPI_Op    &mpi_op,
+                       const Vector<T> &values,
+                       const MPI_Comm  &mpi_communicator,
+                       Vector<T>  &output)
+      {
+#ifdef DEAL_II_WITH_MPI
+        if (job_supports_mpi())
+          {
+            if (values.begin() != output.begin())
+              output.reinit (values.size());
+
+            MPI_Allreduce ((values.begin() != output.begin()
+                            ?
+                            const_cast<void *>(static_cast<const void *>(values.begin()))
+                            :
+                            MPI_IN_PLACE),
+                           output.begin(), values.size(), internal::mpi_type_id((T *)0), mpi_op,
+                           mpi_communicator);
+          }
+        else
+#endif
+          {
+            (void)mpi_op;
+            (void)mpi_communicator;
+            output = values;
+          }
+      }
+
+
 
     }
 
@@ -579,6 +627,16 @@ namespace Utilities
       internal::all_reduce(MPI_SUM, values, mpi_communicator, sums);
     }
 
+    template <typename T>
+    inline
+    void sum (const Vector<T> &values,
+              const MPI_Comm &mpi_communicator,
+              Vector<T> &sums)
+    {
+      internal::all_reduce(MPI_SUM, values, mpi_communicator, sums);
+    }
+
+
     template <int rank, int dim, typename Number>
     inline
     Tensor<rank,dim,Number>
diff --git a/tests/mpi/collective_02_dealii_vector.cc b/tests/mpi/collective_02_dealii_vector.cc
new file mode 100644 (file)
index 0000000..9ab0d66
--- /dev/null
@@ -0,0 +1,88 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2009 - 2015 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+
+// check Utilities::MPI::sum() for dealii::Vector
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/utilities.h>
+#include <deal.II/lac/vector.h>
+#include <fstream>
+
+void test()
+{
+  unsigned int myid = Utilities::MPI::this_mpi_process (MPI_COMM_WORLD);
+  const unsigned int numprocs = Utilities::MPI::n_mpi_processes (MPI_COMM_WORLD);
+
+  {
+    Vector<int> values(2);
+    values[0] = 1;
+    values[1] = 2;
+    Vector<int> sums(2);
+    Utilities::MPI::sum (values,
+                         MPI_COMM_WORLD,
+                         sums);
+    Assert (sums[0] == numprocs, ExcInternalError());
+    Assert (sums[1] == 2*numprocs, ExcInternalError());
+
+    if (myid==0)
+      deallog << sums[0] << ' ' << sums[1] << std::endl;
+  }
+  
+  {
+    Vector<double> values(2);
+    values[0] = 1.5;
+    values[1] = 2.5;
+    Vector<double> sums;
+    Utilities::MPI::sum (values,
+                         MPI_COMM_WORLD,
+                         sums);
+    Assert (sums[0] == 1.5 * numprocs, ExcInternalError());
+    Assert (sums[1] == 2.5 * numprocs, ExcInternalError());
+
+    if (myid==0)
+      deallog << sums[0] << ' ' << sums[1] << std::endl;
+  }
+
+}
+
+
+int main(int argc, char *argv[])
+{
+#ifdef DEAL_II_WITH_MPI
+  Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, numbers::invalid_unsigned_int);
+#else
+  (void)argc;
+  (void)argv;
+  compile_time_error;
+
+#endif
+
+  if (Utilities::MPI::this_mpi_process (MPI_COMM_WORLD) == 0)
+    {
+      std::ofstream logfile("output");
+      deallog.attach(logfile);
+      deallog.depth_console(0);
+      deallog.threshold_double(1.e-10);
+
+      deallog.push("mpi");
+      test();
+      deallog.pop();
+    }
+  else
+    test();
+}
diff --git a/tests/mpi/collective_02_dealii_vector.mpirun=1.output b/tests/mpi/collective_02_dealii_vector.mpirun=1.output
new file mode 100644 (file)
index 0000000..af15788
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL:mpi::1 2
+DEAL:mpi::1.50000 2.50000
diff --git a/tests/mpi/collective_02_dealii_vector.mpirun=4.output b/tests/mpi/collective_02_dealii_vector.mpirun=4.output
new file mode 100644 (file)
index 0000000..8819d0d
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL:mpi::4 8
+DEAL:mpi::6.00000 10.0000
diff --git a/tests/mpi/collective_02_dealii_vector_in_place.cc b/tests/mpi/collective_02_dealii_vector_in_place.cc
new file mode 100644 (file)
index 0000000..c868f6d
--- /dev/null
@@ -0,0 +1,86 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2009 - 2015 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+
+// check Utilities::MPI::sum() for dealii::Vector, but with input=output
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/utilities.h>
+#include <deal.II/lac/vector.h>
+#include <fstream>
+
+void test()
+{
+  unsigned int myid = Utilities::MPI::this_mpi_process (MPI_COMM_WORLD);
+  const unsigned int numprocs = Utilities::MPI::n_mpi_processes (MPI_COMM_WORLD);
+
+  {
+    Vector<int> sums(2);
+    sums[0] = 1;
+    sums[1] = 2;
+    Utilities::MPI::sum (sums,
+                         MPI_COMM_WORLD,
+                         sums);
+    Assert (sums[0] == numprocs, ExcInternalError());
+    Assert (sums[1] == 2*numprocs, ExcInternalError());
+
+    if (myid==0)
+      deallog << sums[0] << ' ' << sums[1] << std::endl;
+  }
+  
+  {
+    Vector<double> sums(2);
+    sums[0] = 1.5;
+    sums[1] = 2.5;
+    Utilities::MPI::sum (sums,
+                         MPI_COMM_WORLD,
+                         sums);
+    Assert (sums[0] == 1.5 * numprocs, ExcInternalError());
+    Assert (sums[1] == 2.5 * numprocs, ExcInternalError());
+
+    if (myid==0)
+      deallog << sums[0] << ' ' << sums[1] << std::endl;
+  }
+
+}
+
+
+int main(int argc, char *argv[])
+{
+#ifdef DEAL_II_WITH_MPI
+  Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, numbers::invalid_unsigned_int);
+#else
+  (void)argc;
+  (void)argv;
+  compile_time_error;
+
+#endif
+
+  if (Utilities::MPI::this_mpi_process (MPI_COMM_WORLD) == 0)
+    {
+      std::ofstream logfile("output");
+      deallog.attach(logfile);
+      deallog.depth_console(0);
+      deallog.threshold_double(1.e-10);
+
+      deallog.push("mpi");
+      test();
+      deallog.pop();
+    }
+  else
+    test();
+}
diff --git a/tests/mpi/collective_02_dealii_vector_in_place.mpirun=1.output b/tests/mpi/collective_02_dealii_vector_in_place.mpirun=1.output
new file mode 100644 (file)
index 0000000..af15788
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL:mpi::1 2
+DEAL:mpi::1.50000 2.50000
diff --git a/tests/mpi/collective_02_dealii_vector_in_place.mpirun=4.output b/tests/mpi/collective_02_dealii_vector_in_place.mpirun=4.output
new file mode 100644 (file)
index 0000000..8819d0d
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL:mpi::4 8
+DEAL:mpi::6.00000 10.0000

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.