MFEM  v4.5.2
Finite element discretization library
Public Member Functions | Protected Attributes | Friends | List of all members
mfem::TAutoDiffVector< dtype > Class Template Reference

Templated vector data type. More...

#include <tadvector.hpp>

Public Member Functions

 TAutoDiffVector ()
 Default constructor for Vector. Sets size = 0 and data = NULL. More...
 
 TAutoDiffVector (const TAutoDiffVector< dtype > &v)
 Copy constructor. Allocates a new data array and copies the data. More...
 
 TAutoDiffVector (const Vector &v)
 
 TAutoDiffVector (int s)
 Creates vector of size s. More...
 
 TAutoDiffVector (dtype *_data, int _size)
 Creates a vector referencing an array of doubles, owned by someone else. More...
 
void Load (std::istream **in, int np, int *dim)
 Reads a vector from multiple files. More...
 
void Load (std::istream &in, int Size)
 Load a vector from an input stream. More...
 
void Load (std::istream &in)
 Load a vector from an input stream, reading the size from the stream. More...
 
void SetSize (int s)
 Resize the vector to size s. More...
 
void SetDataAndSize (dtype *d, int s)
 Set the Vector data and size. More...
 
void NewDataAndSize (dtype *d, int s)
 Set the Vector data and size, deleting the old data, if owned. More...
 
void MakeRef (TAutoDiffVector< dtype > &base, int offset, int size_)
 Reset the Vector to be a reference to a sub-vector of base. More...
 
void MakeRef (TAutoDiffVector< dtype > &base, int offset)
 Reset the Vector to be a reference to a sub-vector of base without changing its current size. More...
 
void Destroy ()
 Destroy a vector. More...
 
int Size () const
 Returns the size of the vector. More...
 
int Capacity () const
 Return the size of the currently allocated data array. More...
 
dtype * GetData () const
 Return a pointer to the beginning of the Vector data. More...
 
 operator dtype * ()
 Conversion to double *. More...
 
 operator const dtype * () const
 Conversion to const double *. More...
 
bool OwnsData () const
 Read the Vector data (host pointer) ownership flag. More...
 
void StealData (dtype **p)
 Changes the ownership of the data; after the call the Vector is empty. More...
 
dtype * StealData ()
 Changes the ownership of the data; after the call the Vector is empty. More...
 
dtype & Elem (int i)
 Access Vector entries. Index i = 0 .. size-1. More...
 
const dtype & Elem (int i) const
 Read only access to Vector entries. Index i = 0 .. size-1. More...
 
dtype & operator() (int i)
 Access Vector entries using () for 0-based indexing. More...
 
const dtype & operator() (int i) const
 Read only access to Vector entries using () for 0-based indexing. More...
 
dtype operator* (const dtype *v) const
 Dot product with a dtype * array. More...
 
dtype operator* (const TAutoDiffVector< dtype > &v) const
 Return the inner-product. More...
 
dtype operator* (const Vector &v) const
 
TAutoDiffVector< dtype > & operator= (const dtype *v)
 Copy Size() entries from v. More...
 
TAutoDiffVector< dtype > & operator= (const TAutoDiffVector< dtype > &v)
 Copy assignment. More...
 
TAutoDiffVector< dtype > & operator= (const Vector &v)
 
template<typename ivtype >
TAutoDiffVectoroperator= (ivtype value)
 Redefine '=' for vector = constant. More...
 
template<typename ivtype >
TAutoDiffVectoroperator*= (ivtype c)
 
template<typename ivtype >
TAutoDiffVectoroperator/= (ivtype c)
 
TAutoDiffVectoroperator-= (const TAutoDiffVector< dtype > &v)
 
template<typename ivtype >
TAutoDiffVectoroperator-= (ivtype v)
 
TAutoDiffVectoroperator+= (const TAutoDiffVector< dtype > &v)
 
template<typename ivtype >
TAutoDiffVectoroperator+= (ivtype v)
 
template<typename ivtype , typename vtype >
TAutoDiffVectorAdd (const ivtype a, const vtype &v)
 (*this) += a * Va More...
 
template<typename ivtype , typename vtype >
TAutoDiffVectorSet (const ivtype a, const vtype &v)
 (*this) = a * x More...
 
template<typename vtype >
void SetVector (const vtype &v, int offset)
 
void Neg ()
 (*this) = -(*this) More...
 
void Swap (TAutoDiffVector< dtype > &other)
 Swap the contents of two Vectors. More...
 
 ~TAutoDiffVector ()
 Destroys vector. More...
 
void Print (std::ostream &os=mfem::out, int width=8) const
 Prints vector to stream os with width entries per line. More...
 
void Randomize (int seed=0)
 Set random values in the vector. More...
 
dtype Norml2 () const
 Returns the l2 norm of the vector. More...
 
dtype Normlinf () const
 Returns the l_infinity norm of the vector. More...
 
dtype Norml1 () const
 Returns the l_1 norm of the vector. More...
 

Protected Attributes

dtype * data
 
int size
 
int capacity
 

Friends

template<typename vtype1 , typename vtype2 >
void add (const vtype1 &v1, const vtype2 &v2, TAutoDiffVector< dtype > &v)
 Set v = v1 + v2. More...
 
template<typename vtype1 , typename vtype2 >
void add (const vtype1 &v1, dtype alpha, const vtype2 &v2, TAutoDiffVector< dtype > &v)
 Set v = v1 + alpha * v2. More...
 
template<typename vtype1 , typename vtype2 >
void add (const dtype a, const vtype1 &x, const dtype b, const vtype2 &y, TAutoDiffVector< dtype > &z)
 
template<typename vtype1 , typename vtype2 >
void add (const dtype a, const vtype1 &x, const vtype2 &y, TAutoDiffVector< dtype > &z)
 
template<typename vtype1 , typename vtype2 >
void subtract (const vtype1 &x, const vtype2 &y, TAutoDiffVector< dtype > &z)
 
template<typename ivtype , typename vtype1 , typename vtype2 >
void subtract (const ivtype a, const vtype1 &x, const vtype2 &y, TAutoDiffVector< dtype > &z)
 

Detailed Description

template<typename dtype>
class mfem::TAutoDiffVector< dtype >

Templated vector data type.

The main goal of the TAutoDiffVector class is to serve as a data container for representing vectors in classes, methods, and functions utilized with automatic differentiation (AD). The functionality/interface is copied from the standard MFEM dense vector mfem::Vector. The basic idea is to utilize the templated vector class in combination with AD during the development phase. The AD parts can be replaced with optimized code once the initial development of the application is complete. The common interface between TAutoDiffVector and Vector will ease the transition from AD to hand-optimized code as it does not require a change in the interface or the code structure. TAutoDiffVector is intended to be utilized for dense serial vectors.

Definition at line 40 of file tadvector.hpp.

Constructor & Destructor Documentation

◆ TAutoDiffVector() [1/5]

template<typename dtype>
mfem::TAutoDiffVector< dtype >::TAutoDiffVector ( )
inline

Default constructor for Vector. Sets size = 0 and data = NULL.

Definition at line 49 of file tadvector.hpp.

◆ TAutoDiffVector() [2/5]

template<typename dtype>
mfem::TAutoDiffVector< dtype >::TAutoDiffVector ( const TAutoDiffVector< dtype > &  v)
inline

Copy constructor. Allocates a new data array and copies the data.

Definition at line 57 of file tadvector.hpp.

◆ TAutoDiffVector() [3/5]

template<typename dtype>
mfem::TAutoDiffVector< dtype >::TAutoDiffVector ( const Vector v)
inline

Definition at line 78 of file tadvector.hpp.

◆ TAutoDiffVector() [4/5]

template<typename dtype>
mfem::TAutoDiffVector< dtype >::TAutoDiffVector ( int  s)
inlineexplicit

Creates vector of size s.

Warning
Entries are not initialized to zero!

Definition at line 101 of file tadvector.hpp.

◆ TAutoDiffVector() [5/5]

template<typename dtype>
mfem::TAutoDiffVector< dtype >::TAutoDiffVector ( dtype *  _data,
int  _size 
)
inline

Creates a vector referencing an array of doubles, owned by someone else.

The pointer _data can be NULL. The data array can be replaced later with SetData().

Definition at line 120 of file tadvector.hpp.

◆ ~TAutoDiffVector()

template<typename dtype>
mfem::TAutoDiffVector< dtype >::~TAutoDiffVector ( )
inline

Destroys vector.

Definition at line 589 of file tadvector.hpp.

Member Function Documentation

◆ Add()

template<typename dtype>
template<typename ivtype , typename vtype >
TAutoDiffVector& mfem::TAutoDiffVector< dtype >::Add ( const ivtype  a,
const vtype &  v 
)
inline

(*this) += a * Va

Definition at line 455 of file tadvector.hpp.

◆ Capacity()

template<typename dtype>
int mfem::TAutoDiffVector< dtype >::Capacity ( ) const
inline

Return the size of the currently allocated data array.

It is always true that Capacity() >= Size().

Definition at line 250 of file tadvector.hpp.

◆ Destroy()

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::Destroy ( )
inline

Destroy a vector.

Definition at line 238 of file tadvector.hpp.

◆ Elem() [1/2]

template<typename dtype>
dtype& mfem::TAutoDiffVector< dtype >::Elem ( int  i)
inline

Access Vector entries. Index i = 0 .. size-1.

Definition at line 291 of file tadvector.hpp.

◆ Elem() [2/2]

template<typename dtype>
const dtype& mfem::TAutoDiffVector< dtype >::Elem ( int  i) const
inline

Read only access to Vector entries. Index i = 0 .. size-1.

Definition at line 293 of file tadvector.hpp.

◆ GetData()

template<typename dtype>
dtype* mfem::TAutoDiffVector< dtype >::GetData ( ) const
inline

Return a pointer to the beginning of the Vector data.

Warning
This method should be used with caution as it gives write access to the data of const-qualified Vectors.

Definition at line 255 of file tadvector.hpp.

◆ Load() [1/3]

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::Load ( std::istream **  in,
int  np,
int *  dim 
)
inline

Reads a vector from multiple files.

Definition at line 132 of file tadvector.hpp.

◆ Load() [2/3]

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::Load ( std::istream &  in,
int  Size 
)
inline

Load a vector from an input stream.

Definition at line 156 of file tadvector.hpp.

◆ Load() [3/3]

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::Load ( std::istream &  in)
inline

Load a vector from an input stream, reading the size from the stream.

Definition at line 168 of file tadvector.hpp.

◆ MakeRef() [1/2]

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::MakeRef ( TAutoDiffVector< dtype > &  base,
int  offset,
int  size_ 
)
inline

Reset the Vector to be a reference to a sub-vector of base.

Definition at line 224 of file tadvector.hpp.

◆ MakeRef() [2/2]

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::MakeRef ( TAutoDiffVector< dtype > &  base,
int  offset 
)
inline

Reset the Vector to be a reference to a sub-vector of base without changing its current size.

Definition at line 231 of file tadvector.hpp.

◆ Neg()

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::Neg ( )
inline

(*this) = -(*this)

Definition at line 488 of file tadvector.hpp.

◆ NewDataAndSize()

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::NewDataAndSize ( dtype *  d,
int  s 
)
inline

Set the Vector data and size, deleting the old data, if owned.

The Vector does not assume ownership of the new data. The new size is also used as the new Capacity().

See also
SetDataAndSize().

Definition at line 221 of file tadvector.hpp.

◆ Norml1()

template<typename dtype>
dtype mfem::TAutoDiffVector< dtype >::Norml1 ( ) const
inline

Returns the l_1 norm of the vector.

Definition at line 686 of file tadvector.hpp.

◆ Norml2()

template<typename dtype>
dtype mfem::TAutoDiffVector< dtype >::Norml2 ( ) const
inline

Returns the l2 norm of the vector.

Definition at line 638 of file tadvector.hpp.

◆ Normlinf()

template<typename dtype>
dtype mfem::TAutoDiffVector< dtype >::Normlinf ( ) const
inline

Returns the l_infinity norm of the vector.

Definition at line 676 of file tadvector.hpp.

◆ operator const dtype *()

template<typename dtype>
mfem::TAutoDiffVector< dtype >::operator const dtype * ( ) const
inline

Conversion to const double *.

Note
This conversion function makes it possible to use [] for indexing in addition to the overloaded operator()(int).

Definition at line 268 of file tadvector.hpp.

◆ operator dtype *()

template<typename dtype>
mfem::TAutoDiffVector< dtype >::operator dtype * ( )
inline

Conversion to double *.

Note
This conversion function makes it possible to use [] for indexing in addition to the overloaded operator()(int).

Definition at line 263 of file tadvector.hpp.

◆ operator()() [1/2]

template<typename dtype>
dtype& mfem::TAutoDiffVector< dtype >::operator() ( int  i)
inline

Access Vector entries using () for 0-based indexing.

Note
If MFEM_DEBUG is enabled, bounds checking is performed.

Definition at line 297 of file tadvector.hpp.

◆ operator()() [2/2]

template<typename dtype>
const dtype& mfem::TAutoDiffVector< dtype >::operator() ( int  i) const
inline

Read only access to Vector entries using () for 0-based indexing.

Note
If MFEM_DEBUG is enabled, bounds checking is performed.

Definition at line 307 of file tadvector.hpp.

◆ operator*() [1/3]

template<typename dtype>
dtype mfem::TAutoDiffVector< dtype >::operator* ( const dtype *  v) const
inline

Dot product with a dtype * array.

Definition at line 316 of file tadvector.hpp.

◆ operator*() [2/3]

template<typename dtype>
dtype mfem::TAutoDiffVector< dtype >::operator* ( const TAutoDiffVector< dtype > &  v) const
inline

Return the inner-product.

Definition at line 327 of file tadvector.hpp.

◆ operator*() [3/3]

template<typename dtype>
dtype mfem::TAutoDiffVector< dtype >::operator* ( const Vector v) const
inline

Definition at line 338 of file tadvector.hpp.

◆ operator*=()

template<typename dtype>
template<typename ivtype >
TAutoDiffVector& mfem::TAutoDiffVector< dtype >::operator*= ( ivtype  c)
inline

Definition at line 394 of file tadvector.hpp.

◆ operator+=() [1/2]

template<typename dtype>
TAutoDiffVector& mfem::TAutoDiffVector< dtype >::operator+= ( const TAutoDiffVector< dtype > &  v)
inline

Definition at line 433 of file tadvector.hpp.

◆ operator+=() [2/2]

template<typename dtype>
template<typename ivtype >
TAutoDiffVector& mfem::TAutoDiffVector< dtype >::operator+= ( ivtype  v)
inline

Definition at line 444 of file tadvector.hpp.

◆ operator-=() [1/2]

template<typename dtype>
TAutoDiffVector& mfem::TAutoDiffVector< dtype >::operator-= ( const TAutoDiffVector< dtype > &  v)
inline

Definition at line 413 of file tadvector.hpp.

◆ operator-=() [2/2]

template<typename dtype>
template<typename ivtype >
TAutoDiffVector& mfem::TAutoDiffVector< dtype >::operator-= ( ivtype  v)
inline

Definition at line 424 of file tadvector.hpp.

◆ operator/=()

template<typename dtype>
template<typename ivtype >
TAutoDiffVector& mfem::TAutoDiffVector< dtype >::operator/= ( ivtype  c)
inline

Definition at line 404 of file tadvector.hpp.

◆ operator=() [1/4]

template<typename dtype>
TAutoDiffVector<dtype>& mfem::TAutoDiffVector< dtype >::operator= ( const dtype *  v)
inline

Copy Size() entries from v.

Definition at line 350 of file tadvector.hpp.

◆ operator=() [2/4]

template<typename dtype>
TAutoDiffVector<dtype>& mfem::TAutoDiffVector< dtype >::operator= ( const TAutoDiffVector< dtype > &  v)
inline

Copy assignment.

Note
Defining this method overwrites the implicitly defined copy assignment operator.

Definition at line 362 of file tadvector.hpp.

◆ operator=() [3/4]

template<typename dtype>
TAutoDiffVector<dtype>& mfem::TAutoDiffVector< dtype >::operator= ( const Vector v)
inline

Definition at line 372 of file tadvector.hpp.

◆ operator=() [4/4]

template<typename dtype>
template<typename ivtype >
TAutoDiffVector& mfem::TAutoDiffVector< dtype >::operator= ( ivtype  value)
inline

Redefine '=' for vector = constant.

Definition at line 384 of file tadvector.hpp.

◆ OwnsData()

template<typename dtype>
bool mfem::TAutoDiffVector< dtype >::OwnsData ( ) const
inline

Read the Vector data (host pointer) ownership flag.

Definition at line 271 of file tadvector.hpp.

◆ Print()

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::Print ( std::ostream &  os = mfem::out,
int  width = 8 
) const
inline

Prints vector to stream os with width entries per line.

Definition at line 592 of file tadvector.hpp.

◆ Randomize()

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::Randomize ( int  seed = 0)
inline

Set random values in the vector.

Definition at line 619 of file tadvector.hpp.

◆ Set()

template<typename dtype>
template<typename ivtype , typename vtype >
TAutoDiffVector& mfem::TAutoDiffVector< dtype >::Set ( const ivtype  a,
const vtype &  v 
)
inline

(*this) = a * x

Definition at line 467 of file tadvector.hpp.

◆ SetDataAndSize()

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::SetDataAndSize ( dtype *  d,
int  s 
)
inline

Set the Vector data and size.

The Vector does not assume ownership of the new data. The new size is

Warning
This method should be called only when OwnsData() is false.
See also
NewDataAndSize().

Definition at line 206 of file tadvector.hpp.

◆ SetSize()

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::SetSize ( int  s)
inline

Resize the vector to size s.

If the new size is less than or equal to Capacity() then the internal data array remains the same. Otherwise, the old array is deleted, if owned, and a new array of size s is allocated without copying the previous content of the Vector.

Warning
In the second case above (new size greater than current one), the vector will allocate new data array, even if it did not own the original data! Also, new entries are not initialized!

Definition at line 183 of file tadvector.hpp.

◆ SetVector()

template<typename dtype>
template<typename vtype >
void mfem::TAutoDiffVector< dtype >::SetVector ( const vtype &  v,
int  offset 
)
inline

Definition at line 478 of file tadvector.hpp.

◆ Size()

template<typename dtype>
int mfem::TAutoDiffVector< dtype >::Size ( ) const
inline

Returns the size of the vector.

Definition at line 246 of file tadvector.hpp.

◆ StealData() [1/2]

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::StealData ( dtype **  p)
inline

Changes the ownership of the data; after the call the Vector is empty.

Definition at line 274 of file tadvector.hpp.

◆ StealData() [2/2]

template<typename dtype>
dtype* mfem::TAutoDiffVector< dtype >::StealData ( )
inline

Changes the ownership of the data; after the call the Vector is empty.

Definition at line 283 of file tadvector.hpp.

◆ Swap()

template<typename dtype>
void mfem::TAutoDiffVector< dtype >::Swap ( TAutoDiffVector< dtype > &  other)
inline

Swap the contents of two Vectors.

Definition at line 497 of file tadvector.hpp.

Friends And Related Function Documentation

◆ add [1/4]

template<typename dtype>
template<typename vtype1 , typename vtype2 >
void add ( const vtype1 &  v1,
const vtype2 &  v2,
TAutoDiffVector< dtype > &  v 
)
friend

Set v = v1 + v2.

Definition at line 506 of file tadvector.hpp.

◆ add [2/4]

template<typename dtype>
template<typename vtype1 , typename vtype2 >
void add ( const vtype1 &  v1,
dtype  alpha,
const vtype2 &  v2,
TAutoDiffVector< dtype > &  v 
)
friend

Set v = v1 + alpha * v2.

Definition at line 518 of file tadvector.hpp.

◆ add [3/4]

template<typename dtype>
template<typename vtype1 , typename vtype2 >
void add ( const dtype  a,
const vtype1 &  x,
const dtype  b,
const vtype2 &  y,
TAutoDiffVector< dtype > &  z 
)
friend

Definition at line 532 of file tadvector.hpp.

◆ add [4/4]

template<typename dtype>
template<typename vtype1 , typename vtype2 >
void add ( const dtype  a,
const vtype1 &  x,
const vtype2 &  y,
TAutoDiffVector< dtype > &  z 
)
friend

Definition at line 548 of file tadvector.hpp.

◆ subtract [1/2]

template<typename dtype>
template<typename vtype1 , typename vtype2 >
void subtract ( const vtype1 &  x,
const vtype2 &  y,
TAutoDiffVector< dtype > &  z 
)
friend

Definition at line 563 of file tadvector.hpp.

◆ subtract [2/2]

template<typename dtype>
template<typename ivtype , typename vtype1 , typename vtype2 >
void subtract ( const ivtype  a,
const vtype1 &  x,
const vtype2 &  y,
TAutoDiffVector< dtype > &  z 
)
friend

Definition at line 575 of file tadvector.hpp.

Member Data Documentation

◆ capacity

template<typename dtype>
int mfem::TAutoDiffVector< dtype >::capacity
protected

Definition at line 45 of file tadvector.hpp.

◆ data

template<typename dtype>
dtype* mfem::TAutoDiffVector< dtype >::data
protected

Definition at line 43 of file tadvector.hpp.

◆ size

template<typename dtype>
int mfem::TAutoDiffVector< dtype >::size
protected

Definition at line 44 of file tadvector.hpp.


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