]> https://gitweb.dealii.org/ - dealii.git/commitdiff
MatrixFreeOperators::LaplaceOperator vectorization width 1 14386/head
authorSebastian Proell <sebastian.proell@tum.de>
Wed, 2 Nov 2022 10:55:36 +0000 (11:55 +0100)
committerSebastian Proell <sebastian.proell@tum.de>
Wed, 2 Nov 2022 10:55:36 +0000 (11:55 +0100)
include/deal.II/matrix_free/operators.h
tests/matrix_free/laplace_operator_05.cc [new file with mode: 0644]
tests/matrix_free/laplace_operator_05.with_trilinos=true.with_p4est=true.mpirun=4.output [new file with mode: 0644]

index 1eec2fda2b931b39d7db5c2532a0b0699b18f91d..cf0d1ecd95155d8a08bd47fb8d518014199c35a9 100644 (file)
@@ -983,8 +983,9 @@ namespace MatrixFreeOperators
                                       fe_degree,
                                       n_q_points_1d,
                                       n_components_compute,
-                                      value_type> &phi,
-                         const unsigned int        cell) const;
+                                      value_type,
+                                      VectorizedArrayType> &phi,
+                         const unsigned int                 cell) const;
 
     /**
      * User-provided heterogeneity coefficient.
@@ -2273,8 +2274,9 @@ namespace MatrixFreeOperators
         fe_degree,
         n_q_points_1d,
         n_components_compute,
-        typename Base<dim, VectorType, VectorizedArrayType>::value_type> &phi,
-      const unsigned int cell) const
+        typename Base<dim, VectorType, VectorizedArrayType>::value_type,
+        VectorizedArrayType> &phi,
+      const unsigned int      cell) const
   {
     phi.evaluate(EvaluationFlags::gradients);
     if (scalar_coefficient.get())
@@ -2342,8 +2344,13 @@ namespace MatrixFreeOperators
   {
     using Number =
       typename Base<dim, VectorType, VectorizedArrayType>::value_type;
-    FEEvaluation<dim, fe_degree, n_q_points_1d, n_components, Number> phi(
-      data, this->selected_rows[0]);
+    FEEvaluation<dim,
+                 fe_degree,
+                 n_q_points_1d,
+                 n_components,
+                 Number,
+                 VectorizedArrayType>
+      phi(data, this->selected_rows[0]);
     for (unsigned int cell = cell_range.first; cell < cell_range.second; ++cell)
       {
         phi.reinit(cell);
@@ -2379,9 +2386,14 @@ namespace MatrixFreeOperators
     using Number =
       typename Base<dim, VectorType, VectorizedArrayType>::value_type;
 
-    FEEvaluation<dim, fe_degree, n_q_points_1d, 1, Number> eval(
-      data, this->selected_rows[0]);
-    FEEvaluation<dim, fe_degree, n_q_points_1d, n_components, Number>
+    FEEvaluation<dim, fe_degree, n_q_points_1d, 1, Number, VectorizedArrayType>
+      eval(data, this->selected_rows[0]);
+    FEEvaluation<dim,
+                 fe_degree,
+                 n_q_points_1d,
+                 n_components,
+                 Number,
+                 VectorizedArrayType>
       eval_vector(data, this->selected_rows[0]);
     for (unsigned int cell = cell_range.first; cell < cell_range.second; ++cell)
       {
diff --git a/tests/matrix_free/laplace_operator_05.cc b/tests/matrix_free/laplace_operator_05.cc
new file mode 100644 (file)
index 0000000..401134a
--- /dev/null
@@ -0,0 +1,249 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2017 - 2022 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.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+
+
+// This is the same as laplace_operator_01.cc, but tests vectorization length 1
+
+#include <deal.II/base/function.h>
+#include <deal.II/base/utilities.h>
+
+#include <deal.II/distributed/tria.h>
+
+#include <deal.II/dofs/dof_handler.h>
+#include <deal.II/dofs/dof_tools.h>
+
+#include <deal.II/fe/fe_q.h>
+#include <deal.II/fe/fe_values.h>
+
+#include <deal.II/grid/grid_generator.h>
+#include <deal.II/grid/manifold_lib.h>
+
+#include <deal.II/lac/affine_constraints.h>
+#include <deal.II/lac/linear_operator.h>
+#include <deal.II/lac/trilinos_sparse_matrix.h>
+#include <deal.II/lac/trilinos_sparsity_pattern.h>
+
+#include <deal.II/matrix_free/operators.h>
+
+#include <deal.II/numerics/vector_tools.h>
+
+#include <iostream>
+
+#include "../tests.h"
+
+using VectorizedArrayType = VectorizedArray<double, 1>;
+
+
+template <int dim, int fe_degree>
+void
+test()
+{
+  using number = double;
+
+  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);
+
+  AffineConstraints<double> constraints(relevant_set);
+  DoFTools::make_hanging_node_constraints(dof, constraints);
+  VectorTools::interpolate_boundary_values(dof,
+                                           0,
+                                           Functions::ZeroFunction<dim>(),
+                                           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;
+
+  auto mf_data =
+    std::make_shared<MatrixFree<dim, number, VectorizedArrayType>>();
+  {
+    const QGauss<1> quad(fe_degree + 1);
+    typename MatrixFree<dim, number, VectorizedArrayType>::AdditionalData data;
+    data.tasks_parallel_scheme =
+      MatrixFree<dim, number, VectorizedArrayType>::AdditionalData::none;
+    data.tasks_block_size = 7;
+    mf_data->reinit(MappingQ1<dim>{}, dof, constraints, quad, data);
+  }
+
+  MatrixFreeOperators::LaplaceOperator<
+    dim,
+    fe_degree,
+    fe_degree + 1,
+    1,
+    LinearAlgebra::distributed::Vector<number>,
+    VectorizedArrayType>
+    mf;
+  mf.initialize(mf_data);
+  mf.compute_diagonal();
+  LinearAlgebra::distributed::Vector<number> in, out, ref;
+  mf_data->initialize_dof_vector(in);
+  out.reinit(in);
+  ref.reinit(in);
+
+  for (unsigned int i = 0; i < in.local_size(); ++i)
+    {
+      const unsigned int glob_index = owned_set.nth_index_in_set(i);
+      if (constraints.is_constrained(glob_index))
+        continue;
+      in.local_element(i) = random_value<double>();
+    }
+
+  mf.vmult(out, in);
+
+  // Check that things work with LinearOperator too. This verifies that the
+  // detection mechanism for initialize_dof_vector() works.
+  {
+    LinearAlgebra::distributed::Vector<number> out2;
+    mf.initialize_dof_vector(out2);
+
+    const auto op_mf = linear_operator<decltype(out2)>(mf);
+    op_mf.vmult(out2, in);
+
+    out2 -= out;
+    AssertThrow(out2.l2_norm() == 0.0, ExcInternalError());
+  }
+
+  // assemble trilinos sparse matrix with
+  // (v, 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<dim> quadrature_formula(fe_degree + 1);
+
+    FEValues<dim> fe_values(dof.get_fe(),
+                            quadrature_formula,
+                            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<double> cell_matrix(dofs_per_cell, dofs_per_cell);
+    std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
+
+    typename DoFHandler<dim>::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_point < n_q_points; ++q_point)
+            for (unsigned int i = 0; i < dofs_per_cell; ++i)
+              {
+                for (unsigned int j = 0; j < dofs_per_cell; ++j)
+                  cell_matrix(i, j) += (fe_values.shape_grad(i, q_point) *
+                                        fe_values.shape_grad(j, q_point)) *
+                                       fe_values.JxW(q_point);
+              }
+
+          cell->get_dof_indices(local_dof_indices);
+          constraints.distribute_local_to_global(cell_matrix,
+                                                 local_dof_indices,
+                                                 sparse_matrix);
+        }
+  }
+  sparse_matrix.compress(VectorOperation::add);
+
+  sparse_matrix.vmult(ref, in);
+  out -= ref;
+
+  // For completeness, try again with LinearOperator for the same detection
+  // reasons as above:
+  {
+    LinearAlgebra::distributed::Vector<number> out2;
+    mf.initialize_dof_vector(out2);
+
+    const auto op_mf = linear_operator<decltype(out2)>(sparse_matrix);
+    op_mf.vmult(out2, in);
+
+    out2 -= ref;
+    AssertThrow(out2.l2_norm() == 0.0, ExcInternalError());
+  }
+
+  const double diff_norm = out.linfty_norm();
+  deallog << "Norm of difference: " << diff_norm << std::endl << std::endl;
+}
+
+
+int
+main(int argc, char **argv)
+{
+  Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1);
+
+  mpi_initlog();
+  deallog.push("0");
+
+  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/laplace_operator_05.with_trilinos=true.with_p4est=true.mpirun=4.output b/tests/matrix_free/laplace_operator_05.with_trilinos=true.with_p4est=true.mpirun=4.output
new file mode 100644 (file)
index 0000000..25e13d5
--- /dev/null
@@ -0,0 +1,13 @@
+
+DEAL:0:2d::Testing FE_Q<2>(1)
+DEAL:0:2d::Norm of difference: 0
+DEAL:0:2d::
+DEAL:0:2d::Testing FE_Q<2>(2)
+DEAL:0:2d::Norm of difference: 0
+DEAL:0:2d::
+DEAL:0:3d::Testing FE_Q<3>(1)
+DEAL:0:3d::Norm of difference: 0
+DEAL:0:3d::
+DEAL:0:3d::Testing FE_Q<3>(2)
+DEAL:0:3d::Norm of difference: 0
+DEAL:0:3d::

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.