Cholla 3.0.1-dev
Cholla - Massively parallel hydro on GPUs
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
cuda_utilities::DeviceVector< T > Class Template Reference

A templatized class to encapsulate a device global memory pointer in a std::vector like interface complete with most of the usual methods. This class is intended to be used only in host code and does not work device side; Passing the pointer to a kernel can be done with the data() method. This class works for any device side pointer, scalar or array valued. More...

#include <DeviceVector.h>

Public Types

typedef T value_type
 

Public Member Functions

 DeviceVector () noexcept
 
 DeviceVector (size_t const size, bool const initialize=false)
 Construct a new Device Vector object by calling the _allocate private method.
 
 DeviceVector (DeviceVector< T > &&other) noexcept
 
DeviceVectoroperator= (DeviceVector< T > &&other) noexcept
 
 ~DeviceVector ()
 Destroy the Device Vector object by calling the _deAllocate private method.
 
 DeviceVector (const DeviceVector< T > &)=delete
 
DeviceVector< T > & operator= (const DeviceVector< T > &other)=delete
 
T * data ()
 Get the raw device pointer.
 
size_t size ()
 Get the number of elements in the array.
 
operator[] (size_t const &index)
 Overload the [] operator to return a value from device memory. This method performs a cudaMemcpy to copy the desired element to the host then returns it. Unlike the at() method this method does not perform bounds checking.
 
at (size_t const index)
 Return a value from device memory. This method performs a cudaMemcpy to copy the desired element to the host then returns it. Unlike the [] overload this method perform bounds checking.
 
void assign (T const &hostValue, size_t const &index=0)
 Assign a single value in the array. Should generally only be used when the pointer points to a scalar value. By default this writes hostValue to the 0th element of the array.
 
void resize (size_t const newSize)
 Resize the device container to contain newSize elements. If newSize is greater than the current size then all the values are kept and the rest of the array is default initialized. If newSize is smaller than the current size then the array is truncated and values at locations greater than newSize are lost. Keeping the values in the array requires that the new array be allocated, the values be copied, then the old array be freed; as such this method is quite slow and can use a large amount of memory. If you don't care about the values in the array then use the reset method.
 
void reset (size_t const newSize)
 Reset the size of the array. This frees the old array and allocates a new one; all values in the array may be lost. The values in memory are not initialized and therefore the behaviour of the default values is undefined.
 
void cpyHostToDevice (const T *arrIn, size_t const &arrSize)
 Copy the first arrSize elements of arrIn to the device.
 
void cpyHostToDevice (std::vector< T > const &vecIn)
 Copy the contents of a std::vector to the device.
 
void cpyDeviceToHost (T *arrOut, size_t const &arrSize)
 Copy the array from the device to a host array. Checks if the host array is large enough based on the arrSize parameter.
 
void cpyDeviceToHost (std::vector< T > &vecOut)
 Copy the array from the device to a host std::vector. Checks if the host array is large enough.
 

Detailed Description

template<typename T>
class cuda_utilities::DeviceVector< T >

A templatized class to encapsulate a device global memory pointer in a std::vector like interface complete with most of the usual methods. This class is intended to be used only in host code and does not work device side; Passing the pointer to a kernel can be done with the data() method. This class works for any device side pointer, scalar or array valued.

Template Parameters
TAny trivially copyable type where sizeof(T) returns correct results should work, but non-primitive types have not been tested.

Constructor & Destructor Documentation

◆ DeviceVector() [1/3]

template<typename T >
cuda_utilities::DeviceVector< T >::DeviceVector ( )
inlinenoexcept

Construct an empty DeviceVector

◆ DeviceVector() [2/3]

template<typename T >
cuda_utilities::DeviceVector< T >::DeviceVector ( size_t const  size,
bool const  initialize = false 
)

Construct a new Device Vector object by calling the _allocate private method.

Parameters
[in]sizeThe number of elements desired in the array. Can be any positive integer.
[in]initialize(optional) If true then initialize the GPU memory to int(0)

◆ DeviceVector() [3/3]

template<typename T >
cuda_utilities::DeviceVector< T >::DeviceVector ( DeviceVector< T > &&  other)
inlinenoexcept

Define a move constructor

Member Function Documentation

◆ assign()

template<typename T >
void cuda_utilities::DeviceVector< T >::assign ( T const &  hostValue,
size_t const &  index = 0 
)

Assign a single value in the array. Should generally only be used when the pointer points to a scalar value. By default this writes hostValue to the 0th element of the array.

Parameters
[in]hostValueThe value to write to the device array
[in]indexThe location to write the value to, defaults to zero.

◆ at()

template<typename T >
T cuda_utilities::DeviceVector< T >::at ( size_t const  index)

Return a value from device memory. This method performs a cudaMemcpy to copy the desired element to the host then returns it. Unlike the [] overload this method perform bounds checking.

Parameters
[in]indexThe index of the desired value
Returns
T The value at dev_ptr[index]

◆ cpyDeviceToHost() [1/2]

template<typename T >
void cuda_utilities::DeviceVector< T >::cpyDeviceToHost ( std::vector< T > &  vecOut)
inline

Copy the array from the device to a host std::vector. Checks if the host array is large enough.

Parameters
[out]vecOutThe std::vector to copy the device array into

◆ cpyDeviceToHost() [2/2]

template<typename T >
void cuda_utilities::DeviceVector< T >::cpyDeviceToHost ( T *  arrOut,
size_t const &  arrSize 
)

Copy the array from the device to a host array. Checks if the host array is large enough based on the arrSize parameter.

Parameters
[out]arrOutThe pointer to the host array
[in]arrSizeThe number of elements allocated in the host array

◆ cpyHostToDevice() [1/2]

template<typename T >
void cuda_utilities::DeviceVector< T >::cpyHostToDevice ( const T *  arrIn,
size_t const &  arrSize 
)

Copy the first arrSize elements of arrIn to the device.

Parameters
[in]arrInThe pointer to the array to be copied to the device
[in]arrSizeThe number of elements/size of the array to copy to the device

◆ cpyHostToDevice() [2/2]

template<typename T >
void cuda_utilities::DeviceVector< T >::cpyHostToDevice ( std::vector< T > const &  vecIn)
inline

Copy the contents of a std::vector to the device.

Parameters
[in]vecInThe array whose contents are to be copied

◆ data()

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

Get the raw device pointer.

Returns
T* The pointer for the array in global memory

◆ operator=()

template<typename T >
DeviceVector & cuda_utilities::DeviceVector< T >::operator= ( DeviceVector< T > &&  other)
inlinenoexcept

Define move assignment

◆ operator[]()

template<typename T >
T cuda_utilities::DeviceVector< T >::operator[] ( size_t const &  index)

Overload the [] operator to return a value from device memory. This method performs a cudaMemcpy to copy the desired element to the host then returns it. Unlike the at() method this method does not perform bounds checking.

Parameters
[in]indexThe index of the desired value
Returns
T The value at dev_ptr[index]

◆ reset()

template<typename T >
void cuda_utilities::DeviceVector< T >::reset ( size_t const  newSize)

Reset the size of the array. This frees the old array and allocates a new one; all values in the array may be lost. The values in memory are not initialized and therefore the behaviour of the default values is undefined.

Parameters
newSize

◆ resize()

template<typename T >
void cuda_utilities::DeviceVector< T >::resize ( size_t const  newSize)

Resize the device container to contain newSize elements. If newSize is greater than the current size then all the values are kept and the rest of the array is default initialized. If newSize is smaller than the current size then the array is truncated and values at locations greater than newSize are lost. Keeping the values in the array requires that the new array be allocated, the values be copied, then the old array be freed; as such this method is quite slow and can use a large amount of memory. If you don't care about the values in the array then use the reset method.

Parameters
[in]newSizeThe desired size of the array

◆ size()

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

Get the number of elements in the array.

Returns
size_t The number of elements in the array

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