From 58973207b6b3fde732c498dfdc400e060de36a2d Mon Sep 17 00:00:00 2001 From: Bruno Turcksin Date: Mon, 8 May 2017 21:21:43 -0400 Subject: [PATCH] Add all_zero() to VectorSpaceVector. --- include/deal.II/lac/cuda_vector.h | 5 ++++ .../deal.II/lac/la_parallel_block_vector.h | 2 +- include/deal.II/lac/la_parallel_vector.h | 2 +- include/deal.II/lac/la_vector.h | 5 ++++ include/deal.II/lac/la_vector.templates.h | 15 ++++++++++ include/deal.II/lac/trilinos_epetra_vector.h | 5 ++++ include/deal.II/lac/vector_space_vector.h | 7 ++++- source/lac/cuda_vector.cu | 8 +++++ source/lac/trilinos_epetra_vector.cc | 29 +++++++++++++++++++ 9 files changed, 75 insertions(+), 3 deletions(-) diff --git a/include/deal.II/lac/cuda_vector.h b/include/deal.II/lac/cuda_vector.h index c10ac4129f..4bf4c99711 100644 --- a/include/deal.II/lac/cuda_vector.h +++ b/include/deal.II/lac/cuda_vector.h @@ -169,6 +169,11 @@ namespace LinearAlgebra */ virtual void equ(const Number a, const VectorSpaceVector &V) override; + /** + * Return wether the vector contains only elements with value zero. + */ + virtual bool all_zero() const override; + /** * Return the mean value of all the entries of this vector. */ diff --git a/include/deal.II/lac/la_parallel_block_vector.h b/include/deal.II/lac/la_parallel_block_vector.h index 7dbccdf7bd..a81fc411a3 100644 --- a/include/deal.II/lac/la_parallel_block_vector.h +++ b/include/deal.II/lac/la_parallel_block_vector.h @@ -367,7 +367,7 @@ namespace LinearAlgebra * This function is mainly for internal consistency checks and should * seldom be used when not in debug mode since it uses quite some time. */ - bool all_zero () const; + virtual bool all_zero () const override; /** * Compute the mean value of all the entries in the vector. diff --git a/include/deal.II/lac/la_parallel_vector.h b/include/deal.II/lac/la_parallel_vector.h index 6a53c1e0b0..f09a287d6c 100644 --- a/include/deal.II/lac/la_parallel_vector.h +++ b/include/deal.II/lac/la_parallel_vector.h @@ -897,7 +897,7 @@ namespace LinearAlgebra * This is a collective operation. This function is expensive, because * potentially all elements have to be checked. */ - bool all_zero () const; + virtual bool all_zero () const override; /** * Compute the mean value of all the entries in the vector. diff --git a/include/deal.II/lac/la_vector.h b/include/deal.II/lac/la_vector.h index 380954c434..bcb2d3b9c4 100644 --- a/include/deal.II/lac/la_vector.h +++ b/include/deal.II/lac/la_vector.h @@ -225,6 +225,11 @@ namespace LinearAlgebra */ virtual void equ(const Number a, const VectorSpaceVector &V); + /** + * Return wether the vector contains only elements with value zero. + */ + virtual bool all_zero() const override; + /** * Return the mean value of all the entries of this vector. */ diff --git a/include/deal.II/lac/la_vector.templates.h b/include/deal.II/lac/la_vector.templates.h index c823b74777..f07f4e8237 100644 --- a/include/deal.II/lac/la_vector.templates.h +++ b/include/deal.II/lac/la_vector.templates.h @@ -327,6 +327,21 @@ namespace LinearAlgebra + template + bool Vector::all_zero() const + { + Assert(this->size(), ExcEmptyObject()); + + const size_type size = this->size(); + for (size_type i=0; ival[i] != Number()) + return false; + + return true; + } + + + template typename Vector::value_type Vector::mean_value() const { diff --git a/include/deal.II/lac/trilinos_epetra_vector.h b/include/deal.II/lac/trilinos_epetra_vector.h index 2be2a661b0..7c81db4d9d 100644 --- a/include/deal.II/lac/trilinos_epetra_vector.h +++ b/include/deal.II/lac/trilinos_epetra_vector.h @@ -179,6 +179,11 @@ namespace LinearAlgebra */ virtual void equ(const double a, const VectorSpaceVector &V); + /** + * Return wether the vector contains only elements with value zero. + */ + virtual bool all_zero() const override; + /** * Return the mean value of the element of this vector. */ diff --git a/include/deal.II/lac/vector_space_vector.h b/include/deal.II/lac/vector_space_vector.h index 8a18ff9f65..a70b4b5297 100644 --- a/include/deal.II/lac/vector_space_vector.h +++ b/include/deal.II/lac/vector_space_vector.h @@ -1,6 +1,6 @@ // --------------------------------------------------------------------- // -// Copyright (C) 2015 - 2016 by the deal.II authors +// Copyright (C) 2015 - 2017 by the deal.II authors // // This file is part of the deal.II library. // @@ -141,6 +141,11 @@ namespace LinearAlgebra */ virtual void equ(const Number a, const VectorSpaceVector &V) = 0; + /** + * Return whether the vector contains only elements with value zero. + */ + virtual bool all_zero() const = 0; + /** * Return the mean value of all the entries of this vector. */ diff --git a/source/lac/cuda_vector.cu b/source/lac/cuda_vector.cu index 4d0921f2f0..42504b7fda 100644 --- a/source/lac/cuda_vector.cu +++ b/source/lac/cuda_vector.cu @@ -915,6 +915,14 @@ namespace LinearAlgebra + template + bool Vector::all_zero() const + { + return (linfty_norm() == 0) ? true : false; + } + + + template typename Vector::value_type Vector::mean_value() const { diff --git a/source/lac/trilinos_epetra_vector.cc b/source/lac/trilinos_epetra_vector.cc index c44ff85cad..53476c7bf6 100644 --- a/source/lac/trilinos_epetra_vector.cc +++ b/source/lac/trilinos_epetra_vector.cc @@ -389,6 +389,35 @@ namespace LinearAlgebra + bool Vector::all_zero() const + { + // get a representation of the vector and + // loop over all the elements + double *start_ptr = (*vector)[0]; + const double *ptr = start_ptr, + *eptr = start_ptr + vector->MyLength(); + unsigned int flag = 0; + while (ptr != eptr) + { + if (*ptr != 0) + { + flag = 1; + break; + } + ++ptr; + } + + // Check that the vector is zero on _all_ processors. + const Epetra_MpiComm *mpi_comm + = dynamic_cast(&vector->Map().Comm()); + Assert(mpi_comm != nullptr, ExcInternalError()); + unsigned int num_nonzero = Utilities::MPI::sum(flag, mpi_comm->Comm()); + + return num_nonzero == 0; + } + + + double Vector::mean_value() const { double mean_value(0.); -- 2.39.5