Gromacs  2024.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Public Member Functions
gmx::PaddedVector< T, Allocator > Class Template Reference

#include <gromacs/math/include/gromacs/math/paddedvector.h>

Description

template<typename T, typename Allocator = Allocator<T, AlignedAllocationPolicy>>
class gmx::PaddedVector< T, Allocator >

PaddedVector is a container of elements in contiguous storage that allocates extra memory for safe SIMD-style loads for operations used in GROMACS.

Template Parameters
Tthe type of objects within the container
Allocatorthe allocator used. Can be any standard-compliant allocator, such gmx::Allocator used for alignment and/or pinning.

The interface resembles std::vector. However, access intended to include padded elements must be via ArrayRef objects explicitly created to view those elements. Most other aspects of this vector refer to the unpadded view, e.g. iterators, data(), size().

The underlying storage is allocated with extra elements, properly initialized, that ensure that any operations accessing the any non-additional element that operate on memory equivalent to a full SIMD lane do so on allocated memory that has been initialized, so that memory traps will not occur, and arithmetic operations will not cause e.g. floating-point exceptions so long as the values in the padded elements are properly managed.

Proper initialization is tricker than it would first appear, since we intend this container to be used with scalar and class types (e.g. RVec). Resize and construction operations use "default insertion" which leads to zero initialization for the former, and calling the default constructor for the latter. BasicVector has a default constructor that leaves the elements uninitialized, which is particularly risky for elements only present as padding. Thus the implementation specifically initializes the padded elements to zero, which makes no difference to the scalar template instantiations, and makes the BasicVector ones safer to use.

Because the allocator can be configured, the memory allocation can have other attributes such as SIMD alignment or being pinned to physical memory for efficient transfers. The default allocator ensures alignment, but std::allocator also works.

Public Types

using value_type = T
 Standard helper types.
 
using allocator_type = Allocator
 
using size_type = Index
 
using reference = value_type &
 
using const_reference = const value_type &
 
using storage_type = std::vector< T, allocator_type >
 
using pointer = typename storage_type::pointer
 
using const_pointer = typename storage_type::const_pointer
 
using iterator = typename storage_type::iterator
 
using const_iterator = typename storage_type::const_iterator
 
using difference_type = typename storage_type::iterator::difference_type
 

Public Member Functions

 PaddedVector (size_type count, const allocator_type &allocator=Allocator())
 Constructor that specifies the initial size.
 
 PaddedVector (size_type count, value_type const &v, const allocator_type &allocator=Allocator())
 Constructor that specifies the initial size and an element to copy.
 
 PaddedVector (allocator_type const &allocator)
 Default constructor with allocator.
 
 PaddedVector (PaddedVector const &o)
 Copy constructor.
 
 PaddedVector (PaddedVector &&o) noexcept
 Move constructor. More...
 
 unpaddedEnd_ (o.unpaddedEnd_)
 
 PaddedVector (PaddedVector &&o, const Allocator &alloc) noexcept
 Move constructor using alloc for the new vector. More...
 
 PaddedVector (std::initializer_list< value_type > const &il)
 Construct from an initializer list.
 
void reserveWithPadding (const size_type newExtent)
 Reserve storage for the container to contain newExtent elements, plus the required padding.
 
void resizeWithPadding (const size_type newSize)
 Resize the container to contain newSize elements, plus the required padding.
 
size_type size () const
 Return the size of the view without the padding.
 
size_type paddedSize () const
 Return the container size including the padding.
 
bool empty () const
 Return whether the storage is empty.
 
void swap (PaddedVector &x)
 Swap two PaddedVectors.
 
void clear ()
 Clear the vector, ie. set size to zero and remove padding.
 
reference operator[] (int i)
 Indexing operator.
 
const_reference operator[] (int i) const
 Indexing operator as const.
 
ArrayRefWithPadding< T > arrayRefWithPadding ()
 Returns an ArrayRef of elements that includes the padding region, e.g. for use in SIMD code.
 
ArrayRefWithPadding< const T > constArrayRefWithPadding () const
 Returns an ArrayRef of const elements that includes the padding region, e.g. for use in SIMD code.
 
template<typename AlsoT = T, typename = typename std::enable_if<std::is_same<AlsoT, RVec>::value>>
rvecrvec_array ()
 Returns an rvec * pointer for containers of RVec, for use with legacy code. More...
 
template<typename AlsoT = T, typename = typename std::enable_if<std::is_same<AlsoT, RVec>::value>>
const rvecrvec_array () const
 Returns a const rvec * pointer for containers of RVec, for use with legacy code. More...
 
PaddedVectoroperator= (PaddedVector const &o)
 Copy assignment operator.
 
PaddedVectoroperator= (PaddedVector &&o) noexcept
 Move assignment operator.
 
allocator_type get_allocator () const
 Getter for the allocator.
 
pointer data () noexcept
 Iterator getters refer to a view without padding.
 
const_pointer data () const noexcept
 
iterator begin ()
 
iterator end ()
 
const_iterator cbegin ()
 
const_iterator cend ()
 
const_iterator begin () const
 
const_iterator end () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 

Constructor & Destructor Documentation

template<typename T, typename Allocator = Allocator<T, AlignedAllocationPolicy>>
gmx::PaddedVector< T, Allocator >::PaddedVector ( PaddedVector< T, Allocator > &&  o)
inlinenoexcept

Move constructor.

Leaves o in a valid state (ie the destructor can be called).

template<typename T, typename Allocator = Allocator<T, AlignedAllocationPolicy>>
gmx::PaddedVector< T, Allocator >::PaddedVector ( PaddedVector< T, Allocator > &&  o,
const Allocator alloc 
)
inlinenoexcept

Move constructor using alloc for the new vector.

Note that alloc is another instance of the same allocator type as used for PaddedVector. This makes sense e.g. for stateful allocators such as HostAllocator used in PaddedHostVector.

Leaves o in a valid state (ie. the destructor can be called).

Member Function Documentation

template<typename T, typename Allocator = Allocator<T, AlignedAllocationPolicy>>
template<typename AlsoT = T, typename = typename std::enable_if<std::is_same<AlsoT, RVec>::value>>
rvec* gmx::PaddedVector< T, Allocator >::rvec_array ( )
inline

Returns an rvec * pointer for containers of RVec, for use with legacy code.

Todo:
Use std::is_same_v when CUDA 11 is a requirement.
template<typename T, typename Allocator = Allocator<T, AlignedAllocationPolicy>>
template<typename AlsoT = T, typename = typename std::enable_if<std::is_same<AlsoT, RVec>::value>>
const rvec* gmx::PaddedVector< T, Allocator >::rvec_array ( ) const
inline

Returns a const rvec * pointer for containers of RVec, for use with legacy code.

Todo:
Use std::is_same_v when CUDA 11 is a requirement.

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