From fcdf1d12b2014dfb424ede6c6406eb0d9877f5b6 Mon Sep 17 00:00:00 2001 From: heltai Date: Sun, 5 Apr 2009 22:47:21 +0000 Subject: [PATCH] Added VectorView class. git-svn-id: https://svn.dealii.org/trunk@18553 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 14 ++ deal.II/lac/include/lac/vector.h | 7 + deal.II/lac/include/lac/vector_view.h | 180 +++++++++++++++++++++++++ deal.II/lac/source/vector_view.cc | 21 +++ deal.II/lac/source/vector_view.inst.in | 23 ++++ 5 files changed, 245 insertions(+) create mode 100644 deal.II/lac/include/lac/vector_view.h create mode 100644 deal.II/lac/source/vector_view.cc create mode 100644 deal.II/lac/source/vector_view.inst.in diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 866f63a7e5..14d0bcd89a 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -593,6 +593,20 @@ inconvenience this causes.

lac

    +
  1. +

    + New: There is now a new class VectorView that allows views of + arbitrary areas of memory to be seen as a Vector, simplifying + a lot of works for portability issues between different libraries, and + allowing for subviews of Vector classes without the need to + copy back and forth large chunk of memories. +
    + To be used with EXTREME caution, and only when you know exactly what you + are doing, and speed is a necessity. +
    + (Luca Heltai 2009/04/05) +

    +
  2. Updated: The local_to_global functions in ConstraintMatrix got smarter, diff --git a/deal.II/lac/include/lac/vector.h b/deal.II/lac/include/lac/vector.h index 7dbab9911a..3bac18a7e0 100644 --- a/deal.II/lac/include/lac/vector.h +++ b/deal.II/lac/include/lac/vector.h @@ -49,6 +49,8 @@ template class LAPACKFullMatrix; template class BlockVector; +template class VectorView; + /*! @addtogroup Vectors @@ -890,6 +892,11 @@ class Vector : public Subscriptor * the data. */ friend class LAPACKFullMatrix; + /* + * VectorView will access the + * pointer. + */ + friend class VectorView; }; /*@}*/ diff --git a/deal.II/lac/include/lac/vector_view.h b/deal.II/lac/include/lac/vector_view.h new file mode 100644 index 0000000000..20038ec37f --- /dev/null +++ b/deal.II/lac/include/lac/vector_view.h @@ -0,0 +1,180 @@ +//--------------------------------------------------------------------------- +// $Id: vector.h 16829 2008-09-15 18:11:22Z heltai $ +// Version: $Name$ +// +// Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 __dealii__vector_view_h +#define __dealii__vector_view_h + + +#include +#include +#include +#include + +#include + +DEAL_II_NAMESPACE_OPEN + + +/*! @addtogroup Vectors + *@{ + */ + +/** + * View of a numerical vector of data. This class provides an + * interface compatible with the Vector class (from which it + * is inherited), that allows fast access to locations of memory + * already allocated with arrays of type Number. + * + * This is in the same style of the vector view in the Trilinos + * library. + * + * Notice that NO CHECKS are performed on the actual memory, and if + * you try to access illegal areas of memory, your computer will + * suffer from it. Use this class ONLY if you know exactly what you + * are doing. Two constructors are provided. One for read-write + * access, and one for read only access. + * + * You are allowed to use this class on objects of type const + * Vector, however you should be aware of the fact that the + * constness of pointed to array is casted away, which means that you + * should only use the const constructor when the actual object you + * are constructing is itself a constant object. + * + * You WILL be allowed to change the vector afterwards, so make sure + * you know what you are doing, before you change data without + * realizing. + * + * Since this class does not own the memory that you are accessing, + * you have to make sure that the life of the section of memory you + * are viewing is longer than this object. No attempt is made to + * ensure that this is the case. + * + * An example usage of this class is the following: + * + + // Create an array of length 5; + double * array = new double[5]; + // Now create a view of the above array that is compatible with the + // Vector class + VectorView view(5, array); + + view(1) = 4; + + // The following line should output 4. + cout << array[1] << endl; + + // If debug mode is on, then the following triggers an execption: + view(6) = 4; + + // But notice that no checks are performed, so this is legal but WILL + // NOT work + VectorView wrong_view(10, array); + + // Now no assert will be thrown if you type wrong_view(6), but most + // likely a seg fault will occur. + view(6) = 4; + + // Notice that this construction is legal. It will create a copy of + // the array. + const Vector const_copy(view); + + // Now this is the correct way to instantiate a constant view of the + // above vector: + const VectorView correct_const_copy_view(5, const_copy.begin()); + + // While this will compile, BUT WILL NOT COMPLAIN if you try to write + // on it! + VectorView wrong_const_copy_view(5, const_copy.begin()); + + // Now writing to elements of wrong_const_copy_view is allowed, and + // will change the same memory as the const_copy object. + wrong_const_copy_view(1) = 5; + + if(copy_view(1) == wrong_const_copy_view(1)) cout << "Tautology"; + + + * + * + * @note Instantiations for this template are provided for + * @, @, @, + * @@>, @@>, + * @@>; others can be generated in + * application programs (see the section on @ref Instantiations in the + * manual). + * + * @author Luca Heltai + */ +template +class VectorView : public Vector { +public: + /** Read write constructor. Takes the size of the vector, just + * like the standard one, but the data is picked starting from the + * location of the pointer @p ptr. + */ + VectorView(const unsigned int new_size, Number *ptr); + + /** The constant constructor is the same as above, however you + * will not be able to access the data for write access. + * + * You should only use this class by constructing it as a const + * VectorView(size, ptr) object. + * + * Undefined behavior will occur if you construct it as a non + * const object or attempt to write on it. + */ + VectorView(const unsigned int new_size, const Number *ptr); + + /** This desctructor will only reset the internal sizes and the + * interanl pointers, but it will NOT clear the memory. */ + ~VectorView(); +}; + + + +/*@}*/ +/*----------------------- Inline functions ----------------------------------*/ + + + +template +inline +VectorView::VectorView(const unsigned int new_size, Number * ptr) +{ + this->vec_size = new_size; + this->max_vec_size = new_size; + this->val = ptr; +} + + + +template +inline +VectorView::VectorView(const unsigned int new_size, const Number * ptr) +{ + this->vec_size = new_size; + this->max_vec_size = new_size; + this->val = const_cast(ptr); +} + + + +template +inline +VectorView::~VectorView() { + this->vec_size = 0; + this->max_vec_size = 0; + this->val = 0; +} + +DEAL_II_NAMESPACE_CLOSE + +#endif diff --git a/deal.II/lac/source/vector_view.cc b/deal.II/lac/source/vector_view.cc new file mode 100644 index 0000000000..8fdd442420 --- /dev/null +++ b/deal.II/lac/source/vector_view.cc @@ -0,0 +1,21 @@ +//--------------------------------------------------------------------------- +// $Id: vector.cc 15630 2008-01-17 22:29:39Z heltai $ +// Version: $Name$ +// +// Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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. +// +//--------------------------------------------------------------------------- + + +#include + +DEAL_II_NAMESPACE_OPEN + +#include "vector_view.inst" + +DEAL_II_NAMESPACE_CLOSE diff --git a/deal.II/lac/source/vector_view.inst.in b/deal.II/lac/source/vector_view.inst.in new file mode 100644 index 0000000000..3b6414981d --- /dev/null +++ b/deal.II/lac/source/vector_view.inst.in @@ -0,0 +1,23 @@ +//--------------------------------------------------------------------------- +// $Id: vector.cc 15443 2007-11-05 03:43:52Z bangerth $ +// Version: $Name$ +// +// Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 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. +// +//--------------------------------------------------------------------------- + + +for (SCALAR : REAL_SCALARS) + { + template class VectorView; + } + +for (SCALAR : COMPLEX_SCALARS) + { + template class VectorView; + } -- 2.39.5