From fc355b1712109545f1fd24416c404fae37d82593 Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Mon, 2 Oct 2017 20:00:26 +0200 Subject: [PATCH] make sure multivector_inner_product() updates Property of LAPACKFullMatrix accordingly --- .../lac/la_parallel_block_vector.templates.h | 26 +++ tests/mpi/parallel_block_vector_06.cc | 204 ++++++++++++++++++ ...4est=true.with_lapack=true.mpirun=3.output | 6 + 3 files changed, 236 insertions(+) create mode 100644 tests/mpi/parallel_block_vector_06.cc create mode 100644 tests/mpi/parallel_block_vector_06.with_p4est=true.with_lapack=true.mpirun=3.output diff --git a/include/deal.II/lac/la_parallel_block_vector.templates.h b/include/deal.II/lac/la_parallel_block_vector.templates.h index 64c1afca1b..8902adcd7a 100644 --- a/include/deal.II/lac/la_parallel_block_vector.templates.h +++ b/include/deal.II/lac/la_parallel_block_vector.templates.h @@ -21,10 +21,14 @@ #include #include #include +#include DEAL_II_NAMESPACE_OPEN +//Forward type declaration to have special treatment of LAPACKFullMatrix +// in multivector_inner_product() +template class LAPACKFullMatrix; namespace LinearAlgebra { @@ -781,6 +785,27 @@ namespace LinearAlgebra + namespace + { + template + inline + void set_symmetric(FullMatrixType &, const bool) + { + } + + template + inline + void set_symmetric(LAPACKFullMatrix &matrix, const bool symmetric) + { + if (symmetric) + matrix.set_property (LAPACKSupport::symmetric); + else + matrix.set_property (LAPACKSupport::general); + } + } + + + template template void @@ -806,6 +831,7 @@ namespace LinearAlgebra // reset the matrix matrix = Number(); + set_symmetric(matrix, symmetric); if (symmetric) { Assert (m == n, diff --git a/tests/mpi/parallel_block_vector_06.cc b/tests/mpi/parallel_block_vector_06.cc new file mode 100644 index 0000000000..b8d77da3e7 --- /dev/null +++ b/tests/mpi/parallel_block_vector_06.cc @@ -0,0 +1,204 @@ +// --------------------------------------------------------------------- +// +// 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::scalar_product(). +// same as parallel_block_vector_05, but uses LAPACKFUllMatrix and tests +// that internally we set it to be symmetric and so, for example, one can +// immediately run Cholesky factorization + +#include "../tests.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + + + +template +void test (const unsigned int n_blocks = 5) +{ + typedef double number; + + parallel::distributed::Triangulation tria (MPI_COMM_WORLD); + GridGenerator::hyper_cube (tria); + tria.refine_global(1); + typename Triangulation::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 fe (fe_degree); + DoFHandler 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(), + constraints); + constraints.close(); + + std::shared_ptr > mf_data(new MatrixFree ()); + { + const QGauss<1> quad (fe_degree+2); + typename MatrixFree::AdditionalData data; + data.tasks_parallel_scheme = + MatrixFree::AdditionalData::none; + data.tasks_block_size = 7; + mf_data->reinit (dof, constraints, quad, data); + } + + MatrixFreeOperators::MassOperator > mf; + mf.initialize(mf_data); + mf.compute_diagonal(); + + LinearAlgebra::distributed::BlockVector 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 product(n_blocks,n_blocks), diff(n_blocks,n_blocks); + + deallog << "Symmetric:" << std::endl; + + // inside LAPACK matrix should have its Property set as symmetric + // and so we can call compute_cholesky_factorization() below. + 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) + diff(i,j) = left.block(i) * right.block(j) - product(i,j); + + const double diff_norm = diff.frobenius_norm(); + + deallog << "Norm of difference: " << diff_norm << std::endl; + deallog << "Cholesky: " << std::flush; + product.compute_cholesky_factorization(); + deallog << "Ok" << std::endl; + } + + // Non-symmetric case + { + const unsigned int n_blocks_2 = n_blocks+3; + LinearAlgebra::distributed::BlockVector 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 product2(n_blocks_2,n_blocks), diff2(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) + diff2(i,j) = left2.block(i) * right.block(j) - product2(i,j); + + const double diff_norm2 = diff2.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_06.with_p4est=true.with_lapack=true.mpirun=3.output b/tests/mpi/parallel_block_vector_06.with_p4est=true.with_lapack=true.mpirun=3.output new file mode 100644 index 0000000000..3a27e7c754 --- /dev/null +++ b/tests/mpi/parallel_block_vector_06.with_p4est=true.with_lapack=true.mpirun=3.output @@ -0,0 +1,6 @@ + +DEAL:0::Symmetric: +DEAL:0::Norm of difference: 0.000 +DEAL:0::Cholesky: Ok +DEAL:0::Non-Symmetric: +DEAL:0::Norm of difference: 0.000 -- 2.39.5