From 0004f1cbcd251780dca5db668a638a1fed71816a Mon Sep 17 00:00:00 2001 From: David Wells Date: Sun, 15 Jan 2017 18:08:07 -0500 Subject: [PATCH] Make Tensors trivially copyable. 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 | 57 ---------------- tests/base/tensor_trivial_copy.cc | 93 +++++++++++++++++++++++++++ tests/base/tensor_trivial_copy.output | 49 ++++++++++++++ 3 files changed, 142 insertions(+), 57 deletions(-) create mode 100644 tests/base/tensor_trivial_copy.cc create mode 100644 tests/base/tensor_trivial_copy.output diff --git a/include/deal.II/base/tensor.h b/include/deal.II/base/tensor.h index f995fc2f3a..5ceb89556d 100644 --- a/include/deal.II/base/tensor.h +++ b/include/deal.II/base/tensor.h @@ -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 &initializer); - /** * Constructor, where the data is copied from a C-style array. */ @@ -421,11 +406,6 @@ public: */ Number &operator [] (const TableIndices &indices); - /** - * Copy assignment operator. - */ - Tensor &operator = (const Tensor &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 -inline -Tensor<0,dim,Number>::Tensor (const Tensor<0,dim,Number> &p) -{ - value = p.value; -} - - template template inline @@ -639,15 +611,6 @@ Tensor<0,dim,Number>::operator const Number &() const } -template -inline -Tensor<0,dim,Number> &Tensor<0,dim,Number>::operator = (const Tensor<0,dim,Number> &p) -{ - value = p.value; - return *this; -} - - template template inline @@ -786,15 +749,6 @@ Tensor::Tensor () } -template -inline -Tensor::Tensor (const Tensor &initializer) -{ - if (dim > 0) - std::copy (&initializer[0], &initializer[0]+dim, &values[0]); -} - - template inline Tensor::Tensor (const array_type &initializer) @@ -906,17 +860,6 @@ Tensor::operator[] (const TableIndices &indices) } -template -inline -Tensor & -Tensor::operator = (const Tensor &t) -{ - if (dim > 0) - std::copy (&t.values[0], &t.values[0]+dim, &values[0]); - return *this; -} - - template template inline diff --git a/tests/base/tensor_trivial_copy.cc b/tests/base/tensor_trivial_copy.cc new file mode 100644 index 0000000000..0cc106d873 --- /dev/null +++ b/tests/base/tensor_trivial_copy.cc @@ -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 + +#include + +#include + +#include "../tests.h" + +template +void test() +{ + deallog << "Tensor<0, 1> is trivially copyable: " + << boost::has_trivial_copy >::value + << std::endl; + deallog << "Tensor<0, 2> is trivially copyable: " + << boost::has_trivial_copy >::value + << std::endl; + + deallog << "Tensor<1, 1> is trivially copyable: " + << boost::has_trivial_copy >::value + << std::endl; + deallog << "Tensor<1, 2> is trivially copyable: " + << boost::has_trivial_copy >::value + << std::endl; + deallog << "Tensor<1, 3> is trivially copyable: " + << boost::has_trivial_copy >::value + << std::endl; + + deallog << "Tensor<2, 1> is trivially copyable: " + << boost::has_trivial_copy >::value + << std::endl; + deallog << "Tensor<2, 2> is trivially copyable: " + << boost::has_trivial_copy >::value + << std::endl; + deallog << "Tensor<2, 3> is trivially copyable: " + << boost::has_trivial_copy >::value + << std::endl; + + deallog << "Tensor<3, 1> is trivially copyable: " + << boost::has_trivial_copy >::value + << std::endl; + deallog << "Tensor<3, 2> is trivially copyable: " + << boost::has_trivial_copy >::value + << std::endl; + deallog << "Tensor<3, 3> is trivially copyable: " + << boost::has_trivial_copy >::value + << std::endl; +} + +int main() +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + + deallog << std::boolalpha; + deallog << "testing float" + << std::endl; + test(); + + deallog << "testing double" + << std::endl; + test(); + + deallog << "testing std::complex" + << std::endl; + test >(); + + deallog << "testing std::complex" + << std::endl; + test >(); +} diff --git a/tests/base/tensor_trivial_copy.output b/tests/base/tensor_trivial_copy.output new file mode 100644 index 0000000000..7f848769c4 --- /dev/null +++ b/tests/base/tensor_trivial_copy.output @@ -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 +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 +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 -- 2.39.5