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 (int 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 (int i = 0; i < num_elements; ++i)
89     {
90         if (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_queue_properties_khr device_props = 0;
111     cl_queue_properties_khr queue_prop_def[] = { CL_QUEUE_PROPERTIES, 0, 0 };
112 
113     // Query extension
114     if (!is_extension_available(deviceID, "cl_khr_create_command_queue"))
115     {
116         log_info("extension cl_khr_create_command_queue is not supported.\n");
117         return 0;
118     }
119 
120     error = create_single_kernel_helper(context, &program, &kernel, 1, queue_test_kernel, "vec_cpy");
121     test_error(error, "create_single_kernel_helper failed");
122 
123     log_info("Queue property NULL. Testing ... \n");
124     error = enqueue_kernel(context, NULL,deviceID, kernel, (size_t)num_elements);
125     test_error(error, "enqueue_kernel failed");
126 
127     error = clGetDeviceInfo(deviceID, CL_DEVICE_QUEUE_PROPERTIES, sizeof(device_props), &device_props, NULL);
128     test_error(error, "clGetDeviceInfo for CL_DEVICE_QUEUE_PROPERTIES failed");
129 
130     if (device_props & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE)
131     {
132         log_info("Queue property CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE supported. Testing ... \n");
133         queue_prop_def[1] = CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE;
134         error = enqueue_kernel(context, queue_prop_def, deviceID, kernel, (size_t)num_elements);
135         test_error(error, "enqueue_kernel failed");
136     } else
137     {
138         log_info("Queue property CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE not supported \n");
139     }
140 
141     if (device_props & CL_QUEUE_PROFILING_ENABLE)
142     {
143         log_info("Queue property CL_QUEUE_PROFILING_ENABLE supported. Testing ... \n");
144         queue_prop_def[1] = CL_QUEUE_PROFILING_ENABLE;
145         error = enqueue_kernel(context, queue_prop_def, deviceID, kernel, (size_t)num_elements);
146         test_error(error, "enqueue_kernel failed");
147     } else
148     {
149         log_info("Queue property CL_QUEUE_PROFILING_ENABLE not supported \n");
150     }
151 
152     if (device_props & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE && device_props & CL_QUEUE_PROFILING_ENABLE)
153     {
154         log_info("Queue property CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE & CL_QUEUE_PROFILING_ENABLE supported. Testing ... \n");
155         queue_prop_def[1] = CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE|CL_QUEUE_PROFILING_ENABLE;
156         error = enqueue_kernel(context, queue_prop_def, deviceID, kernel, (size_t)num_elements);
157         test_error(error, "enqueue_kernel failed");
158     }
159     else
160     {
161         log_info("Queue property CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE or CL_QUEUE_PROFILING_ENABLE not supported \n");
162     }
163 
164     return 0;
165 }