From 01fbb861229284937d9a783eeb6b5bf421d4a1ab Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Wed, 20 Feb 2019 16:49:54 +0100 Subject: [PATCH] Add a few more TpetraWrappers and EpetraWrapper tests with a matrix-vector product --- include/deal.II/lac/trilinos_sparse_matrix.h | 1 + tests/trilinos/sparse_matrix_vector_12.cc | 126 ++++++++++++++++++ tests/trilinos/sparse_matrix_vector_12.output | 2 + tests/trilinos/sparse_matrix_vector_13.cc | 125 +++++++++++++++++ tests/trilinos/sparse_matrix_vector_13.output | 2 + tests/trilinos/sparse_matrix_vector_14.cc | 126 ++++++++++++++++++ tests/trilinos/sparse_matrix_vector_14.output | 2 + tests/trilinos/sparse_matrix_vector_15.cc | 125 +++++++++++++++++ tests/trilinos/sparse_matrix_vector_15.output | 2 + 9 files changed, 511 insertions(+) create mode 100644 tests/trilinos/sparse_matrix_vector_12.cc create mode 100644 tests/trilinos/sparse_matrix_vector_12.output create mode 100644 tests/trilinos/sparse_matrix_vector_13.cc create mode 100644 tests/trilinos/sparse_matrix_vector_13.output create mode 100644 tests/trilinos/sparse_matrix_vector_14.cc create mode 100644 tests/trilinos/sparse_matrix_vector_14.output create mode 100644 tests/trilinos/sparse_matrix_vector_15.cc create mode 100644 tests/trilinos/sparse_matrix_vector_15.output diff --git a/include/deal.II/lac/trilinos_sparse_matrix.h b/include/deal.II/lac/trilinos_sparse_matrix.h index 183875e6be..b07e06c6f3 100644 --- a/include/deal.II/lac/trilinos_sparse_matrix.h +++ b/include/deal.II/lac/trilinos_sparse_matrix.h @@ -1566,6 +1566,7 @@ namespace TrilinosWrappers * diff --git a/tests/trilinos/sparse_matrix_vector_12.cc b/tests/trilinos/sparse_matrix_vector_12.cc new file mode 100644 index 0000000000..398cf9150a --- /dev/null +++ b/tests/trilinos/sparse_matrix_vector_12.cc @@ -0,0 +1,126 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2013 - 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.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// check SparseMatrix::vmult, vmult_add with +// LinearAlgebra::EpetraWrappers::Vector + +#include + +#include +#include +#include + +#include +#include + +#include "../tests.h" + + +void +test(LinearAlgebra::EpetraWrappers::Vector &v, + LinearAlgebra::EpetraWrappers::Vector &w) +{ + LinearAlgebra::ReadWriteVector read_write_v(v.size()); + LinearAlgebra::ReadWriteVector read_write_w(w.size()); + + TrilinosWrappers::SparseMatrix m(w.size(), v.size(), v.size()); + for (unsigned int i = 0; i < m.m(); ++i) + for (unsigned int j = 0; j < m.n(); ++j) + m.set(i, j, i + 2 * j); + + for (unsigned int i = 0; i < v.size(); ++i) + read_write_v(i) = i; + + m.compress(VectorOperation::insert); + v.import(read_write_v, VectorOperation::insert); + + // w:=Mv + m.vmult(w, v); + + // make sure we get the expected result + read_write_w.import(w, VectorOperation::insert); + for (unsigned int i = 0; i < m.m(); ++i) + { + double result = 0; + for (unsigned int j = 0; j < m.n(); ++j) + result += (i + 2 * j) * j; + AssertThrow(read_write_w(i) == result, ExcInternalError()); + } + + m.vmult_add(w, v); + + // make sure we get the expected result + read_write_w.import(w, VectorOperation::insert); + for (unsigned int i = 0; i < m.m(); ++i) + { + double result = 0; + for (unsigned int j = 0; j < m.n(); ++j) + result += (i + 2 * j) * j; + AssertThrow(read_write_w(i) == result + result, ExcInternalError()); + } + + deallog << "OK" << std::endl; +} + + + +int +main(int argc, char **argv) +{ + initlog(); + + Utilities::MPI::MPI_InitFinalize mpi_initialization( + argc, argv, testing_max_num_threads()); + + + try + { + { + LinearAlgebra::EpetraWrappers::Vector v(complete_index_set(100), + MPI_COMM_SELF); + LinearAlgebra::EpetraWrappers::Vector w(complete_index_set(95), + MPI_COMM_SELF); + test(v, w); + } + } + catch (std::exception &exc) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Exception on processing: " << std::endl + << exc.what() << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + + return 1; + } + catch (...) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Unknown exception!" << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + return 1; + }; +} diff --git a/tests/trilinos/sparse_matrix_vector_12.output b/tests/trilinos/sparse_matrix_vector_12.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/trilinos/sparse_matrix_vector_12.output @@ -0,0 +1,2 @@ + +DEAL::OK diff --git a/tests/trilinos/sparse_matrix_vector_13.cc b/tests/trilinos/sparse_matrix_vector_13.cc new file mode 100644 index 0000000000..e13c99656c --- /dev/null +++ b/tests/trilinos/sparse_matrix_vector_13.cc @@ -0,0 +1,125 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2013 - 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.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// check SparseMatrix::Tvmult, Tvmult_add with +// LinearAlgebra::EpetraWrappers::Vector + +#include + +#include +#include +#include + +#include +#include + +#include "../tests.h" + + +void +test(LinearAlgebra::EpetraWrappers::Vector &v, + LinearAlgebra::EpetraWrappers::Vector &w) +{ + LinearAlgebra::ReadWriteVector read_write_v(v.size()); + LinearAlgebra::ReadWriteVector read_write_w(w.size()); + + TrilinosWrappers::SparseMatrix m(v.size(), w.size(), w.size()); + for (unsigned int i = 0; i < m.m(); ++i) + for (unsigned int j = 0; j < m.n(); ++j) + m.set(i, j, i + 2 * j); + + for (unsigned int i = 0; i < v.size(); ++i) + read_write_v(i) = i; + + m.compress(VectorOperation::insert); + v.import(read_write_v, VectorOperation::insert); + + // w:=Mv + m.Tvmult(w, v); + + // make sure we get the expected result + read_write_w.import(w, VectorOperation::insert); + for (unsigned int i = 0; i < m.n(); ++i) + { + double result = 0; + for (unsigned int j = 0; j < m.m(); ++j) + result += (j + 2 * i) * j; + AssertThrow(read_write_w(i) == result, ExcInternalError()); + } + + m.Tvmult_add(w, v); + // make sure we get the expected result + read_write_w.import(w, VectorOperation::insert); + for (unsigned int i = 0; i < m.n(); ++i) + { + double result = 0; + for (unsigned int j = 0; j < m.m(); ++j) + result += (j + 2 * i) * j; + AssertThrow(read_write_w(i) == result + result, ExcInternalError()); + } + + deallog << "OK" << std::endl; +} + + + +int +main(int argc, char **argv) +{ + initlog(); + + Utilities::MPI::MPI_InitFinalize mpi_initialization( + argc, argv, testing_max_num_threads()); + + + try + { + { + LinearAlgebra::EpetraWrappers::Vector v(complete_index_set(95), + MPI_COMM_SELF); + LinearAlgebra::EpetraWrappers::Vector w(complete_index_set(100), + MPI_COMM_SELF); + test(v, w); + } + } + catch (std::exception &exc) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Exception on processing: " << std::endl + << exc.what() << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + + return 1; + } + catch (...) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Unknown exception!" << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + return 1; + }; +} diff --git a/tests/trilinos/sparse_matrix_vector_13.output b/tests/trilinos/sparse_matrix_vector_13.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/trilinos/sparse_matrix_vector_13.output @@ -0,0 +1,2 @@ + +DEAL::OK diff --git a/tests/trilinos/sparse_matrix_vector_14.cc b/tests/trilinos/sparse_matrix_vector_14.cc new file mode 100644 index 0000000000..fed0ac1c85 --- /dev/null +++ b/tests/trilinos/sparse_matrix_vector_14.cc @@ -0,0 +1,126 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2013 - 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.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// check SparseMatrix::vmult, vmult_add with +// LinearAlgebra::TpetraWrappers::Vector + +#include + +#include +#include +#include + +#include +#include + +#include "../tests.h" + + +void +test(LinearAlgebra::TpetraWrappers::Vector &v, + LinearAlgebra::TpetraWrappers::Vector &w) +{ + LinearAlgebra::ReadWriteVector read_write_v(v.size()); + LinearAlgebra::ReadWriteVector read_write_w(w.size()); + + TrilinosWrappers::SparseMatrix m(w.size(), v.size(), v.size()); + for (unsigned int i = 0; i < m.m(); ++i) + for (unsigned int j = 0; j < m.n(); ++j) + m.set(i, j, i + 2 * j); + + for (unsigned int i = 0; i < v.size(); ++i) + read_write_v(i) = i; + + m.compress(VectorOperation::insert); + v.import(read_write_v, VectorOperation::insert); + + // w:=Mv + m.vmult(w, v); + + // make sure we get the expected result + read_write_w.import(w, VectorOperation::insert); + for (unsigned int i = 0; i < m.m(); ++i) + { + double result = 0; + for (unsigned int j = 0; j < m.n(); ++j) + result += (i + 2 * j) * j; + AssertThrow(read_write_w(i) == result, ExcInternalError()); + } + + m.vmult_add(w, v); + + // make sure we get the expected result + read_write_w.import(w, VectorOperation::insert); + for (unsigned int i = 0; i < m.m(); ++i) + { + double result = 0; + for (unsigned int j = 0; j < m.n(); ++j) + result += (i + 2 * j) * j; + AssertThrow(read_write_w(i) == result + result, ExcInternalError()); + } + + deallog << "OK" << std::endl; +} + + + +int +main(int argc, char **argv) +{ + initlog(); + + Utilities::MPI::MPI_InitFinalize mpi_initialization( + argc, argv, testing_max_num_threads()); + + + try + { + { + LinearAlgebra::TpetraWrappers::Vector v(complete_index_set(100), + MPI_COMM_SELF); + LinearAlgebra::TpetraWrappers::Vector w(complete_index_set(95), + MPI_COMM_SELF); + test(v, w); + } + } + catch (std::exception &exc) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Exception on processing: " << std::endl + << exc.what() << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + + return 1; + } + catch (...) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Unknown exception!" << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + return 1; + }; +} diff --git a/tests/trilinos/sparse_matrix_vector_14.output b/tests/trilinos/sparse_matrix_vector_14.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/trilinos/sparse_matrix_vector_14.output @@ -0,0 +1,2 @@ + +DEAL::OK diff --git a/tests/trilinos/sparse_matrix_vector_15.cc b/tests/trilinos/sparse_matrix_vector_15.cc new file mode 100644 index 0000000000..870712bbe8 --- /dev/null +++ b/tests/trilinos/sparse_matrix_vector_15.cc @@ -0,0 +1,125 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2013 - 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.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// check SparseMatrix::Tvmult, Tvmult_add with +// LinearAlgebra::TpetraWrappers::Vector + +#include + +#include +#include +#include + +#include +#include + +#include "../tests.h" + + +void +test(LinearAlgebra::TpetraWrappers::Vector &v, + LinearAlgebra::TpetraWrappers::Vector &w) +{ + LinearAlgebra::ReadWriteVector read_write_v(v.size()); + LinearAlgebra::ReadWriteVector read_write_w(w.size()); + + TrilinosWrappers::SparseMatrix m(v.size(), w.size(), w.size()); + for (unsigned int i = 0; i < m.m(); ++i) + for (unsigned int j = 0; j < m.n(); ++j) + m.set(i, j, i + 2 * j); + + for (unsigned int i = 0; i < v.size(); ++i) + read_write_v(i) = i; + + m.compress(VectorOperation::insert); + v.import(read_write_v, VectorOperation::insert); + + // w:=Mv + m.Tvmult(w, v); + + // make sure we get the expected result + read_write_w.import(w, VectorOperation::insert); + for (unsigned int i = 0; i < m.n(); ++i) + { + double result = 0; + for (unsigned int j = 0; j < m.m(); ++j) + result += (j + 2 * i) * j; + AssertThrow(read_write_w(i) == result, ExcInternalError()); + } + + m.Tvmult_add(w, v); + // make sure we get the expected result + read_write_w.import(w, VectorOperation::insert); + for (unsigned int i = 0; i < m.n(); ++i) + { + double result = 0; + for (unsigned int j = 0; j < m.m(); ++j) + result += (j + 2 * i) * j; + AssertThrow(read_write_w(i) == result + result, ExcInternalError()); + } + + deallog << "OK" << std::endl; +} + + + +int +main(int argc, char **argv) +{ + initlog(); + + Utilities::MPI::MPI_InitFinalize mpi_initialization( + argc, argv, testing_max_num_threads()); + + + try + { + { + LinearAlgebra::TpetraWrappers::Vector v(complete_index_set(95), + MPI_COMM_SELF); + LinearAlgebra::TpetraWrappers::Vector w(complete_index_set(100), + MPI_COMM_SELF); + test(v, w); + } + } + catch (std::exception &exc) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Exception on processing: " << std::endl + << exc.what() << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + + return 1; + } + catch (...) + { + std::cerr << std::endl + << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Unknown exception!" << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + return 1; + }; +} diff --git a/tests/trilinos/sparse_matrix_vector_15.output b/tests/trilinos/sparse_matrix_vector_15.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/trilinos/sparse_matrix_vector_15.output @@ -0,0 +1,2 @@ + +DEAL::OK -- 2.39.5