]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Added VectorView class.
authorLuca Heltai <luca.heltai@sissa.it>
Sun, 5 Apr 2009 22:47:21 +0000 (22:47 +0000)
committerLuca Heltai <luca.heltai@sissa.it>
Sun, 5 Apr 2009 22:47:21 +0000 (22:47 +0000)
git-svn-id: https://svn.dealii.org/trunk@18553 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/lac/include/lac/vector.h
deal.II/lac/include/lac/vector_view.h [new file with mode: 0644]
deal.II/lac/source/vector_view.cc [new file with mode: 0644]
deal.II/lac/source/vector_view.inst.in [new file with mode: 0644]

index 866f63a7e52b8e2a3ead4c4e06bb4d6661d97540..14d0bcd89a450c6473757c9ce1ea3091da22caba 100644 (file)
@@ -593,6 +593,20 @@ inconvenience this causes.
 <h3>lac</h3>
 
 <ol>
+  <li>
+  <p>
+  New: There is now a new class VectorView<Number> that allows views of
+  arbitrary areas of memory to be seen as a Vector<Number>, simplifying 
+  a lot of works for portability issues between different libraries, and 
+  allowing for subviews of Vector<Number> classes without the need to 
+  copy back and forth large chunk of memories.
+  <br>
+  To be used with EXTREME caution, and only when you know exactly what you 
+  are doing, and speed is a necessity.
+  <br>
+  (Luca Heltai 2009/04/05)
+  </p>
+
   <li>
   <p>
   Updated: The local_to_global functions in ConstraintMatrix got smarter,
index 7dbab9911afd47727a9c3ae056f21479908c8351..3bac18a7e05331bc0042282850967832eb7c95b1 100644 (file)
@@ -49,6 +49,8 @@ template<typename number> class LAPACKFullMatrix;
 
 template <typename> class BlockVector;
 
+template <typename> class VectorView;
+
 
 
 /*! @addtogroup Vectors
@@ -890,6 +892,11 @@ class Vector : public Subscriptor
                                      * the data.
                                      */
     friend class LAPACKFullMatrix<Number>;
+                                    /*
+                                     * VectorView will access the
+                                     * pointer.
+                                     */
+    friend class VectorView<Number>;
 };
 
 /*@}*/
diff --git a/deal.II/lac/include/lac/vector_view.h b/deal.II/lac/include/lac/vector_view.h
new file mode 100644 (file)
index 0000000..20038ec
--- /dev/null
@@ -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 <base/config.h>
+#include <base/exceptions.h>
+#include <base/subscriptor.h>
+#include <lac/vector.h>
+
+#include <cstdio>
+
+DEAL_II_NAMESPACE_OPEN
+
+
+/*! @addtogroup Vectors
+ *@{
+ */
+
+/**
+ * View of a numerical vector of data. This class provides an
+ * interface compatible with the Vector<double> 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<Number>, 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:
+ *
+ <code>
+ // 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<double> class
+ VectorView<double> 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<double> 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<double> const_copy(view);
+ // Now this is the correct way to instantiate a constant view of the
+ // above vector:
+ const VectorView<double> correct_const_copy_view(5, const_copy.begin());
+ // While this will compile, BUT WILL NOT COMPLAIN if you try to write
+ // on it!
+ VectorView<double> 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";
+ </code>
+ *
+ *
+ * @note Instantiations for this template are provided for
+ * <tt>@<float@>, @<double@>, @<long double@>,
+ * @<std::complex@<float@>@>, @<std::complex@<double@>@>,
+ * @<std::complex@<long double@>@></tt>; others can be generated in
+ * application programs (see the section on @ref Instantiations in the
+ * manual).
+ * 
+ * @author Luca Heltai
+ */
+template<typename Number>
+class VectorView : public Vector<Number> {
+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<double>(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<typename Number>
+inline
+VectorView<Number>::VectorView(const unsigned int new_size, Number * ptr)
+{
+    this->vec_size     = new_size;
+    this->max_vec_size = new_size;
+    this->val          = ptr;
+}
+
+
+
+template<typename Number>
+inline
+VectorView<Number>::VectorView(const unsigned int new_size, const Number * ptr) 
+{
+    this->vec_size     = new_size;
+    this->max_vec_size = new_size;
+    this->val          = const_cast<Number*>(ptr);
+}
+
+
+
+template<typename Number>
+inline
+VectorView<Number>::~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 (file)
index 0000000..8fdd442
--- /dev/null
@@ -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 <lac/vector_view.h>
+
+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 (file)
index 0000000..3b64149
--- /dev/null
@@ -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<SCALAR>;
+  }
+
+for (SCALAR : COMPLEX_SCALARS)
+  {
+    template class VectorView<SCALAR>;
+  }

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.