1 //
2 // Copyright (c) 2018 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
17
18 #include "testBase.h"
19 #include "harness/typeWrappers.h"
20 #include "harness/conversions.h"
21 #include <sstream>
22 #include <string>
23 #include <vector>
24
25 using namespace std;
26 /*
27 The test against cl_khr_create_command_queue extension. It validates if devices with Opencl 1.X can use clCreateCommandQueueWithPropertiesKHR function.
28 Based on device capabilities test will create queue with NULL properties, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE property and
29 CL_QUEUE_PROFILING_ENABLE property. Finally simple kernel will be executed on such queue.
30 */
31
32 const char *queue_test_kernel[] = {
33 "__kernel void vec_cpy(__global int *src, __global int *dst)\n"
34 "{\n"
35 " int tid = get_global_id(0);\n"
36 "\n"
37 " dst[tid] = src[tid];\n"
38 "\n"
39 "}\n" };
40
enqueue_kernel(cl_context context,const cl_queue_properties_khr * queue_prop_def,cl_device_id deviceID,clKernelWrapper & kernel,size_t num_elements)41 int enqueue_kernel(cl_context context, const cl_queue_properties_khr *queue_prop_def, cl_device_id deviceID, clKernelWrapper& kernel, size_t num_elements)
42 {
43 clMemWrapper streams[2];
44 int error;
45 std::vector<int> buf(num_elements);
46 clCreateCommandQueueWithPropertiesKHR_fn clCreateCommandQueueWithPropertiesKHR = NULL;
47 cl_platform_id platform;
48 clEventWrapper event;
49
50 error = clGetDeviceInfo(deviceID, CL_DEVICE_PLATFORM, sizeof(cl_platform_id), &platform, NULL);
51 test_error(error, "clGetDeviceInfo for CL_DEVICE_PLATFORM failed");
52
53 clCreateCommandQueueWithPropertiesKHR = (clCreateCommandQueueWithPropertiesKHR_fn) clGetExtensionFunctionAddressForPlatform(platform, "clCreateCommandQueueWithPropertiesKHR");
54 if (clCreateCommandQueueWithPropertiesKHR == NULL)
55 {
56 log_error("ERROR: clGetExtensionFunctionAddressForPlatform failed\n");
57 return -1;
58 }
59
60 clCommandQueueWrapper queue = clCreateCommandQueueWithPropertiesKHR(context, deviceID, queue_prop_def, &error);
61 test_error(error, "clCreateCommandQueueWithPropertiesKHR failed");
62
63 for (size_t i = 0; i < num_elements; ++i)
64 {
65 buf[i] = i;
66 }
67
68 streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, num_elements * sizeof(int), buf.data(), &error);
69 test_error( error, "clCreateBuffer failed." );
70 streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_ALLOC_HOST_PTR, num_elements * sizeof(int), NULL, &error);
71 test_error( error, "clCreateBuffer failed." );
72
73 error = clSetKernelArg(kernel, 0, sizeof(streams[0]), &streams[0]);
74 test_error( error, "clSetKernelArg failed." );
75
76 error = clSetKernelArg(kernel, 1, sizeof(streams[1]), &streams[1]);
77 test_error( error, "clSetKernelArg failed." );
78
79 error = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &num_elements, NULL, 0, NULL, &event);
80 test_error( error, "clEnqueueNDRangeKernel failed." );
81
82 error = clWaitForEvents(1, &event);
83 test_error(error, "clWaitForEvents failed.");
84
85 error = clEnqueueReadBuffer(queue, streams[1], CL_TRUE, 0, num_elements, buf.data(), 0, NULL, NULL);
86 test_error( error, "clEnqueueReadBuffer failed." );
87
88 for (size_t i = 0; i < num_elements; ++i)
89 {
90 if (static_cast<size_t>(buf[i]) != i)
91 {
92 log_error("ERROR: Incorrect vector copy result.");
93 return -1;
94 }
95 }
96
97 return 0;
98 }
99
test_queue_properties(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)100 int test_queue_properties(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
101 {
102 if (num_elements <= 0)
103 {
104 num_elements = 128;
105 }
106 int error = 0;
107
108 clProgramWrapper program;
109 clKernelWrapper kernel;
110 cl_command_queue_properties device_props = 0;
111 cl_command_queue_properties queue_prop_def[] = { CL_QUEUE_PROPERTIES, 0,
112 0 };
113
114 // Query extension
115 if (!is_extension_available(deviceID, "cl_khr_create_command_queue"))
116 {
117 log_info("extension cl_khr_create_command_queue is not supported.\n");
118 return 0;
119 }
120
121 error = create_single_kernel_helper(context, &program, &kernel, 1, queue_test_kernel, "vec_cpy");
122 test_error(error, "create_single_kernel_helper failed");
123
124 log_info("Queue property NULL. Testing ... \n");
125 error = enqueue_kernel(context, NULL,deviceID, kernel, (size_t)num_elements);
126 test_error(error, "enqueue_kernel failed");
127
128 error = clGetDeviceInfo(deviceID, CL_DEVICE_QUEUE_PROPERTIES, sizeof(device_props), &device_props, NULL);
129 test_error(error, "clGetDeviceInfo for CL_DEVICE_QUEUE_PROPERTIES failed");
130
131 if (device_props & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE)
132 {
133 log_info("Queue property CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE supported. Testing ... \n");
134 queue_prop_def[1] = CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE;
135 error = enqueue_kernel(context, queue_prop_def, deviceID, kernel, (size_t)num_elements);
136 test_error(error, "enqueue_kernel failed");
137 } else
138 {
139 log_info("Queue property CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE not supported \n");
140 }
141
142 if (device_props & CL_QUEUE_PROFILING_ENABLE)
143 {
144 log_info("Queue property CL_QUEUE_PROFILING_ENABLE supported. Testing ... \n");
145 queue_prop_def[1] = CL_QUEUE_PROFILING_ENABLE;
146 error = enqueue_kernel(context, queue_prop_def, deviceID, kernel, (size_t)num_elements);
147 test_error(error, "enqueue_kernel failed");
148 } else
149 {
150 log_info("Queue property CL_QUEUE_PROFILING_ENABLE not supported \n");
151 }
152
153 if (device_props & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE && device_props & CL_QUEUE_PROFILING_ENABLE)
154 {
155 log_info("Queue property CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE & CL_QUEUE_PROFILING_ENABLE supported. Testing ... \n");
156 queue_prop_def[1] = CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE|CL_QUEUE_PROFILING_ENABLE;
157 error = enqueue_kernel(context, queue_prop_def, deviceID, kernel, (size_t)num_elements);
158 test_error(error, "enqueue_kernel failed");
159 }
160 else
161 {
162 log_info("Queue property CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE or CL_QUEUE_PROFILING_ENABLE not supported \n");
163 }
164
165 return 0;
166 }
167