From fc55cbe84601d6e8cec535df053f6bb002e9c340 Mon Sep 17 00:00:00 2001 From: Martin Kronbichler Date: Sun, 19 Feb 2017 14:35:33 +0100 Subject: [PATCH] More tests --- tests/matrix_free/matrix_vector_22.cc | 284 +++++++++++++++++ ...h_mpi=true.with_p4est=true.mpirun=3.output | 13 + tests/matrix_free/matrix_vector_23.cc | 297 ++++++++++++++++++ ...h_mpi=true.with_p4est=true.mpirun=3.output | 13 + 4 files changed, 607 insertions(+) create mode 100644 tests/matrix_free/matrix_vector_22.cc create mode 100644 tests/matrix_free/matrix_vector_22.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=3.output create mode 100644 tests/matrix_free/matrix_vector_23.cc create mode 100644 tests/matrix_free/matrix_vector_23.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=3.output diff --git a/tests/matrix_free/matrix_vector_22.cc b/tests/matrix_free/matrix_vector_22.cc new file mode 100644 index 0000000000..c78b19ffe1 --- /dev/null +++ b/tests/matrix_free/matrix_vector_22.cc @@ -0,0 +1,284 @@ +// --------------------------------------------------------------------- +// +// 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 tests the correctness of matrix free matrix-vector products for two +// vectors on the same DoFHandler. Similar to matrix_vector_12 but using +// the std::vector interface. + +#include "../tests.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +template +void +helmholtz_operator (const MatrixFree &data, + std::vector *> &dst, + const std::vector *> &src, + const std::pair &cell_range) +{ + FEEvaluation fe_eval (data); + const unsigned int n_q_points = fe_eval.n_q_points; + + for (unsigned int cell=cell_range.first; cell +class MatrixFreeTest +{ +public: + typedef VectorizedArray vector_t; + static const std::size_t n_vectors = VectorizedArray::n_array_elements; + + MatrixFreeTest(const MatrixFree &data_in): + data (data_in) + {}; + + void vmult (std::vector *> &dst, + const std::vector *> &src) const + { + for (unsigned int i=0; i &, + std::vector *> &, + const std::vector *> &, + const std::pair &)> + wrap = helmholtz_operator; + data.cell_loop (wrap, dst, src); + }; + +private: + const MatrixFree &data; +}; + + + + + +template +void test () +{ + 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, ZeroFunction(), + constraints); + constraints.close(); + + deallog << "Testing " << dof.get_fe().get_name() << std::endl; + //std::cout << "Number of cells: " << tria.n_global_active_cells() << std::endl; + //std::cout << "Number of degrees of freedom: " << dof.n_dofs() << std::endl; + //std::cout << "Number of constraints: " << constraints.n_constraints() << std::endl; + + MatrixFree mf_data; + { + const QGauss<1> quad (fe_degree+1); + typename MatrixFree::AdditionalData data; + data.tasks_parallel_scheme = + MatrixFree::AdditionalData::none; + data.tasks_block_size = 7; + mf_data.reinit (dof, constraints, quad, data); + } + + MatrixFreeTest mf (mf_data); + LinearAlgebra::distributed::Vector ref; + std::vector > in(2), out(2); + for (unsigned int i=0; i<2; ++i) + { + mf_data.initialize_dof_vector (in[i]); + mf_data.initialize_dof_vector (out[i]); + } + mf_data.initialize_dof_vector (ref); + + for (unsigned int i=0; i *> in_ptr(2), out_ptr(2); + in_ptr[0] = &in[0]; + in_ptr[1] = &in[1]; + out_ptr[0] = &out[0]; + out_ptr[1] = &out[1]; + mf.vmult (out_ptr, in_ptr); + + + // assemble trilinos sparse matrix with + // (\nabla v, \nabla u) + (v, 10 * u) for + // reference + TrilinosWrappers::SparseMatrix sparse_matrix; + { + TrilinosWrappers::SparsityPattern csp (owned_set, MPI_COMM_WORLD); + DoFTools::make_sparsity_pattern (dof, csp, constraints, true, + Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)); + csp.compress(); + sparse_matrix.reinit (csp); + } + { + QGauss quadrature_formula(fe_degree+1); + + FEValues fe_values (dof.get_fe(), quadrature_formula, + update_values | update_gradients | + update_JxW_values); + + const unsigned int dofs_per_cell = dof.get_fe().dofs_per_cell; + const unsigned int n_q_points = quadrature_formula.size(); + + FullMatrix cell_matrix (dofs_per_cell, dofs_per_cell); + std::vector local_dof_indices (dofs_per_cell); + + typename DoFHandler::active_cell_iterator + cell = dof.begin_active(), + endc = dof.end(); + for (; cell!=endc; ++cell) + if (cell->is_locally_owned()) + { + cell_matrix = 0; + fe_values.reinit (cell); + + for (unsigned int q_point=0; q_pointget_dof_indices(local_dof_indices); + constraints.distribute_local_to_global (cell_matrix, + local_dof_indices, + sparse_matrix); + } + } + sparse_matrix.compress(VectorOperation::add); + + deallog << "Norm of difference (component 1/2): "; + for (unsigned int i=0; i<2; ++i) + { + sparse_matrix.vmult (ref, in[i]); + out[i] -= ref; + const double diff_norm = out[i].linfty_norm(); + deallog << diff_norm << " "; + } + deallog << std::endl << std::endl; +} + + +int main (int argc, char **argv) +{ + Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, 1); + + mpi_initlog(); + deallog.threshold_double(1e-10); + + deallog.push("2d"); + test<2,1>(); + test<2,2>(); + deallog.pop(); + + deallog.push("3d"); + test<3,1>(); + test<3,2>(); + deallog.pop(); +} diff --git a/tests/matrix_free/matrix_vector_22.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=3.output b/tests/matrix_free/matrix_vector_22.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=3.output new file mode 100644 index 0000000000..6d04ca9292 --- /dev/null +++ b/tests/matrix_free/matrix_vector_22.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=3.output @@ -0,0 +1,13 @@ + +DEAL:2d::Testing FE_Q<2>(1) +DEAL:2d::Norm of difference (component 1/2): 0 0 +DEAL:2d:: +DEAL:2d::Testing FE_Q<2>(2) +DEAL:2d::Norm of difference (component 1/2): 0 0 +DEAL:2d:: +DEAL:3d::Testing FE_Q<3>(1) +DEAL:3d::Norm of difference (component 1/2): 0 0 +DEAL:3d:: +DEAL:3d::Testing FE_Q<3>(2) +DEAL:3d::Norm of difference (component 1/2): 0 0 +DEAL:3d:: diff --git a/tests/matrix_free/matrix_vector_23.cc b/tests/matrix_free/matrix_vector_23.cc new file mode 100644 index 0000000000..3e74ebf2af --- /dev/null +++ b/tests/matrix_free/matrix_vector_23.cc @@ -0,0 +1,297 @@ +// --------------------------------------------------------------------- +// +// 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 tests the correctness of matrix free matrix-vector products for three +// vectors on the same DoFHandler. Similar to matrix_vector_22 but using a +// combination of two different evaluation schemes, a scalar one and one with +// two components + +#include "../tests.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +template +void +helmholtz_operator (const MatrixFree &data, + std::vector *> &dst, + const std::vector *> &src, + const std::pair &cell_range) +{ + FEEvaluation fe_eval (data); + FEEvaluation fe_eval2 (data); + const unsigned int n_q_points = fe_eval.n_q_points; + + for (unsigned int cell=cell_range.first; cell +class MatrixFreeTest +{ +public: + typedef VectorizedArray vector_t; + static const std::size_t n_vectors = VectorizedArray::n_array_elements; + + MatrixFreeTest(const MatrixFree &data_in): + data (data_in) + {}; + + void vmult (std::vector *> &dst, + const std::vector *> &src) const + { + for (unsigned int i=0; i &, + std::vector *> &, + const std::vector *> &, + const std::pair &)> + wrap = helmholtz_operator; + data.cell_loop (wrap, dst, src); + }; + +private: + const MatrixFree &data; +}; + + + + + +template +void test () +{ + 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, ZeroFunction(), + constraints); + constraints.close(); + + deallog << "Testing " << dof.get_fe().get_name() << std::endl; + //std::cout << "Number of cells: " << tria.n_global_active_cells() << std::endl; + //std::cout << "Number of degrees of freedom: " << dof.n_dofs() << std::endl; + //std::cout << "Number of constraints: " << constraints.n_constraints() << std::endl; + + MatrixFree mf_data; + { + const QGauss<1> quad (fe_degree+1); + typename MatrixFree::AdditionalData data; + data.tasks_parallel_scheme = + MatrixFree::AdditionalData::none; + mf_data.reinit (dof, constraints, quad, data); + } + + MatrixFreeTest mf (mf_data); + LinearAlgebra::distributed::Vector ref; + std::vector > in(3), out(3); + for (unsigned int i=0; i<3; ++i) + { + mf_data.initialize_dof_vector (in[i]); + mf_data.initialize_dof_vector (out[i]); + } + mf_data.initialize_dof_vector (ref); + + for (unsigned int i=0; i *> in_ptr(3), out_ptr(3); + in_ptr[0] = &in[0]; + in_ptr[1] = &in[1]; + in_ptr[2] = &in[2]; + out_ptr[0] = &out[0]; + out_ptr[1] = &out[1]; + out_ptr[2] = &out[2]; + mf.vmult (out_ptr, in_ptr); + + + // assemble trilinos sparse matrix with + // (\nabla v, \nabla u) + (v, 10 * u) for + // reference + TrilinosWrappers::SparseMatrix sparse_matrix; + { + TrilinosWrappers::SparsityPattern csp (owned_set, MPI_COMM_WORLD); + DoFTools::make_sparsity_pattern (dof, csp, constraints, true, + Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)); + csp.compress(); + sparse_matrix.reinit (csp); + } + { + QGauss quadrature_formula(fe_degree+1); + + FEValues fe_values (dof.get_fe(), quadrature_formula, + update_values | update_gradients | + update_JxW_values); + + const unsigned int dofs_per_cell = dof.get_fe().dofs_per_cell; + const unsigned int n_q_points = quadrature_formula.size(); + + FullMatrix cell_matrix (dofs_per_cell, dofs_per_cell); + std::vector local_dof_indices (dofs_per_cell); + + typename DoFHandler::active_cell_iterator + cell = dof.begin_active(), + endc = dof.end(); + for (; cell!=endc; ++cell) + if (cell->is_locally_owned()) + { + cell_matrix = 0; + fe_values.reinit (cell); + + for (unsigned int q_point=0; q_pointget_dof_indices(local_dof_indices); + constraints.distribute_local_to_global (cell_matrix, + local_dof_indices, + sparse_matrix); + } + } + sparse_matrix.compress(VectorOperation::add); + + deallog << "Norm of difference (component 1/2/3): "; + for (unsigned int i=0; i<3; ++i) + { + sparse_matrix.vmult (ref, in[i]); + out[i] -= ref; + const double diff_norm = out[i].linfty_norm(); + deallog << diff_norm << " "; + } + deallog << std::endl << std::endl; +} + + +int main (int argc, char **argv) +{ + Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, 1); + + mpi_initlog(); + deallog.threshold_double(1e-10); + + deallog.push("2d"); + test<2,1>(); + test<2,2>(); + deallog.pop(); + + deallog.push("3d"); + test<3,1>(); + test<3,2>(); + deallog.pop(); +} diff --git a/tests/matrix_free/matrix_vector_23.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=3.output b/tests/matrix_free/matrix_vector_23.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=3.output new file mode 100644 index 0000000000..096f40a704 --- /dev/null +++ b/tests/matrix_free/matrix_vector_23.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=3.output @@ -0,0 +1,13 @@ + +DEAL:2d::Testing FE_Q<2>(1) +DEAL:2d::Norm of difference (component 1/2/3): 0 0 0 +DEAL:2d:: +DEAL:2d::Testing FE_Q<2>(2) +DEAL:2d::Norm of difference (component 1/2/3): 0 0 0 +DEAL:2d:: +DEAL:3d::Testing FE_Q<3>(1) +DEAL:3d::Norm of difference (component 1/2/3): 0 0 0 +DEAL:3d:: +DEAL:3d::Testing FE_Q<3>(2) +DEAL:3d::Norm of difference (component 1/2/3): 0 0 0 +DEAL:3d:: -- 2.39.5