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 <stdio.h>
17 #include <string.h>
18 #include "harness/testHarness.h"
19 #include "harness/typeWrappers.h"
20 
21 #include <vector>
22 
23 #include "procs.h"
24 #include "utils.h"
25 
check_device_queue(cl_device_id device,cl_context context,cl_command_queue queue,cl_uint size)26 static int check_device_queue(cl_device_id device, cl_context context, cl_command_queue queue, cl_uint size)
27 {
28     cl_int err_ret;
29     cl_context q_context;
30     cl_device_id q_device;
31     cl_command_queue_properties q_properties;
32     cl_uint q_size;
33     size_t size_ret;
34 
35     err_ret = clRetainCommandQueue(queue);
36     test_error(err_ret, "clRetainCommandQueue() failed");
37 
38     err_ret = clGetCommandQueueInfo(queue, CL_QUEUE_CONTEXT, sizeof(q_context), &q_context, &size_ret);
39     test_error(err_ret, "clGetCommandQueueInfo(CL_QUEUE_CONTEXT) failed");
40     if(size_ret != sizeof(q_context) || q_context != context)
41     {
42         log_error("clGetCommandQueueInfo(CL_QUEUE_CONTEXT) returned invalid context\n");
43         return -1;
44     }
45 
46     err_ret = clGetCommandQueueInfo(queue, CL_QUEUE_DEVICE, sizeof(q_device), &q_device, &size_ret);
47     test_error(err_ret, "clGetCommandQueueInfo(CL_QUEUE_DEVICE) failed");
48     if(size_ret != sizeof(q_device) || q_device != device)
49     {
50         log_error("clGetCommandQueueInfo(CL_QUEUE_DEVICE) returned invalid device\n");
51         return -1;
52     }
53 
54     err_ret = clGetCommandQueueInfo(queue, CL_QUEUE_PROPERTIES, sizeof(q_properties), &q_properties, &size_ret);
55     test_error(err_ret, "clGetCommandQueueInfo(CL_QUEUE_PROPERTIES) failed");
56     if(size_ret != sizeof(q_properties) || !(q_properties & (CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE|CL_QUEUE_ON_DEVICE)))
57     {
58         log_error("clGetCommandQueueInfo(CL_QUEUE_PROPERTIES) returned invalid properties\n");
59         return -1;
60     }
61 
62     err_ret = clGetCommandQueueInfo(queue, CL_QUEUE_SIZE, sizeof(q_size), &q_size, &size_ret);
63     test_error(err_ret, "clGetCommandQueueInfo(CL_QUEUE_SIZE) failed");
64     if(size_ret != sizeof(q_size) || q_size < 1)
65     {
66         log_error("clGetCommandQueueInfo(CL_QUEUE_SIZE) returned invalid queue size\n");
67         return -1;
68     }
69 
70     err_ret = clReleaseCommandQueue(queue);
71     test_error(err_ret, "clReleaseCommandQueue() failed");
72 
73 
74     return 0;
75 }
76 
check_device_queues(cl_device_id device,cl_context context,cl_uint num_queues,cl_queue_properties * properties,cl_uint size)77 static int check_device_queues(cl_device_id device, cl_context context, cl_uint num_queues, cl_queue_properties *properties, cl_uint size)
78 {
79     cl_int err_ret, res = 0;
80     cl_uint i;
81     std::vector<clCommandQueueWrapper> queue(num_queues);
82 
83     // Create all queues
84     for(i = 0; i < num_queues; ++i)
85     {
86         queue[i] = clCreateCommandQueueWithProperties(context, device, properties, &err_ret);
87         test_error(err_ret, "clCreateCommandQueueWithProperties(CL_QUEUE_DEVICE) failed");
88     }
89 
90     // Validate all queues
91     for(i = 0; i < num_queues; ++i)
92     {
93         err_ret = check_device_queue(device, context, queue[i], size);
94         if(check_error(err_ret, "Device queue[%d] validation failed", i)) res = -1;
95 
96     }
97     return res;
98 }
99 
test_device_queue(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)100 int test_device_queue(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
101 {
102     cl_int err_ret, res = 0;
103     size_t ret_len;
104     clCommandQueueWrapper dev_queue;
105     cl_uint preffered_size, max_size, max_queues;
106 
107     cl_queue_properties queue_prop_def[] =
108     {
109         CL_QUEUE_PROPERTIES, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE|CL_QUEUE_ON_DEVICE|CL_QUEUE_ON_DEVICE_DEFAULT,
110         0
111     };
112 
113     cl_queue_properties queue_prop[] =
114     {
115         CL_QUEUE_PROPERTIES, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE|CL_QUEUE_ON_DEVICE,
116         0
117     };
118 
119     err_ret = clGetDeviceInfo(device, CL_DEVICE_QUEUE_ON_DEVICE_PREFERRED_SIZE, sizeof(preffered_size), &preffered_size, &ret_len);
120     test_error(err_ret, "clGetDeviceInfo(CL_DEVICE_QUEUE_ON_DEVICE_PREFERRED_SIZE) failed");
121 
122     err_ret = clGetDeviceInfo(device, CL_DEVICE_QUEUE_ON_DEVICE_MAX_SIZE, sizeof(max_size), &max_size, &ret_len);
123     test_error(err_ret, "clGetDeviceInfo(CL_DEVICE_QUEUE_ON_DEVICE_MAX_SIZE) failed");
124 
125     err_ret = clGetDeviceInfo(device, CL_DEVICE_MAX_ON_DEVICE_QUEUES, sizeof(max_queues), &max_queues, &ret_len);
126     test_error(err_ret, "clGetDeviceInfo(CL_DEVICE_MAX_ON_DEVICE_QUEUES) failed");
127 
128     if(max_queues > MAX_QUEUES) max_queues = MAX_QUEUES;
129 
130     dev_queue = clCreateCommandQueueWithProperties(context, device, queue_prop_def, &err_ret);
131     test_error(err_ret, "clCreateCommandQueueWithProperties(CL_QUEUE_DEVICE|CL_QUEUE_DEFAULT) failed");
132 
133     err_ret = check_device_queue(device, context, dev_queue, preffered_size);
134     if(check_error(err_ret, "Default device queue validation failed")) res = -1;
135 
136     log_info("Default device queue is OK.\n");
137 
138     if(max_queues > 1) // Check more queues if supported.
139     {
140         cl_uint q_size = preffered_size-1024;
141         cl_queue_properties queue_prop_size[] =
142         {
143             CL_QUEUE_PROPERTIES, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE|CL_QUEUE_ON_DEVICE,
144             CL_QUEUE_SIZE, q_size,
145             0
146         };
147 
148         cl_queue_properties queue_prop_max[] =
149         {
150             CL_QUEUE_PROPERTIES, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE|CL_QUEUE_ON_DEVICE,
151             CL_QUEUE_SIZE, max_size,
152             0
153         };
154         {
155             err_ret = check_device_queues(device, context, 1, queue_prop, preffered_size);
156             if(check_error(err_ret, "Second device queue validation failed")) res = -1;
157             else log_info("Second device queue is OK.\n");
158         }
159         {
160             err_ret = check_device_queues(device, context, 1, queue_prop_size, q_size);
161             if(check_error(err_ret, "Device queue with size validation failed")) res = -1;
162             else log_info("Device queue with size is OK.\n");
163         }
164         {
165             err_ret = check_device_queues(device, context, 1, queue_prop_max, max_size);
166             if(check_error(err_ret, "Device queue max size validation failed")) res = -1;
167             else log_info("Device queue max size is OK.\n");
168         }
169         {
170             err_ret = check_device_queues(device, context, max_queues, queue_prop, preffered_size);
171             if(check_error(err_ret, "Max number device queue validation failed")) res = -1;
172             else log_info("Max number device queue is OK.\n");
173         }
174         {
175             err_ret = check_device_queues(device, context, max_queues, queue_prop_size, q_size);
176             if(check_error(err_ret, "Max number device queue with size validation failed")) res = -1;
177             else log_info("Max number device queue with size is OK.\n");
178         }
179         {
180             err_ret = check_device_queues(device, context, max_queues, queue_prop_max, max_size);
181             if(check_error(err_ret, "Max number device queue with max size validation failed")) res = -1;
182             else log_info("Max number device queue with max size is OK.\n");
183         }
184     }
185 
186     return res;
187 }
188 
189