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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
#include "test_mem.h"
#include "CL/cl.h"
#include <stdio.h>
START_TEST (test_create_buffer)
{
cl_context ctx;
cl_mem buf;
cl_int result;
char s[] = "Hello, world !";
ctx = clCreateContextFromType(0, CL_DEVICE_TYPE_CPU, 0, 0, &result);
fail_if(
result != CL_SUCCESS,
"unable to create a valid context"
);
buf = clCreateBuffer(0, CL_MEM_READ_WRITE, sizeof(s), 0, &result);
fail_if(
result != CL_INVALID_CONTEXT,
"0 is not a valid context"
);
buf = clCreateBuffer(ctx, 1337, sizeof(s), 0, &result);
fail_if(
result != CL_INVALID_VALUE,
"1337 is not a valid cl_mem_flags"
);
buf = clCreateBuffer(ctx, CL_MEM_USE_HOST_PTR, sizeof(s), 0, &result);
fail_if(
result != CL_INVALID_HOST_PTR,
"host_ptr cannot be NULL if flags is CL_MEM_USE_HOST_PTR"
);
buf = clCreateBuffer(ctx, CL_MEM_COPY_HOST_PTR, sizeof(s), 0, &result);
fail_if(
result != CL_INVALID_HOST_PTR,
"host_ptr cannot be NULL if flags is CL_MEM_COPY_HOST_PTR"
);
buf = clCreateBuffer(ctx, 0, sizeof(s), s, &result);
fail_if(
result != CL_INVALID_HOST_PTR,
"host_ptr must be NULL if flags is not CL_MEM_{COPY/USE}_HOST_PTR"
);
buf = clCreateBuffer(ctx, CL_MEM_USE_HOST_PTR, 0, s, &result);
fail_if(
result != CL_INVALID_BUFFER_SIZE,
"size cannot be 0"
);
buf = clCreateBuffer(ctx, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
sizeof(s), s, &result);
fail_if(
result != CL_SUCCESS,
"cannot create a valid CL_MEM_COPY_HOST_PTR read-write buffer"
);
clReleaseMemObject(buf);
clReleaseContext(ctx);
}
END_TEST
START_TEST (test_create_sub_buffer)
{
cl_context ctx;
cl_mem buf, subbuf;
cl_int result;
char s[] = "Hello, world !";
cl_buffer_region create_info; // "Hello, [world] !"
create_info.origin = 7;
create_info.size = 5;
ctx = clCreateContextFromType(0, CL_DEVICE_TYPE_CPU, 0, 0, &result);
fail_if(
result != CL_SUCCESS,
"unable to create a valid context"
);
buf = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
sizeof(s), s, &result);
fail_if(
result != CL_SUCCESS,
"cannot create a valid CL_MEM_USE_HOST_PTR read-write buffer"
);
subbuf = clCreateSubBuffer(0, CL_MEM_WRITE_ONLY,
CL_BUFFER_CREATE_TYPE_REGION,
(void *)&create_info, &result);
fail_if(
result != CL_INVALID_MEM_OBJECT,
"0 is not a valid mem object"
);
subbuf = clCreateSubBuffer(buf, CL_MEM_READ_ONLY,
CL_BUFFER_CREATE_TYPE_REGION,
(void *)&create_info, &result);
fail_if(
result != CL_INVALID_VALUE,
"READ_ONLY is not compatible with WRITE_ONLY"
);
subbuf = clCreateSubBuffer(buf, CL_MEM_WRITE_ONLY, 1337, (void *)&create_info,
&result);
fail_if(
result != CL_INVALID_VALUE,
"1337 is not a valid buffer_create_type"
);
subbuf = clCreateSubBuffer(buf, CL_MEM_WRITE_ONLY,
CL_BUFFER_CREATE_TYPE_REGION, 0, &result);
fail_if(
result != CL_INVALID_VALUE,
"buffer_create_info cannot be NULL"
);
create_info.size = 0;
subbuf = clCreateSubBuffer(buf, CL_MEM_WRITE_ONLY,
CL_BUFFER_CREATE_TYPE_REGION,
(void *)&create_info, &result);
fail_if(
result != CL_INVALID_BUFFER_SIZE,
"create_info.size cannot be 0"
);
create_info.size = 5;
subbuf = clCreateSubBuffer(buf, CL_MEM_WRITE_ONLY,
CL_BUFFER_CREATE_TYPE_REGION,
(void *)&create_info, &result);
fail_if(
result != CL_SUCCESS || subbuf == 0,
"cannot create a valid sub-buffer"
);
subbuf = clCreateSubBuffer(subbuf, CL_MEM_WRITE_ONLY,
CL_BUFFER_CREATE_TYPE_REGION,
(void *)&create_info, &result);
fail_if(
result != CL_INVALID_MEM_OBJECT,
"we cannot create a sub-buffer of a sub-buffer"
);
clReleaseMemObject(subbuf);
clReleaseMemObject(buf);
clReleaseContext(ctx);
}
END_TEST
START_TEST (test_read_write_subbuf)
{
cl_context ctx;
cl_mem buf, subbuf;
cl_command_queue queue;
cl_device_id device;
cl_int result;
char s[] = "Hello, Denis !";
cl_buffer_region create_info = { // "Hello, [denis] !"
.origin = 7,
.size = 5
};
result = clGetDeviceIDs(0, CL_DEVICE_TYPE_CPU, 1, &device, 0);
fail_if(
result != CL_SUCCESS,
"cannot get a device"
);
ctx = clCreateContext(0, 1, &device, 0, 0, &result);
fail_if(
result != CL_SUCCESS,
"unable to create a valid context"
);
queue = clCreateCommandQueue(ctx, device, 0, &result);
fail_if(
result != CL_SUCCESS || queue == 0,
"cannot create a command queue"
);
buf = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
sizeof(s), s, &result);
fail_if(
result != CL_SUCCESS,
"cannot create a valid CL_MEM_USE_HOST_PTR read-write buffer"
);
subbuf = clCreateSubBuffer(buf, CL_MEM_WRITE_ONLY,
CL_BUFFER_CREATE_TYPE_REGION,
(void *)&create_info, &result);
fail_if(
result != CL_SUCCESS || subbuf == 0,
"cannot create a valid sub-buffer"
);
////
char *hostptr;
char *valid_hostptr = s;
valid_hostptr += create_info.origin;
result = clGetMemObjectInfo(subbuf, CL_MEM_HOST_PTR, sizeof(char *),
(void *)&hostptr, 0);
fail_if(
result != CL_SUCCESS || hostptr != valid_hostptr,
"the host ptr of a subbuffer must point to a subportion of its parent buffer"
);
result = clEnqueueWriteBuffer(queue, subbuf, 1, 0, 5, "world", 0, 0, 0);
fail_if(
result != CL_SUCCESS,
"unable to write to the sub buffer"
);
char data[16];
result = clEnqueueReadBuffer(queue, subbuf, 1, 0, 5, data, 0, 0, 0);
fail_if(
result != CL_SUCCESS,
"unable to read the sub buffer"
);
fail_if(
strncmp(data, "world", 5),
"the subbuffer must contain \"world\""
);
result = clEnqueueReadBuffer(queue, buf, 1, 0, sizeof(s), data, 0, 0, 0);
fail_if(
result != CL_SUCCESS,
"unable to read the buffer"
);
fail_if(
strncmp(data, "Hello, world !", sizeof(s)),
"the buffer must contain \"Hello, world !\""
);
clReleaseMemObject(subbuf);
clReleaseMemObject(buf);
clReleaseContext(ctx);
}
END_TEST
TCase *cl_mem_tcase_create(void)
{
TCase *tc = NULL;
tc = tcase_create("mem");
tcase_add_test(tc, test_create_buffer);
tcase_add_test(tc, test_create_sub_buffer);
tcase_add_test(tc, test_read_write_subbuf);
return tc;
}
|