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 "testBase.h"
17 
18 #if ! defined( _WIN32 )
19     #include "unistd.h" // for "sleep" used in the "while (1)" busy wait loop in
20 #endif
21 // test_event_flush
22 
23 const char *sample_long_test_kernel[] = {
24 "__kernel void sample_test(__global float *src, __global int *dst)\n"
25 "{\n"
26 "    int  tid = get_global_id(0);\n"
27 "     int  i;\n"
28 "\n"
29 "    for( i = 0; i < 10000; i++ )\n"
30 "    {\n"
31 "        dst[tid] = (int)src[tid] * 3;\n"
32 "    }\n"
33 "\n"
34 "}\n" };
35 
create_and_execute_kernel(cl_context inContext,cl_command_queue inQueue,cl_program * outProgram,cl_kernel * outKernel,cl_mem * streams,unsigned int lineCount,const char ** lines,const char * kernelName,cl_event * outEvent)36 int create_and_execute_kernel( cl_context inContext, cl_command_queue inQueue, cl_program *outProgram, cl_kernel *outKernel, cl_mem *streams,
37                               unsigned int lineCount, const char **lines, const char *kernelName, cl_event *outEvent )
38 {
39     size_t threads[1] = { 1000 }, localThreads[1];
40     int error;
41 
42     if( create_single_kernel_helper( inContext, outProgram, outKernel, lineCount, lines, kernelName ) )
43     {
44         return -1;
45     }
46 
47     error = get_max_common_work_group_size( inContext, *outKernel, threads[0], &localThreads[0] );
48     test_error( error, "Unable to get work group size to use" );
49 
50     streams[0] = clCreateBuffer(inContext, CL_MEM_READ_WRITE,
51                                 sizeof(cl_float) * 1000, NULL, &error);
52     test_error( error, "Creating test array failed" );
53     streams[1] = clCreateBuffer(inContext, CL_MEM_READ_WRITE,
54                                 sizeof(cl_int) * 1000, NULL, &error);
55     test_error( error, "Creating test array failed" );
56 
57     /* Set the arguments */
58     error = clSetKernelArg( *outKernel, 0, sizeof( streams[0] ), &streams[0] );
59     test_error( error, "Unable to set kernel arguments" );
60     error = clSetKernelArg( *outKernel, 1, sizeof( streams[1] ), &streams[1] );
61     test_error( error, "Unable to set kernel arguments" );
62 
63     error = clEnqueueNDRangeKernel(inQueue, *outKernel, 1, NULL, threads, localThreads, 0, NULL, outEvent);
64     test_error( error, "Unable to execute test kernel" );
65 
66     return 0;
67 }
68 
69 #define SETUP_EVENT( c, q ) \
70 clProgramWrapper program; \
71 clKernelWrapper kernel; \
72 clMemWrapper streams[2]; \
73 clEventWrapper event; \
74 int error; \
75 if( create_and_execute_kernel( c, q, &program, &kernel, &streams[0], 1, sample_long_test_kernel, "sample_test", &event ) ) return -1;
76 
77 #define FINISH_EVENT(_q) clFinish(_q)
78 
IGetStatusString(cl_int status)79 const char *IGetStatusString( cl_int status )
80 {
81     static char tempString[ 128 ];
82     switch( status )
83     {
84         case CL_COMPLETE:    return "CL_COMPLETE";
85         case CL_RUNNING:    return "CL_RUNNING";
86         case CL_QUEUED:        return "CL_QUEUED";
87         case CL_SUBMITTED:    return "CL_SUBMITTED";
88         default:
89             sprintf( tempString, "<unknown: %d>", (int)status );
90             return tempString;
91     }
92 }
93 
94 /* Note: tests clGetEventStatus and clReleaseEvent (implicitly) */
test_event_get_execute_status(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)95 int test_event_get_execute_status( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
96 {
97     cl_int status;
98     SETUP_EVENT( context, queue );
99 
100     /* Now wait for it to be done */
101     error = clWaitForEvents( 1, &event );
102     test_error( error, "Unable to wait for event" );
103 
104     error = clGetEventInfo( event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
105     test_error( error, "Calling clGetEventStatus to wait for event completion failed" );
106     if( status != CL_COMPLETE )
107     {
108         log_error( "ERROR: Incorrect status returned from clGetErrorStatus after event complete (%d:%s)\n", status, IGetStatusString( status ) );
109         return -1;
110     }
111 
112     FINISH_EVENT(queue);
113     return 0;
114 }
115 
test_event_get_info(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)116 int test_event_get_info( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
117 {
118     SETUP_EVENT( context, queue );
119 
120     /* Verify parameters of clGetEventInfo not already tested by other tests */
121     cl_command_queue otherQueue;
122     size_t size;
123 
124     error = clGetEventInfo( event, CL_EVENT_COMMAND_QUEUE, sizeof( otherQueue ), &otherQueue, &size );
125     test_error( error, "Unable to get event info!" );
126     // We can not check if this is the right queue because this is an opaque object.
127     if( size != sizeof( queue ) )
128     {
129         log_error( "ERROR: Returned command queue size does not validate (expected %d, got %d)\n", (int)sizeof( queue ), (int)size );
130         return -1;
131     }
132 
133     cl_command_type type;
134     error = clGetEventInfo( event, CL_EVENT_COMMAND_TYPE, sizeof( type ), &type, &size );
135     test_error( error, "Unable to get event info!" );
136     if( type != CL_COMMAND_NDRANGE_KERNEL )
137     {
138         log_error( "ERROR: Returned command type does not validate (expected %d, got %d)\n", (int)CL_COMMAND_NDRANGE_KERNEL, (int)type );
139         return -1;
140     }
141     if( size != sizeof( type ) )
142     {
143         log_error( "ERROR: Returned command type size does not validate (expected %d, got %d)\n", (int)sizeof( type ), (int)size );
144         return -1;
145     }
146 
147     cl_uint count;
148     error = clGetEventInfo( event, CL_EVENT_REFERENCE_COUNT, sizeof( count ), &count, &size );
149     test_error( error, "Unable to get event info for CL_EVENT_REFERENCE_COUNT!" );
150     if( size != sizeof( count ) )
151     {
152         log_error( "ERROR: Returned command type size does not validate (expected %d, got %d)\n", (int)sizeof( type ), (int)size );
153         return -1;
154     }
155 
156     cl_context testCtx;
157     error = clGetEventInfo( event, CL_EVENT_CONTEXT, sizeof( testCtx ), &testCtx, &size );
158     test_error( error, "Unable to get event context info!" );
159     if( size != sizeof( context ) )
160     {
161         log_error( "ERROR: Returned context size does not validate (expected %d, got %d)\n", (int)sizeof( context ), (int)size );
162         return -1;
163     }
164     if( testCtx != context )
165     {
166         log_error( "ERROR: Returned context does not match (expected %p, got %p)\n", (void *)context, (void *)testCtx );
167         return -1;
168     }
169 
170     FINISH_EVENT(queue);
171     return 0;
172 }
173 
test_event_get_write_array_status(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)174 int test_event_get_write_array_status( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
175 {
176     cl_mem stream;
177     cl_float testArray[ 1024 * 32 ];
178     cl_event event;
179     int error;
180     cl_int status;
181 
182 
183     stream = clCreateBuffer(context, CL_MEM_READ_WRITE,
184                             sizeof(cl_float) * 1024 * 32, NULL, &error);
185     test_error( error, "Creating test array failed" );
186 
187     error = clEnqueueWriteBuffer(queue, stream, CL_FALSE, 0, sizeof(cl_float)*1024*32, (void *)testArray, 0, NULL, &event);
188     test_error( error, "Unable to set testing kernel data" );
189 
190     /* Now wait for it to be done */
191     error = clWaitForEvents( 1, &event );
192     test_error( error, "Unable to wait for event" );
193 
194     error = clGetEventInfo( event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
195     test_error( error, "Calling clGetEventStatus to wait for event completion failed" );
196     if( status != CL_COMPLETE )
197     {
198         log_error( "ERROR: Incorrect status returned from clGetErrorStatus after array write complete (%d:%s)\n", status, IGetStatusString( status ) );
199         return -1;
200     }
201 
202 
203     clReleaseMemObject( stream );
204     clReleaseEvent( event );
205 
206     return 0;
207 }
208 
test_event_get_read_array_status(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)209 int test_event_get_read_array_status( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
210 {
211     cl_mem stream;
212     cl_float testArray[ 1024 * 32 ];
213     cl_event event;
214     int error;
215     cl_int status;
216 
217 
218     stream = clCreateBuffer(context, CL_MEM_READ_WRITE,
219                             sizeof(cl_float) * 1024 * 32, NULL, &error);
220     test_error( error, "Creating test array failed" );
221 
222     error = clEnqueueReadBuffer(queue, stream, CL_FALSE, 0, sizeof(cl_float)*1024*32, (void *)testArray, 0, NULL, &event);
223     test_error( error, "Unable to get testing kernel data" );
224 
225 
226     /* It should still be running... */
227     error = clGetEventInfo( event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
228     test_error( error, "Calling clGetEventStatus didn't work!" );
229 
230     if( status != CL_RUNNING && status != CL_QUEUED && status != CL_SUBMITTED && status != CL_COMPLETE)
231     {
232         log_error( "ERROR: Incorrect status returned from clGetErrorStatus during array read (%d:%s)\n", status, IGetStatusString( status ) );
233         return -1;
234     }
235 
236     /* Now wait for it to be done */
237     error = clWaitForEvents( 1, &event );
238     test_error( error, "Unable to wait for event" );
239 
240     error = clGetEventInfo( event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
241     test_error( error, "Calling clGetEventStatus to wait for event completion failed" );
242     if( status != CL_COMPLETE )
243     {
244         log_error( "ERROR: Incorrect status returned from clGetErrorStatus after array read complete (%d:%s)\n", status, IGetStatusString( status ) );
245         return -1;
246     }
247 
248 
249     clReleaseMemObject( stream );
250     clReleaseEvent( event );
251 
252     return 0;
253 }
254 
255 /* clGetEventStatus not implemented yet */
256 
test_event_wait_for_execute(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)257 int test_event_wait_for_execute( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
258 {
259     cl_int status;
260     SETUP_EVENT( context, queue );
261 
262     /* Now we wait for it to be done, then test the status again */
263     error = clWaitForEvents( 1, &event );
264     test_error( error, "Unable to wait for execute event" );
265 
266     /* Make sure it worked */
267     error = clGetEventInfo( event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
268     test_error( error, "Calling clGetEventStatus didn't work!" );
269     if( status != CL_COMPLETE )
270     {
271         log_error( "ERROR: Incorrect status returned from clGetErrorStatus after event complete (%d:%s)\n", status, IGetStatusString( status ) );
272         return -1;
273     }
274 
275     FINISH_EVENT(queue);
276     return 0;
277 }
278 
test_event_wait_for_array(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)279 int test_event_wait_for_array( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
280 {
281     cl_mem streams[2];
282     cl_float readArray[ 1024 * 32 ];
283     cl_float writeArray[ 1024 * 32 ];
284     cl_event events[2];
285     int error;
286     cl_int status;
287 
288 
289     streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE,
290                                 sizeof(cl_float) * 1024 * 32, NULL, &error);
291     test_error( error, "Creating test array failed" );
292     streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE,
293                                 sizeof(cl_float) * 1024 * 32, NULL, &error);
294     test_error( error, "Creating test array failed" );
295 
296     error = clEnqueueReadBuffer(queue, streams[0], CL_FALSE, 0, sizeof(cl_float)*1024*32, (void *)readArray, 0, NULL, &events[0]);
297     test_error( error, "Unable to read testing kernel data" );
298 
299     error = clEnqueueWriteBuffer(queue, streams[1], CL_FALSE, 0, sizeof(cl_float)*1024*32, (void *)writeArray, 0, NULL, &events[1]);
300     test_error( error, "Unable to write testing kernel data" );
301 
302     /* Both should still be running */
303     error = clGetEventInfo( events[0], CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
304     test_error( error, "Calling clGetEventStatus didn't work!" );
305     if( status != CL_RUNNING && status != CL_QUEUED && status != CL_SUBMITTED && status != CL_COMPLETE)
306     {
307         log_error( "ERROR: Incorrect status returned from clGetErrorStatus during array read (%d:%s)\n", status, IGetStatusString( status ) );
308         return -1;
309     }
310 
311     error = clGetEventInfo( events[1], CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
312     test_error( error, "Calling clGetEventStatus didn't work!" );
313     if( status != CL_RUNNING && status != CL_QUEUED && status != CL_SUBMITTED && status != CL_COMPLETE)
314     {
315         log_error( "ERROR: Incorrect status returned from clGetErrorStatus during array write (%d:%s)\n", status, IGetStatusString( status ) );
316         return -1;
317     }
318 
319     /* Now try waiting for both */
320     error = clWaitForEvents( 2, events );
321     test_error( error, "Unable to wait for array events" );
322 
323     /* Double check status on both */
324     error = clGetEventInfo( events[0], CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
325     test_error( error, "Calling clGetEventStatus didn't work!" );
326     if( status != CL_COMPLETE )
327     {
328         log_error( "ERROR: Incorrect status returned from clGetErrorStatus after array read complete (%d:%s)\n", status, IGetStatusString( status ) );
329         return -1;
330     }
331 
332     error = clGetEventInfo( events[1], CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
333     test_error( error, "Calling clGetEventStatus didn't work!" );
334     if( status != CL_COMPLETE )
335     {
336         log_error( "ERROR: Incorrect status returned from clGetErrorStatus after array write complete (%d:%s)\n", status, IGetStatusString( status ) );
337         return -1;
338     }
339 
340     clReleaseMemObject( streams[0] );
341     clReleaseMemObject( streams[1] );
342     clReleaseEvent( events[0] );
343     clReleaseEvent( events[1] );
344 
345     return 0;
346 }
347 
test_event_flush(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)348 int test_event_flush( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
349 {
350     int loopCount = 0;
351     cl_int status;
352     SETUP_EVENT( context, queue );
353 
354     /* Now flush. Note that we can't guarantee this actually lets the op finish, but we can guarantee it's no longer queued */
355     error = clFlush( queue );
356     test_error( error, "Unable to flush events" );
357 
358     /* Make sure it worked */
359          while (1) {
360         error = clGetEventInfo( event, CL_EVENT_COMMAND_EXECUTION_STATUS,
361                                                                 sizeof( status ), &status, NULL );
362     test_error( error, "Calling clGetEventStatus didn't work!" );
363 
364         if( status != CL_QUEUED )
365                   break;
366 
367 #if ! defined( _WIN32 )
368         sleep(1); // give it some time here.
369 #else // _WIN32
370             Sleep(1000);
371 #endif
372         ++loopCount;
373           }
374 
375 /*
376 CL_QUEUED (command has been enqueued in the command-queue),
377 CL_SUBMITTED (enqueued command has been submitted by the host to the device associated with the command-queue),
378 CL_RUNNING (device is currently executing this command),
379 CL_COMPLETE (the command has completed), or
380 Error code given by a negative integer value. (command was abnormally terminated – this may be caused by a bad memory access etc.).
381 */
382      if(status != CL_COMPLETE && status != CL_SUBMITTED &&
383         status != CL_RUNNING && status != CL_COMPLETE)
384     {
385         log_error( "ERROR: Incorrect status returned from clGetErrorStatus after event flush (%d:%s)\n", status, IGetStatusString( status ) );
386         return -1;
387     }
388 
389     /* Now wait */
390     error = clFinish( queue );
391     test_error( error, "Unable to finish events" );
392 
393     FINISH_EVENT(queue);
394     return 0;
395 }
396 
397 
test_event_finish_execute(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)398 int test_event_finish_execute( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
399 {
400     cl_int status;
401     SETUP_EVENT( context, queue );
402 
403     /* Now flush and finish all ops */
404     error = clFinish( queue );
405     test_error( error, "Unable to finish all events" );
406 
407     /* Make sure it worked */
408     error = clGetEventInfo( event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
409     test_error( error, "Calling clGetEventStatus didn't work!" );
410     if( status != CL_COMPLETE )
411     {
412         log_error( "ERROR: Incorrect status returned from clGetErrorStatus after event complete (%d:%s)\n", status, IGetStatusString( status ) );
413         return -1;
414     }
415 
416     FINISH_EVENT(queue);
417     return 0;
418 }
419 
test_event_finish_array(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)420 int test_event_finish_array( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
421 {
422     cl_mem streams[2];
423     cl_float readArray[ 1024 * 32 ];
424     cl_float writeArray[ 1024 * 32 ];
425     cl_event events[2];
426     int error;
427     cl_int status;
428 
429 
430     streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE,
431                                 sizeof(cl_float) * 1024 * 32, NULL, &error);
432     test_error( error, "Creating test array failed" );
433     streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE,
434                                 sizeof(cl_float) * 1024 * 32, NULL, &error);
435     test_error( error, "Creating test array failed" );
436 
437     error = clEnqueueReadBuffer(queue, streams[0], CL_FALSE, 0, sizeof(cl_float)*1024*32, (void *)readArray, 0, NULL, &events[0]);
438     test_error( error, "Unable to read testing kernel data" );
439 
440     error = clEnqueueWriteBuffer(queue, streams[1], CL_FALSE, 0, sizeof(cl_float)*1024*32, (void *)writeArray, 0, NULL, &events[1]);
441     test_error( error, "Unable to write testing kernel data" );
442 
443     /* Both should still be running */
444     error = clGetEventInfo( events[0], CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
445     test_error( error, "Calling clGetEventStatus didn't work!" );
446     if( status != CL_RUNNING && status != CL_QUEUED && status != CL_SUBMITTED && status != CL_COMPLETE)
447     {
448         log_error( "ERROR: Incorrect status returned from clGetErrorStatus during array read (%d:%s)\n", status, IGetStatusString( status ) );
449         return -1;
450     }
451 
452     error = clGetEventInfo( events[1], CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
453     test_error( error, "Calling clGetEventStatus didn't work!" );
454     if( status != CL_RUNNING && status != CL_QUEUED && status != CL_SUBMITTED && status != CL_COMPLETE)
455     {
456         log_error( "ERROR: Incorrect status returned from clGetErrorStatus during array write (%d:%s)\n", status, IGetStatusString( status ) );
457         return -1;
458     }
459 
460     /* Now try finishing all ops */
461     error = clFinish( queue );
462     test_error( error, "Unable to finish all events" );
463 
464     /* Double check status on both */
465     error = clGetEventInfo( events[0], CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
466     test_error( error, "Calling clGetEventStatus didn't work!" );
467     if( status != CL_COMPLETE )
468     {
469         log_error( "ERROR: Incorrect status returned from clGetErrorStatus after array read complete (%d:%s)\n", status, IGetStatusString( status ) );
470         return -1;
471     }
472 
473     error = clGetEventInfo( events[1], CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
474     test_error( error, "Calling clGetEventStatus didn't work!" );
475     if( status != CL_COMPLETE )
476     {
477         log_error( "ERROR: Incorrect status returned from clGetErrorStatus after array write complete (%d:%s)\n", status, IGetStatusString( status ) );
478         return -1;
479     }
480 
481     clReleaseMemObject( streams[0] );
482     clReleaseMemObject( streams[1] );
483     clReleaseEvent( events[0] );
484     clReleaseEvent( events[1] );
485 
486     return 0;
487 }
488 
489 
490 #define NUM_EVENT_RUNS 100
491 
test_event_release_before_done(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)492 int test_event_release_before_done( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
493 {
494     // Create a kernel to run
495     clProgramWrapper program;
496     clKernelWrapper kernel[NUM_EVENT_RUNS];
497     size_t threads[1] = { 1000 };
498     cl_event events[NUM_EVENT_RUNS];
499     cl_int status;
500     clMemWrapper streams[NUM_EVENT_RUNS][2];
501     int error, i;
502 
503     // Create a kernel
504     if( create_single_kernel_helper( context, &program, &kernel[0], 1, sample_long_test_kernel, "sample_test" ) )
505     {
506         return -1;
507     }
508 
509     for( i = 1; i < NUM_EVENT_RUNS; i++ ) {
510        kernel[i] = clCreateKernel(program, "sample_test", &error);
511     test_error(error, "Unable to create kernel");
512   }
513 
514     error = get_max_common_work_group_size( context, kernel[0], 1024, &threads[0] );
515     test_error( error, "Unable to get work group size to use" );
516 
517     // Create a set of streams to use as arguments
518     for( i = 0; i < NUM_EVENT_RUNS; i++ )
519     {
520         streams[i][0] =
521             clCreateBuffer(context, CL_MEM_READ_WRITE,
522                            sizeof(cl_float) * threads[0], NULL, &error);
523         streams[i][1] =
524             clCreateBuffer(context, CL_MEM_READ_WRITE,
525                            sizeof(cl_int) * threads[0], NULL, &error);
526         if( ( streams[i][0] == NULL ) || ( streams[i][1] == NULL ) )
527         {
528             log_error( "ERROR: Unable to allocate testing streams" );
529             return -1;
530         }
531     }
532 
533     // Execute the kernels one by one, hopefully making sure they won't be done by the time we get to the end
534     for( i = 0; i < NUM_EVENT_RUNS; i++ )
535     {
536         error = clSetKernelArg( kernel[i], 0, sizeof( cl_mem ), &streams[i][0] );
537         error |= clSetKernelArg( kernel[i], 1, sizeof( cl_mem ), &streams[i][1] );
538         test_error( error, "Unable to set kernel arguments" );
539 
540         error = clEnqueueNDRangeKernel( queue, kernel[i], 1, NULL, threads, threads, 0, NULL, &events[i]);
541         test_error( error, "Unable to execute test kernel" );
542     }
543 
544     // Free all but the last event
545     for( i = 0; i < NUM_EVENT_RUNS - 1; i++ )
546     {
547         clReleaseEvent( events[ i ] );
548     }
549 
550     // Get status on the last one, then free it
551     error = clGetEventInfo( events[ NUM_EVENT_RUNS - 1 ], CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
552     test_error( error, "Unable to get event status" );
553 
554     clReleaseEvent( events[ NUM_EVENT_RUNS - 1 ] );
555 
556     // Was the status still-running?
557     if( status == CL_COMPLETE )
558     {
559         log_info( "WARNING: Events completed before they could be released, so test is a null-op. Increase workload and try again." );
560     }
561     else if( status == CL_RUNNING || status == CL_QUEUED || status == CL_SUBMITTED )
562     {
563         log_info( "Note: Event status was running or queued when released, so test was good.\n" );
564     }
565 
566     // If we didn't crash by now, the test succeeded
567     clFinish( queue );
568 
569     return 0;
570 }
571 
test_event_enqueue_marker(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)572 int test_event_enqueue_marker( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
573 {
574     cl_int status;
575     SETUP_EVENT( context, queue );
576 
577     /* Now we queue a marker and wait for that, which--since it queues afterwards--should guarantee the execute finishes too */
578     clEventWrapper markerEvent;
579     //error = clEnqueueMarker( queue, &markerEvent );
580 
581 #ifdef CL_VERSION_1_2
582     error = clEnqueueMarkerWithWaitList(queue, 0, NULL, &markerEvent );
583 #else
584     error = clEnqueueMarker( queue, &markerEvent );
585 #endif
586        test_error( error, "Unable to queue marker" );
587     /* Now we wait for it to be done, then test the status again */
588     error = clWaitForEvents( 1, &markerEvent );
589     test_error( error, "Unable to wait for marker event" );
590 
591     /* Check the status of the first event */
592     error = clGetEventInfo( event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof( status ), &status, NULL );
593     test_error( error, "Calling clGetEventInfo didn't work!" );
594     if( status != CL_COMPLETE )
595     {
596         log_error( "ERROR: Incorrect status returned from clGetEventInfo after event complete (%d:%s)\n", status, IGetStatusString( status ) );
597         return -1;
598     }
599 
600     FINISH_EVENT(queue);
601     return 0;
602 }
603 
604 #ifdef CL_VERSION_1_2
test_event_enqueue_marker_with_event_list(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)605 int test_event_enqueue_marker_with_event_list( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
606 {
607 
608     cl_int status;
609     SETUP_EVENT( context, queue );
610     cl_event event_list[3]={ NULL, NULL, NULL};
611 
612     size_t threads[1] = { 10 }, localThreads[1]={1};
613     cl_uint event_count=2;
614     error= clEnqueueNDRangeKernel( queue,kernel,1,NULL, threads, localThreads, 0, NULL, &event_list[0]);
615       test_error( error, " clEnqueueMarkerWithWaitList   1 " );
616 
617     error= clEnqueueNDRangeKernel( queue,kernel,1,NULL, threads, localThreads, 0, NULL, &event_list[1]);
618       test_error( error, " clEnqueueMarkerWithWaitList 2" );
619 
620     error= clEnqueueNDRangeKernel( queue,kernel,1,NULL, threads, localThreads, 0, NULL, NULL);
621       test_error( error, " clEnqueueMarkerWithWaitList  3" );
622 
623     // test the case event returned
624     error =clEnqueueMarkerWithWaitList(queue, event_count, event_list,  &event_list[2]);
625       test_error( error, " clEnqueueMarkerWithWaitList " );
626 
627     error = clReleaseEvent(event_list[0]);
628     error |= clReleaseEvent(event_list[1]);
629     test_error( error, "clReleaseEvent" );
630 
631     error= clEnqueueNDRangeKernel( queue,kernel,1,NULL, threads, localThreads, 0, NULL, &event_list[0]);
632     test_error( error, " clEnqueueMarkerWithWaitList   1 -1 " );
633 
634     error= clEnqueueNDRangeKernel( queue,kernel,1,NULL, threads, localThreads, 0, NULL, &event_list[1]);
635     test_error( error, " clEnqueueMarkerWithWaitList  2-2" );
636 
637     // test the case event =NULL,   caused [CL_INVALID_VALUE] : OpenCL Error : clEnqueueMarkerWithWaitList failed: event is a NULL value
638     error =clEnqueueMarkerWithWaitList(queue, event_count, event_list,  NULL);
639     test_error( error, " clEnqueueMarkerWithWaitList " );
640 
641     error = clReleaseEvent(event_list[0]);
642     error |= clReleaseEvent(event_list[1]);
643     error |= clReleaseEvent(event_list[2]);
644     test_error( error, "clReleaseEvent" );
645 
646     FINISH_EVENT(queue);
647     return 0;
648 }
649 
test_event_enqueue_barrier_with_event_list(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)650 int test_event_enqueue_barrier_with_event_list( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
651 {
652 
653     cl_int status;
654     SETUP_EVENT( context, queue );
655     cl_event event_list[3]={ NULL, NULL, NULL};
656 
657     size_t threads[1] = { 10 }, localThreads[1]={1};
658     cl_uint event_count=2;
659     error= clEnqueueNDRangeKernel( queue,kernel,1,NULL, threads, localThreads, 0, NULL, &event_list[0]);
660     test_error( error, " clEnqueueBarrierWithWaitList   1 " );
661 
662     error= clEnqueueNDRangeKernel( queue,kernel,1,NULL, threads, localThreads, 0, NULL, &event_list[1]);
663     test_error( error, " clEnqueueBarrierWithWaitList 2" );
664 
665     error= clEnqueueNDRangeKernel( queue,kernel,1,NULL, threads, localThreads, 0, NULL, NULL);
666     test_error( error, " clEnqueueBarrierWithWaitList  20" );
667 
668     // test the case event returned
669     error =clEnqueueBarrierWithWaitList(queue, event_count, event_list,  &event_list[2]);
670     test_error( error, " clEnqueueBarrierWithWaitList " );
671 
672     clReleaseEvent(event_list[0]);
673     clReleaseEvent(event_list[1]);
674 
675     error= clEnqueueNDRangeKernel( queue,kernel,1,NULL, threads, localThreads, 0, NULL, &event_list[0]);
676     test_error( error, " clEnqueueBarrierWithWaitList   1 " );
677 
678     error= clEnqueueNDRangeKernel( queue,kernel,1,NULL, threads, localThreads, 0, NULL, &event_list[1]);
679     test_error( error, " clEnqueueBarrierWithWaitList 2" );
680 
681     // test the case event =NULL,   caused [CL_INVALID_VALUE] : OpenCL Error : clEnqueueMarkerWithWaitList failed: event is a NULL value
682     error = clEnqueueBarrierWithWaitList(queue, event_count, event_list,  NULL);
683     test_error( error, " clEnqueueBarrierWithWaitList " );
684 
685     clReleaseEvent(event_list[0]);
686     clReleaseEvent(event_list[1]);
687     clReleaseEvent(event_list[2]);
688 
689     FINISH_EVENT(queue);
690     return 0;
691 }
692 #endif
693