5 #ifndef DEVICE_MEM_POINTER_MATRIX_CU_H
6 #define DEVICE_MEM_POINTER_MATRIX_CU_H
31 class DeviceMemPointer_Matrix_CU
35 DeviceMemPointer_Matrix_CU(T* start,
int rows,
int cols,
int deviceID);
36 ~DeviceMemPointer_Matrix_CU();
38 void copyHostToDevice(
int rows=-1,
int cols=-1)
const;
39 void copyDeviceToHost(
int rows=-1,
int cols=-1)
const;
40 T* getDeviceDataPointer()
const;
41 int getDeviceID()
const;
42 void changeDeviceData();
46 T* m_deviceDataPointer;
51 inline int size() {
return m_rows * m_cols; }
53 mutable bool deviceDataHasChanged;
66 DeviceMemPointer_Matrix_CU<T>::DeviceMemPointer_Matrix_CU(T* start,
int rows,
int cols,
int deviceID) : m_hostDataPointer(start), m_rows(rows), m_cols(cols), m_deviceID(deviceID)
69 size_t sizeVec = rows*cols*
sizeof(T);
71 DEBUG_TEXT_LEVEL2(
"Alloc: " <<numElements <<
"\n")
73 err = cudaMalloc((
void**)&m_deviceDataPointer, sizeVec);
74 if(err != cudaSuccess){std::cerr<<
"Error allocating memory on device\n";}
76 deviceDataHasChanged =
false;
83 DeviceMemPointer_Matrix_CU<T>::~DeviceMemPointer_Matrix_CU()
85 DEBUG_TEXT_LEVEL2(
"DeAlloc: " <<m_numElements <<
"\n")
87 cudaFree(m_deviceDataPointer);
97 void DeviceMemPointer_Matrix_CU<T>::copyHostToDevice(
int rows,
int cols)
const
99 if(m_hostDataPointer != NULL)
101 DEBUG_TEXT_LEVEL2(
"HOST_TO_DEVICE!!!\n")
107 _rows = ((rows == -1) ? m_rows : rows);
108 _cols = ((cols == -1) ? m_cols : cols);
110 sizeVec = _rows * _cols * sizeof(T);
112 err = cudaMemcpy(m_deviceDataPointer, m_hostDataPointer, sizeVec, cudaMemcpyHostToDevice);
113 if(err != cudaSuccess){std::cerr<<
"Error copying data to device\n" <<cudaGetErrorString(err) <<
"\n";}
115 deviceDataHasChanged =
false;
125 template <
typename T>
126 void DeviceMemPointer_Matrix_CU<T>::copyDeviceToHost(
int rows,
int cols)
const
128 if(deviceDataHasChanged && m_hostDataPointer != NULL)
130 DEBUG_TEXT_LEVEL2(
"DEVICE_TO_HOST!!!\n")
137 _rows = ((rows == -1) ? m_rows : rows);
138 _cols = ((cols == -1) ? m_cols : cols);
140 sizeVec = _rows * _cols * sizeof(T);
142 err = cudaMemcpy(m_hostDataPointer, m_deviceDataPointer, sizeVec, cudaMemcpyDeviceToHost);
143 if(err != cudaSuccess){std::cerr<<
"Error copying data from device: " <<cudaGetErrorString(err) <<
"\n";}
145 deviceDataHasChanged =
false;
152 template <
typename T>
153 T* DeviceMemPointer_Matrix_CU<T>::getDeviceDataPointer()
const
155 return m_deviceDataPointer;
161 template <
typename T>
162 int DeviceMemPointer_Matrix_CU<T>::getDeviceID()
const
170 template <
typename T>
171 void DeviceMemPointer_Matrix_CU<T>::changeDeviceData()
173 deviceDataHasChanged =
true;
Defines a few macros that can be used to output text when debugging. The macros use std::cerr...