]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Add several more tests.
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 8 Nov 2013 23:18:09 +0000 (23:18 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 8 Nov 2013 23:18:09 +0000 (23:18 +0000)
git-svn-id: https://svn.dealii.org/trunk@31596 0785d39b-7218-0410-832d-ea1e28bc413d

tests/base/work_stream_03_graph.cc [new file with mode: 0644]
tests/base/work_stream_03_graph.output [new file with mode: 0644]
tests/base/work_stream_05.cc [new file with mode: 0644]
tests/base/work_stream_05.output [new file with mode: 0644]
tests/base/work_stream_05_graph.cc [new file with mode: 0644]
tests/base/work_stream_05_graph.output [new file with mode: 0644]

diff --git a/tests/base/work_stream_03_graph.cc b/tests/base/work_stream_03_graph.cc
new file mode 100644 (file)
index 0000000..4aface3
--- /dev/null
@@ -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 <deal.II/base/logstream.h>
+#include <deal.II/base/quadrature_lib.h>
+#include <deal.II/base/work_stream.h>
+#include <deal.II/lac/vector.h>
+#include <deal.II/base/parallel.h>
+
+#include <deal.II/grid/tria.h>
+#include <deal.II/dofs/dof_handler.h>
+#include <deal.II/grid/grid_generator.h>
+#include <deal.II/fe/fe_nothing.h>
+#include <deal.II/fe/fe_values.h>
+
+#include <fstream>
+#include <vector>
+
+
+template<int dim>
+double
+value(const Point<dim> &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<int dim>
+  struct Scratch
+  {
+    Scratch(const FiniteElement<dim> &fe, const Quadrature<dim> &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<dim> &fe_collection;
+    const Quadrature<dim> &quadrature_collection;
+
+    FEValues<dim> x_fe_values;
+
+    std::vector<double> rhs_values;
+  };
+
+  struct CopyData
+  {
+    std::vector<double> cell_rhs;
+  };
+}
+
+void
+zero_subrange(const unsigned int begin, const unsigned int end,
+              std::vector<double> &dst)
+{
+  for (unsigned int i = begin; i < end; ++i)
+    dst[i] = 0;
+}
+
+
+void
+zero_element(std::vector<double> &dst,
+             const unsigned int i)
+{
+  dst[i] = 0;
+}
+
+
+template<int dim>
+void
+mass_assembler(const typename Triangulation<dim>::active_cell_iterator &cell,
+               Scratch<dim> &data, CopyData &copy_data)
+{
+  data.x_fe_values.reinit(cell);
+
+  const Point<dim> 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 <int dim>
+std::vector<types::global_dof_index>
+conflictor (const typename Triangulation<dim>::active_cell_iterator &)
+{
+  return std::vector<types::global_dof_index>(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<double> tmp;
+
+      double sum = 0;
+      Scratch<dim> 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<std::vector<types::global_dof_index>
+                                                                             (const Triangulation<dim>::active_cell_iterator &)>
+                                                         (&conflictor<dim>)),
+                      &mass_assembler<dim>,
+                      std_cxx1x::bind(&copy_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 (file)
index 0000000..d1b460b
--- /dev/null
@@ -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 (file)
index 0000000..5f74904
--- /dev/null
@@ -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 <iomanip>
+#include <iomanip>
+#include <fstream>
+#include <cmath>
+
+#include <deal.II/base/work_stream.h>
+#include <deal.II/lac/vector.h>
+
+
+Vector<double> result(100);
+
+
+struct ScratchData
+{};
+
+
+struct CopyData
+{
+  unsigned int computed;
+};
+
+
+void worker (const std::vector<unsigned int>::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<unsigned int> 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<double> comp(result.size());
+  for (unsigned int i=0; i<v.size(); ++i)
+    {
+      const unsigned int ad_computed = v[i] * 2;
+      for (unsigned int j=0; j<5; ++j)
+       comp((ad_computed+j) % result.size()) += ad_computed;
+    }
+
+
+  // and compare
+  for (unsigned int i=0; i<result.size(); ++i)
+    Assert (result(i) == comp(i), ExcInternalError());
+
+  for (unsigned int i=0; i<result.size(); ++i)
+    deallog << result(i) << std::endl;
+}
+
+
+
+
+int main()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  test ();
+}
diff --git a/tests/base/work_stream_05.output b/tests/base/work_stream_05.output
new file mode 100644 (file)
index 0000000..728770d
--- /dev/null
@@ -0,0 +1,101 @@
+
+DEAL::2576.00
+DEAL::1592.00
+DEAL::2200.00
+DEAL::1208.00
+DEAL::1824.00
+DEAL::1224.00
+DEAL::1848.00
+DEAL::1240.00
+DEAL::1872.00
+DEAL::1256.00
+DEAL::1896.00
+DEAL::1272.00
+DEAL::1920.00
+DEAL::1288.00
+DEAL::1944.00
+DEAL::1304.00
+DEAL::1968.00
+DEAL::1320.00
+DEAL::1992.00
+DEAL::1336.00
+DEAL::2016.00
+DEAL::1352.00
+DEAL::2040.00
+DEAL::1368.00
+DEAL::2064.00
+DEAL::1384.00
+DEAL::2088.00
+DEAL::1400.00
+DEAL::2112.00
+DEAL::1416.00
+DEAL::2136.00
+DEAL::1432.00
+DEAL::2160.00
+DEAL::1448.00
+DEAL::2184.00
+DEAL::1464.00
+DEAL::2208.00
+DEAL::1480.00
+DEAL::2232.00
+DEAL::1496.00
+DEAL::2256.00
+DEAL::1512.00
+DEAL::2280.00
+DEAL::1528.00
+DEAL::2304.00
+DEAL::1544.00
+DEAL::2328.00
+DEAL::1560.00
+DEAL::2352.00
+DEAL::1576.00
+DEAL::2376.00
+DEAL::1592.00
+DEAL::2400.00
+DEAL::1608.00
+DEAL::2424.00
+DEAL::1624.00
+DEAL::2448.00
+DEAL::1640.00
+DEAL::2472.00
+DEAL::1656.00
+DEAL::2496.00
+DEAL::1672.00
+DEAL::2520.00
+DEAL::1688.00
+DEAL::2544.00
+DEAL::1704.00
+DEAL::2568.00
+DEAL::1720.00
+DEAL::2592.00
+DEAL::1736.00
+DEAL::2616.00
+DEAL::1752.00
+DEAL::2640.00
+DEAL::1768.00
+DEAL::2664.00
+DEAL::1784.00
+DEAL::2688.00
+DEAL::1800.00
+DEAL::2712.00
+DEAL::1816.00
+DEAL::2736.00
+DEAL::1832.00
+DEAL::2760.00
+DEAL::1848.00
+DEAL::2784.00
+DEAL::1864.00
+DEAL::2808.00
+DEAL::1880.00
+DEAL::2832.00
+DEAL::1896.00
+DEAL::2856.00
+DEAL::1912.00
+DEAL::2880.00
+DEAL::1928.00
+DEAL::2904.00
+DEAL::1944.00
+DEAL::2928.00
+DEAL::1960.00
+DEAL::2952.00
+DEAL::1976.00
diff --git a/tests/base/work_stream_05_graph.cc b/tests/base/work_stream_05_graph.cc
new file mode 100644 (file)
index 0000000..c0c88a2
--- /dev/null
@@ -0,0 +1,115 @@
+// ---------------------------------------------------------------------
+// $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.
+//
+// ---------------------------------------------------------------------
+
+
+// like _05, but with graph coloring
+
+#include "../tests.h"
+#include <iomanip>
+#include <iomanip>
+#include <fstream>
+#include <cmath>
+
+#include <deal.II/base/work_stream.h>
+#include <deal.II/lac/vector.h>
+
+
+Vector<double> result(100);
+
+
+struct ScratchData
+{};
+
+
+struct CopyData
+{
+  unsigned int computed;
+};
+
+
+void worker (const std::vector<unsigned int>::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<types::global_dof_index>
+conflictor (const std::vector<unsigned int>::iterator &i)
+{
+  std::vector<types::global_dof_index> 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<unsigned int> 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<std::vector<types::global_dof_index>
+                                                                          (const std::vector<unsigned int>::iterator &)>
+                                                      (&conflictor)),
+                  &worker, &copier,
+                   ScratchData(),
+                   CopyData());
+
+  // now simulate what we should have gotten
+  Vector<double> comp(result.size());
+  for (unsigned int i=0; i<v.size(); ++i)
+    {
+      const unsigned int ad_computed = v[i] * 2;
+      for (unsigned int j=0; j<5; ++j)
+       comp((ad_computed+j) % result.size()) += ad_computed;
+    }
+
+
+  // and compare
+  for (unsigned int i=0; i<result.size(); ++i)
+    Assert (result(i) == comp(i), ExcInternalError());
+
+  for (unsigned int i=0; i<result.size(); ++i)
+    deallog << result(i) << std::endl;
+}
+
+
+
+
+int main()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  test ();
+}
diff --git a/tests/base/work_stream_05_graph.output b/tests/base/work_stream_05_graph.output
new file mode 100644 (file)
index 0000000..728770d
--- /dev/null
@@ -0,0 +1,101 @@
+
+DEAL::2576.00
+DEAL::1592.00
+DEAL::2200.00
+DEAL::1208.00
+DEAL::1824.00
+DEAL::1224.00
+DEAL::1848.00
+DEAL::1240.00
+DEAL::1872.00
+DEAL::1256.00
+DEAL::1896.00
+DEAL::1272.00
+DEAL::1920.00
+DEAL::1288.00
+DEAL::1944.00
+DEAL::1304.00
+DEAL::1968.00
+DEAL::1320.00
+DEAL::1992.00
+DEAL::1336.00
+DEAL::2016.00
+DEAL::1352.00
+DEAL::2040.00
+DEAL::1368.00
+DEAL::2064.00
+DEAL::1384.00
+DEAL::2088.00
+DEAL::1400.00
+DEAL::2112.00
+DEAL::1416.00
+DEAL::2136.00
+DEAL::1432.00
+DEAL::2160.00
+DEAL::1448.00
+DEAL::2184.00
+DEAL::1464.00
+DEAL::2208.00
+DEAL::1480.00
+DEAL::2232.00
+DEAL::1496.00
+DEAL::2256.00
+DEAL::1512.00
+DEAL::2280.00
+DEAL::1528.00
+DEAL::2304.00
+DEAL::1544.00
+DEAL::2328.00
+DEAL::1560.00
+DEAL::2352.00
+DEAL::1576.00
+DEAL::2376.00
+DEAL::1592.00
+DEAL::2400.00
+DEAL::1608.00
+DEAL::2424.00
+DEAL::1624.00
+DEAL::2448.00
+DEAL::1640.00
+DEAL::2472.00
+DEAL::1656.00
+DEAL::2496.00
+DEAL::1672.00
+DEAL::2520.00
+DEAL::1688.00
+DEAL::2544.00
+DEAL::1704.00
+DEAL::2568.00
+DEAL::1720.00
+DEAL::2592.00
+DEAL::1736.00
+DEAL::2616.00
+DEAL::1752.00
+DEAL::2640.00
+DEAL::1768.00
+DEAL::2664.00
+DEAL::1784.00
+DEAL::2688.00
+DEAL::1800.00
+DEAL::2712.00
+DEAL::1816.00
+DEAL::2736.00
+DEAL::1832.00
+DEAL::2760.00
+DEAL::1848.00
+DEAL::2784.00
+DEAL::1864.00
+DEAL::2808.00
+DEAL::1880.00
+DEAL::2832.00
+DEAL::1896.00
+DEAL::2856.00
+DEAL::1912.00
+DEAL::2880.00
+DEAL::1928.00
+DEAL::2904.00
+DEAL::1944.00
+DEAL::2928.00
+DEAL::1960.00
+DEAL::2952.00
+DEAL::1976.00

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.