Graphitti
A toolkit/software architecture to ease creating high-performance neural network simulators
Loading...
Searching...
No Matches
DeviceVector< T > Class Template Reference

Public Types

using reference
 Reference type that handles std::vector<bool> specialization.
 

Public Member Functions

 DeviceVector (size_t size=0)
 Constructor that initializes the host vector without GPU allocation.
 
 DeviceVector (const DeviceVector &)=delete
 Copy constructor is deleted to prevent accidental copies that could lead to GPU memory leaks.
 
DeviceVectoroperator= (const DeviceVector &)=delete
 Copy assignment is deleted to prevent accidental copies that could lead to GPU memory leaks.
 
void push_back (const T &value)
 Adds an element to the end of the host vector.
 
void resize (size_t new_size)
 Resizes the host vector to contain new_size elements.
 
void resize (size_t new_size, const T &value)
 Resizes the host vector to new_size elements, initializing new elements with value.
 
void clear ()
 Removes all elements from the host vector.
 
void reserve (size_t new_cap)
 Reserves storage in the host vector for at least new_cap elements.
 
size_t size () const
 Returns the number of elements in the host vector.
 
bool empty () const
 Checks if the host vector contains no elements.
 
void assign (size_t n, const T &value)
 Replaces host vector contents with n copies of value.
 
const std::vector< T > & getHostVector () const
 Gets a const reference to the underlying host vector.
 
std::vector< T > getHostVector ()
 Gets a copy of the host vector.
 
 operator std::vector< T > & ()
 Implicit conversion to host vector reference.
 
 operator const std::vector< T > & () const
 Implicit conversion to const host vector reference.
 
reference operator[] (size_t idx)
 Array subscript operator for host vector access.
 
const T operator[] (size_t idx) const
 Const array subscript operator for host vector access.
 
T * data ()
 Gets pointer to contiguous host memory array.
 
const T * data () const
 Gets const pointer to contiguous host memory array.
 
T & at (size_t idx)
 Safe element access for host vector with bounds checking.
 
const T & at (size_t idx) const
 Safe const element access for host vector with bounds checking.
 
T & front ()
 Access first element of host vector.
 
const T & front () const
 Access first element of host vector (const)
 
T & back ()
 Access last element of host vector.
 
const T & back () const
 Access last element of host vector (const)
 
auto begin ()
 Gets iterator to the first element of host vector.
 
auto end ()
 Gets iterator to one past the last element.
 
auto begin () const
 Gets const iterator to the first element.
 
auto end () const
 Gets const iterator to one past the last element.
 

Detailed Description

template<typename T>
class DeviceVector< T >

Definition at line 77 of file DeviceVector.h.

Member Typedef Documentation

◆ reference

template<typename T >
using DeviceVector< T >::reference
Initial value:
typename std::conditional_t<std::is_same_v<T, bool>, std::vector<bool>::reference, T &>

Reference type that handles std::vector<bool> specialization.

Definition at line 84 of file DeviceVector.h.

Constructor & Destructor Documentation

◆ DeviceVector()

template<typename T >
DeviceVector< T >::DeviceVector ( size_t size = 0)
inlineexplicit

Constructor that initializes the host vector without GPU allocation.

Parameters
sizeInitial size of the host vector (defaults to 0)
Postcondition
Host vector is created with 'size' default-constructed elements
Device pointer is nullptr (no GPU memory allocated)

Definition at line 91 of file DeviceVector.h.

Member Function Documentation

◆ assign()

template<typename T >
void DeviceVector< T >::assign ( size_t n,
const T & value )
inline

Replaces host vector contents with n copies of value.

Parameters
nNumber of elements to assign
valueValue to fill the vector with
Postcondition
Host vector contains exactly n elements
All elements are copies of value

Definition at line 169 of file DeviceVector.h.

◆ at() [1/2]

template<typename T >
T & DeviceVector< T >::at ( size_t idx)
inline

Safe element access for host vector with bounds checking.

Parameters
idxIndex of the element to access
Returns
Reference to the host vector element at specified index
Note
Performs bounds checking unlike operator[]
Exceptions
std::out_of_rangeif idx >= size()

Definition at line 253 of file DeviceVector.h.

◆ at() [2/2]

template<typename T >
const T & DeviceVector< T >::at ( size_t idx) const
inline

Safe const element access for host vector with bounds checking.

Parameters
idxIndex of the element to access
Returns
Const reference to the host vector element at specified index
Note
Performs bounds checking unlike operator[]
Exceptions
std::out_of_rangeif idx >= size()

Definition at line 263 of file DeviceVector.h.

◆ back() [1/2]

template<typename T >
T & DeviceVector< T >::back ( )
inline

Access last element of host vector.

Returns
Reference to the last element
Precondition
Vector must not be empty
Exceptions
std::out_of_rangeif vector is empty

Definition at line 290 of file DeviceVector.h.

◆ back() [2/2]

template<typename T >
const T & DeviceVector< T >::back ( ) const
inline

Access last element of host vector (const)

Returns
Const reference to the last element
Precondition
Vector must not be empty
Exceptions
std::out_of_rangeif vector is empty

Definition at line 299 of file DeviceVector.h.

◆ begin() [1/2]

template<typename T >
auto DeviceVector< T >::begin ( )
inline

Gets iterator to the first element of host vector.

Returns
Iterator to the beginning
Note
Enables range-based for loop support

Definition at line 307 of file DeviceVector.h.

◆ begin() [2/2]

template<typename T >
auto DeviceVector< T >::begin ( ) const
inline

Gets const iterator to the first element.

Returns
Const iterator to the beginning
Warning
Iterator only valid for host memory

Definition at line 323 of file DeviceVector.h.

◆ clear()

template<typename T >
void DeviceVector< T >::clear ( )
inline

Removes all elements from the host vector.

Postcondition
Host vector becomes empty (size = 0)
Capacity remains unchanged

Definition at line 135 of file DeviceVector.h.

◆ data() [1/2]

template<typename T >
T * DeviceVector< T >::data ( )
inline

Gets pointer to contiguous host memory array.

Returns
Pointer to the first element in host memory
Note
Returns nullptr if vector is empty

Definition at line 235 of file DeviceVector.h.

◆ data() [2/2]

template<typename T >
const T * DeviceVector< T >::data ( ) const
inline

Gets const pointer to contiguous host memory array.

Returns
Const pointer to the first element in host memory
Note
Returns nullptr if vector is empty

Definition at line 243 of file DeviceVector.h.

◆ empty()

template<typename T >
bool DeviceVector< T >::empty ( ) const
inline

Checks if the host vector contains no elements.

Returns
true if the host vector is empty, false otherwise

Definition at line 159 of file DeviceVector.h.

◆ end() [1/2]

template<typename T >
auto DeviceVector< T >::end ( )
inline

Gets iterator to one past the last element.

Returns
Iterator to the end
Note
Required for range-based for loop support

Definition at line 315 of file DeviceVector.h.

◆ end() [2/2]

template<typename T >
auto DeviceVector< T >::end ( ) const
inline

Gets const iterator to one past the last element.

Returns
Const iterator to the end
Warning
Iterator only valid for host memory

Definition at line 331 of file DeviceVector.h.

◆ front() [1/2]

template<typename T >
T & DeviceVector< T >::front ( )
inline

Access first element of host vector.

Returns
Reference to the first element
Precondition
Vector must not be empty
Exceptions
std::out_of_rangeif vector is empty

Definition at line 272 of file DeviceVector.h.

◆ front() [2/2]

template<typename T >
const T & DeviceVector< T >::front ( ) const
inline

Access first element of host vector (const)

Returns
Const reference to the first element
Precondition
Vector must not be empty
Exceptions
std::out_of_rangeif vector is empty

Definition at line 281 of file DeviceVector.h.

◆ getHostVector() [1/2]

template<typename T >
std::vector< T > DeviceVector< T >::getHostVector ( )
inline

Gets a copy of the host vector.

Returns
Copy of the host vector

Definition at line 183 of file DeviceVector.h.

◆ getHostVector() [2/2]

template<typename T >
const std::vector< T > & DeviceVector< T >::getHostVector ( ) const
inline

Gets a const reference to the underlying host vector.

Returns
Const reference to the host vector

Definition at line 176 of file DeviceVector.h.

◆ operator const std::vector< T > &()

template<typename T >
DeviceVector< T >::operator const std::vector< T > & ( ) const
inline

Implicit conversion to const host vector reference.

Returns
Const reference to the underlying host vector
Note
Allows using const DeviceVector as a const std::vector
Thread-safe for concurrent reads

Definition at line 201 of file DeviceVector.h.

◆ operator std::vector< T > &()

template<typename T >
DeviceVector< T >::operator std::vector< T > & ( )
inline

Implicit conversion to host vector reference.

Returns
Reference to the underlying host vector
Note
Allows using DeviceVector as a std::vector
Warning
Modifications affect host memory only

Definition at line 192 of file DeviceVector.h.

◆ operator[]() [1/2]

template<typename T >
reference DeviceVector< T >::operator[] ( size_t idx)
inline

Array subscript operator for host vector access.

Parameters
idxIndex of the element to access
Returns
Reference (or proxy for bool) to the element
Note
Using reference type alias to match std::vector<bool> behavior that allows bit-level manipulation
Warning
No bounds checking is performed

Definition at line 211 of file DeviceVector.h.

◆ operator[]() [2/2]

template<typename T >
const T DeviceVector< T >::operator[] ( size_t idx) const
inline

Const array subscript operator for host vector access.

Parameters
idxIndex of the element to access
Returns
Copy of the element (actual bool for bool type)
Note
Special handling for bool type to return actual bool value
Warning
No bounds checking is performed

Definition at line 224 of file DeviceVector.h.

◆ push_back()

template<typename T >
void DeviceVector< T >::push_back ( const T & value)
inline

Adds an element to the end of the host vector.

Parameters
valueThe element to append
Postcondition
New element is added at the end of host vector
Note
Only modifies host memory, device memory remains unchanged

Definition at line 107 of file DeviceVector.h.

◆ reserve()

template<typename T >
void DeviceVector< T >::reserve ( size_t new_cap)
inline

Reserves storage in the host vector for at least new_cap elements.

Parameters
new_capMinimum capacity to reserve
Postcondition
Host vector capacity is at least new_cap
Note
No reallocation occurs until size exceeds new capacity
Size remains unchanged

Definition at line 145 of file DeviceVector.h.

◆ resize() [1/2]

template<typename T >
void DeviceVector< T >::resize ( size_t new_size)
inline

Resizes the host vector to contain new_size elements.

Parameters
new_sizeNew size of the vector
Postcondition
Host vector contains exactly new_size elements
Note
Only affects host memory, device memory size remains unchanged

Definition at line 116 of file DeviceVector.h.

◆ resize() [2/2]

template<typename T >
void DeviceVector< T >::resize ( size_t new_size,
const T & value )
inline

Resizes the host vector to new_size elements, initializing new elements with value.

Parameters
new_sizeNew size of the vector
valueValue to initialize new elements with
Postcondition
Host vector contains exactly new_size elements
New elements (if any) are copies of value
Note
Only affects host memory, device memory size remains unchanged

Definition at line 127 of file DeviceVector.h.

◆ size()

template<typename T >
size_t DeviceVector< T >::size ( ) const
inline

Returns the number of elements in the host vector.

Returns
Current number of elements in host vector

Definition at line 152 of file DeviceVector.h.


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