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
{
/**
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.
*
}
}
+ 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;
+ }
+ }
+
+
}
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>
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// 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();
+}
--- /dev/null
+
+DEAL:mpi::1 2
+DEAL:mpi::1.50000 2.50000
--- /dev/null
+
+DEAL:mpi::4 8
+DEAL:mpi::6.00000 10.0000
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// 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();
+}
--- /dev/null
+
+DEAL:mpi::1 2
+DEAL:mpi::1.50000 2.50000
--- /dev/null
+
+DEAL:mpi::4 8
+DEAL:mpi::6.00000 10.0000