]> https://gitweb.dealii.org/ - dealii.git/commitdiff
add LA::d::BlockVector::multivector_inner_product()
authorDenis Davydov <davydden@gmail.com>
Sat, 30 Sep 2017 21:36:42 +0000 (23:36 +0200)
committerDenis Davydov <davydden@gmail.com>
Tue, 3 Oct 2017 05:58:07 +0000 (07:58 +0200)
doc/news/changes/minor/20170930DenisDavydov [new file with mode: 0644]
include/deal.II/lac/la_parallel_block_vector.h
include/deal.II/lac/la_parallel_block_vector.templates.h
source/lac/la_parallel_block_vector.cc
source/lac/la_parallel_block_vector.inst.in
tests/mpi/parallel_block_vector_05.cc [new file with mode: 0644]
tests/mpi/parallel_block_vector_05.with_p4est=true.mpirun=1.output [new file with mode: 0644]
tests/mpi/parallel_block_vector_05.with_p4est=true.mpirun=4.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20170930DenisDavydov b/doc/news/changes/minor/20170930DenisDavydov
new file mode 100644 (file)
index 0000000..125ce68
--- /dev/null
@@ -0,0 +1,4 @@
+New: add LinearAlgebra::distributed::BlockVector<Number>::multivector_inner_product()
+to perform inner product between each block of the two block vectors.
+<br>
+(Denis Davydov, 2017/09/30)
index 11b0101221a4120222419bd3d7c8976a54a73472..37006d8407338ffe72116667a3259451fa0ea26a 100644 (file)
@@ -447,6 +447,24 @@ namespace LinearAlgebra
        */
       virtual Number operator* (const VectorSpaceVector<Number> &V) const override;
 
+      /**
+       * Calculate the scalar product between each block of this vector and @p V
+       * and store the result in a full matrix @p matrix. This function
+       * computes the result by forming $A_{ij}=U_i \cdot V_j$ where $U_i$
+       * and $V_i$ indicate the $i$th block (not element!) of $U$ and the
+       * $j$th block of $V$, respectively. If @p symmetric is
+       * <code>true</code>, it is assumed that inner product results in a
+       * square symmetric matrix and almost half of the scalar products can be avoided.
+       *
+       * Obviously, this function can only be used if each block in this
+       * object and @p V are of the same size.
+       *
+       * @note Internally, a single global reduction will be called to
+       * accumulate scalar product between locally owned degrees of freedom.
+       */
+      template <typename FullMatrixType>
+      void multivector_inner_product(FullMatrixType &matrix, const BlockVector<Number> &V, const bool symmetric = false) const;
+
       /**
        * Add @p a to all components. Note that @p a is a scalar not a vector.
        */
index 1200f1fcbf8589b7ba10ebc01a8f52a88d32fd06..64c1afca1b44cedfac02d332a346ba0c51c8fab1 100644 (file)
@@ -779,6 +779,56 @@ namespace LinearAlgebra
               MemoryConsumption::memory_consumption (this->components));
     }
 
+
+
+    template <typename Number>
+    template <typename FullMatrixType>
+    void
+    BlockVector<Number>::multivector_inner_product(FullMatrixType &matrix,
+                                                   const BlockVector<Number> &V,
+                                                   const bool symmetric) const
+    {
+      const unsigned int m = this->n_blocks();
+      const unsigned int n = V.n_blocks();
+
+      // in case one vector is empty and the second one is not, the
+      // FullMatrix resized to (m,n) will have 0 both in m() and n()
+      // which is how TableBase<N,T>::reinit() works as of deal.ii@8.5.0.
+      // Since in this case there is nothing to do anyway -- return immediately.
+      if (n==0 || m==0)
+        return;
+
+      Assert (matrix.m() == m,
+              dealii::ExcDimensionMismatch(matrix.m(),m));
+      Assert (matrix.n() == n,
+              dealii::ExcDimensionMismatch(matrix.n(),n));
+
+      // reset the matrix
+      matrix = Number();
+
+      if (symmetric)
+        {
+          Assert (m == n,
+                  dealii::ExcDimensionMismatch(m,n));
+
+          for (unsigned int i = 0; i < m; i++)
+            for (unsigned int j = i; j < n; j++)
+              matrix(i,j) = this->block(i).inner_product_local(V.block(j));
+
+          for (unsigned int i = 0; i < m; i++)
+            for (unsigned int j = i+1; j < n; j++)
+              matrix(j,i) = matrix(i,j);
+        }
+      else
+        {
+          for (unsigned int i = 0; i < m; ++i)
+            for (unsigned int j = 0; j < n; ++j)
+              matrix(i,j) = this->block(i).inner_product_local(V.block(j));
+        }
+
+      Utilities::MPI::sum(matrix, this->block(0).get_mpi_communicator(), matrix);
+    }
+
   } // end of namespace distributed
 
 } // end of namespace parallel
index 56fc41983d80ea4e487cd4ce21aab4a795a545dd..9e8b8397e06c9f1eb676bb524ef5725e3defcc7e 100644 (file)
@@ -16,6 +16,9 @@
 #include <deal.II/lac/la_parallel_block_vector.h>
 #include <deal.II/lac/la_parallel_block_vector.templates.h>
 
+#include <deal.II/lac/full_matrix.h>
+#include <deal.II/lac/lapack_full_matrix.h>
+
 DEAL_II_NAMESPACE_OPEN
 
 
index 5fab92fb70509d5b98d6b931bcd4f63ae5b2f23c..41ecec5924ad9ede7f23e456bb4d39646db8b5e2 100644 (file)
@@ -22,6 +22,12 @@ for (SCALAR : REAL_SCALARS)
     namespace distributed
     \{
     template class BlockVector<SCALAR>;
+    template
+    void
+    BlockVector<SCALAR>::multivector_inner_product(FullMatrix<SCALAR> &, const BlockVector<SCALAR> &V, const bool) const;
+    template
+    void
+    BlockVector<SCALAR>::multivector_inner_product(LAPACKFullMatrix<SCALAR> &, const BlockVector<SCALAR> &V, const bool) const;
     \}
     \}
 }
diff --git a/tests/mpi/parallel_block_vector_05.cc b/tests/mpi/parallel_block_vector_05.cc
new file mode 100644 (file)
index 0000000..4078094
--- /dev/null
@@ -0,0 +1,200 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2017 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.
+//
+// ---------------------------------------------------------------------
+
+
+
+// this BlockVector<Number>::scalar_product().
+// Triangulation and Mass operator are the same as in matrix_free/mass_operator_01.cc
+
+#include "../tests.h"
+
+#include <deal.II/base/utilities.h>
+#include <deal.II/base/function.h>
+#include <deal.II/distributed/tria.h>
+#include <deal.II/grid/grid_generator.h>
+#include <deal.II/grid/tria_boundary_lib.h>
+#include <deal.II/dofs/dof_tools.h>
+#include <deal.II/dofs/dof_handler.h>
+#include <deal.II/lac/constraint_matrix.h>
+#include <deal.II/matrix_free/operators.h>
+#include <deal.II/fe/fe_q.h>
+#include <deal.II/numerics/vector_tools.h>
+#include <deal.II/lac/la_parallel_block_vector.h>
+
+#include <iostream>
+
+
+
+
+template <int dim, int fe_degree>
+void test (const unsigned int n_blocks = 5)
+{
+  typedef double number;
+
+  parallel::distributed::Triangulation<dim> tria (MPI_COMM_WORLD);
+  GridGenerator::hyper_cube (tria);
+  tria.refine_global(1);
+  typename Triangulation<dim>::active_cell_iterator
+  cell = tria.begin_active (),
+  endc = tria.end();
+  cell = tria.begin_active ();
+  for (; cell!=endc; ++cell)
+    if (cell->is_locally_owned())
+      if (cell->center().norm()<0.2)
+        cell->set_refine_flag();
+  tria.execute_coarsening_and_refinement();
+  if (dim < 3 && fe_degree < 2)
+    tria.refine_global(2);
+  else
+    tria.refine_global(1);
+  if (tria.begin(tria.n_levels()-1)->is_locally_owned())
+    tria.begin(tria.n_levels()-1)->set_refine_flag();
+  if (tria.last()->is_locally_owned())
+    tria.last()->set_refine_flag();
+  tria.execute_coarsening_and_refinement();
+  cell = tria.begin_active ();
+  for (unsigned int i=0; i<10-3*dim; ++i)
+    {
+      cell = tria.begin_active ();
+      unsigned int counter = 0;
+      for (; cell!=endc; ++cell, ++counter)
+        if (cell->is_locally_owned())
+          if (counter % (7-i) == 0)
+            cell->set_refine_flag();
+      tria.execute_coarsening_and_refinement();
+    }
+
+  FE_Q<dim> fe (fe_degree);
+  DoFHandler<dim> dof (tria);
+  dof.distribute_dofs(fe);
+
+  IndexSet owned_set = dof.locally_owned_dofs();
+  IndexSet relevant_set;
+  DoFTools::extract_locally_relevant_dofs (dof, relevant_set);
+
+  ConstraintMatrix constraints (relevant_set);
+  DoFTools::make_hanging_node_constraints(dof, constraints);
+  VectorTools::interpolate_boundary_values (dof, 0, Functions::ZeroFunction<dim>(),
+                                            constraints);
+  constraints.close();
+
+  std::shared_ptr<MatrixFree<dim,number> > mf_data(new MatrixFree<dim,number> ());
+  {
+    const QGauss<1> quad (fe_degree+2);
+    typename MatrixFree<dim,number>::AdditionalData data;
+    data.tasks_parallel_scheme =
+      MatrixFree<dim,number>::AdditionalData::none;
+    data.tasks_block_size = 7;
+    mf_data->reinit (dof, constraints, quad, data);
+  }
+
+  MatrixFreeOperators::MassOperator<dim,fe_degree, fe_degree+2, 1, LinearAlgebra::distributed::Vector<number> > mf;
+  mf.initialize(mf_data);
+  mf.compute_diagonal();
+
+  LinearAlgebra::distributed::BlockVector<number> left(n_blocks), right(n_blocks);
+  for (unsigned int b = 0; b < n_blocks; ++b)
+    {
+      mf_data->initialize_dof_vector (left.block(b));
+      mf_data->initialize_dof_vector (right.block(b));
+
+      for (unsigned int i=0; i<right.block(b).local_size(); ++i)
+        {
+          const unsigned int glob_index =
+            owned_set.nth_index_in_set (i);
+          if (constraints.is_constrained(glob_index))
+            continue;
+          right.block(b).local_element(i) = (double)Testing::rand()/RAND_MAX;
+        }
+
+      mf.vmult (left.block(b), right.block(b));
+    }
+
+  {
+    FullMatrix<number> product(n_blocks,n_blocks), reference(n_blocks,n_blocks);
+
+    deallog << "Symmetric:" << std::endl;
+
+    left.multivector_inner_product(product, right, true);
+
+    for (unsigned int i = 0; i < n_blocks; ++i)
+      for (unsigned int j = 0; j < n_blocks; ++j)
+        reference(i,j) = left.block(i) * right.block(j);
+
+    product.add(-1.,reference);
+
+    const double diff_norm = product.frobenius_norm();
+
+    deallog << "Norm of difference: " << diff_norm << std::endl;
+  }
+
+  // Non-symmetric case
+  {
+    const unsigned int n_blocks_2 = n_blocks+3;
+    LinearAlgebra::distributed::BlockVector<number> left2(n_blocks_2);
+    for (unsigned int b = 0; b < left2.n_blocks(); ++b)
+      {
+        mf_data->initialize_dof_vector (left2.block(b));
+        for (unsigned int i=0; i<left2.block(b).local_size(); ++i)
+          {
+            const unsigned int glob_index =
+              owned_set.nth_index_in_set (i);
+            if (constraints.is_constrained(glob_index))
+              continue;
+            left2.block(b).local_element(i) = (double)Testing::rand()/RAND_MAX;
+          }
+      }
+
+    FullMatrix<number> product2(n_blocks_2,n_blocks), reference2(n_blocks_2,n_blocks);
+
+    deallog << "Non-Symmetric:" << std::endl;
+
+    left2.multivector_inner_product(product2, right, false);
+
+    for (unsigned int i = 0; i < n_blocks_2; ++i)
+      for (unsigned int j = 0; j < n_blocks; ++j)
+        reference2(i,j) = left2.block(i) * right.block(j);
+
+    product2.add(-1.,reference2);
+
+    const double diff_norm2 = product2.frobenius_norm();
+
+    deallog << "Norm of difference: " << diff_norm2 << std::endl;
+  }
+
+
+}
+
+
+int main (int argc, char **argv)
+{
+  Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, testing_max_num_threads());
+
+  unsigned int myid = Utilities::MPI::this_mpi_process (MPI_COMM_WORLD);
+  deallog.push(Utilities::int_to_string(myid));
+
+  if (myid == 0)
+    {
+      initlog();
+      deallog << std::setprecision(4);
+
+      test<2,1>();
+    }
+  else
+    {
+      test<2,1>();
+    }
+}
+
diff --git a/tests/mpi/parallel_block_vector_05.with_p4est=true.mpirun=1.output b/tests/mpi/parallel_block_vector_05.with_p4est=true.mpirun=1.output
new file mode 100644 (file)
index 0000000..6063f6f
--- /dev/null
@@ -0,0 +1,5 @@
+
+DEAL:0::Symmetric:
+DEAL:0::Norm of difference: 0.000
+DEAL:0::Non-Symmetric:
+DEAL:0::Norm of difference: 0.000
diff --git a/tests/mpi/parallel_block_vector_05.with_p4est=true.mpirun=4.output b/tests/mpi/parallel_block_vector_05.with_p4est=true.mpirun=4.output
new file mode 100644 (file)
index 0000000..6063f6f
--- /dev/null
@@ -0,0 +1,5 @@
+
+DEAL:0::Symmetric:
+DEAL:0::Norm of difference: 0.000
+DEAL:0::Non-Symmetric:
+DEAL:0::Norm of difference: 0.000

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.