Reference documentation for deal.II version Git f81eda9982 2020-03-28 21:30:57 -0400
LinearAlgebra::ReadWriteVector< Number > Class Template Reference

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

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

## Classes

class  FunctorTemplate

## Public Types

using value_type = Number

## Public Member Functions

1: Basic Object-handling

virtual void reinit (const size_type size, const bool omit_zeroing_entries=false)

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)

void reinit (const TrilinosWrappers::MPI::Vector &trilinos_vec)

template<typename Functor >
void apply (const Functor &func)

void swap (ReadWriteVector< Number > &v)

template<typename Number2 >

ReadWriteVector< Number > & operator= (const Number s)

template<typename MemorySpace >
void import (const distributed::Vector< Number, MemorySpace > &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, const std::shared_ptr< const CommunicationPatternBase > &communication_pattern=std::shared_ptr< const CommunicationPatternBase >())

void import (const EpetraWrappers::Vector &epetra_vec, VectorOperation::values operation, const std::shared_ptr< const CommunicationPatternBase > &communication_pattern=std::shared_ptr< const CommunicationPatternBase >())

void import (const CUDAWrappers::Vector< Number > &cuda_vec, VectorOperation::values operation, const 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

2: Data-Access
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)

3: Modification of vectors
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 (std::atomic< bool > *const validity, const std::string &identifier="") const

void unsubscribe (std::atomic< bool > *const validity, const std::string &identifier="") const

unsigned int n_subscriptions () const

template<typename StreamType >
void list_subscribers (StreamType &stream) const

void list_subscribers () const

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

## Protected Member Functions

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

IndexSet stored_elements

IndexSet source_stored_elements

std::shared_ptr< CommunicationPatternBasecomm_pattern

std::unique_ptr< Number[], decltype(std::free) * > values

Static Public Member Functions inherited from Subscriptor
static ::ExceptionBaseExcInUse (int arg1, std::string arg2, std::string arg3)

static ::ExceptionBaseExcNoSubscriber (std::string arg1, std::string arg2)

## Detailed Description

### template<typename Number> class LinearAlgebra::ReadWriteVector< Number >

ReadWriteVector is intended to represent vectors in $${\mathbb R}^N$$ for which it stores all or a subset of elements. The latter case in important in parallel computations, where $$N$$ may be so large that no processor can actually all elements of a solution vector, but where this is also not necessary: one typically only has to store the values of degrees of freedom that live on cells that are locally owned plus potentially those degrees of freedom that live on ghost cells.

This class allows to access individual elements to be read or written. However, it does not allow global operations such as taking the norm. ReadWriteVector can be used to read and write elements in vectors derived from VectorSpaceVector such as TrilinosWrappers::MPI::Vector and PETScWrappers::MPI::Vector.

### Storing elements

Most of the time, one will simply read from or write into a vector of the current class using the global numbers of these degrees of freedom. This is done using operator()() or operator[]() which call global_to_local() to transform the global index into a local one. In such cases, it is clear that one can only access elements of the vector that the current object indeed stores.

However, it is also possible to access elements in the order in which they are stored by the current object. In other words, one is not interested in accessing elements with their global indices, but instead using an enumeration that only takes into account the elements that are actually stored. This is facilitated by the local_element() function. To this end, it is necessary to know in which order the current class stores its element. The elements of all the consecutive ranges are stored in ascending order of the first index of each range. The function IndexSet::largest_range_starting_index() can be used to get the first index of the largest range.

Definition at line 131 of file read_write_vector.h.

## ◆ value_type

template<typename Number>
 using LinearAlgebra::ReadWriteVector< Number >::value_type = Number

Declare standard types used in all containers. These types parallel those in the C++ standard libraries vector<...> class.

Definition at line 139 of file read_write_vector.h.

## Constructor & Destructor Documentation

template<typename Number>

Empty constructor.

template<typename Number>

Copy constructor.

template<typename Number>
explicit

Construct a vector given the size, the stored elements have their index in [0,size).

template<typename Number>
explicit

Construct a vector whose stored elements indices are given by the IndexSet locally_stored_indices.

template<typename Number>
overridedefault

Destructor.

## ◆ reinit() [1/4]

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

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 in LinearAlgebra::Vector< Number >.

## ◆ reinit() [2/4]

template<typename Number>
template<typename Number2 >
 void LinearAlgebra::ReadWriteVector< 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).

## ◆ reinit() [3/4]

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

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 in LinearAlgebra::Vector< Number >.

## ◆ reinit() [4/4]

template<typename Number>
 void LinearAlgebra::ReadWriteVector< Number >::reinit ( const TrilinosWrappers::MPI::Vector & trilinos_vec )

Initialize this ReadWriteVector by supplying access to all locally available entries in the given ghosted or non-ghosted vector.

Note
This function currently copies the values from the argument into the ReadWriteVector, so modifications here will not modify trilinos_vec.

This function is mainly written for backwards-compatibility to get element access to a ghosted TrilinosWrappers::MPI::Vector inside the library.

## ◆ apply()

template<typename Number>
template<typename Functor >
 void LinearAlgebra::ReadWriteVector< Number >::apply ( const Functor & func )

Apply the functor func to each element of the vector. The functor should look like

struct Functor
{
void operator() (Number &value);
};
Note

## ◆ swap()

template<typename Number>

Swap the contents of this vector and the other vector v. One could do this operation with a temporary variable and copying over the data elements, but this function is significantly more efficient since it only swaps the pointers to the data of the two vectors and therefore does not need to allocate temporary storage and move data around.

This function is analogous to the swap function of all C++ standard containers. Also, there is a global function swap(u,v) that simply calls u.swap(v), again in analogy to standard functions.

## ◆ operator=() [1/3]

template<typename Number>

Copies the data and the IndexSet of the input vector in_vector.

## ◆ operator=() [2/3]

template<typename Number>
template<typename Number2 >

Copies the data and the IndexSet of the input vector in_vector.

## ◆ operator=() [3/3]

template<typename Number>

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

## ◆ import() [1/6]

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

Imports all the elements present in the vector's IndexSet from the input vector vec. VectorOperation::values operation is used to decide if the elements in V should be added to the current vector or replace the current elements. The last parameter can be used if the same communication pattern is used multiple times. This can be used to improve performance.

## ◆ import() [2/6]

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

Imports all the elements present in the vector's IndexSet from the input vector petsc_vec. VectorOperation::values operation is used to decide if the elements in V should be added to the current vector or replace the current elements. The last parameter can be used if the same communication pattern is used multiple times. This can be used to improve performance.

## ◆ import() [3/6]

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

Imports all the elements present in the vector's IndexSet from the input vector trilinos_vec. VectorOperation::values operation is used to decide if the elements in V should be added to the current vector or replace the current elements. The last parameter can be used if the same communication pattern is used multiple times. This can be used to improve performance.

Note
The trilinos_vec is not allowed to have ghost entries.

## ◆ import() [4/6]

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

Imports all the elements present in the vector's IndexSet from the input vector epetra_vec. VectorOperation::values operation is used to decide if the elements in V should be added to the current vector or replace the current elements. The last parameter can be used if the same communication pattern is used multiple times. This can be used to improve performance.

## ◆ import() [5/6]

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

Import all the elements present in the vector's IndexSet from the input vector cuda_vec. VectorOperation::values operation is used to decide if the elements in V should be added to the current vector or replace the current elements. The last parameter is not used.

## ◆ size()

template<typename Number>
 size_type LinearAlgebra::ReadWriteVector< Number >::size ( ) const

The value returned by this function denotes the dimension of the vector spaces that are modeled by objects of this kind. However, objects of the current class do not actually stores all elements of vectors of this space but may, in fact store only a subset. The number of elements stored is returned by n_elements() and is smaller or equal to the number returned by the current function.

## ◆ n_elements()

template<typename Number>
 size_type LinearAlgebra::ReadWriteVector< Number >::n_elements ( ) const

This function returns the number of elements stored. It is smaller or equal to the dimension of the vector space that is modeled by an object of this kind. This dimension is return by size().

## ◆ get_stored_elements()

template<typename Number>
 const IndexSet& LinearAlgebra::ReadWriteVector< Number >::get_stored_elements ( ) const

Return the IndexSet that represents the indices of the elements stored.

## ◆ begin() [1/2]

template<typename Number>
 iterator LinearAlgebra::ReadWriteVector< Number >::begin ( )

Make the ReadWriteVector class a bit like the vector<> class of the C++ standard library by returning iterators to the start and end of the locally stored elements of this vector.

## ◆ begin() [2/2]

template<typename Number>
 const_iterator LinearAlgebra::ReadWriteVector< Number >::begin ( ) const

Return constant iterator to the start of the locally stored elements of the vector.

## ◆ end() [1/2]

template<typename Number>
 iterator LinearAlgebra::ReadWriteVector< Number >::end ( )

Return an iterator pointing to the element past the end of the array of locally stored entries.

## ◆ end() [2/2]

template<typename Number>
 const_iterator LinearAlgebra::ReadWriteVector< Number >::end ( ) const

Return a constant iterator pointing to the element past the end of the array of the locally stored entries.

## ◆ operator()() [1/2]

template<typename Number>
 Number LinearAlgebra::ReadWriteVector< Number >::operator() ( const size_type global_index ) const

Read access to the data in the position corresponding to global_index. An exception is thrown if global_index is not stored by the current object.

## ◆ operator()() [2/2]

template<typename Number>
 Number& LinearAlgebra::ReadWriteVector< Number >::operator() ( const size_type global_index )

Read and write access to the data in the position corresponding to global_index. An exception is thrown if global_index is not stored by the current object.

## ◆ operator[]() [1/2]

template<typename Number>
 Number LinearAlgebra::ReadWriteVector< Number >::operator[] ( const size_type global_index ) const

Read access to the data in the position corresponding to global_index. An exception is thrown if global_index is not stored by the current object.

This function does the same thing as operator().

## ◆ operator[]() [2/2]

template<typename Number>
 Number& LinearAlgebra::ReadWriteVector< Number >::operator[] ( const size_type global_index )

Read and write access to the data in the position corresponding to global_index. An exception is thrown if global_index is not stored by the current object.

This function does the same thing as operator().

## ◆ extract_subvector_to() [1/2]

template<typename Number>
template<typename Number2 >
 void LinearAlgebra::ReadWriteVector< Number >::extract_subvector_to ( const std::vector< size_type > & indices, std::vector< Number2 > & values ) const

Instead of getting individual elements of a vector via operator(), this function allows getting a whole set of elements at once. The indices of the elements to be read are stated in the first argument, the corresponding values are returned in the second.

If the current vector is called v, then this function is the equivalent to the code

for (unsigned int i=0; i<indices.size(); ++i)
values[i] = v[indices[i]];
Precondition
The sizes of the indices and values arrays must be identical.

## ◆ extract_subvector_to() [2/2]

template<typename Number>
template<typename ForwardIterator , typename OutputIterator >
 void LinearAlgebra::ReadWriteVector< Number >::extract_subvector_to ( ForwardIterator indices_begin, const ForwardIterator indices_end, OutputIterator values_begin ) const

Instead of getting individual elements of a vector via operator(), this function allows getting a whole set of elements at once. In contrast to the previous function, this function obtains the indices of the elements by dereferencing all elements of the iterator range provided by the first two arguments, and puts the vector values into memory locations obtained by dereferencing a range of iterators starting at the location pointed to by the third argument.

If the current vector is called v, then this function is the equivalent to the code

ForwardIterator indices_p = indices_begin;
OutputIterator values_p = values_begin;
while (indices_p != indices_end)
{
*values_p = v[*indices_p];
++indices_p;
++values_p;
}
Precondition
It must be possible to write into as many memory locations starting at values_begin as there are iterators between indices_begin and indices_end.

## ◆ local_element() [1/2]

template<typename Number>
 Number LinearAlgebra::ReadWriteVector< Number >::local_element ( const size_type local_index ) const

Read access to the data field specified by local_index. When you access elements in the order in which they are stored, it is necessary that you know in which they are stored. In other words, you need to know the map between the global indices of the elements this class stores, and the local indices into the contiguous array of these global elements. For this, see the general documentation of this class.

Performance: Direct array access (fast).

## ◆ local_element() [2/2]

template<typename Number>
 Number& LinearAlgebra::ReadWriteVector< Number >::local_element ( const size_type local_index )

Read and write access to the data field specified by local_index. When you access elements in the order in which they are stored, it is necessary that you know in which they are stored. In other words, you need to know the map between the global indices of the elements this class stores, and the local indices into the contiguous array of these global elements. For this, see the general documentation of this class.

Performance: Direct array access (fast).

template<typename Number>
template<typename Number2 >
 void LinearAlgebra::ReadWriteVector< Number >::add ( const std::vector< size_type > & indices, const std::vector< Number2 > & values )

This function adds a whole set of values stored in values to the vector components specified by indices.

template<typename Number>
template<typename Number2 >
 void LinearAlgebra::ReadWriteVector< Number >::add ( const std::vector< size_type > & indices, const ReadWriteVector< Number2 > & values )

This function is similar to the previous one but takes a ReadWriteVector of values.

template<typename Number>
template<typename Number2 >
 void LinearAlgebra::ReadWriteVector< Number >::add ( const size_type n_elements, const size_type * indices, const Number2 * values )

Take an address where n_elements are stored contiguously and add them into the vector. Handles all cases which are not covered by the other two add() functions above.

## ◆ print()

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

Prints the vector to the output stream out.

## ◆ memory_consumption()

template<typename Number>
 std::size_t LinearAlgebra::ReadWriteVector< Number >::memory_consumption ( ) const

Return the memory consumption of this class in bytes.

## ◆ import() [6/6]

template<typename Number>
 void LinearAlgebra::ReadWriteVector< Number >::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 )
protected

Import all the elements present in the vector's IndexSet from the input vector multivector. This is an helper function and it should not be used directly.

## ◆ global_to_local()

template<typename Number>
 unsigned int LinearAlgebra::ReadWriteVector< Number >::global_to_local ( const types::global_dof_index global_index ) const
inlineprotected

Return the local position of global_index.

Definition at line 650 of file read_write_vector.h.

## ◆ resize_val()

template<typename Number>
 void LinearAlgebra::ReadWriteVector< Number >::resize_val ( const size_type new_allocated_size )
protected

A helper function that is used to resize the val array.

## ◆ create_epetra_comm_pattern()

template<typename Number>
 EpetraWrappers::CommunicationPattern LinearAlgebra::ReadWriteVector< Number >::create_epetra_comm_pattern ( const IndexSet & source_index_set, const MPI_Comm & mpi_comm )
protected

Return a EpetraWrappers::CommunicationPattern and store it for future use.

## ◆ stored_elements

template<typename Number>
protected

Indices of the elements stored.

Definition at line 687 of file read_write_vector.h.

## ◆ source_stored_elements

template<typename Number>
protected

IndexSet of the elements of the last imported vector;

Definition at line 692 of file read_write_vector.h.

## ◆ comm_pattern

template<typename Number>
protected

CommunicationPattern for the communication between the source_stored_elements IndexSet and the current vector.

Definition at line 698 of file read_write_vector.h.

## ◆ values

template<typename Number>
protected

Pointer to the array of local elements of this vector.

Definition at line 703 of file read_write_vector.h.