1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// **************************************************************************
//
// PARALUTION www.paralution.com
//
// Copyright (C) 2015 PARALUTION Labs UG (haftungsbeschränkt) & Co. KG
// Am Hasensprung 6, 76571 Gaggenau
// Handelsregister: Amtsgericht Mannheim, HRA 706051
// Vertreten durch:
// PARALUTION Labs Verwaltungs UG (haftungsbeschränkt)
// Am Hasensprung 6, 76571 Gaggenau
// Handelsregister: Amtsgericht Mannheim, HRB 721277
// Geschäftsführer: Dimitar Lukarski, Nico Trost
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// **************************************************************************
// PARALUTION version 1.1.0
#include "../../utils/def.hpp"
#include "../../utils/log.hpp"
#include "ocl_allocate_free.hpp"
#include "ocl_utils.hpp"
namespace paralution {
// Allocate memory on device
template <typename DataType>
void allocate_ocl(const int size, cl_context ocl_context, DataType **ptr) {
LOG_DEBUG(0, "allocate_ocl()",
size);
if (size > 0) {
assert (*ptr == NULL);
cl_int err;
// Allocate memory on device
cl_mem data = clCreateBuffer(ocl_context, CL_MEM_READ_WRITE, sizeof(DataType)*size, NULL, &err);
CHECK_OCL_ERROR(err, __FILE__, __LINE__);
*ptr = (DataType*) data;
assert (*ptr != NULL);
}
}
// Free memory on device
template <typename DataType>
void free_ocl(DataType **ptr) {
LOG_DEBUG(0, "free_ocl()",
"");
// Free memory on device
cl_int err = clReleaseMemObject((cl_mem) *ptr);
CHECK_OCL_ERROR(err, __FILE__, __LINE__);
*ptr = NULL;
}
// Set object on device to specific value (sync)
template<typename DataType>
void ocl_set_to(const int size, const DataType val, DataType *ptr, cl_command_queue ocl_cmdQueue) {
LOG_DEBUG(0, "ocl_set_to()",
"size=" << size << " value=" << val);
assert (ptr != NULL);
if (size > 0) {
cl_event event;
cl_int err = clEnqueueFillBuffer(ocl_cmdQueue, (cl_mem) ptr, &val, sizeof(DataType), 0,
size*sizeof(DataType), 0, NULL, &event);
CHECK_OCL_ERROR(err, __FILE__, __LINE__);
err = clWaitForEvents(1, &event);
CHECK_OCL_ERROR(err, __FILE__, __LINE__);
err = clReleaseEvent(event);
CHECK_OCL_ERROR(err, __FILE__, __LINE__);
}
}
// Copy object from host to device memory (sync)
template <typename DataType>
void ocl_host2dev(const int size, const DataType *src, DataType *dst, cl_command_queue ocl_cmdQueue) {
LOG_DEBUG(0, "ocl_host2dev()",
size);
if (size > 0) {
assert (src != NULL);
assert (dst != NULL);
// Copy object from host to device memory
cl_int err = clEnqueueWriteBuffer(ocl_cmdQueue, (cl_mem) dst, CL_TRUE, 0, size*sizeof(DataType), src, 0, NULL, NULL);
CHECK_OCL_ERROR(err, __FILE__, __LINE__);
}
}
// Copy object from device to host memory (sync)
template<typename DataType>
void ocl_dev2host(const int size, const DataType *src, DataType *dst, cl_command_queue ocl_cmdQueue) {
LOG_DEBUG(0, "ocl_dev2host()",
size);
if (size > 0) {
assert (src != NULL);
assert (dst != NULL);
// Copy object from device to host memory
cl_int err = clEnqueueReadBuffer(ocl_cmdQueue, (cl_mem) src, CL_TRUE, 0, size*sizeof(DataType), dst, 0, NULL, NULL);
CHECK_OCL_ERROR(err, __FILE__, __LINE__);
}
}
// Copy object from device to device memory (internal copy, sync)
template<typename DataType>
void ocl_dev2dev(const int size, const DataType *src, DataType *dst, cl_command_queue ocl_cmdQueue) {
LOG_DEBUG(0, "ocl_dev2dev()",
size);
if (size > 0) {
assert (src != NULL);
assert (dst != NULL);
// Copy object from device to device memory (internal copy)
cl_event event;
cl_int err = clEnqueueCopyBuffer(ocl_cmdQueue, (cl_mem) src, (cl_mem) dst, 0, 0, size*sizeof(DataType), 0, NULL, &event);
CHECK_OCL_ERROR(err, __FILE__, __LINE__);
err = clWaitForEvents(1, &event);
CHECK_OCL_ERROR(err, __FILE__, __LINE__);
err = clReleaseEvent(event);
CHECK_OCL_ERROR(err, __FILE__, __LINE__);
}
}
template void allocate_ocl<double >(const int size, cl_context ocl_context, double **ptr);
template void allocate_ocl<float >(const int size, cl_context ocl_context, float **ptr);
template void allocate_ocl<int >(const int size, cl_context ocl_context, int **ptr);
template void allocate_ocl<unsigned int>(const int size, cl_context ocl_context, unsigned int **ptr);
template void allocate_ocl<char >(const int size, cl_context ocl_context, char **ptr);
template void free_ocl<double >(double **ptr);
template void free_ocl<float >(float **ptr);
template void free_ocl<int >(int **ptr);
template void free_ocl<unsigned int>(unsigned int **ptr);
template void free_ocl<char >(char **ptr);
template void ocl_set_to<double >(const int size, const double val, double *ptr, cl_command_queue ocl_cmdQueue);
template void ocl_set_to<float >(const int size, const float val, float *ptr, cl_command_queue ocl_cmdQueue);
template void ocl_set_to<int >(const int size, const int val, int *ptr, cl_command_queue ocl_cmdQueue);
template void ocl_set_to<unsigned int>(const int size, const unsigned int val, unsigned int *ptr, cl_command_queue ocl_cmdQueue);
template void ocl_set_to<char >(const int size, const char val, char *ptr, cl_command_queue ocl_cmdQueue);
template void ocl_host2dev<double >(const int size, const double *src, double *dst, cl_command_queue ocl_cmdQueue);
template void ocl_host2dev<float >(const int size, const float *src, float *dst, cl_command_queue ocl_cmdQueue);
template void ocl_host2dev<int >(const int size, const int *src, int *dst, cl_command_queue ocl_cmdQueue);
template void ocl_host2dev<unsigned int>(const int size, const unsigned int *src, unsigned int *dst, cl_command_queue ocl_cmdQueue);
template void ocl_host2dev<char >(const int size, const char *src, char *dst, cl_command_queue ocl_cmdQueue);
template void ocl_dev2host<double >(const int size, const double *src, double *dst, cl_command_queue ocl_cmdQueue);
template void ocl_dev2host<float >(const int size, const float *src, float *dst, cl_command_queue ocl_cmdQueue);
template void ocl_dev2host<int >(const int size, const int *src, int *dst, cl_command_queue ocl_cmdQueue);
template void ocl_dev2host<unsigned int>(const int size, const unsigned int *src, unsigned int *dst, cl_command_queue ocl_cmdQueue);
template void ocl_dev2host<char >(const int size, const char *src, char *dst, cl_command_queue ocl_cmdQueue);
template void ocl_dev2dev<double >(const int size, const double *src, double *dst, cl_command_queue ocl_cmdQueue);
template void ocl_dev2dev<float >(const int size, const float *src, float *dst, cl_command_queue ocl_cmdQueue);
template void ocl_dev2dev<int >(const int size, const int *src, int *dst, cl_command_queue ocl_cmdQueue);
template void ocl_dev2dev<unsigned int>(const int size, const unsigned int *src, unsigned int *dst, cl_command_queue ocl_cmdQueue);
template void ocl_dev2dev<char >(const int size, const char *src, char *dst, cl_command_queue ocl_cmdQueue);
}