SkePU(integratedwithStarPU)  0.8.1
 All Classes Namespaces Files Functions Enumerations Friends Macros Groups Pages
device_mem_pointer_matrix_cu.h
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 #include "debug.h"
14 
15 namespace skepu
16 {
17 
30 template <typename T>
31 class DeviceMemPointer_Matrix_CU
32 {
33 
34 public:
35  DeviceMemPointer_Matrix_CU(T* start, int rows, int cols, int deviceID);
36  ~DeviceMemPointer_Matrix_CU();
37 
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();
43 
44 private:
45  T* m_hostDataPointer;
46  T* m_deviceDataPointer;
47  int m_rows;
48  int m_cols;
49  int m_deviceID;
50 
51  inline int size() { return m_rows * m_cols; }
52 
53  mutable bool deviceDataHasChanged;
54 };
55 
65 template <typename T>
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)
67 {
68  cudaError_t err;
69  size_t sizeVec = rows*cols*sizeof(T);
70 
71  DEBUG_TEXT_LEVEL2("Alloc: " <<numElements <<"\n")
72 
73  err = cudaMalloc((void**)&m_deviceDataPointer, sizeVec);
74  if(err != cudaSuccess){std::cerr<<"Error allocating memory on device\n";}
75 
76  deviceDataHasChanged = false;
77 }
78 
82 template <typename T>
83 DeviceMemPointer_Matrix_CU<T>::~DeviceMemPointer_Matrix_CU()
84 {
85  DEBUG_TEXT_LEVEL2("DeAlloc: " <<m_numElements <<"\n")
86 
87  cudaFree(m_deviceDataPointer);
88 }
89 
96 template <typename T>
97 void DeviceMemPointer_Matrix_CU<T>::copyHostToDevice(int rows, int cols) const
98 {
99  if(m_hostDataPointer != NULL)
100  {
101  DEBUG_TEXT_LEVEL2("HOST_TO_DEVICE!!!\n")
102 
103  cudaError_t err;
104  size_t sizeVec;
105  int _rows, _cols;
106 
107  _rows = ((rows == -1) ? m_rows : rows);
108  _cols = ((cols == -1) ? m_cols : cols);
109 
110  sizeVec = _rows * _cols * sizeof(T);
111 
112  err = cudaMemcpy(m_deviceDataPointer, m_hostDataPointer, sizeVec, cudaMemcpyHostToDevice);
113  if(err != cudaSuccess){std::cerr<<"Error copying data to device\n" <<cudaGetErrorString(err) <<"\n";}
114 
115  deviceDataHasChanged = false;
116  }
117 }
118 
125 template <typename T>
126 void DeviceMemPointer_Matrix_CU<T>::copyDeviceToHost(int rows, int cols) const
127 {
128  if(deviceDataHasChanged && m_hostDataPointer != NULL)
129  {
130  DEBUG_TEXT_LEVEL2("DEVICE_TO_HOST!!!\n")
131 
132  cudaError_t err;
133  size_t sizeVec;
134 
135  int _rows, _cols;
136 
137  _rows = ((rows == -1) ? m_rows : rows);
138  _cols = ((cols == -1) ? m_cols : cols);
139 
140  sizeVec = _rows * _cols * sizeof(T);
141 
142  err = cudaMemcpy(m_hostDataPointer, m_deviceDataPointer, sizeVec, cudaMemcpyDeviceToHost);
143  if(err != cudaSuccess){std::cerr<<"Error copying data from device: " <<cudaGetErrorString(err) <<"\n";}
144 
145  deviceDataHasChanged = false;
146  }
147 }
148 
152 template <typename T>
153 T* DeviceMemPointer_Matrix_CU<T>::getDeviceDataPointer() const
154 {
155  return m_deviceDataPointer;
156 }
157 
161 template <typename T>
162 int DeviceMemPointer_Matrix_CU<T>::getDeviceID() const
163 {
164  return m_deviceID;
165 }
166 
170 template <typename T>
171 void DeviceMemPointer_Matrix_CU<T>::changeDeviceData()
172 {
173  deviceDataHasChanged = true;
174 }
175 
176 }
177 
178 #endif
179 
180 #endif
181 
Defines a few macros that can be used to output text when debugging. The macros use std::cerr...