SkePU  1.2
 All Classes Namespaces Files Functions Variables Enumerations Friends Macros Groups Pages
device_mem_pointer_matrix_cl.h
Go to the documentation of this file.
1 
5 #ifndef DEVICE_MEM_POINTER_MATRIX_CL_H
6 #define DEVICE_MEM_POINTER_MATRIX_CL_H
7 
8 #ifdef SKEPU_OPENCL
9 
10 #include <iostream>
11 #ifdef USE_MAC_OPENCL
12 #include <OpenCL/opencl.h>
13 #else
14 #include <CL/cl.h>
15 #endif
16 
17 #include "device_cl.h"
18 
19 
20 namespace skepu
21 {
22 
35 template <typename T>
37 {
38 
39 public:
40  DeviceMemPointer_Matrix_CL(T* start, int rows, int cols, Device_CL* device);
42 
43  void copyHostToDevice(int rows = -1, int cols = -1) const;
44  void copyDeviceToHost(int rows = -1, int cols = -1) const;
45  cl_mem getDeviceDataPointer() const;
46  void changeDeviceData();
47 
48 private:
49  T* m_hostDataPointer;
50  cl_mem m_deviceDataPointer;
51  int m_rows;
52  int m_cols;
53  Device_CL* m_device;
54 
55  mutable bool deviceDataHasChanged;
56 };
57 
67 template <typename T>
68 DeviceMemPointer_Matrix_CL<T>::DeviceMemPointer_Matrix_CL(T* start, int rows, int cols, Device_CL* device) : m_hostDataPointer(start), m_rows(rows), m_cols (cols), m_device(device)
69 {
70  cl_int err;
71  size_t sizeVec = rows*cols*sizeof(T);
72 
73  DEBUG_TEXT_LEVEL2("Alloc: " <<sizeVec <<"\n")
74 
75  m_deviceDataPointer = clCreateBuffer(m_device->getContext(), CL_MEM_READ_WRITE, sizeVec, NULL, &err);
76  if(err != CL_SUCCESS)
77  {
78  std::cerr<<"Error allocating memory on device\n";
79  }
80 
81  deviceDataHasChanged = false;
82 }
83 
87 template <typename T>
89 {
90  DEBUG_TEXT_LEVEL2("DeAlloc: " <<m_rows*m_cols*sizeof(T) <<"\n")
91 
92  clReleaseMemObject(m_deviceDataPointer);
93 }
94 
101 template <typename T>
103 {
104  DEBUG_TEXT_LEVEL2("HOST_TO_DEVICE!!!\n")
105 
106  cl_int err;
107  size_t sizeVec;
108 
109  int _rows, _cols;
110  _rows = ((rows == -1) ? m_rows : rows);
111  _cols = ((cols == -1) ? m_cols : cols);
112 
113 
114  sizeVec = _rows * _cols * sizeof(T);
115 
116 #ifdef SKEPU_MEASURE_TIME_DISTRIBUTION
117  copyUpTimer.start();
118 #endif
119 
120  err = clEnqueueWriteBuffer(m_device->getQueue(), m_deviceDataPointer, CL_TRUE, 0, sizeVec, (void*)m_hostDataPointer, 0, NULL, NULL);
121  if(err != CL_SUCCESS)
122  {
123  std::cerr<<"Error copying data to device\n";
124  }
125 
126 #ifdef SKEPU_MEASURE_TIME_DISTRIBUTION
127  copyUpTimer.stop();
128 #endif
129 
130  deviceDataHasChanged = false;
131 }
132 
139 template <typename T>
141 {
142  if(deviceDataHasChanged)
143  {
144  DEBUG_TEXT_LEVEL2("DEVICE_TO_HOST!!!\n")
145 
146  cl_int err;
147  size_t sizeVec;
148  int _rows, _cols;
149  _rows = ((rows == -1) ? m_rows : rows);
150  _cols = ((cols == -1) ? m_cols : cols);
151 
152  sizeVec = _rows * _cols * sizeof(T);
153 
154 #ifdef SKEPU_MEASURE_TIME_DISTRIBUTION
155  copyDownTimer.start();
156 #endif
157 
158  err = clEnqueueReadBuffer(m_device->getQueue(), m_deviceDataPointer, CL_TRUE, 0, sizeVec, (void*)m_hostDataPointer, 0, NULL, NULL);
159  if(err != CL_SUCCESS)
160  {
161  std::cerr<<"Error copying data from device\n";
162  }
163 
164 
165 #ifdef SKEPU_MEASURE_TIME_DISTRIBUTION
166  copyDownTimer.stop();
167 #endif
168 
169  deviceDataHasChanged = false;
170  }
171 }
172 
176 template <typename T>
178 {
179  return m_deviceDataPointer;
180 }
181 
185 template <typename T>
187 {
188  deviceDataHasChanged = true;
189 }
190 
191 }
192 
193 #endif
194 
195 #endif
196 
cl_mem getDeviceDataPointer() const
Definition: device_mem_pointer_matrix_cl.h:177
Contains a class declaration for the object that represents an OpenCL device.
DeviceMemPointer_Matrix_CL(T *start, int rows, int cols, Device_CL *device)
Definition: device_mem_pointer_matrix_cl.h:68
const cl_context & getContext() const
Definition: device_cl.h:170
A class representing an OpenCL device memory allocation for Matrix container. Not used much right now...
Definition: device_mem_pointer_matrix_cl.h:36
void changeDeviceData()
Definition: device_mem_pointer_matrix_cl.h:186
A class representing an OpenCL device.
Definition: device_cl.h:36
void copyDeviceToHost(int rows=-1, int cols=-1) const
Definition: device_mem_pointer_matrix_cl.h:140
~DeviceMemPointer_Matrix_CL()
Definition: device_mem_pointer_matrix_cl.h:88
void copyHostToDevice(int rows=-1, int cols=-1) const
Definition: device_mem_pointer_matrix_cl.h:102