// // Copyright (c) 2017 The Khronos Group Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include "testBase.h" #include "harness/testHarness.h" const char *write_kernels[] = { "__kernel void write_up(__global int *dst, int length)\n" "{\n" "\n" " dst[get_global_id(0)] *= 2;\n" "\n" "}\n" "__kernel void write_down(__global int *dst, int length)\n" "{\n" "\n" " dst[get_global_id(0)]--;\n" "\n" "}\n" }; #define TEST_SIZE 10000 #define TEST_COUNT 100 #define RANDOMIZE 1 #define DEBUG_OUT 0 /* Tests event dependencies by running two kernels that use the same buffer. If two_queues is set they are run in separate queues. If test_enqueue_wait_for_events is set then clEnqueueWaitForEvent is called between them. If test_barrier is set then clEnqueueBarrier is called between them (only for single queue). If neither are set, nothing is done to prevent them from executing in the wrong order. This can be used for verification. */ int test_event_enqueue_wait_for_events_run_test( cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements, int two_queues, int two_devices, int test_enqueue_wait_for_events, int test_barrier, int use_waitlist, int use_marker) { cl_int error = CL_SUCCESS; size_t threads[3] = {TEST_SIZE,0,0}; int i, loop_count, event_count, expected_value, failed; int expected_if_only_queue[2]; int max_count = TEST_SIZE; cl_platform_id platform; cl_command_queue queues[2]; // Not a wrapper so we don't autorelease if they are the same clCommandQueueWrapper queueWrappers[2]; // If they are different, we use the wrapper so it will auto release clContextWrapper context_to_use; clMemWrapper data; clProgramWrapper program; clKernelWrapper kernel1[TEST_COUNT], kernel2[TEST_COUNT]; clEventWrapper event[TEST_COUNT*4+2]; // If we usemarkers we get 2 more events per iteration if (test_enqueue_wait_for_events) log_info("\tTesting with clEnqueueBarrierWithWaitList as barrier function.\n"); if (test_barrier) log_info("\tTesting with clEnqueueBarrierWithWaitList as barrier function.\n"); if (use_waitlist) log_info("\tTesting with waitlist-based depenednecies between kernels.\n"); if (use_marker) log_info("\tTesting with clEnqueueMarker as a barrier function.\n"); if (test_barrier && (two_queues || two_devices)) { log_error("\tTest requested with clEnqueueBarrier across two queues. This is not a valid combination.\n"); return -1; } error = clGetPlatformIDs(1, &platform, NULL); test_error(error, "clGetPlatformIDs failed."); // If we are to use two devices, then get them and create a context with both. cl_device_id *two_device_ids; if (two_devices) { two_device_ids = (cl_device_id*)malloc(sizeof(cl_device_id)*2); cl_uint number_returned; error = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 2, two_device_ids, &number_returned); test_error( error, "clGetDeviceIDs for CL_DEVICE_TYPE_ALL failed."); if (number_returned != 2) { log_info("Failed to obtain two devices. Test can not run.\n"); free(two_device_ids); return 0; } for (i=0; i<2; i++) { cl_device_type type; error = clGetDeviceInfo(two_device_ids[i], CL_DEVICE_TYPE, sizeof(cl_device_type), &type, NULL); test_error( error, "clGetDeviceInfo failed."); if (type & CL_DEVICE_TYPE_CPU) log_info("\tDevice %d is CL_DEVICE_TYPE_CPU.\n", i); if (type & CL_DEVICE_TYPE_GPU) log_info("\tDevice %d is CL_DEVICE_TYPE_GPU.\n", i); if (type & CL_DEVICE_TYPE_ACCELERATOR) log_info("\tDevice %d is CL_DEVICE_TYPE_ACCELERATOR.\n", i); if (type & CL_DEVICE_TYPE_DEFAULT) log_info("\tDevice %d is CL_DEVICE_TYPE_DEFAULT.\n", i); } context_to_use = clCreateContext(NULL, 2, two_device_ids, notify_callback, NULL, &error); test_error(error, "clCreateContext failed for two devices."); log_info("\tTesting with two devices.\n"); } else { context_to_use = clCreateContext(NULL, 1, &deviceID, NULL, NULL, &error); test_error(error, "clCreateContext failed for one device."); log_info("\tTesting with one device.\n"); } // If we are using two queues then create them cl_command_queue_properties props = CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE; if (two_queues) { // Get a second queue if (two_devices) { if( !checkDeviceForQueueSupport( two_device_ids[ 0 ], props ) || !checkDeviceForQueueSupport( two_device_ids[ 1 ], props ) ) { log_info( "WARNING: One or more device for multi-device test does not support out-of-order exec mode; skipping test.\n" ); return -1942; } queueWrappers[0] = clCreateCommandQueue(context_to_use, two_device_ids[0], props, &error); test_error(error, "clCreateCommandQueue for first queue on first device failed."); queueWrappers[1] = clCreateCommandQueue(context_to_use, two_device_ids[1], props, &error); test_error(error, "clCreateCommandQueue for second queue on second device failed."); } else { // Single device has already been checked for out-of-order exec support queueWrappers[0] = clCreateCommandQueue(context_to_use, deviceID, props, &error); test_error(error, "clCreateCommandQueue for first queue failed."); queueWrappers[1] = clCreateCommandQueue(context_to_use, deviceID, props, &error); test_error(error, "clCreateCommandQueue for second queue failed."); } // Ugly hack to make sure we only have the wrapper auto-release if they are different queues queues[0] = queueWrappers[0]; queues[1] = queueWrappers[1]; log_info("\tTesting with two queues.\n"); } else { // (Note: single device has already been checked for out-of-order exec support) // Otherwise create one queue and have the second one be the same queueWrappers[0] = clCreateCommandQueue(context_to_use, deviceID, props, &error); test_error(error, "clCreateCommandQueue for first queue failed."); queues[0] = queueWrappers[0]; queues[1] = (cl_command_queue)queues[0]; log_info("\tTesting with one queue.\n"); } // Setup - create a buffer and the two kernels data = clCreateBuffer(context_to_use, CL_MEM_READ_WRITE, TEST_SIZE*sizeof(cl_int), NULL, &error); test_error( error, "clCreateBuffer failed"); // Initialize the values to zero cl_int *values = (cl_int*)malloc(TEST_SIZE*sizeof(cl_int)); for (i=0; i<(int)TEST_SIZE; i++) values[i] = 0; error = clEnqueueWriteBuffer(queues[0], data, CL_TRUE, 0, TEST_SIZE*sizeof(cl_int), values, 0, NULL, NULL); test_error( error, "clEnqueueWriteBuffer failed"); expected_value = 0; // Build the kernels if (create_single_kernel_helper( context_to_use, &program, &kernel1[0], 1, write_kernels, "write_up" )) return -1; error = clSetKernelArg(kernel1[0], 0, sizeof(data), &data); error |= clSetKernelArg(kernel1[0], 1, sizeof(max_count), &max_count); test_error( error, "clSetKernelArg 1 failed"); for (i=1; i