From 06e97f5a6195f6e0b870821213d2797527244cea Mon Sep 17 00:00:00 2001 From: bangerth Date: Fri, 8 Nov 2013 23:18:09 +0000 Subject: [PATCH] Add several more tests. git-svn-id: https://svn.dealii.org/trunk@31596 0785d39b-7218-0410-832d-ea1e28bc413d --- tests/base/work_stream_03_graph.cc | 185 +++++++++++++++++++++++++ tests/base/work_stream_03_graph.output | 13 ++ tests/base/work_stream_05.cc | 99 +++++++++++++ tests/base/work_stream_05.output | 101 ++++++++++++++ tests/base/work_stream_05_graph.cc | 115 +++++++++++++++ tests/base/work_stream_05_graph.output | 101 ++++++++++++++ 6 files changed, 614 insertions(+) create mode 100644 tests/base/work_stream_03_graph.cc create mode 100644 tests/base/work_stream_03_graph.output create mode 100644 tests/base/work_stream_05.cc create mode 100644 tests/base/work_stream_05.output create mode 100644 tests/base/work_stream_05_graph.cc create mode 100644 tests/base/work_stream_05_graph.output diff --git a/tests/base/work_stream_03_graph.cc b/tests/base/work_stream_03_graph.cc new file mode 100644 index 0000000000..4aface31ad --- /dev/null +++ b/tests/base/work_stream_03_graph.cc @@ -0,0 +1,185 @@ +// --------------------------------------------------------------------- +// $Id$ +// +// Copyright (C) 2006 - 2013 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. +// +// --------------------------------------------------------------------- + + +// Like _03 but with a graph coloring algorithm for the iterators. The +// graph coloring here is simple since all writes conflict (we just +// add to a scalar) + +#include "../tests.h" +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + + +template +double +value(const Point &p) +{ + double val = 0; + for (unsigned int d = 0; d < dim; ++d) + for (unsigned int i = 0; i <= 1; ++i) + val += std::pow(p[d], 1. * i); + return val; +} + +namespace +{ + template + struct Scratch + { + Scratch(const FiniteElement &fe, const Quadrature &quadrature) : + fe_collection(fe), quadrature_collection(quadrature), x_fe_values( + fe_collection, quadrature_collection, update_q_points), rhs_values( + quadrature_collection.size()) + { + } + + Scratch(const Scratch &data) : + fe_collection(data.fe_collection), quadrature_collection( + data.quadrature_collection), x_fe_values(fe_collection, + quadrature_collection, update_q_points), rhs_values( + data.rhs_values) + { + } + + const FiniteElement &fe_collection; + const Quadrature &quadrature_collection; + + FEValues x_fe_values; + + std::vector rhs_values; + }; + + struct CopyData + { + std::vector cell_rhs; + }; +} + +void +zero_subrange(const unsigned int begin, const unsigned int end, + std::vector &dst) +{ + for (unsigned int i = begin; i < end; ++i) + dst[i] = 0; +} + + +void +zero_element(std::vector &dst, + const unsigned int i) +{ + dst[i] = 0; +} + + +template +void +mass_assembler(const typename Triangulation::active_cell_iterator &cell, + Scratch &data, CopyData ©_data) +{ + data.x_fe_values.reinit(cell); + + const Point q = data.x_fe_values.quadrature_point(0); + + // this appears to be the key: the following two ways both overwrite some + // of the memory in which we store the quadrature point location. + parallel::apply_to_subranges(0U, copy_data.cell_rhs.size(), + std_cxx1x::bind(&zero_subrange, std_cxx1x::_1, std_cxx1x::_2, + std_cxx1x::ref(copy_data.cell_rhs)), 1); + + Assert(q == data.x_fe_values.quadrature_point(0), + ExcInternalError()); + + copy_data.cell_rhs[0] = value(data.x_fe_values.quadrature_point(0)); +} + + +void +copy_local_to_global(const CopyData &data, double *sum) +{ + *sum += data.cell_rhs[0]; +} + + +// the function that defines conclicts. we always write into the same +// field, so we need to always return the same index +template +std::vector +conflictor (const typename Triangulation::active_cell_iterator &) +{ + return std::vector(1, types::global_dof_index()); +} + + +void +do_project() +{ + static const int dim = 3; + + Triangulation < dim > triangulation; + GridGenerator::hyper_cube (triangulation); + triangulation.refine_global(2); + + FE_Nothing < dim > fe; + QMidpoint < dim > q; + + for (unsigned int i = 0; i < 12; ++i) + { + std::vector tmp; + + double sum = 0; + Scratch assembler_data(fe, q); + CopyData copy_data; + copy_data.cell_rhs.resize(8); + WorkStream::run(GraphColoring::make_graph_coloring (triangulation.begin_active(), + triangulation.end(), + std_cxx1x::function + (const Triangulation::active_cell_iterator &)> + (&conflictor)), + &mass_assembler, + std_cxx1x::bind(©_local_to_global, std_cxx1x::_1, &sum), + assembler_data, copy_data, 8, 1); + + Assert (std::fabs(sum-288.) < 1e-12, ExcInternalError()); + deallog << sum << std::endl; + } +} + + +int main() +{ + std::ofstream logfile("output"); + deallog << std::setprecision(3); + + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + do_project(); +} diff --git a/tests/base/work_stream_03_graph.output b/tests/base/work_stream_03_graph.output new file mode 100644 index 0000000000..d1b460b43f --- /dev/null +++ b/tests/base/work_stream_03_graph.output @@ -0,0 +1,13 @@ + +DEAL::288. +DEAL::288. +DEAL::288. +DEAL::288. +DEAL::288. +DEAL::288. +DEAL::288. +DEAL::288. +DEAL::288. +DEAL::288. +DEAL::288. +DEAL::288. diff --git a/tests/base/work_stream_05.cc b/tests/base/work_stream_05.cc new file mode 100644 index 0000000000..5f74904d82 --- /dev/null +++ b/tests/base/work_stream_05.cc @@ -0,0 +1,99 @@ +// --------------------------------------------------------------------- +// $Id$ +// +// Copyright (C) 2008 - 2013 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. +// +// --------------------------------------------------------------------- + + +// a test for WorkStream where we really do write conflicting entries +// into a global vector + +#include "../tests.h" +#include +#include +#include +#include + +#include +#include + + +Vector result(100); + + +struct ScratchData +{}; + + +struct CopyData +{ + unsigned int computed; +}; + + +void worker (const std::vector::iterator &i, + ScratchData &, + CopyData &ad) +{ + ad.computed = *i * 2; +} + +void copier (const CopyData &ad) +{ + // write into the five elements of 'result' starting at ad.computed%result.size() + for (unsigned int j=0; j<5; ++j) + result((ad.computed+j) % result.size()) += ad.computed; +} + + + +void test () +{ + std::vector v; + for (unsigned int i=0; i<200; ++i) + v.push_back (i); + + WorkStream::run (v.begin(), v.end(), &worker, &copier, + ScratchData(), + CopyData()); + + // now simulate what we should have gotten + Vector comp(result.size()); + for (unsigned int i=0; i +#include +#include +#include + +#include +#include + + +Vector result(100); + + +struct ScratchData +{}; + + +struct CopyData +{ + unsigned int computed; +}; + + +void worker (const std::vector::iterator &i, + ScratchData &, + CopyData &ad) +{ + ad.computed = *i * 2; +} + +void copier (const CopyData &ad) +{ + // write into the five elements of 'result' starting at ad.computed%result.size() + for (unsigned int j=0; j<5; ++j) + result((ad.computed+j) % result.size()) += ad.computed; +} + + +// the function that computes conflicts +std::vector +conflictor (const std::vector::iterator &i) +{ + std::vector conflicts; + const unsigned int ad_computed = *i * 2; + for (unsigned int j=0; j<5; ++j) + conflicts.push_back ((ad_computed+j) % result.size()); + + return conflicts; +} + + + +void test () +{ + std::vector v; + for (unsigned int i=0; i<200; ++i) + v.push_back (i); + + WorkStream::run (GraphColoring::make_graph_coloring (v.begin(), v.end(), + std_cxx1x::function + (const std::vector::iterator &)> + (&conflictor)), + &worker, &copier, + ScratchData(), + CopyData()); + + // now simulate what we should have gotten + Vector comp(result.size()); + for (unsigned int i=0; i