Reference documentation for deal.II version Git 2375602 2018-06-23 12:24:17 -0500
Public Member Functions | Static Public Member Functions | Private Member Functions | Friends | List of all members
LinearAlgebra::Vector< Number > Class Template Reference

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

Inheritance diagram for LinearAlgebra::Vector< Number >:
[legend]

Public Member Functions

 Vector ()=default
 
 Vector (const Vector< Number > &V)
 
 Vector (const size_type n)
 
template<typename InputIterator >
 Vector (const InputIterator first, const InputIterator last)
 
virtual void reinit (const size_type size, const bool omit_zeroing_entries=false) override
 
template<typename Number2 >
void reinit (const ReadWriteVector< Number2 > &in_vector, const bool omit_zeroing_entries=false)
 
virtual void reinit (const IndexSet &locally_stored_indices, const bool omit_zeroing_entries=false) override
 
virtual void reinit (const VectorSpaceVector< Number > &V, const bool omit_zeroing_entries=false) override
 
Vector< Number > & operator= (const Vector< Number > &in_vector)
 
template<typename Number2 >
Vector< Number > & operator= (const Vector< Number2 > &in_vector)
 
virtual Vector< Number > & operator= (const Number s) override
 
virtual Vector< Number > & operator*= (const Number factor) override
 
virtual Vector< Number > & operator/= (const Number factor) override
 
virtual Vector< Number > & operator+= (const VectorSpaceVector< Number > &V) override
 
virtual Vector< Number > & operator-= (const VectorSpaceVector< Number > &V) override
 
virtual Number operator* (const VectorSpaceVector< Number > &V) const override
 
virtual void import (const ReadWriteVector< Number > &V, VectorOperation::values operation, std::shared_ptr< const CommunicationPatternBase > communication_pattern=std::shared_ptr< const CommunicationPatternBase >()) override
 
virtual void add (const Number a) override
 
virtual void add (const Number a, const VectorSpaceVector< Number > &V) override
 
virtual void add (const Number a, const VectorSpaceVector< Number > &V, const Number b, const VectorSpaceVector< Number > &W) override
 
virtual void sadd (const Number s, const Number a, const VectorSpaceVector< Number > &V) override
 
virtual void scale (const VectorSpaceVector< Number > &scaling_factors) override
 
virtual void equ (const Number a, const VectorSpaceVector< Number > &V) override
 
virtual bool all_zero () const override
 
virtual value_type mean_value () const override
 
virtual VectorSpaceVector< Number >::real_type l1_norm () const override
 
virtual VectorSpaceVector< Number >::real_type l2_norm () const override
 
virtual VectorSpaceVector< Number >::real_type linfty_norm () const override
 
virtual Number add_and_dot (const Number a, const VectorSpaceVector< Number > &V, const VectorSpaceVector< Number > &W) override
 
virtual size_type size () const override
 
virtual ::IndexSet locally_owned_elements () const override
 
virtual void print (std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const override
 
void block_write (std::ostream &out) const
 
void block_read (std::istream &in)
 
virtual std::size_t memory_consumption () const override
 
- Public Member Functions inherited from LinearAlgebra::ReadWriteVector< Number >
 ReadWriteVector ()
 
 ReadWriteVector (const ReadWriteVector< Number > &in_vector)
 
 ReadWriteVector (const size_type size)
 
 ReadWriteVector (const IndexSet &locally_stored_indices)
 
 ~ReadWriteVector () override=default
 
template<typename Number2 >
void reinit (const ReadWriteVector< Number2 > &in_vector, const bool omit_zeroing_entries=false)
 
void reinit (const TrilinosWrappers::MPI::Vector &trilinos_vec)
 
template<typename Functor >
void apply (const Functor &func)
 
void swap (ReadWriteVector< Number > &v)
 
ReadWriteVector< Number > & operator= (const ReadWriteVector< Number > &in_vector)
 
template<typename Number2 >
ReadWriteVector< Number > & operator= (const ReadWriteVector< Number2 > &in_vector)
 
ReadWriteVector< Number > & operator= (const Number s)
 
void import (const distributed::Vector< Number > &vec, VectorOperation::values operation, const std::shared_ptr< const CommunicationPatternBase > &communication_pattern=std::shared_ptr< const CommunicationPatternBase >())
 
void import (const PETScWrappers::MPI::Vector &petsc_vec, VectorOperation::values operation, const std::shared_ptr< const CommunicationPatternBase > &communication_pattern=std::shared_ptr< const CommunicationPatternBase >())
 
void import (const TrilinosWrappers::MPI::Vector &trilinos_vec, VectorOperation::values operation, std::shared_ptr< const CommunicationPatternBase > communication_pattern=std::shared_ptr< const CommunicationPatternBase >())
 
void import (const EpetraWrappers::Vector &epetra_vec, VectorOperation::values operation, std::shared_ptr< const CommunicationPatternBase > communication_pattern=std::shared_ptr< const CommunicationPatternBase >())
 
void import (const CUDAWrappers::Vector< Number > &cuda_vec, VectorOperation::values operation, std::shared_ptr< const CommunicationPatternBase > communication_pattern=std::shared_ptr< const CommunicationPatternBase >())
 
size_type size () const
 
size_type n_elements () const
 
const IndexSetget_stored_elements () const
 
iterator begin ()
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 
Number operator() (const size_type global_index) const
 
Number & operator() (const size_type global_index)
 
Number operator[] (const size_type global_index) const
 
Number & operator[] (const size_type global_index)
 
template<typename Number2 >
void extract_subvector_to (const std::vector< size_type > &indices, std::vector< Number2 > &values) const
 
template<typename ForwardIterator , typename OutputIterator >
void extract_subvector_to (ForwardIterator indices_begin, const ForwardIterator indices_end, OutputIterator values_begin) const
 
Number local_element (const size_type local_index) const
 
Number & local_element (const size_type local_index)
 
template<typename Number2 >
void add (const std::vector< size_type > &indices, const std::vector< Number2 > &values)
 
template<typename Number2 >
void add (const std::vector< size_type > &indices, const ReadWriteVector< Number2 > &values)
 
template<typename Number2 >
void add (const size_type n_elements, const size_type *indices, const Number2 *values)
 
void print (std::ostream &out, const unsigned int precision=3, const bool scientific=true) const
 
std::size_t memory_consumption () const
 
- Public Member Functions inherited from Subscriptor
 Subscriptor ()
 
 Subscriptor (const Subscriptor &)
 
 Subscriptor (Subscriptor &&) noexcept
 
virtual ~Subscriptor ()
 
Subscriptoroperator= (const Subscriptor &)
 
Subscriptoroperator= (Subscriptor &&) noexcept
 
void subscribe (const char *identifier=nullptr) const
 
void unsubscribe (const char *identifier=nullptr) const
 
unsigned int n_subscriptions () const
 
void list_subscribers () const
 
template<class Archive >
void serialize (Archive &ar, const unsigned int version)
 
- Public Member Functions inherited from LinearAlgebra::VectorSpaceVector< Number >
virtual void compress (VectorOperation::values)
 
virtual ~VectorSpaceVector ()=default
 

Static Public Member Functions

static::ExceptionBase & ExcVectorTypeNotCompatible ()
 
- Static Public Member Functions inherited from Subscriptor
static::ExceptionBase & ExcInUse (int arg1, std::string arg2, std::string arg3)
 
static::ExceptionBase & ExcNoSubscriber (std::string arg1, std::string arg2)
 

Private Member Functions

template<typename Archive >
void serialize (Archive &ar, const unsigned int version)
 

Friends

template<typename Number2 >
class Vector
 

Additional Inherited Members

- Public Types inherited from LinearAlgebra::ReadWriteVector< Number >
typedef Number value_type
 
- Protected Member Functions inherited from LinearAlgebra::ReadWriteVector< Number >
void import (const Epetra_MultiVector &multivector, const IndexSet &locally_owned_elements, VectorOperation::values operation, const MPI_Comm &mpi_comm, const std::shared_ptr< const CommunicationPatternBase > &communication_pattern)
 
unsigned int global_to_local (const types::global_dof_index global_index) const
 
void resize_val (const size_type new_allocated_size)
 
EpetraWrappers::CommunicationPattern create_epetra_comm_pattern (const IndexSet &source_index_set, const MPI_Comm &mpi_comm)
 
- Protected Attributes inherited from LinearAlgebra::ReadWriteVector< Number >
IndexSet stored_elements
 
IndexSet source_stored_elements
 
std::shared_ptr< CommunicationPatternBasecomm_pattern
 
std::unique_ptr< Number[], decltype(free)* > values
 
std::shared_ptr<::parallel::internal::TBBPartitioner > thread_loop_partitioner
 

Detailed Description

template<typename Number>
class LinearAlgebra::Vector< Number >

Numerical vector of data. This class derives from both LinearAlgebra::ReadWriteVector and LinearAlgebra::VectorSpaceVector. As opposed to the array of the C++ standard library, this class implements an element of a vector space suitable for numerical computations.

Author
Bruno Turcksin, 2015.

Definition at line 65 of file la_vector.h.

Constructor & Destructor Documentation

template<typename Number>
LinearAlgebra::Vector< Number >::Vector ( )
default

Constructor. Create a vector of dimension zero.

template<typename Number >
Vector< Number >::Vector ( const Vector< Number > &  V)
inline

Copy constructor. Sets the dimension to that of the given vector and copies all elements.

Definition at line 399 of file la_vector.h.

template<typename Number >
Vector< Number >::Vector ( const size_type  n)
inlineexplicit

Constructor. Set dimension to n and initialize all elements with zero.

The constructor is made explicit to avoid accident like this: v=0;. Presumably, the user wants to set every element of the vector to zero, but instead, what happens is this call: v=Vector<Number>(0);, i.e. the vector is replaced by one of length zero.

Definition at line 406 of file la_vector.h.

template<typename Number >
template<typename InputIterator >
Vector< Number >::Vector ( const InputIterator  first,
const InputIterator  last 
)
inline

Initialize the vector with a given range of values pointed to by the iterators. This function exists in analogy to the std::vector class.

Definition at line 414 of file la_vector.h.

Member Function Documentation

template<typename Number>
virtual void LinearAlgebra::Vector< Number >::reinit ( const size_type  size,
const bool  omit_zeroing_entries = false 
)
overridevirtual

Set the global size of the vector to size. The stored elements have their index in [0,size).

If the flag omit_zeroing_entries is set to false, the memory will be initialized with zero, otherwise the memory will be untouched (and the user must make sure to fill it with reasonable data before using it).

Reimplemented from LinearAlgebra::ReadWriteVector< Number >.

template<typename Number>
template<typename Number2 >
void LinearAlgebra::Vector< Number >::reinit ( const ReadWriteVector< Number2 > &  in_vector,
const bool  omit_zeroing_entries = false 
)

Uses the same IndexSet as the one of the input vector in_vector and allocates memory for this vector.

If the flag omit_zeroing_entries is set to false, the memory will be initialized with zero, otherwise the memory will be untouched (and the user must make sure to fill it with reasonable data before using it).

template<typename Number>
virtual void LinearAlgebra::Vector< Number >::reinit ( const IndexSet locally_stored_indices,
const bool  omit_zeroing_entries = false 
)
overridevirtual

Initializes the vector. The indices are specified by locally_stored_indices.

If the flag omit_zeroing_entries is set to false, the memory will be initialized with zero, otherwise the memory will be untouched (and the user must make sure to fill it with reasonable data before using it). locally_stored_indices.

Reimplemented from LinearAlgebra::ReadWriteVector< Number >.

template<typename Number>
virtual void LinearAlgebra::Vector< Number >::reinit ( const VectorSpaceVector< Number > &  V,
const bool  omit_zeroing_entries = false 
)
overridevirtual

Change the dimension to that of the vector V. The elements of V are not copied.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
Vector<Number>& LinearAlgebra::Vector< Number >::operator= ( const Vector< Number > &  in_vector)

Copies the data of the input vector in_vector.

template<typename Number>
template<typename Number2 >
Vector<Number>& LinearAlgebra::Vector< Number >::operator= ( const Vector< Number2 > &  in_vector)

Copies the data of the input vector in_vector.

template<typename Number>
virtual Vector<Number>& LinearAlgebra::Vector< Number >::operator= ( const Number  s)
overridevirtual

Sets all elements of the vector to the scalar s. This operation is only allowed if s is equal to zero.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual Vector<Number>& LinearAlgebra::Vector< Number >::operator*= ( const Number  factor)
overridevirtual

Multiply the entire vector by a fixed factor.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual Vector<Number>& LinearAlgebra::Vector< Number >::operator/= ( const Number  factor)
overridevirtual

Divide the entire vector by a fixed factor.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual Vector<Number>& LinearAlgebra::Vector< Number >::operator+= ( const VectorSpaceVector< Number > &  V)
overridevirtual

Add the vector V to the present one.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual Vector<Number>& LinearAlgebra::Vector< Number >::operator-= ( const VectorSpaceVector< Number > &  V)
overridevirtual

Subtract the vector V from the present one.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual Number LinearAlgebra::Vector< Number >::operator* ( const VectorSpaceVector< Number > &  V) const
overridevirtual

Return the scalar product of two vectors.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual void LinearAlgebra::Vector< Number >::import ( const ReadWriteVector< Number > &  V,
VectorOperation::values  operation,
std::shared_ptr< const CommunicationPatternBase communication_pattern = std::shared_ptr< const CommunicationPatternBase >() 
)
overridevirtual

This function is not implemented and will throw an exception.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual void LinearAlgebra::Vector< Number >::add ( const Number  a)
overridevirtual

Add a to all components. Note that a is a scalar not a vector.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual void LinearAlgebra::Vector< Number >::add ( const Number  a,
const VectorSpaceVector< Number > &  V 
)
overridevirtual

Simple addition of a multiple of a vector, i.e. *this += a*V.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual void LinearAlgebra::Vector< Number >::add ( const Number  a,
const VectorSpaceVector< Number > &  V,
const Number  b,
const VectorSpaceVector< Number > &  W 
)
overridevirtual

Multiple addition of a multiple of a vector, i.e. *this += a*V+b*W.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual void LinearAlgebra::Vector< Number >::sadd ( const Number  s,
const Number  a,
const VectorSpaceVector< Number > &  V 
)
overridevirtual

Scaling and simple addition of a multiple of a vector, i.e. *this = s*(*this)+a*V.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual void LinearAlgebra::Vector< Number >::scale ( const VectorSpaceVector< Number > &  scaling_factors)
overridevirtual

Scale each element of this vector by the corresponding element in the argument. This function is mostly meant to simulate multiplication (and immediate re-assignment) by a diagonal scaling matrix.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual void LinearAlgebra::Vector< Number >::equ ( const Number  a,
const VectorSpaceVector< Number > &  V 
)
overridevirtual

Assignment *this = a*V.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual bool LinearAlgebra::Vector< Number >::all_zero ( ) const
overridevirtual

Return whether the vector contains only elements with value zero.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual value_type LinearAlgebra::Vector< Number >::mean_value ( ) const
overridevirtual

Return the mean value of all the entries of this vector.

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual VectorSpaceVector<Number>::real_type LinearAlgebra::Vector< Number >::l1_norm ( ) const
overridevirtual

Return the l1 norm of the vector (i.e., the sum of the absolute values of all entries).

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual VectorSpaceVector<Number>::real_type LinearAlgebra::Vector< Number >::l2_norm ( ) const
overridevirtual

Return the l2 norm of the vector (i.e., the square root of the sum of the square of all entries among all processors).

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual VectorSpaceVector<Number>::real_type LinearAlgebra::Vector< Number >::linfty_norm ( ) const
overridevirtual

Return the maximum norm of the vector (i.e., the maximum absolute value among all entries and among all processors).

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number>
virtual Number LinearAlgebra::Vector< Number >::add_and_dot ( const Number  a,
const VectorSpaceVector< Number > &  V,
const VectorSpaceVector< Number > &  W 
)
overridevirtual

Perform a combined operation of a vector addition and a subsequent inner product, returning the value of the inner product. In other words, the result of this function is the same as if the user called

this->add(a, V);
return_value = *this * W;

The reason this function exists is that this operation involves less memory transfer than calling the two functions separately. This method only needs to load three vectors, this, V, W, whereas calling separate methods means to load the calling vector this twice. Since most vector operations are memory transfer limited, this reduces the time by 25% (or 50% if W equals this).

For complex-valued vectors, the scalar product in the second step is implemented as \(\left<v,w\right>=\sum_i v_i \bar{w_i}\).

Implements LinearAlgebra::VectorSpaceVector< Number >.

template<typename Number >
Vector< Number >::size_type Vector< Number >::size ( ) const
inlineoverridevirtual

Return the global size of the vector, equal to the sum of the number of locally owned indices among all processors.

Implements LinearAlgebra::VectorSpaceVector< Number >.

Definition at line 425 of file la_vector.h.

template<typename Number >
IndexSet Vector< Number >::locally_owned_elements ( ) const
inlineoverridevirtual

Return an index set that describes which elements of this vector are owned by the current processor. As a consequence, the index sets returned on different processors if this is a distributed vector will form disjoint sets that add up to the complete index set. Obviously, if a vector is created on only one processor, then the result would satisfy

vec.locally_owned_elements() == complete_index_set(vec.size())

Implements LinearAlgebra::VectorSpaceVector< Number >.

Definition at line 434 of file la_vector.h.

template<typename Number >
void Vector< Number >::print ( std::ostream &  out,
const unsigned int  precision = 3,
const bool  scientific = true,
const bool  across = true 
) const
inlineoverridevirtual

Prints the vector to the output stream out.

Implements LinearAlgebra::VectorSpaceVector< Number >.

Definition at line 443 of file la_vector.h.

template<typename Number>
void LinearAlgebra::Vector< Number >::block_write ( std::ostream &  out) const

Write the vector en bloc to a file. This is done in a binary mode, so the output is neither readable by humans nor (probably) by other computers using a different operating system or number format.

template<typename Number>
void LinearAlgebra::Vector< Number >::block_read ( std::istream &  in)

Read a vector en block from a file. This is done using the inverse operations to the above function, so it is reasonably fast because the bitstream is not interpreted.

The vector is resized if necessary.

A primitive form of error checking is performed which will recognize the bluntest attempts to interpret some data as a vector stored bitwise to a file, but not more.

template<typename Number >
std::size_t Vector< Number >::memory_consumption ( ) const
inlineoverridevirtual

Return the memory consumption of this class in bytes.

Implements LinearAlgebra::VectorSpaceVector< Number >.

Definition at line 471 of file la_vector.h.

template<typename Number >
template<typename Archive >
void Vector< Number >::serialize ( Archive &  ar,
const unsigned int  version 
)
inlineprivate

Serialize the data of this object using boost. This function is necessary to use boost::archive::text_iarchive and boost::archive::text_oarchive.

Definition at line 456 of file la_vector.h.

Friends And Related Function Documentation

template<typename Number>
template<typename Number2 >
friend class Vector
friend

Make all other ReadWriteVector types friends.

Typedef for the vector type used.

Implementation of a parallel vector class. The design of this class is similar to the standard Vector class in deal.II, with the exception that storage is distributed with MPI.

The vector is designed for the following scheme of parallel partitioning:

  • The indices held by individual processes (locally owned part) in the MPI parallelization form a contiguous range [my_first_index,my_last_index).
  • Ghost indices residing on arbitrary positions of other processors are allowed. It is in general more efficient if ghost indices are clustered, since they are stored as a set of intervals. The communication pattern of the ghost indices is determined when calling the function reinit (locally_owned, ghost_indices, communicator), and retained until the partitioning is changed. This allows for efficient parallel communication of indices. In particular, it stores the communication pattern, rather than having to compute it again for every communication. For more information on ghost vectors, see also the glossary entry on vectors with ghost elements.
  • Besides the usual global access operator() it is also possible to access vector entries in the local index space with the function local_element(). Locally owned indices are placed first, [0, local_size()), and then all ghost indices follow after them contiguously, [local_size(), local_size()+n_ghost_entries()).

Functions related to parallel functionality:

  • The function compress() goes through the data associated with ghost indices and communicates it to the owner process, which can then add it to the correct position. This can be used e.g. after having run an assembly routine involving ghosts that fill this vector. Note that the insert mode of compress() does not set the elements included in ghost entries but simply discards them, assuming that the owning processor has set them to the desired value already (See also the glossary entry on compress).
  • The update_ghost_values() function imports the data from the owning processor to the ghost indices in order to provide read access to the data associated with ghosts.
  • It is possible to split the above functions into two phases, where the first initiates the communication and the second one finishes it. These functions can be used to overlap communication with computations in other parts of the code.
  • Of course, reduction operations (like norms) make use of collective all-to-all MPI communications.

This vector can take two different states with respect to ghost elements:

  • After creation and whenever zero_out_ghosts() is called (or operator= (0.)), the vector does only allow writing into ghost elements but not reading from ghost elements.
  • After a call to update_ghost_values(), the vector does not allow writing into ghost elements but only reading from them. This is to avoid undesired ghost data artifacts when calling compress() after modifying some vector entries. The current status of the ghost entries (read mode or write mode) can be queried by the method has_ghost_elements(), which returns true exactly when ghost elements have been updated and false otherwise, irrespective of the actual number of ghost entries in the vector layout (for that information, use n_ghost_entries() instead).

This vector uses the facilities of the class ::Vector<Number> for implementing the operations on the local range of the vector. In particular, it also inherits thread parallelism that splits most vector-vector operations into smaller chunks if the program uses multiple threads. This may or may not be desired when working also with MPI.

Limitations regarding the vector size

This vector class is based on two different number types for indexing. The so-called global index type encodes the overall size of the vector. Its type is types::global_dof_index. The largest possible value is 2^32-1 or approximately 4 billion in case 64 bit integers are disabled at configuration of deal.II (default case) or 2^64-1 or approximately 10^19 if 64 bit integers are enabled (see the glossary entry on When to use types::global_dof_index instead of unsigned int for further information).

The second relevant index type is the local index used within one MPI rank. As opposed to the global index, the implementation assumes 32-bit unsigned integers unconditionally. In other words, to actually use a vector with more than four billion entries, you need to use MPI with more than one rank (which in general is a safe assumption since four billion entries consume at least 16 GB of memory for floats or 32 GB of memory for doubles) and enable 64-bit indices. If more than 4 billion local elements are present, the implementation tries to detect that, which triggers an exception and aborts the code. Note, however, that the detection of overflow is tricky and the detection mechanism might fail in some circumstances. Therefore, it is strongly recommended to not rely on this class to automatically detect the unsupported case.

Author
Katharina Kormann, Martin Kronbichler, 2010, 2011
Deprecated:
Use LinearAlgebra::distributed::Vector instead.

Definition at line 392 of file la_vector.h.


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