1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include "harness/compat.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #include "procs.h"
25 #include "harness/errorHelpers.h"
26
27
verify_copy_buffer(int * inptr,int * outptr,int n)28 static int verify_copy_buffer(int *inptr, int *outptr, int n)
29 {
30 int i;
31
32 for (i=0; i<n; i++){
33 if ( outptr[i] != inptr[i] )
34 return -1;
35 }
36
37 return 0;
38 }
39
40
test_copy(cl_command_queue queue,cl_context context,int num_elements,MTdata d)41 static int test_copy( cl_command_queue queue, cl_context context, int num_elements, MTdata d )
42 {
43 cl_mem buffers[2];
44 cl_int *int_input_ptr, *int_output_ptr;
45 cl_int err;
46 int i;
47 int src_flag_id, dst_flag_id;
48 int errors = 0;
49
50 size_t min_alignment = get_min_alignment(context);
51
52 int_input_ptr = (cl_int*) align_malloc(sizeof(cl_int) * num_elements, min_alignment);
53 int_output_ptr = (cl_int*)align_malloc(sizeof(cl_int) * num_elements, min_alignment);
54
55 for (src_flag_id=0; src_flag_id < NUM_FLAGS; src_flag_id++) {
56 for (dst_flag_id=0; dst_flag_id < NUM_FLAGS; dst_flag_id++) {
57 log_info("Testing with cl_mem_flags src: %s dst: %s\n", flag_set_names[src_flag_id], flag_set_names[dst_flag_id]);
58
59 for (i=0; i<num_elements; i++){
60 int_input_ptr[i] = (int)genrand_int32( d );
61 int_output_ptr[i] = 0xdeaddead; // seed with incorrect data
62 }
63
64 if ((flag_set[src_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[src_flag_id] & CL_MEM_COPY_HOST_PTR))
65 buffers[0] = clCreateBuffer(context, flag_set[src_flag_id], sizeof(cl_int) * num_elements, int_input_ptr, &err);
66 else
67 buffers[0] = clCreateBuffer(context, flag_set[src_flag_id], sizeof(cl_int) * num_elements, NULL, &err);
68 if ( err != CL_SUCCESS ){
69 print_error(err, " clCreateBuffer failed\n" );
70 align_free( (void *)int_input_ptr );
71 align_free( (void *)int_output_ptr );
72 return -1;
73 }
74
75 if ((flag_set[dst_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[dst_flag_id] & CL_MEM_COPY_HOST_PTR))
76 buffers[1] = clCreateBuffer(context, flag_set[dst_flag_id], sizeof(cl_int) * num_elements, int_output_ptr, &err);
77 else
78 buffers[1] = clCreateBuffer(context, flag_set[dst_flag_id], sizeof(cl_int) * num_elements, NULL, &err);
79 if ( err != CL_SUCCESS ){
80 print_error(err, " clCreateBuffer failed\n" );
81 clReleaseMemObject( buffers[0] );
82 align_free( (void *)int_input_ptr );
83 align_free( (void *)int_output_ptr );
84 return -1;
85 }
86
87 if (!(flag_set[src_flag_id] & CL_MEM_USE_HOST_PTR) && !(flag_set[src_flag_id] & CL_MEM_COPY_HOST_PTR)) {
88 err = clEnqueueWriteBuffer(queue, buffers[0], CL_TRUE, 0, sizeof(cl_int)*num_elements, (void *)int_input_ptr, 0, NULL, NULL);
89 if ( err != CL_SUCCESS ){
90 print_error( err, "clEnqueueWriteBuffer failed" );
91 clReleaseMemObject( buffers[0] );
92 clReleaseMemObject( buffers[1] );
93 align_free( (void *)int_output_ptr );
94 align_free( (void *)int_input_ptr );
95 return -1;
96 }
97 }
98
99 err = clEnqueueCopyBuffer(queue, buffers[0], buffers[1], 0, 0, sizeof(cl_int)*num_elements, 0, NULL, NULL);
100 if ( err != CL_SUCCESS ){
101 print_error( err, "clCopyArray failed" );
102 clReleaseMemObject( buffers[0] );
103 clReleaseMemObject( buffers[1] );
104 align_free( (void *)int_output_ptr );
105 align_free( (void *)int_input_ptr );
106 return -1;
107 }
108
109 err = clEnqueueReadBuffer( queue, buffers[1], true, 0, sizeof(int)*num_elements, (void *)int_output_ptr, 0, NULL, NULL );
110 if ( err != CL_SUCCESS ){
111 print_error( err, "clEnqueueReadBuffer failed" );
112 clReleaseMemObject( buffers[0] );
113 clReleaseMemObject( buffers[1] );
114 align_free( (void *)int_output_ptr );
115 align_free( (void *)int_input_ptr );
116 return -1;
117 }
118
119 if ( verify_copy_buffer(int_input_ptr, int_output_ptr, num_elements) ){
120 log_error( " test failed\n" );
121 errors++;
122 }
123 else{
124 log_info( " test passed\n" );
125 }
126 // cleanup
127 clReleaseMemObject( buffers[0] );
128 clReleaseMemObject( buffers[1] );
129 } // dst flags
130 } // src flags
131 // cleanup
132 align_free( (void *)int_output_ptr );
133 align_free( (void *)int_input_ptr );
134
135 return errors;
136
137 } // end test_copy()
138
139
testPartialCopy(cl_command_queue queue,cl_context context,int num_elements,cl_uint srcStart,cl_uint dstStart,int size,MTdata d)140 static int testPartialCopy( cl_command_queue queue, cl_context context, int num_elements, cl_uint srcStart, cl_uint dstStart, int size, MTdata d )
141 {
142 cl_mem buffers[2];
143 int *inptr, *outptr;
144 cl_int err;
145 int i;
146 int src_flag_id, dst_flag_id;
147 int errors = 0;
148
149 size_t min_alignment = get_min_alignment(context);
150
151 inptr = (int *)align_malloc( sizeof(int) * num_elements, min_alignment);
152 if ( ! inptr ){
153 log_error( " unable to allocate %d bytes of memory\n", (int)sizeof(int) * num_elements );
154 return -1;
155 }
156 outptr = (int *)align_malloc( sizeof(int) * num_elements, min_alignment);
157 if ( ! outptr ){
158 log_error( " unable to allocate %d bytes of memory\n", (int)sizeof(int) * num_elements );
159 align_free( (void *)inptr );
160 return -1;
161 }
162
163 for (src_flag_id=0; src_flag_id < NUM_FLAGS; src_flag_id++) {
164 for (dst_flag_id=0; dst_flag_id < NUM_FLAGS; dst_flag_id++) {
165 log_info("Testing with cl_mem_flags src: %s dst: %s\n", flag_set_names[src_flag_id], flag_set_names[dst_flag_id]);
166
167 for (i=0; i<num_elements; i++){
168 inptr[i] = (int)genrand_int32( d );
169 outptr[i] = (int)0xdeaddead; // seed with incorrect data
170 }
171
172 if ((flag_set[src_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[src_flag_id] & CL_MEM_COPY_HOST_PTR))
173 buffers[0] = clCreateBuffer(context, flag_set[src_flag_id], sizeof(cl_int) * num_elements, inptr, &err);
174 else
175 buffers[0] = clCreateBuffer(context, flag_set[src_flag_id], sizeof(cl_int) * num_elements, NULL, &err);
176 if ( err != CL_SUCCESS ){
177 print_error(err, " clCreateBuffer failed\n" )
178 align_free( (void *)outptr );
179 align_free( (void *)inptr );
180 return -1;
181 }
182
183 if ((flag_set[dst_flag_id] & CL_MEM_USE_HOST_PTR) || (flag_set[dst_flag_id] & CL_MEM_COPY_HOST_PTR))
184 buffers[1] = clCreateBuffer(context, flag_set[dst_flag_id], sizeof(cl_int) * num_elements, outptr, &err);
185 else
186 buffers[1] = clCreateBuffer(context, flag_set[dst_flag_id], sizeof(cl_int) * num_elements, NULL, &err);
187 if ( err != CL_SUCCESS ){
188 print_error(err, " clCreateBuffer failed\n" );
189 clReleaseMemObject( buffers[0] );
190 align_free( (void *)outptr );
191 align_free( (void *)inptr );
192 return -1;
193 }
194
195 if (!(flag_set[src_flag_id] & CL_MEM_USE_HOST_PTR) && !(flag_set[src_flag_id] & CL_MEM_COPY_HOST_PTR)){
196 err = clEnqueueWriteBuffer(queue, buffers[0], CL_TRUE, 0, sizeof(cl_int)*num_elements, (void *)inptr, 0, NULL, NULL);
197 if ( err != CL_SUCCESS ){
198 print_error( err, "clEnqueueWriteBuffer failed" );
199 clReleaseMemObject( buffers[1] );
200 clReleaseMemObject( buffers[0] );
201 align_free( (void *)outptr );
202 align_free( (void *)inptr );
203 return -1;
204 }
205 }
206
207 err = clEnqueueCopyBuffer(queue, buffers[0], buffers[1], srcStart*sizeof(cl_int), dstStart*sizeof(cl_int), sizeof(cl_int)*size, 0, NULL, NULL);
208 if ( err != CL_SUCCESS){
209 print_error( err, "clEnqueueCopyBuffer failed" );
210 clReleaseMemObject( buffers[1] );
211 clReleaseMemObject( buffers[0] );
212 align_free( (void *)outptr );
213 align_free( (void *)inptr );
214 return -1;
215 }
216
217 err = clEnqueueReadBuffer( queue, buffers[1], true, 0, sizeof(int)*num_elements, (void *)outptr, 0, NULL, NULL );
218 if ( err != CL_SUCCESS){
219 print_error( err, "clEnqueueReadBuffer failed" );
220 clReleaseMemObject( buffers[1] );
221 clReleaseMemObject( buffers[0] );
222 align_free( (void *)outptr );
223 align_free( (void *)inptr );
224 return -1;
225 }
226
227 if ( verify_copy_buffer(inptr + srcStart, outptr + dstStart, size) ){
228 log_error("buffer_COPY test failed\n");
229 errors++;
230 }
231 else{
232 log_info("buffer_COPY test passed\n");
233 }
234 // cleanup
235 clReleaseMemObject( buffers[1] );
236 clReleaseMemObject( buffers[0] );
237 } // dst mem flags
238 } // src mem flags
239 // cleanup
240 align_free( (void *)outptr );
241 align_free( (void *)inptr );
242
243 return errors;
244
245 } // end testPartialCopy()
246
247
test_buffer_copy(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)248 int test_buffer_copy( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements )
249 {
250 int i, err = 0;
251 int size;
252 MTdata d = init_genrand( gRandomSeed );
253
254 // test the preset size
255 log_info( "set size: %d: ", num_elements );
256 if (test_copy( queue, context, num_elements, d ))
257 err++;
258
259 // now test random sizes
260 for ( i = 0; i < 8; i++ ){
261 size = (int)get_random_float(2.f,131072.f, d);
262 log_info( "random size: %d: ", size );
263 if (test_copy( queue, context, size, d ))
264 err++;
265 }
266
267 free_mtdata(d);
268
269 return err;
270
271 } // end test_buffer_copy()
272
273
test_buffer_partial_copy(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)274 int test_buffer_partial_copy( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements )
275 {
276 int i, err = 0;
277 int size;
278 cl_uint srcStart, dstStart;
279 MTdata d = init_genrand( gRandomSeed );
280
281 // now test copy of partial sizes
282 for ( i = 0; i < 8; i++ ){
283 srcStart = (cl_uint)get_random_float( 0.f, (float)(num_elements - 8), d );
284 size = (int)get_random_float( 8.f, (float)(num_elements - srcStart), d );
285 dstStart = (cl_uint)get_random_float( 0.f, (float)(num_elements - size), d );
286 log_info( "random partial copy from %d to %d, size: %d: ", (int)srcStart, (int)dstStart, size );
287 if (testPartialCopy( queue, context, num_elements, srcStart, dstStart, size, d ))
288 err++;
289 }
290
291 free_mtdata(d);
292 return err;
293
294 } // end test_buffer_partial_copy()
295
296