From: Bruno Turcksin Date: Tue, 31 May 2016 17:33:15 +0000 (-0400) Subject: Add a functor interface to ReadWriteVector. X-Git-Tag: v8.5.0-rc1~994^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2657%2Fhead;p=dealii.git Add a functor interface to ReadWriteVector. --- diff --git a/include/deal.II/grid/filtered_iterator.h b/include/deal.II/grid/filtered_iterator.h index 59fa12fb83..ab6ba61229 100644 --- a/include/deal.II/grid/filtered_iterator.h +++ b/include/deal.II/grid/filtered_iterator.h @@ -697,7 +697,7 @@ private: /** * Abstract function which in derived classes denotes the evaluation of - * the predicate on the give iterator. + * the predicate on the given iterator. */ virtual bool operator () (const BaseIterator &bi) const = 0; diff --git a/include/deal.II/lac/read_write_vector.h b/include/deal.II/lac/read_write_vector.h index 1841f4a2d1..00c1d7d31d 100644 --- a/include/deal.II/lac/read_write_vector.h +++ b/include/deal.II/lac/read_write_vector.h @@ -195,6 +195,24 @@ namespace LinearAlgebra void reinit (const IndexSet &locally_stored_indices, const bool omit_zeroing_entries = false); +#ifdef DEAL_II_WITH_CXX11 + /** + * Apply the functor @p func to each element of the vector. The functor + * should look like + * + * struct Functor + * { + * void operator() (Number &value); + * }; + * + * + * @note This function requires C++11 and read_write_vector.templates.h + * needs to be included. + */ + template + void apply(const Functor &func); +#endif + /** * Swap the contents of this vector and the other vector @p v. One could * do this operation with a temporary variable and copying over the data @@ -517,6 +535,42 @@ namespace LinearAlgebra * Make all other ReadWriteVector types friends. */ template friend class ReadWriteVector; + +#ifdef DEAL_II_WITH_CXX11 + private: + /** + * This class provides a wrapper around a Functor which acts on + * single elements of the vector. This is necessary to use + * tbb::parallel_for which requires a TBBForFunctor. + */ + template + class FunctorTemplate + { + public: + /** + * Constructor. Take a functor and store a copy of it. + */ + FunctorTemplate(ReadWriteVector &parent, + const Functor &functor); + + /** + * Evaluate the element with the stored copy of the functor. + */ + virtual void operator() (const size_type begin, + const size_type end); + + private: + /** + * Alias to the ReadWriteVector object that owns the FunctorTemplate. + */ + ReadWriteVector &parent; + + /** + * Copy of the functor. + */ + const Functor &functor; + }; +#endif }; /*@}*/ @@ -790,6 +844,33 @@ namespace LinearAlgebra } } + + +#ifdef DEAL_II_WITH_CXX11 + template + template + inline + ReadWriteVector::FunctorTemplate::FunctorTemplate( + ReadWriteVector &parent, + const Functor &functor) + : + parent(parent), + functor(functor) + {} + + + + template + template + void + ReadWriteVector::FunctorTemplate::operator() (const size_type begin, + const size_type end) + { + for (size_type i=begin; i + template + void + ReadWriteVector::apply(const Functor &func) + { + FunctorTemplate functor(*this, func); + internal::parallel_for(functor, n_elements(), thread_loop_partitioner); + } +#endif + + + template ReadWriteVector & ReadWriteVector::operator= (const ReadWriteVector &in_vector) diff --git a/tests/lac/readwritevector_functor.cc b/tests/lac/readwritevector_functor.cc new file mode 100644 index 0000000000..5c33474ea4 --- /dev/null +++ b/tests/lac/readwritevector_functor.cc @@ -0,0 +1,55 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2016 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. +// +// --------------------------------------------------------------------- + +// Check the functor interface + + +#include "../tests.h" +#include +#include +#include +#include + +#include + +struct Functor +{ + void operator() (double &value) const + { + value *= 2.; + } +}; + +void test() +{ + const unsigned int size = 25; + LinearAlgebra::ReadWriteVector vector(size); + for (unsigned int i=0; i