SkePU(integratedwithStarPU)  0.8.1
 All Classes Namespaces Files Functions Enumerations Friends Macros Groups Pages
device_mem_pointer_matrix_cl.h
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 #include "debug.h"
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: " <<rows <<" X "<<cols <<"\n")
74 
75  m_deviceDataPointer = clCreateBuffer(m_device->getContext(), CL_MEM_READ_WRITE, sizeVec, NULL, &err);
76  if(err != CL_SUCCESS){std::cerr<<"Error allocating memory on device\n";}
77 
78  deviceDataHasChanged = false;
79 }
80 
84 template <typename T>
86 {
87  DEBUG_TEXT_LEVEL2("DeAlloc: " <<m_rows<<" X "<<m_cols <<"\n")
88 
89  clReleaseMemObject(m_deviceDataPointer);
90 }
91 
98 template <typename T>
100 {
101  DEBUG_TEXT_LEVEL2("HOST_TO_DEVICE!!!\n")
102 
103  cl_int err;
104  size_t sizeVec;
105 
106  int _rows, _cols;
107  _rows = ((rows == -1) ? m_rows : rows);
108  _cols = ((cols == -1) ? m_cols : cols);
109 
110 
111  sizeVec = _rows * _cols * sizeof(T);
112 
113 #ifdef SKEPU_MEASURE_TIME_DISTRIBUTION
114 copyUpTimer.start();
115 #endif
116 
117  err = clEnqueueWriteBuffer(m_device->getQueue(), m_deviceDataPointer, CL_TRUE, 0, sizeVec, (void*)m_hostDataPointer, 0, NULL, NULL);
118  if(err != CL_SUCCESS){std::cerr<<"Error copying data to device\n";}
119 
120 #ifdef SKEPU_MEASURE_TIME_DISTRIBUTION
121 copyUpTimer.stop();
122 #endif
123 
124  deviceDataHasChanged = false;
125 }
126 
133 template <typename T>
135 {
136  if(deviceDataHasChanged)
137  {
138  DEBUG_TEXT_LEVEL2("DEVICE_TO_HOST!!!\n")
139 
140  cl_int err;
141  size_t sizeVec;
142  int _rows, _cols;
143  _rows = ((rows == -1) ? m_rows : rows);
144  _cols = ((cols == -1) ? m_cols : cols);
145 
146  sizeVec = _rows * _cols * sizeof(T);
147 
148 #ifdef SKEPU_MEASURE_TIME_DISTRIBUTION
149 copyDownTimer.start();
150 #endif
151 
152  err = clEnqueueReadBuffer(m_device->getQueue(), m_deviceDataPointer, CL_TRUE, 0, sizeVec, (void*)m_hostDataPointer, 0, NULL, NULL);
153  if(err != CL_SUCCESS){std::cerr<<"Error copying data from device\n";}
154 
155 
156 #ifdef SKEPU_MEASURE_TIME_DISTRIBUTION
157 copyDownTimer.stop();
158 #endif
159 
160  deviceDataHasChanged = false;
161  }
162 }
163 
167 template <typename T>
169 {
170  return m_deviceDataPointer;
171 }
172 
176 template <typename T>
178 {
179  deviceDataHasChanged = true;
180 }
181 
182 }
183 
184 #endif
185 
186 #endif
187 
cl_mem getDeviceDataPointer() const
Definition: device_mem_pointer_matrix_cl.h:168
Defines a few macros that can be used to output text when debugging. The macros use std::cerr...
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:159
A class representing an OpenCL device memory allocation for Matrix.
Definition: device_mem_pointer_matrix_cl.h:36
void changeDeviceData()
Definition: device_mem_pointer_matrix_cl.h:177
A class representing an OpenCL device.
Definition: device_cl.h:37
void copyDeviceToHost(int rows=-1, int cols=-1) const
Definition: device_mem_pointer_matrix_cl.h:134
~DeviceMemPointer_Matrix_CL()
Definition: device_mem_pointer_matrix_cl.h:85
void copyHostToDevice(int rows=-1, int cols=-1) const
Definition: device_mem_pointer_matrix_cl.h:99