From d08cca725e55394fd4436af3fa4e8cbf9cc0b910 Mon Sep 17 00:00:00 2001 From: bangerth Date: Sun, 4 Oct 2009 22:52:50 +0000 Subject: [PATCH] Move the SynchronousIterator class into namespace dealii and to its own file. git-svn-id: https://svn.dealii.org/trunk@19697 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/parallel.h | 243 +--------------- .../base/include/base/synchronous_iterator.h | 263 ++++++++++++++++++ deal.II/examples/step-35/step-35.cc | 30 +- 3 files changed, 282 insertions(+), 254 deletions(-) create mode 100644 deal.II/base/include/base/synchronous_iterator.h diff --git a/deal.II/base/include/base/parallel.h b/deal.II/base/include/base/parallel.h index b5f496ee0e..3c0aa8dc0b 100644 --- a/deal.II/base/include/base/parallel.h +++ b/deal.II/base/include/base/parallel.h @@ -17,12 +17,12 @@ #include #include #include +#include #include #include #include -#include #include #if DEAL_II_USE_MT == 1 @@ -48,241 +48,6 @@ namespace parallel { namespace internal { - /** - * A class that represents a set of - * iterators each of which are - * incremented by one at the same - * time. This is typically used in calls - * like std::transform(a.begin(), - * a.end(), b.begin(), functor); - * where we have synchronous iterators - * marching through the containers - * a,b. If an object of this - * type represents the end of a range, - * only the first element is considered - * (we only have a.end(), - * not b.end()) - * - * The template argument of the current - * class shall be of type - * std_cxx1x::tuple with - * arguments equal to the iterator types. - * - * This type, and the helper functions - * associated with it, are used as the - * Value concept for the blocked_range - * type of the Threading Building Blocks. - */ - template - struct SynchronousIterators - { - /** - * Constructor. - */ - SynchronousIterators (const Iterators &i); - - /** - * Copy constructor. - */ - SynchronousIterators (const SynchronousIterators &i); - - /** - * Storage for the iterators - * represented by the current class. - */ - Iterators iterators; - }; - - - - template - inline - SynchronousIterators:: - SynchronousIterators (const Iterators &i) - : - iterators (i) - {} - - - template - inline - SynchronousIterators:: - SynchronousIterators (const SynchronousIterators &i) - : - iterators (i.iterators) - {} - - - - /** - * Return whether the first element of - * the first argument is less than the - * first element of the second - * argument. Since the objects compared - * march forward all elements at the same - * time, comparing the first element is - * sufficient. - */ - template - inline - bool - operator< (const SynchronousIterators &a, - const SynchronousIterators &b) - { - return std_cxx1x::get<0>(a.iterators) < std_cxx1x::get<0>(b.iterators); - } - - - - /** - * Return the distance between the first - * and the second argument. Since the - * objects compared march forward all - * elements at the same time, - * differencing the first element is - * sufficient. - */ - template - inline - std::size_t - operator- (const SynchronousIterators &a, - const SynchronousIterators &b) - { - Assert (std::distance (std_cxx1x::get<0>(b.iterators), - std_cxx1x::get<0>(a.iterators)) >= 0, - ExcInternalError()); - return std::distance (std_cxx1x::get<0>(b.iterators), - std_cxx1x::get<0>(a.iterators)); - } - - - /** - * Advance a tuple of iterators by $n$. - */ - template - inline - void advance (std_cxx1x::tuple &t, - const unsigned int n) - { - std::advance (std_cxx1x::get<0>(t), n); - std::advance (std_cxx1x::get<1>(t), n); - } - - /** - * Advance a tuple of iterators by $n$. - */ - template - inline - void advance (std_cxx1x::tuple &t, - const unsigned int n) - { - std::advance (std_cxx1x::get<0>(t), n); - std::advance (std_cxx1x::get<1>(t), n); - std::advance (std_cxx1x::get<2>(t), n); - } - - /** - * Advance a tuple of iterators by $n$. - */ - template - inline - void advance (std_cxx1x::tuple &t, - const unsigned int n) - { - std::advance (std_cxx1x::get<0>(t), n); - std::advance (std_cxx1x::get<1>(t), n); - std::advance (std_cxx1x::get<2>(t), n); - std::advance (std_cxx1x::get<3>(t), n); - } - - - - /** - * Advance a tuple of iterators by 1. - */ - template - inline - void advance_by_one (std_cxx1x::tuple &t) - { - ++std_cxx1x::get<0>(t); - ++std_cxx1x::get<1>(t); - } - - /** - * Advance a tuple of iterators by 1. - */ - template - inline - void advance_by_one (std_cxx1x::tuple &t) - { - ++std_cxx1x::get<0>(t); - ++std_cxx1x::get<1>(t); - ++std_cxx1x::get<2>(t); - } - - /** - * Advance a tuple of iterators by 1. - */ - template - inline - void advance_by_one (std_cxx1x::tuple &t) - { - ++std_cxx1x::get<0>(t); - ++std_cxx1x::get<1>(t); - ++std_cxx1x::get<2>(t); - ++std_cxx1x::get<3>(t); - } - - - - /** - * Advance the elements of this iterator - * by $n$. - */ - template - inline - SynchronousIterators - operator + (const SynchronousIterators &a, - const std::size_t n) - { - SynchronousIterators x (a); - parallel::internal::advance (x.iterators, n); - return x; - } - - /** - * Advance the elements of this iterator - * by 1. - */ - template - inline - SynchronousIterators - operator ++ (SynchronousIterators &a) - { - parallel::internal::advance_by_one (a.iterators); - return a; - } - - - /** - * Compare synch iterators for - * inequality. Since they march in synch, - * comparing only the first element is - * sufficient. - */ - template - inline - bool - operator != (const SynchronousIterators &a, - const SynchronousIterators &b) - { - return (std_cxx1x::get<0>(a.iterators) != - std_cxx1x::get<0>(b.iterators)); - } - - /** * Convert a function object of type F * into an object that can be applied to @@ -420,7 +185,7 @@ namespace parallel *out++ = predicate (*in++); #else typedef std_cxx1x::tuple Iterators; - typedef internal::SynchronousIterators SyncIterators; + typedef SynchronousIterators SyncIterators; Iterators x_begin (begin_in, out); Iterators x_end (end_in, OutputIterator()); tbb::parallel_for (tbb::blocked_range(x_begin, @@ -480,7 +245,7 @@ namespace parallel typedef std_cxx1x::tuple Iterators; - typedef internal::SynchronousIterators SyncIterators; + typedef SynchronousIterators SyncIterators; Iterators x_begin (begin_in1, in2, out); Iterators x_end (end_in1, InputIterator2(), OutputIterator()); tbb::parallel_for (tbb::blocked_range(x_begin, @@ -542,7 +307,7 @@ namespace parallel typedef std_cxx1x::tuple Iterators; - typedef internal::SynchronousIterators SyncIterators; + typedef SynchronousIterators SyncIterators; Iterators x_begin (begin_in1, in2, in3, out); Iterators x_end (end_in1, InputIterator2(), InputIterator3(), OutputIterator()); diff --git a/deal.II/base/include/base/synchronous_iterator.h b/deal.II/base/include/base/synchronous_iterator.h new file mode 100644 index 0000000000..8f67ed3af3 --- /dev/null +++ b/deal.II/base/include/base/synchronous_iterator.h @@ -0,0 +1,263 @@ +//--------------------------------------------------------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2008, 2009 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//--------------------------------------------------------------------------- +#ifndef __deal2__synchronous_iterator_h +#define __deal2__synchronous_iterator_h + + +#include + +#include + +#include + +DEAL_II_NAMESPACE_OPEN + +/** + * A class that represents a set of + * iterators each of which are + * incremented by one at the same + * time. This is typically used in calls + * like std::transform(a.begin(), + * a.end(), b.begin(), functor); + * where we have synchronous iterators + * marching through the containers + * a,b. If an object of this + * type represents the end of a range, + * only the first element is considered + * (we only have a.end(), + * not b.end()) + * + * The template argument of the current + * class shall be of type + * std_cxx1x::tuple with + * arguments equal to the iterator types. + * + * This type, and the helper functions + * associated with it, are used as the + * Value concept for the blocked_range + * type of the Threading Building Blocks. + * + * @author Wolfgang Bangerth, 2008 + */ +template +struct SynchronousIterators +{ + /** + * Constructor. + */ + SynchronousIterators (const Iterators &i); + + /** + * Copy constructor. + */ + SynchronousIterators (const SynchronousIterators &i); + + /** + * Storage for the iterators + * represented by the current class. + */ + Iterators iterators; +}; + + + +template +inline +SynchronousIterators:: +SynchronousIterators (const Iterators &i) + : + iterators (i) +{} + + +template +inline +SynchronousIterators:: +SynchronousIterators (const SynchronousIterators &i) + : + iterators (i.iterators) +{} + + + + /** + * Return whether the first element of + * the first argument is less than the + * first element of the second + * argument. Since the objects compared + * march forward all elements at the same + * time, comparing the first element is + * sufficient. + */ +template +inline +bool +operator< (const SynchronousIterators &a, + const SynchronousIterators &b) +{ + return std_cxx1x::get<0>(a.iterators) < std_cxx1x::get<0>(b.iterators); +} + + + + /** + * Return the distance between the first + * and the second argument. Since the + * objects compared march forward all + * elements at the same time, + * differencing the first element is + * sufficient. + */ +template +inline +std::size_t +operator- (const SynchronousIterators &a, + const SynchronousIterators &b) +{ + Assert (std::distance (std_cxx1x::get<0>(b.iterators), + std_cxx1x::get<0>(a.iterators)) >= 0, + ExcInternalError()); + return std::distance (std_cxx1x::get<0>(b.iterators), + std_cxx1x::get<0>(a.iterators)); +} + + + /** + * Advance a tuple of iterators by $n$. + */ +template +inline +void advance (std_cxx1x::tuple &t, + const unsigned int n) +{ + std::advance (std_cxx1x::get<0>(t), n); + std::advance (std_cxx1x::get<1>(t), n); +} + + /** + * Advance a tuple of iterators by $n$. + */ +template +inline +void advance (std_cxx1x::tuple &t, + const unsigned int n) +{ + std::advance (std_cxx1x::get<0>(t), n); + std::advance (std_cxx1x::get<1>(t), n); + std::advance (std_cxx1x::get<2>(t), n); +} + + /** + * Advance a tuple of iterators by $n$. + */ +template +inline +void advance (std_cxx1x::tuple &t, + const unsigned int n) +{ + std::advance (std_cxx1x::get<0>(t), n); + std::advance (std_cxx1x::get<1>(t), n); + std::advance (std_cxx1x::get<2>(t), n); + std::advance (std_cxx1x::get<3>(t), n); +} + + + + /** + * Advance a tuple of iterators by 1. + */ +template +inline +void advance_by_one (std_cxx1x::tuple &t) +{ + ++std_cxx1x::get<0>(t); + ++std_cxx1x::get<1>(t); +} + + /** + * Advance a tuple of iterators by 1. + */ +template +inline +void advance_by_one (std_cxx1x::tuple &t) +{ + ++std_cxx1x::get<0>(t); + ++std_cxx1x::get<1>(t); + ++std_cxx1x::get<2>(t); +} + + /** + * Advance a tuple of iterators by 1. + */ +template +inline +void advance_by_one (std_cxx1x::tuple &t) +{ + ++std_cxx1x::get<0>(t); + ++std_cxx1x::get<1>(t); + ++std_cxx1x::get<2>(t); + ++std_cxx1x::get<3>(t); +} + + + + /** + * Advance the elements of this iterator + * by $n$. + */ +template +inline +SynchronousIterators +operator + (const SynchronousIterators &a, + const std::size_t n) +{ + SynchronousIterators x (a); + dealii::advance (x.iterators, n); + return x; +} + + /** + * Advance the elements of this iterator + * by 1. + */ +template +inline +SynchronousIterators +operator ++ (SynchronousIterators &a) +{ + dealii::advance_by_one (a.iterators); + return a; +} + + + /** + * Compare synch iterators for + * inequality. Since they march in synch, + * comparing only the first element is + * sufficient. + */ +template +inline +bool +operator != (const SynchronousIterators &a, + const SynchronousIterators &b) +{ + return (std_cxx1x::get<0>(a.iterators) != + std_cxx1x::get<0>(b.iterators)); +} + +DEAL_II_NAMESPACE_CLOSE + +#endif diff --git a/deal.II/examples/step-35/step-35.cc b/deal.II/examples/step-35/step-35.cc index a20365b328..f4a7dbf311 100644 --- a/deal.II/examples/step-35/step-35.cc +++ b/deal.II/examples/step-35/step-35.cc @@ -434,10 +434,10 @@ class NavierStokesProjection std::vector boundary_indicators; Triangulation triangulation; - + FE_Q fe_velocity; FE_Q fe_pressure; - + DoFHandler dof_handler_velocity; DoFHandler dof_handler_pressure; @@ -447,7 +447,7 @@ class NavierStokesProjection SparsityPattern sparsity_pattern_velocity; SparsityPattern sparsity_pattern_pressure; SparsityPattern sparsity_pattern_pres_vel; - + SparseMatrix vel_Laplace_plus_Mass; SparseMatrix vel_it_matrix[dim]; SparseMatrix vel_Mass; @@ -457,7 +457,7 @@ class NavierStokesProjection SparseMatrix pres_Mass; SparseMatrix pres_Diff[dim]; SparseMatrix pres_iterative; - + Vector pres_n; Vector pres_n_minus_1; Vector phi_n; @@ -554,7 +554,7 @@ class NavierStokesProjection typename DoFHandler::active_cell_iterator > IteratorTuple; - typedef parallel::internal::SynchronousIterators IteratorPair; + typedef SynchronousIterators IteratorPair; void init_gradient_operator(); @@ -566,7 +566,7 @@ class NavierStokesProjection FullMatrix local_grad; std::vector vel_local_dof_indices; std::vector pres_local_dof_indices; - + InitGradPerTaskData (const unsigned int dd, const unsigned int vdpc, const unsigned int pdpc) @@ -579,7 +579,7 @@ class NavierStokesProjection pres_local_dof_indices (pdpc) {} }; - + struct InitGradScratchData { unsigned int nqp; @@ -606,11 +606,11 @@ class NavierStokesProjection data.fe_val_pres.get_update_flags()) {} }; - + void assemble_one_cell_of_gradient (const IteratorPair &SI, InitGradScratchData &scratch, InitGradPerTaskData &data); - + void copy_gradient_local_to_global (const InitGradPerTaskData &data); // The same general layout also applies @@ -618,7 +618,7 @@ class NavierStokesProjection // implementing the assembly of the // advection term: void assemble_advection_term(); - + struct AdvectionPerTaskData { FullMatrix local_advection; @@ -629,7 +629,7 @@ class NavierStokesProjection local_dof_indices (dpc) {} }; - + struct AdvectionScratchData { unsigned int nqp; @@ -649,7 +649,7 @@ class NavierStokesProjection u_star_tmp (nqp), fe_val (fe, quad, flags) {} - + AdvectionScratchData (const AdvectionScratchData &data) : nqp (data.nqp), @@ -666,7 +666,7 @@ class NavierStokesProjection void assemble_one_cell_of_advection (const typename DoFHandler::active_cell_iterator &cell, AdvectionScratchData &scratch, AdvectionPerTaskData &data); - + void copy_advection_local_to_global (const AdvectionPerTaskData &data); // The final few functions implement the @@ -676,7 +676,7 @@ class NavierStokesProjection void diffusion_component_solve (const unsigned int d); void plot_solution (const unsigned int step); - + void assemble_vorticity (const bool reinit_prec); }; @@ -978,7 +978,7 @@ void NavierStokesProjection::run (const bool verbose, const unsigned int n_of_plots) { ConditionalOStream verbose_cout (std::cout, verbose); - + unsigned int n_steps = (T - t_0)/dt; vel_exact.set_time (2.*dt); plot_solution(1); -- 2.39.5