OpenMPCD
ImplementationDetails/DeviceMemoryManager.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * Implements some of the functionality of `OpenMPCD::CUDA::DeviceMemoryManager`.
4  */
5 
6 #ifndef OPENMPCD_CUDA_IMPLEMENTATIONDETAILS_DEVICEMEMORYMANAGER_HPP
7 #define OPENMPCD_CUDA_IMPLEMENTATIONDETAILS_DEVICEMEMORYMANAGER_HPP
8 
10 #include <OpenMPCD/CUDA/Macros.hpp>
11 
12 #include <boost/scoped_array.hpp>
13 
14 #include <string.h>
15 
16 namespace OpenMPCD
17 {
18 namespace CUDA
19 {
20 
21 template<typename T>
23  const T* const src, T* const dest, const std::size_t count)
24 {
25  #ifdef OPENMPCD_DEBUG
26  if(!src)
28 
29  if(!dest)
31 
32  if(!isHostMemoryPointer(src))
34 
35  if(!isDeviceMemoryPointer(dest))
37  #endif
38 
39  cudaMemcpy(dest, src, sizeof(T) * count, cudaMemcpyHostToDevice);
41 }
42 
43 template<typename T>
45  const T* const src, T* const dest, const std::size_t count)
46 {
47  #ifdef OPENMPCD_DEBUG
48  if(!src)
50 
51  if(!dest)
53 
54  if(!isDeviceMemoryPointer(src))
56 
57  if(!isHostMemoryPointer(dest))
59  #endif
60 
61  cudaMemcpy(dest, src, sizeof(T) * count, cudaMemcpyDeviceToHost);
63 }
64 
65 template<typename T>
67  const T* const src, T* const dest, const std::size_t count)
68 {
69  #ifdef OPENMPCD_DEBUG
70  if(!src)
72 
73  if(!dest)
75 
76  if(!isDeviceMemoryPointer(src))
78 
79  if(!isDeviceMemoryPointer(dest))
81  #endif
82 
83  cudaMemcpy(dest, src, sizeof(T) * count, cudaMemcpyDeviceToDevice);
85 }
86 
87 template<typename T>
89  T* const start, const std::size_t numberOfElements)
90 {
91  #ifdef OPENMPCD_DEBUG
92  if(start == NULL)
94 
95  if(!isDeviceMemoryPointer(start))
97  #endif
98 
99  cudaMemset(start, 0, sizeof(T) * numberOfElements);
101 }
102 
103 template<typename T>
105  const T* const host, const T* const device, const std::size_t count)
106 {
107  #ifdef OPENMPCD_DEBUG
108  if(!host)
110 
111  if(!device)
113 
114  if(!isHostMemoryPointer(host))
116 
117  if(!isDeviceMemoryPointer(device))
119 
120  if(count == 0)
122  #endif
123 
124  const std::size_t byteCount = sizeof(T) * count;
125 
126  boost::scoped_array<char> copyFromDevice(new char[byteCount]);
127  cudaMemcpy(copyFromDevice.get(), device, byteCount, cudaMemcpyDeviceToHost);
129 
130  return memcmp(host, copyFromDevice.get(), byteCount) == 0;
131 }
132 
133 } //namespace CUDA
134 } //namespace OpenMPCD
135 
136 #endif //OPENMPCD_CUDA_IMPLEMENTATIONDETAILS_DEVICEMEMORYMANAGER_HPP
OPENMPCD_CUDA_THROW_ON_ERROR
#define OPENMPCD_CUDA_THROW_ON_ERROR
Throws if the last CUDA call was not successful.
Definition: Macros.hpp:96
OPENMPCD_THROW
#define OPENMPCD_THROW(ExceptionType, message)
Throws the given ExceptionType, passing the given message along with file and line number information...
Definition: Exceptions.hpp:22
OpenMPCD::CUDA::DeviceMemoryManager::isDeviceMemoryPointer
static bool isDeviceMemoryPointer(const void *const ptr)
Returns whether the given pointer is a pointer to CUDA Device memory.
Definition: DeviceMemoryManager.cpp:81
OpenMPCD::CUDA::DeviceMemoryManager::copyElementsFromHostToDevice
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.
Definition: ImplementationDetails/DeviceMemoryManager.hpp:22
OpenMPCD::CUDA::DeviceMemoryManager::isHostMemoryPointer
static bool isHostMemoryPointer(const void *const ptr)
Returns whether the given pointer is a pointer to Host memory.
Definition: DeviceMemoryManager.cpp:105
OpenMPCD::CUDA::DeviceMemoryManager::copyElementsFromDeviceToDevice
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.
Definition: ImplementationDetails/DeviceMemoryManager.hpp:66
OpenMPCD::CUDA::DeviceMemoryManager::zeroMemory
static void zeroMemory(T *const start, const std::size_t numberOfElements)
Writes zero-bytes to the given Device memory region.
Definition: ImplementationDetails/DeviceMemoryManager.hpp:88
Macros.hpp
runtime.hpp
OpenMPCD::CUDA::DeviceMemoryManager::copyElementsFromDeviceToHost
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.
Definition: ImplementationDetails/DeviceMemoryManager.hpp:44
OpenMPCD::CUDA::DeviceMemoryManager::elementMemoryEqualOnHostAndDevice
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 represent...
Definition: ImplementationDetails/DeviceMemoryManager.hpp:104
OpenMPCD::NULLPointerException
NULL-pointer exception.
Definition: Exceptions.hpp:96
OpenMPCD::InvalidArgumentException
Invalid argument exception.
Definition: Exceptions.hpp:128