OpenMPCD
Public Member Functions | Static Public Member Functions | List of all members
OpenMPCD::CUDA::DeviceMemoryManager Class Reference

Class for managing memory on the CUDA Device. More...

#include <DeviceMemoryManager.hpp>

Public Member Functions

 DeviceMemoryManager ()
 The constructor. More...
 
 ~DeviceMemoryManager ()
 The destructor. More...
 
template<typename Pointee >
Pointee * allocateMemory (const unsigned int instanceCount)
 Allocates Device memory for the given number of instances of the supplied type. More...
 
template<typename Pointee >
void allocateMemory (Pointee **pointerToPointer, const unsigned int instanceCount)
 Allocates Device memory for the given number of instances of the supplied type. More...
 
void freeMemory (void *const pointer)
 Frees the Device memory pointed to by the given pointer. More...
 
void setAutofree (const bool enable)
 Sets, or unsets, the autofree flag. More...
 

Static Public Member Functions

template<typename Pointee >
static Pointee * allocateMemoryUnregistered (const unsigned int instanceCount)
 Allocates Device memory for the given number of instances of the supplied type. More...
 
template<typename Pointee >
static void allocateMemoryUnregistered (Pointee **pointerToPointer, const unsigned int instanceCount)
 Allocates Device memory for the given number of instances of the supplied type. More...
 
static void freeMemoryUnregistered (void *const pointer)
 Frees the Device memory pointed to by the given pointer. More...
 
static bool isDeviceMemoryPointer (const void *const ptr)
 Returns whether the given pointer is a pointer to CUDA Device memory. More...
 
static bool isHostMemoryPointer (const void *const ptr)
 Returns whether the given pointer is a pointer to Host memory. More...
 
template<typename T >
static void copyElementsFromHostToDevice (const T *const src, T *const dest, const std::size_t count)
 Copies count elements of type T from the Host to the Device. More...
 
template<typename T >
static void copyElementsFromDeviceToHost (const T *const src, T *const dest, const std::size_t count)
 Copies count elements of type T from the Device to the Host. More...
 
template<typename T >
static void copyElementsFromDeviceToDevice (const T *const src, T *const dest, const std::size_t count)
 Copies count elements of type T from the Device to the Device. More...
 
template<typename T >
static void zeroMemory (T *const start, const std::size_t numberOfElements)
 Writes zero-bytes to the given Device memory region. More...
 
template<typename T >
static bool elementMemoryEqualOnHostAndDevice (const T *const host, const T *const device, const std::size_t count)
 Returns whether the count elements of type T at host in Host memory have the same byte-wise representation as the count elements at device on the CUDA Device. More...
 

Detailed Description

Class for managing memory on the CUDA Device.

Definition at line 21 of file DeviceMemoryManager.hpp.

Constructor & Destructor Documentation

◆ DeviceMemoryManager()

DeviceMemoryManager::DeviceMemoryManager ( )

The constructor.

The autofree feature is disabled by default.

Definition at line 12 of file DeviceMemoryManager.cpp.

◆ ~DeviceMemoryManager()

DeviceMemoryManager::~DeviceMemoryManager ( )

The destructor.

If there are Device buffers which have not been freed, and the autofree flag has not been set, prints corresponding warning messages to stderr.

Definition at line 17 of file DeviceMemoryManager.cpp.

Member Function Documentation

◆ allocateMemory() [1/2]

template<typename Pointee >
Pointee* OpenMPCD::CUDA::DeviceMemoryManager::allocateMemory ( const unsigned int  instanceCount)
inline

Allocates Device memory for the given number of instances of the supplied type.

The returned pointer points to at least instanceCount * sizeof(Pointee) bytes of allocated Device memory.

Memory allocated through this function is registered with this instance. Unless the memory is freed via a call to this instance's freeMemory, or the autofree flag is set via setAutofree, this instance will, during destruction, print error messages about every unfreed allocation in the destructor.

Exceptions
OpenMPCD::MemoryManagementExceptionThrows if the buffer could not be allocated.
Template Parameters
PointeeThe pointee type.
Parameters
[in]instanceCountThe number of instances the returned buffer should fit at a minimum. If 0, nullptr is returned.

Definition at line 62 of file DeviceMemoryManager.hpp.

◆ allocateMemory() [2/2]

template<typename Pointee >
void OpenMPCD::CUDA::DeviceMemoryManager::allocateMemory ( Pointee **  pointerToPointer,
const unsigned int  instanceCount 
)
inline

Allocates Device memory for the given number of instances of the supplied type.

After a successful call, the given pointer points to at least instanceCount * sizeof(Pointee) bytes of allocated Device memory.

Memory allocated through this function is registered with this instance. Unless the memory is freed via a call to this instance's freeMemory, or the autofree flag is set via setAutofree, this instance will, during destruction, print error messages about every unfreed allocation in the destructor.

Exceptions
OpenMPCD::NULLPointerExceptionIf OPENMPCD_DEBUG is defined, throws if pointerToPointer is nullptr.
OpenMPCD::MemoryManagementExceptionThrows if the buffer could not be allocated.
Template Parameters
PointeeThe pointee type.
Parameters
[out]pointerToPointerA valid pointer to the pointer that is to be set. Will be set to nullptr if instanceCount == 0.
[in]instanceCountThe number of instances the returned buffer should fit at a minimum.

Definition at line 91 of file DeviceMemoryManager.hpp.

◆ allocateMemoryUnregistered() [1/2]

template<typename Pointee >
static Pointee* OpenMPCD::CUDA::DeviceMemoryManager::allocateMemoryUnregistered ( const unsigned int  instanceCount)
inlinestatic

Allocates Device memory for the given number of instances of the supplied type.

The returned pointer points to at least instanceCount * sizeof(Pointee) bytes of allocated Device memory.

Memory allocated through this function is not registered with this instance, in contrast to allocateMemory.

Exceptions
OpenMPCD::MemoryManagementExceptionThrows if the buffer could not be allocated.
Template Parameters
PointeeThe pointee type.
Parameters
[in]instanceCountThe number of instances the returned buffer should fit at a minimum. If 0, nullptr is returned.

Definition at line 121 of file DeviceMemoryManager.hpp.

◆ allocateMemoryUnregistered() [2/2]

template<typename Pointee >
static void OpenMPCD::CUDA::DeviceMemoryManager::allocateMemoryUnregistered ( Pointee **  pointerToPointer,
const unsigned int  instanceCount 
)
inlinestatic

Allocates Device memory for the given number of instances of the supplied type.

After a successful call, the given pointer points to at least instanceCount * sizeof(Pointee) bytes of allocated Device memory.

Memory allocated through this function is not registered with this instance, in contrast to allocateMemory.

Exceptions
OpenMPCD::NULLPointerExceptionIf OPENMPCD_DEBUG is defined, throws if pointerToPointer is nullptr.
OpenMPCD::MemoryManagementExceptionThrows if the buffer could not be allocated.
Template Parameters
PointeeThe pointee type.
Parameters
[out]pointerToPointerA valid pointer to the pointer that is to be set. Will be set to nullptr if instanceCount == 0.
[in]instanceCountThe number of instances the returned buffer should fit at a minimum.

Definition at line 154 of file DeviceMemoryManager.hpp.

◆ copyElementsFromDeviceToDevice()

template<typename T >
void OpenMPCD::CUDA::DeviceMemoryManager::copyElementsFromDeviceToDevice ( const T *const  src,
T *const  dest,
const std::size_t  count 
)
static

Copies count elements of type T from the Device to the Device.

Exceptions
OpenMPCD::NULLPointerExceptionIf OPENMPCD_DEBUG is defined, throws if src == nullptr.
OpenMPCD::NULLPointerExceptionIf OPENMPCD_DEBUG is defined, throws if dest == nullptr
OpenMPCD::InvalidArgumentExceptionIf OPENMPCD_DEBUG is defined, throws if src is not a Device pointer.
OpenMPCD::InvalidArgumentExceptionIf OPENMPCD_DEBUG is defined, throws if dest is not a Device pointer.
Template Parameters
TThe type of elements to copy.
Parameters
[in]srcThe source to copy from.
[out]destThe destination to copy to.
[in]countThe number of elements of type T to copy.

Definition at line 66 of file ImplementationDetails/DeviceMemoryManager.hpp.

◆ copyElementsFromDeviceToHost()

template<typename T >
void OpenMPCD::CUDA::DeviceMemoryManager::copyElementsFromDeviceToHost ( const T *const  src,
T *const  dest,
const std::size_t  count 
)
static

Copies count elements of type T from the Device to the Host.

Exceptions
OpenMPCD::NULLPointerExceptionIf OPENMPCD_DEBUG is defined, throws if src == nullptr.
OpenMPCD::NULLPointerExceptionIf OPENMPCD_DEBUG is defined, throws if dest == nullptr
OpenMPCD::InvalidArgumentExceptionIf OPENMPCD_DEBUG is defined, throws if src is not a Device pointer.
OpenMPCD::InvalidArgumentExceptionIf OPENMPCD_DEBUG is defined, throws if dest is not a Host pointer.
Template Parameters
TThe type of elements to copy.
Parameters
[in]srcThe source to copy from.
[out]destThe destination to copy to.
[in]countThe number of elements of type T to copy.

Definition at line 44 of file ImplementationDetails/DeviceMemoryManager.hpp.

◆ copyElementsFromHostToDevice()

template<typename T >
void OpenMPCD::CUDA::DeviceMemoryManager::copyElementsFromHostToDevice ( const T *const  src,
T *const  dest,
const std::size_t  count 
)
static

Copies count elements of type T from the Host to the Device.

Exceptions
OpenMPCD::NULLPointerExceptionIf OPENMPCD_DEBUG is defined, throws if src == nullptr.
OpenMPCD::NULLPointerExceptionIf OPENMPCD_DEBUG is defined, throws if dest == nullptr
OpenMPCD::InvalidArgumentExceptionIf OPENMPCD_DEBUG is defined, throws if src is not a Host pointer.
OpenMPCD::InvalidArgumentExceptionIf OPENMPCD_DEBUG is defined, throws if dest is not a Device pointer.
Template Parameters
TThe type of elements to copy.
Parameters
[in]srcThe source to copy from.
[out]destThe destination to copy to.
[in]countThe number of elements of type T to copy.

Definition at line 22 of file ImplementationDetails/DeviceMemoryManager.hpp.

◆ elementMemoryEqualOnHostAndDevice()

template<typename T >
bool OpenMPCD::CUDA::DeviceMemoryManager::elementMemoryEqualOnHostAndDevice ( const T *const  host,
const T *const  device,
const std::size_t  count 
)
static

Returns whether the count elements of type T at host in Host memory have the same byte-wise representation as the count elements at device on the CUDA Device.

Exceptions
OpenMPCD::NULLPointerExceptionIf OPENMPCD_DEBUG is defined, throws if src == nullptr.
OpenMPCD::NULLPointerExceptionIf OPENMPCD_DEBUG is defined, throws if dest == nullptr
OpenMPCD::InvalidArgumentExceptionIf OPENMPCD_DEBUG is defined, throws if host is not a Host pointer.
OpenMPCD::InvalidArgumentExceptionIf OPENMPCD_DEBUG is defined, throws if device is not a Device pointer.
OpenMPCD::InvalidArgumentExceptionIf OPENMPCD_DEBUG is defined, throws if count == 0.
Template Parameters
TThe type of elements to copy.
Parameters
[in]hostPointer to the data on the Host.
[out]devicePointer to the data on the CUDA Device.
[in]countThe number of elements of type T to compare, which must not be 0.

Definition at line 104 of file ImplementationDetails/DeviceMemoryManager.hpp.

◆ freeMemory()

void DeviceMemoryManager::freeMemory ( void *const  pointer)

Frees the Device memory pointed to by the given pointer.

Exceptions
OpenMPCD::MemoryManagementExceptionThrows if the given non-nullptr pointer has not been allocated through and registered with this instance, i.e. if the given pointer has not been created via a call to allocateMemory.
Parameters
[in]pointerThe pointer to free. If it is nullptr, nothing happens.

Definition at line 40 of file DeviceMemoryManager.cpp.

◆ freeMemoryUnregistered()

void DeviceMemoryManager::freeMemoryUnregistered ( void *const  pointer)
static

Frees the Device memory pointed to by the given pointer.

Note that one should not use this function with pointers allocated in a way that the pointer is registered with an instance of this class, e.g. via allocateMemory, since otherwise, that memory is either going to be freed twice, or a (spurious) warning about unfreed memory may be produced.

Exceptions
OpenMPCD::MemoryManagementExceptionIf OPENMPCD_DEBUG is defined, throws if pointer != nullptr && !isDeviceMemoryPointer(pointer).
Parameters
[in]pointerThe pointer to free. If it is nullptr, nothing happens.

Definition at line 53 of file DeviceMemoryManager.cpp.

◆ isDeviceMemoryPointer()

bool DeviceMemoryManager::isDeviceMemoryPointer ( const void *const  ptr)
static

Returns whether the given pointer is a pointer to CUDA Device memory.

Returns false if ptr == nullptr.

Exceptions
OpenMPCD::ExceptionThrows on an error.
Parameters
[in]ptrThe pointer to check.

Definition at line 81 of file DeviceMemoryManager.cpp.

◆ isHostMemoryPointer()

bool DeviceMemoryManager::isHostMemoryPointer ( const void *const  ptr)
static

Returns whether the given pointer is a pointer to Host memory.

If this function returns true, it does necessarily not mean that it is permissible to dereference the pointer.

Returns false if ptr == nullptr.

Exceptions
OpenMPCD::ExceptionThrows on an error.
Parameters
[in]ptrThe pointer to check.

Definition at line 105 of file DeviceMemoryManager.cpp.

◆ setAutofree()

void OpenMPCD::CUDA::DeviceMemoryManager::setAutofree ( const bool  enable)
inline

Sets, or unsets, the autofree flag.

If the autofree flag is set, all reserved device memory is freed upon destruction of this instance, without raising warnings or errors.

Parameters
[in]enableWhether to enable the autofree feature.

Definition at line 207 of file DeviceMemoryManager.hpp.

◆ zeroMemory()

template<typename T >
void OpenMPCD::CUDA::DeviceMemoryManager::zeroMemory ( T *const  start,
const std::size_t  numberOfElements 
)
static

Writes zero-bytes to the given Device memory region.

Exceptions
OpenMPCD::NULLPointerExceptionIf OPENMPCD_DEBUG is defined, throws if start == nullptr
OpenMPCD::InvalidArgumentExceptionIf OPENMPCD_DEBUG is defined, throws if start is not a Device pointer.
Template Parameters
TThe type of elements pointed to by start.
Parameters
[in]startThe first element to write zero-bytes to.
[in]numberOfElementsThe number of elements of type T to write zero-bytes to.

Definition at line 88 of file ImplementationDetails/DeviceMemoryManager.hpp.


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