mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
mx::cuda::cudaPtr< T > Struct Template Reference

template<typename T>
struct mx::cuda::cudaPtr< T >

A smart-pointer wrapper for cuda device pointers.

Definition at line 46 of file cudaPtr.hpp.

#include <math/cuda/cudaPtr.hpp>

Public Types

typedef T hostPtrT
 The host data type. More...
 
typedef T devicePtrT
 The device data type. More...
 

Public Member Functions

 ~cudaPtr ()
 Destructor, frees memory if allocated. More...
 
int resize (size_t sz)
 Resize the memory allocation, in 1D. More...
 
int resize (size_t x_sz, size_t y_sz)
 Resize the memory allocation, in 2D. More...
 
int resize (size_t x_sz, size_t y_sz, size_t z_sz)
 Resize the memory allocation, in 3D. More...
 
cudaError_t initialize ()
 Initialize the array bytes to 0. More...
 
int free ()
 Free the memory allocation. More...
 
int upload (const hostPtrT *src)
 Copy from the host to the device, after allocation. More...
 
int upload (const hostPtrT *src, size_t sz)
 Copy from the host to the device with allocation. More...
 
int download (hostPtrT *dest)
 Copy from the device to the host. More...
 
cpp2cudaType< devicePtrT >::cudaType * operator() ()
 Conversion operator, accesses the device pointer for use in Cuda functions. More...
 
const cpp2cudaType< devicePtrT >::cudaType * operator() () const
 Conversion operator, accesses the device pointer for use in Cuda functions. More...
 

Public Attributes

devicePtrTm_devicePtr {nullptr}
 The device pointer. More...
 
size_t m_size {0}
 The allocated size. More...
 

Member Typedef Documentation

◆ devicePtrT

template<typename T >
typedef T mx::cuda::cudaPtr< T >::devicePtrT

The device data type.

Definition at line 54 of file cudaPtr.hpp.

◆ hostPtrT

template<typename T >
typedef T mx::cuda::cudaPtr< T >::hostPtrT

The host data type.

Definition at line 50 of file cudaPtr.hpp.

Constructor & Destructor Documentation

◆ ~cudaPtr()

template<typename T >
mx::cuda::cudaPtr< T >::~cudaPtr

Destructor, frees memory if allocated.

Definition at line 177 of file cudaPtr.hpp.

Member Function Documentation

◆ download()

template<typename T >
int mx::cuda::cudaPtr< T >::download ( hostPtrT dest)

Copy from the device to the host.

Test:

Scenario: scaling a vector with cublas [test doc]

Scenario: scaling and accumulating a vector with cublas [test doc]

Scenario: multiplying two vectors element by element [test doc]

Parameters
[in]destThe host location, allocated.

Definition at line 274 of file cudaPtr.hpp.

Referenced by SCENARIO().

◆ free()

template<typename T >
int mx::cuda::cudaPtr< T >::free

Free the memory allocation.

Returns
0 on success.
a cuda error code otherwise.

Definition at line 225 of file cudaPtr.hpp.

◆ initialize()

template<typename T >
cudaError_t mx::cuda::cudaPtr< T >::initialize

Initialize the array bytes to 0.

Just a wrapper to cudaMemset.

Definition at line 219 of file cudaPtr.hpp.

Referenced by SCENARIO().

◆ operator()() [1/2]

template<typename T >
cpp2cudaType<devicePtrT>::cudaType* mx::cuda::cudaPtr< T >::operator() ( )
inline

Conversion operator, accesses the device pointer for use in Cuda functions.

Test:

Scenario: scaling and accumulating a vector with cublas [test doc]

Scenario: multiplying two vectors element by element [test doc]

Definition at line 159 of file cudaPtr.hpp.

References mx::cuda::cudaPtr< T >::m_devicePtr.

◆ operator()() [2/2]

template<typename T >
const cpp2cudaType<devicePtrT>::cudaType* mx::cuda::cudaPtr< T >::operator() ( ) const
inline

Conversion operator, accesses the device pointer for use in Cuda functions.

Test:
Scenario: scaling a vector with cublas [test doc]

Definition at line 169 of file cudaPtr.hpp.

References mx::cuda::cudaPtr< T >::m_devicePtr.

◆ resize() [1/3]

template<typename T >
int mx::cuda::cudaPtr< T >::resize ( size_t  sz)

Resize the memory allocation, in 1D.

If no size change, this is a no-op.

Returns
0 on success.
a cuda error code otherwise.
Test:

Scenario: scaling a vector with cublas [test doc]

Scenario: scaling and accumulating a vector with cublas [test doc]

Scenario: multiplying two vectors element by element [test doc]

Parameters
[in]szthe new size

Definition at line 183 of file cudaPtr.hpp.

Referenced by SCENARIO().

◆ resize() [2/3]

template<typename T >
int mx::cuda::cudaPtr< T >::resize ( size_t  x_sz,
size_t  y_sz 
)

Resize the memory allocation, in 2D.

If no size change, this is a no-op.

Returns
0 on success.
a cuda error code otherwise.
Parameters
[in]x_szthe new x size,
[in]y_szthe new y size

Definition at line 202 of file cudaPtr.hpp.

◆ resize() [3/3]

template<typename T >
int mx::cuda::cudaPtr< T >::resize ( size_t  x_sz,
size_t  y_sz,
size_t  z_sz 
)

Resize the memory allocation, in 3D.

If no size change, this is a no-op.

Returns
0 on success.
a cuda error code otherwise.
Parameters
[in]x_szthe new x size,
[in]y_szthe new y size,
[in]z_szthe new z size

Definition at line 210 of file cudaPtr.hpp.

◆ upload() [1/2]

template<typename T >
int mx::cuda::cudaPtr< T >::upload ( const hostPtrT src)

Copy from the host to the device, after allocation.

The device pointer must be allocated.

Returns
0 on success.
a cuda error code otherwise.
Test:
Scenario: multiplying two vectors element by element [test doc]
Parameters
[in]srcThe host location

Definition at line 245 of file cudaPtr.hpp.

Referenced by SCENARIO().

◆ upload() [2/2]

template<typename T >
int mx::cuda::cudaPtr< T >::upload ( const hostPtrT src,
size_t  sz 
)

Copy from the host to the device with allocation.

The device pointer will be re-allocated as needed.

Returns
0 on success.
a cuda error code otherwise.
Test:

Scenario: scaling a vector with cublas [test doc]

Scenario: scaling and accumulating a vector with cublas [test doc]

Scenario: multiplying two vectors element by element [test doc]

Parameters
[in]srcThe host location
[in]szThe size of the array

Definition at line 260 of file cudaPtr.hpp.

Member Data Documentation

◆ m_devicePtr

template<typename T >
devicePtrT* mx::cuda::cudaPtr< T >::m_devicePtr {nullptr}

The device pointer.

Definition at line 57 of file cudaPtr.hpp.

Referenced by mx::cuda::cudaPtr< T >::operator()().

◆ m_size

template<typename T >
size_t mx::cuda::cudaPtr< T >::m_size {0}

The allocated size.

Definition at line 60 of file cudaPtr.hpp.


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