OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
pfc::gpu::GPUVector< T > Class Template Reference

Simple GPU memory container (RAII) More...

#include <gpu_vector.hpp>

Public Member Functions

 GPUVector (size_t size)
 Construct a GPUVector with the specified size.
 
 ~GPUVector ()
 Destructor - automatically frees GPU memory.
 
 GPUVector (const GPUVector &)=delete
 
GPUVectoroperator= (const GPUVector &)=delete
 
 GPUVector (GPUVector &&other) noexcept
 Move constructor.
 
GPUVectoroperator= (GPUVector &&other) noexcept
 Move assignment operator.
 
T * data ()
 Get pointer to GPU memory.
 
const T * data () const
 Get const pointer to GPU memory.
 
size_t size () const
 Get the number of elements.
 
bool empty () const
 Check if the vector is empty.
 
void copy_from_host (const std::vector< T > &host_data)
 Copy data from host (CPU) to device (GPU)
 
void copy_to_host (std::vector< T > &host_data) const
 Copy data from device (GPU) to host (CPU)
 
std::vector< T > to_host () const
 Copy data from device to host and return as vector.
 

Detailed Description

template<typename T>
class pfc::gpu::GPUVector< T >

Simple GPU memory container (RAII)

Similar to std::vector but memory lives on GPU. Fixed size (no resizing).

Template Parameters
TElement type (must be trivially copyable)
Note
This class only works when CUDA is enabled at compile time. On systems without CUDA, the class exists but operations will fail at runtime (or compile-time if properly guarded).
Warning
Do not use this class directly on systems without CUDA. Always check OpenPFC_ENABLE_CUDA before using.

Constructor & Destructor Documentation

◆ GPUVector()

template<typename T >
pfc::gpu::GPUVector< T >::GPUVector ( size_t  size)
inlineexplicit

Construct a GPUVector with the specified size.

Allocates GPU memory for size elements of type T.

Parameters
sizeNumber of elements to allocate
Exceptions
std::runtime_errorif GPU memory allocation fails
Note
On systems without CUDA, this will throw at runtime. Always guard usage with ifdef OpenPFC_ENABLE_CUDA.
Here is the call graph for this function:

Member Function Documentation

◆ copy_from_host()

template<typename T >
void pfc::gpu::GPUVector< T >::copy_from_host ( const std::vector< T > &  host_data)
inline

Copy data from host (CPU) to device (GPU)

Parameters
host_dataSource data on CPU
Exceptions
std::runtime_errorif sizes don't match or copy fails

◆ copy_to_host()

template<typename T >
void pfc::gpu::GPUVector< T >::copy_to_host ( std::vector< T > &  host_data) const
inline

Copy data from device (GPU) to host (CPU)

Parameters
host_dataDestination vector (will be resized if needed)
Here is the caller graph for this function:

◆ data()

template<typename T >
T * pfc::gpu::GPUVector< T >::data ( )
inline

Get pointer to GPU memory.

Returns
Pointer to device memory (nullptr if size is 0)
Warning
This pointer is only valid on the GPU. Do not dereference on the CPU. Use copy_from_host/copy_to_host for CPU access.

◆ to_host()

template<typename T >
std::vector< T > pfc::gpu::GPUVector< T >::to_host ( ) const
inline

Copy data from device to host and return as vector.

Returns
std::vector<T> containing the data from GPU
Here is the call graph for this function:

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