Reference documentation for deal.II version 8.5.1
Public Types | Public Member Functions | List of all members
VectorView< Number > Class Template Reference

#include <deal.II/lac/vector_view.h>

Inheritance diagram for VectorView< Number >:
[legend]

Public Types

typedef types::global_dof_index size_type
 
- Public Types inherited from Vector< Number >
typedef Number value_type
 
typedef numbers::NumberTraits< Number >::real_type real_type
 

Public Member Functions

 VectorView (const size_type new_size, Number *ptr)
 
 VectorView (const size_type new_size, const Number *ptr)
 
 ~VectorView ()
 
virtual void reinit (const size_type N, const bool omit_zeroing_entries=false)
 
void reinit (const size_type N, Number *ptr)
 
void reinit (const size_type N, const Number *ptr)
 
virtual void swap (Vector< Number > &v)
 
- Public Member Functions inherited from Vector< Number >
 Vector ()
 
 Vector (const Vector< Number > &v)
 
 Vector (Vector< Number > &&v)
 
template<typename OtherNumber >
 Vector (const Vector< OtherNumber > &v)
 
 Vector (const PETScWrappers::Vector &v)
 
 Vector (const PETScWrappers::MPI::Vector &v)
 
 Vector (const TrilinosWrappers::MPI::Vector &v)
 
 Vector (const TrilinosWrappers::Vector &v)
 
 Vector (const size_type n)
 
template<typename InputIterator >
 Vector (const InputIterator first, const InputIterator last)
 
virtual ~Vector ()
 
void compress (::VectorOperation::values operation=::VectorOperation::unknown) const
 
template<typename Number2 >
void reinit (const Vector< Number2 > &V, const bool omit_zeroing_entries=false)
 
Vector< Number > & operator= (const Number s)
 
Vector< Number > & operator= (const Vector< Number > &v)
 
Vector< Number > & operator= (Vector< Number > &&v)
 
template<typename Number2 >
Vector< Number > & operator= (const Vector< Number2 > &v)
 
Vector< Number > & operator= (const BlockVector< Number > &v)
 
Vector< Number > & operator= (const PETScWrappers::Vector &v)
 
Vector< Number > & operator= (const PETScWrappers::MPI::Vector &v)
 
Vector< Number > & operator= (const TrilinosWrappers::MPI::Vector &v)
 
Vector< Number > & operator= (const TrilinosWrappers::Vector &v)
 
template<typename Number2 >
bool operator== (const Vector< Number2 > &v) const
 
template<typename Number2 >
bool operator!= (const Vector< Number2 > &v) const
 
template<typename Number2 >
Number operator* (const Vector< Number2 > &V) const
 
real_type norm_sqr () const
 
Number mean_value () const
 
real_type l1_norm () const
 
real_type l2_norm () const
 
real_type lp_norm (const real_type p) const
 
real_type linfty_norm () const
 
Number add_and_dot (const Number a, const Vector< Number > &V, const Vector< Number > &W)
 
iterator begin ()
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 
Number operator() (const size_type i) const
 
Number & operator() (const size_type i)
 
Number operator[] (const size_type i) const
 
Number & operator[] (const size_type i)
 
template<typename OtherNumber >
void extract_subvector_to (const std::vector< size_type > &indices, std::vector< OtherNumber > &values) const
 
template<typename ForwardIterator , typename OutputIterator >
void extract_subvector_to (ForwardIterator indices_begin, const ForwardIterator indices_end, OutputIterator values_begin) const
 
Vector< Number > & operator+= (const Vector< Number > &V)
 
Vector< Number > & operator-= (const Vector< Number > &V)
 
template<typename OtherNumber >
void add (const std::vector< size_type > &indices, const std::vector< OtherNumber > &values)
 
template<typename OtherNumber >
void add (const std::vector< size_type > &indices, const Vector< OtherNumber > &values)
 
template<typename OtherNumber >
void add (const size_type n_elements, const size_type *indices, const OtherNumber *values)
 
void add (const Number s)
 
void add (const Vector< Number > &V) 1
 
void add (const Number a, const Vector< Number > &V, const Number b, const Vector< Number > &W)
 
void add (const Number a, const Vector< Number > &V)
 
void sadd (const Number s, const Vector< Number > &V)
 
void sadd (const Number s, const Number a, const Vector< Number > &V)
 
void sadd (const Number s, const Number a, const Vector< Number > &V, const Number b, const Vector< Number > &W) 1
 
void sadd (const Number s, const Number a, const Vector< Number > &V, const Number b, const Vector< Number > &W, const Number c, const Vector< Number > &X) 1
 
Vector< Number > & operator*= (const Number factor)
 
Vector< Number > & operator/= (const Number factor)
 
void scale (const Vector< Number > &scaling_factors)
 
template<typename Number2 >
void scale (const Vector< Number2 > &scaling_factors)
 
void equ (const Number a, const Vector< Number > &u)
 
template<typename Number2 >
void equ (const Number a, const Vector< Number2 > &u)
 
void equ (const Number a, const Vector< Number > &u, const Number b, const Vector< Number > &v) 1
 
void equ (const Number a, const Vector< Number > &u, const Number b, const Vector< Number > &v, const Number c, const Vector< Number > &w) 1
 
void ratio (const Vector< Number > &a, const Vector< Number > &b) 1
 
void update_ghost_values () const
 
void print (const char *format=0) const 1
 
void print (std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
 
void print (LogStream &out, const unsigned int width=6, const bool across=true) const 1
 
void block_write (std::ostream &out) const
 
void block_read (std::istream &in)
 
template<class Archive >
void save (Archive &ar, const unsigned int version) const
 
template<class Archive >
void load (Archive &ar, const unsigned int version)
 
bool in_local_range (const size_type global_index) const
 
IndexSet locally_owned_elements () const
 
std::size_t size () const
 
bool all_zero () const
 
bool is_non_negative () const
 
std::size_t memory_consumption () const
 
- Public Member Functions inherited from Subscriptor
 Subscriptor ()
 
 Subscriptor (const Subscriptor &)
 
 Subscriptor (Subscriptor &&)
 
virtual ~Subscriptor ()
 
Subscriptoroperator= (const Subscriptor &)
 
Subscriptoroperator= (Subscriptor &&)
 
void subscribe (const char *identifier=0) const
 
void unsubscribe (const char *identifier=0) const
 
unsigned int n_subscriptions () const
 
void list_subscribers () const
 
template<class Archive >
void serialize (Archive &ar, const unsigned int version)
 

Additional Inherited Members

- Static Public Member Functions inherited from Subscriptor
static ::ExceptionBaseExcInUse (int arg1, char *arg2, std::string &arg3)
 
static ::ExceptionBaseExcNoSubscriber (char *arg1, char *arg2)
 
- Static Public Attributes inherited from Vector< Number >
static const bool supports_distributed_data = false
 
- Protected Attributes inherited from Vector< Number >
size_type vec_size
 
size_type max_vec_size
 
Number * val
 
std_cxx11::shared_ptr< parallel::internal::TBBPartitioner > thread_loop_partitioner
 

Detailed Description

template<typename Number>
class VectorView< Number >

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.

You should consider using the VectorView object ONLY when ALL of the following requirements are met:

  1. Your application requires a Vector<Number> object.
  2. All you have at your disposal is a Number* pointer.
  3. You are ABSOLUTELY SURE that the above pointer points to a valid area of memory of the correct size.
  4. You really believe that making a copy of that memory would be too expensive.
  5. You really know what you are doing.

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. Once again, 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, and you are allowed to use this class on objects of type const Number*. However you should be aware of the fact that the constness of the array pointed to is ignored, which means that you should only use the const constructor when the actual object you are constructing is itself a constant object. As a corollary, you will be allowed to call even functions of the base class that change data of the array; this being a violation of the C++ type model, you should make sure that this only happens if it is really valid and, in general, if you know what you are doing.

Since this class does not own the memory that you are accessing, you have to make sure that the lifetime 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<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 exception:
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 segfault 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";
Note
Instantiations for this template are provided for <float>, <double>, <std::complex<float>>, <std::complex<double>>; others can be generated in application programs (see the section on Template instantiations in the manual).
Author
Luca Heltai, 2009

Definition at line 71 of file vector.h.

Member Typedef Documentation

◆ size_type

template<typename Number>
typedef types::global_dof_index VectorView< Number >::size_type

Declare type for container size.

Definition at line 138 of file vector_view.h.

Constructor & Destructor Documentation

◆ VectorView() [1/2]

template<typename Number>
VectorView< Number >::VectorView ( const size_type  new_size,
Number *  ptr 
)

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 ptr.

◆ VectorView() [2/2]

template<typename Number>
VectorView< Number >::VectorView ( const size_type  new_size,
const 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()

template<typename Number>
VectorView< Number >::~VectorView ( )

This destructor will only reset the internal sizes and the internal pointers, but it will NOT clear the memory.

Member Function Documentation

◆ reinit() [1/3]

template<typename Number>
virtual void VectorView< Number >::reinit ( const size_type  N,
const bool  omit_zeroing_entries = false 
)
virtual

The reinit function of this object has a behavior which is different from the one of the base class. VectorView does not handle memory, and you should not attempt to resize the memory that is pointed to by this object using the reinit function. You can, however, resize the view that you have of the original object. Notice that it is your own responsibility to ensure that the memory you are pointing to is big enough.

Similarly to what happens in the base class, if 'omit_zeroing_entries' is false, then the entire content of the vector is set to 0, otherwise the content of the memory is left unchanged.

Notice that the following snippet of code may not produce what you expect:

// Create a vector of length 1.
Vector<double> long_vector(1);
// Make a view of it
VectorView<double> view_of_long_vector(1, long_vector.begin());
// Resize the original vector to a bigger size
long_vector.reinit(100);
// And the view, leaving the memory untouched
view_of_long_vector.reinit(100, true);

In the above case, the Vector<double>::reinit method is called, and a NEW area of memory is reserved, possibly not starting at the same place as before. However, the VectorView<double> object keeps pointing to the same old area. After the two reinits, any call to view_of_long_vector(i), with i>0 might cause an attempt to access invalid areas of memory, or might function properly, depending on whether or not the system was able to allocate some memory consecutively after the original allocation.

In any case, you should not rely on this behavior, and you should only call this reinit function if you really know what you are doing.

Reimplemented from Vector< Number >.

◆ reinit() [2/3]

template<typename Number>
void VectorView< Number >::reinit ( const size_type  N,
Number *  ptr 
)

This reinit function is equivalent to constructing a new object with the given size, starting from the pointer ptr.

◆ reinit() [3/3]

template<typename Number>
void VectorView< Number >::reinit ( const size_type  N,
const Number *  ptr 
)

This reinit function is equivalent to constructing a new object with the given size, starting from the pointer ptr. The same considerations made for the constructor apply here.

◆ swap()

template<typename Number>
virtual void VectorView< Number >::swap ( Vector< Number > &  v)
virtual

This function is here to prevent memory corruption. It should never be called, and will throw an exception if you try to do so.

Reimplemented from Vector< Number >.


The documentation for this class was generated from the following files: