SkePU  1.2
 All Classes Namespaces Files Functions Variables Enumerations Friends Macros Groups Pages
device_mem_pointer_matrix_cu.h
Go to the documentation of this file.
1 
5 #ifndef DEVICE_MEM_POINTER_MATRIX_CU_H
6 #define DEVICE_MEM_POINTER_MATRIX_CU_H
7 
8 #ifdef SKEPU_CUDA
9 
10 #include <iostream>
11 #include <cuda.h>
12 
13 
14 #include "device_cu.h"
15 
16 namespace skepu
17 {
18 
33 template <typename T>
35 {
36 
37 public:
38  DeviceMemPointer_Matrix_CU(T* start, int rows, int cols, Device_CU *device, bool usePitch=false);
40 
41  void copyHostToDevice(int rows=-1, int cols=-1) const;
42  void copyDeviceToHost(int rows=-1, int cols=-1) const;
43  T* getDeviceDataPointer() const;
44  int getDeviceID() const;
45  void changeDeviceData();
46 
47  size_t m_pitch;
48 private:
49  T* m_hostDataPointer;
50  T* m_deviceDataPointer;
51  int m_rows;
52  int m_cols;
53  int m_deviceID;
54  Device_CU *m_dev;
55 
56  bool m_usePitch;
57 
58  inline int size()
59  {
60  return m_rows * m_cols;
61  }
62 
63  mutable bool deviceDataHasChanged;
64 };
65 
75 template <typename T>
76 DeviceMemPointer_Matrix_CU<T>::DeviceMemPointer_Matrix_CU(T* start, int rows, int cols, Device_CU *device, bool usePitch) : m_hostDataPointer(start), m_rows(rows), m_cols(cols), m_dev(device)
77 {
78  cudaError_t err;
79  size_t sizeVec = rows*cols*sizeof(T);
80 
81  DEBUG_TEXT_LEVEL2("Alloc: " << rows*cols <<"\n")
82 
83  m_deviceID = m_dev->getDeviceID();
84 
85  cudaSetDevice(m_deviceID);
86 
87  m_usePitch = usePitch;
88  if(usePitch)
89  {
90  err = cudaMallocPitch((void**)&m_deviceDataPointer, &m_pitch, cols * sizeof(T), rows);
91  m_pitch = (m_pitch)/sizeof(T);
92  }
93  else
94  {
95  err = cudaMalloc((void**)&m_deviceDataPointer, sizeVec);
96  m_pitch = cols;
97  }
98  if(err != cudaSuccess)
99  {
100  std::cerr<<"Error allocating memory on device\n";
101  }
102 
103  deviceDataHasChanged = false;
104 }
105 
106 
107 
108 
112 template <typename T>
114 {
115  DEBUG_TEXT_LEVEL2("DeAlloc: " <<"\n")
116 
117  cudaSetDevice(m_deviceID);
118 
119  cudaFree(m_deviceDataPointer);
120 }
121 
128 template <typename T>
130 {
131  if(m_hostDataPointer != NULL)
132  {
133  DEBUG_TEXT_LEVEL2("HOST_TO_DEVICE!!!\n")
134 
135  cudaError_t err;
136  size_t sizeVec;
137  int _rows, _cols;
138 
139  _rows = ((rows == -1) ? m_rows : rows);
140  _cols = ((cols == -1) ? m_cols : cols);
141 
142  sizeVec = _rows * _cols * sizeof(T);
143 
144  cudaSetDevice(m_deviceID);
145 
146 #ifdef USE_PINNED_MEMORY
147  if(m_usePitch)
148  err = cudaMemcpy2DAsync(m_deviceDataPointer,m_pitch*sizeof(T),m_hostDataPointer,_cols*sizeof(T), _cols*sizeof(T), _rows, cudaMemcpyHostToDevice, m_dev->stream);
149  else
150  err = cudaMemcpyAsync(m_deviceDataPointer, m_hostDataPointer, sizeVec, cudaMemcpyHostToDevice, m_dev->stream);
151 #else
152  if(m_usePitch)
153  err = cudaMemcpy2D(m_deviceDataPointer,m_pitch*sizeof(T),m_hostDataPointer,_cols*sizeof(T), _cols*sizeof(T), _rows, cudaMemcpyHostToDevice);
154  else
155  err = cudaMemcpy(m_deviceDataPointer, m_hostDataPointer, sizeVec, cudaMemcpyHostToDevice);
156 #endif
157 
158  if(err != cudaSuccess)
159  {
160  std::cerr<<"Error copying data to device\n" <<cudaGetErrorString(err) <<"\n";
161  }
162 
163  deviceDataHasChanged = false;
164  }
165 }
166 
173 template <typename T>
175 {
176  if(deviceDataHasChanged && m_hostDataPointer != NULL)
177  {
178  DEBUG_TEXT_LEVEL2("DEVICE_TO_HOST!!!\n")
179 
180  cudaError_t err;
181  size_t sizeVec;
182 
183  int _rows, _cols;
184 
185  _rows = ((rows == -1) ? m_rows : rows);
186  _cols = ((cols == -1) ? m_cols : cols);
187 
188  sizeVec = _rows * _cols * sizeof(T);
189 
190  cudaSetDevice(m_deviceID);
191 
192 #ifdef USE_PINNED_MEMORY
193  if(m_usePitch)
194  err = cudaMemcpy2DAsync(m_hostDataPointer,_cols*sizeof(T),m_deviceDataPointer,m_pitch*sizeof(T), _cols*sizeof(T), _rows, cudaMemcpyDeviceToHost, m_dev->stream);
195  else
196  err = cudaMemcpyAsync(m_hostDataPointer, m_deviceDataPointer, sizeVec, cudaMemcpyDeviceToHost, m_dev->stream);
197  cudaStreamSynchronize(m_dev->stream);
198 #else
199  if(m_usePitch)
200  err = cudaMemcpy2D(m_hostDataPointer,_cols*sizeof(T),m_deviceDataPointer,m_pitch*sizeof(T), _cols*sizeof(T), _rows, cudaMemcpyDeviceToHost);
201  else
202  err = cudaMemcpy(m_hostDataPointer, m_deviceDataPointer, sizeVec, cudaMemcpyDeviceToHost);
203 #endif
204 
205  if(err != cudaSuccess)
206  {
207  std::cerr<<"Error copying data from device: " <<cudaGetErrorString(err) <<"\n";
208  }
209 
210  deviceDataHasChanged = false;
211  }
212 }
213 
217 template <typename T>
219 {
220  return m_deviceDataPointer;
221 }
222 
226 template <typename T>
228 {
229  return m_deviceID;
230 }
231 
235 template <typename T>
237 {
238  deviceDataHasChanged = true;
239 }
240 
241 }
242 
243 #endif
244 
245 #endif
246 
247 
~DeviceMemPointer_Matrix_CU()
Definition: device_mem_pointer_matrix_cu.h:113
void copyHostToDevice(int rows=-1, int cols=-1) const
Definition: device_mem_pointer_matrix_cu.h:129
void changeDeviceData()
Definition: device_mem_pointer_matrix_cu.h:236
T * getDeviceDataPointer() const
Definition: device_mem_pointer_matrix_cu.h:218
DeviceMemPointer_Matrix_CU(T *start, int rows, int cols, Device_CU *device, bool usePitch=false)
Definition: device_mem_pointer_matrix_cu.h:76
int getDeviceID() const
Definition: device_mem_pointer_matrix_cu.h:227
Contains a class declaration for the object that represents a CUDA device.
A class representing a CUDA 2D device memory allocation for Matrix container.
Definition: device_mem_pointer_matrix_cu.h:34
unsigned int getDeviceID() const
Definition: device_cu.h:346
A class representing a CUDA device.
Definition: device_cu.h:30
void copyDeviceToHost(int rows=-1, int cols=-1) const
Definition: device_mem_pointer_matrix_cu.h:174