]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Make Tensors trivially copyable. 3805/head
authorDavid Wells <wellsd2@rpi.edu>
Sun, 15 Jan 2017 23:08:07 +0000 (18:08 -0500)
committerDavid Wells <wellsd2@rpi.edu>
Mon, 16 Jan 2017 05:20:00 +0000 (00:20 -0500)
The implicitly defined copy constructors and assignment operators are no
different from the ones implemented prior to this patch, so we can clean things
up a bit by just using the defaults here.

This has the nice effect of making the class "trivially copyable", which means
compilers may optimize things by using memcpy to copy things instead of the copy
constructor.

include/deal.II/base/tensor.h
tests/base/tensor_trivial_copy.cc [new file with mode: 0644]
tests/base/tensor_trivial_copy.output [new file with mode: 0644]

index f995fc2f3a8d675aaa1233074bd9ba5ca9b0ab27..5ceb89556d7e7d0931b30340657cded5226877f4 100644 (file)
@@ -132,11 +132,6 @@ public:
    */
   Tensor ();
 
-  /**
-   * Copy constructor.
-   */
-  Tensor (const Tensor<0,dim,Number> &initializer);
-
   /**
    * Constructor from tensors with different underlying scalar type. This
    * obviously requires that the @p OtherNumber type is convertible to @p
@@ -168,11 +163,6 @@ public:
    */
   operator const Number &() const;
 
-  /**
-   * Copy assignment operator.
-   */
-  Tensor<0,dim,Number> &operator = (const Tensor<0,dim,Number> &rhs);
-
   /**
    * Assignment from tensors with different underlying scalar type. This
    * obviously requires that the @p OtherNumber type is convertible to @p
@@ -371,11 +361,6 @@ public:
    */
   Tensor ();
 
-  /**
-   * Copy constructor.
-   */
-  Tensor (const Tensor<rank_,dim,Number> &initializer);
-
   /**
    * Constructor, where the data is copied from a C-style array.
    */
@@ -421,11 +406,6 @@ public:
    */
   Number &operator [] (const TableIndices<rank_> &indices);
 
-  /**
-   * Copy assignment operator.
-   */
-  Tensor &operator = (const Tensor<rank_,dim,Number> &rhs);
-
   /**
    * Assignment operator from tensors with different underlying scalar type.
    * This obviously requires that the @p OtherNumber type is convertible to @p
@@ -595,14 +575,6 @@ Tensor<0,dim,Number>::Tensor ()
 }
 
 
-template <int dim, typename Number>
-inline
-Tensor<0,dim,Number>::Tensor (const Tensor<0,dim,Number> &p)
-{
-  value = p.value;
-}
-
-
 template <int dim, typename Number>
 template <typename OtherNumber>
 inline
@@ -639,15 +611,6 @@ Tensor<0,dim,Number>::operator const Number &() const
 }
 
 
-template <int dim, typename Number>
-inline
-Tensor<0,dim,Number> &Tensor<0,dim,Number>::operator = (const Tensor<0,dim,Number> &p)
-{
-  value = p.value;
-  return *this;
-}
-
-
 template <int dim, typename Number>
 template <typename OtherNumber>
 inline
@@ -786,15 +749,6 @@ Tensor<rank_,dim,Number>::Tensor ()
 }
 
 
-template <int rank_, int dim, typename Number>
-inline
-Tensor<rank_,dim,Number>::Tensor (const Tensor<rank_,dim,Number> &initializer)
-{
-  if (dim > 0)
-    std::copy (&initializer[0], &initializer[0]+dim, &values[0]);
-}
-
-
 template <int rank_, int dim, typename Number>
 inline
 Tensor<rank_,dim,Number>::Tensor (const array_type &initializer)
@@ -906,17 +860,6 @@ Tensor<rank_,dim,Number>::operator[] (const TableIndices<rank_> &indices)
 }
 
 
-template <int rank_, int dim, typename Number>
-inline
-Tensor<rank_,dim,Number> &
-Tensor<rank_,dim,Number>::operator = (const Tensor<rank_,dim,Number> &t)
-{
-  if (dim > 0)
-    std::copy (&t.values[0], &t.values[0]+dim, &values[0]);
-  return *this;
-}
-
-
 template <int rank_, int dim, typename Number>
 template <typename OtherNumber>
 inline
diff --git a/tests/base/tensor_trivial_copy.cc b/tests/base/tensor_trivial_copy.cc
new file mode 100644 (file)
index 0000000..0cc106d
--- /dev/null
@@ -0,0 +1,93 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2017 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.
+//
+// ---------------------------------------------------------------------
+
+
+// Verify that Tensor is trivially copyable.
+
+// TODO not all compilers that support enough of a subset of C++11 to compile
+// the library (notably GCC 4.8) implement std::is_trivally_copyable. At some
+// point in the future we should use that instead of the boost equivalent.
+
+#include <deal.II/base/tensor.h>
+
+#include <boost/type_traits.hpp>
+
+#include <complex>
+
+#include "../tests.h"
+
+template <typename Number>
+void test()
+{
+  deallog << "Tensor<0, 1> is trivially copyable: "
+          << boost::has_trivial_copy<Tensor<0, 1, Number> >::value
+          << std::endl;
+  deallog << "Tensor<0, 2> is trivially copyable: "
+          << boost::has_trivial_copy<Tensor<0, 2, Number> >::value
+          << std::endl;
+
+  deallog << "Tensor<1, 1> is trivially copyable: "
+          << boost::has_trivial_copy<Tensor<1, 1, Number> >::value
+          << std::endl;
+  deallog << "Tensor<1, 2> is trivially copyable: "
+          << boost::has_trivial_copy<Tensor<1, 2, Number> >::value
+          << std::endl;
+  deallog << "Tensor<1, 3> is trivially copyable: "
+          << boost::has_trivial_copy<Tensor<1, 3, Number> >::value
+          << std::endl;
+
+  deallog << "Tensor<2, 1> is trivially copyable: "
+          << boost::has_trivial_copy<Tensor<2, 1, Number> >::value
+          << std::endl;
+  deallog << "Tensor<2, 2> is trivially copyable: "
+          << boost::has_trivial_copy<Tensor<2, 2, Number> >::value
+          << std::endl;
+  deallog << "Tensor<2, 3> is trivially copyable: "
+          << boost::has_trivial_copy<Tensor<2, 3, Number> >::value
+          << std::endl;
+
+  deallog << "Tensor<3, 1> is trivially copyable: "
+          << boost::has_trivial_copy<Tensor<3, 1, Number> >::value
+          << std::endl;
+  deallog << "Tensor<3, 2> is trivially copyable: "
+          << boost::has_trivial_copy<Tensor<3, 2, Number> >::value
+          << std::endl;
+  deallog << "Tensor<3, 3> is trivially copyable: "
+          << boost::has_trivial_copy<Tensor<3, 3, Number> >::value
+          << std::endl;
+}
+
+int main()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+
+  deallog << std::boolalpha;
+  deallog << "testing float"
+          << std::endl;
+  test<float>();
+
+  deallog << "testing double"
+          << std::endl;
+  test<double>();
+
+  deallog << "testing std::complex<float>"
+          << std::endl;
+  test<std::complex<float> >();
+
+  deallog << "testing std::complex<double>"
+          << std::endl;
+  test<std::complex<double> >();
+}
diff --git a/tests/base/tensor_trivial_copy.output b/tests/base/tensor_trivial_copy.output
new file mode 100644 (file)
index 0000000..7f84876
--- /dev/null
@@ -0,0 +1,49 @@
+
+DEAL::testing float
+DEAL::Tensor<0, 1> is trivially copyable: true
+DEAL::Tensor<0, 2> is trivially copyable: true
+DEAL::Tensor<1, 1> is trivially copyable: true
+DEAL::Tensor<1, 2> is trivially copyable: true
+DEAL::Tensor<1, 3> is trivially copyable: true
+DEAL::Tensor<2, 1> is trivially copyable: true
+DEAL::Tensor<2, 2> is trivially copyable: true
+DEAL::Tensor<2, 3> is trivially copyable: true
+DEAL::Tensor<3, 1> is trivially copyable: true
+DEAL::Tensor<3, 2> is trivially copyable: true
+DEAL::Tensor<3, 3> is trivially copyable: true
+DEAL::testing double
+DEAL::Tensor<0, 1> is trivially copyable: true
+DEAL::Tensor<0, 2> is trivially copyable: true
+DEAL::Tensor<1, 1> is trivially copyable: true
+DEAL::Tensor<1, 2> is trivially copyable: true
+DEAL::Tensor<1, 3> is trivially copyable: true
+DEAL::Tensor<2, 1> is trivially copyable: true
+DEAL::Tensor<2, 2> is trivially copyable: true
+DEAL::Tensor<2, 3> is trivially copyable: true
+DEAL::Tensor<3, 1> is trivially copyable: true
+DEAL::Tensor<3, 2> is trivially copyable: true
+DEAL::Tensor<3, 3> is trivially copyable: true
+DEAL::testing std::complex<float>
+DEAL::Tensor<0, 1> is trivially copyable: true
+DEAL::Tensor<0, 2> is trivially copyable: true
+DEAL::Tensor<1, 1> is trivially copyable: true
+DEAL::Tensor<1, 2> is trivially copyable: true
+DEAL::Tensor<1, 3> is trivially copyable: true
+DEAL::Tensor<2, 1> is trivially copyable: true
+DEAL::Tensor<2, 2> is trivially copyable: true
+DEAL::Tensor<2, 3> is trivially copyable: true
+DEAL::Tensor<3, 1> is trivially copyable: true
+DEAL::Tensor<3, 2> is trivially copyable: true
+DEAL::Tensor<3, 3> is trivially copyable: true
+DEAL::testing std::complex<double>
+DEAL::Tensor<0, 1> is trivially copyable: true
+DEAL::Tensor<0, 2> is trivially copyable: true
+DEAL::Tensor<1, 1> is trivially copyable: true
+DEAL::Tensor<1, 2> is trivially copyable: true
+DEAL::Tensor<1, 3> is trivially copyable: true
+DEAL::Tensor<2, 1> is trivially copyable: true
+DEAL::Tensor<2, 2> is trivially copyable: true
+DEAL::Tensor<2, 3> is trivially copyable: true
+DEAL::Tensor<3, 1> is trivially copyable: true
+DEAL::Tensor<3, 2> is trivially copyable: true
+DEAL::Tensor<3, 3> is trivially copyable: true

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.