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 #include <time.h>
26
27 extern int gWimpyMode;
28
29 #ifdef CL_VERSION_2_0
30
31 static const char* enqueue_block_first_kernel[] =
32 {
33 NL, "void block_fn(uint num, __global int* res)"
34 NL, "{"
35 NL, " size_t tid = get_global_id(0);"
36 NL, ""
37 NL, " for(int i = 1 ; i < tid ; i++)"
38 NL, " {"
39 NL, " for(int j = 0 ; j < num ; j++)"
40 NL, " atomic_add(res+tid, 1);"
41 NL, " }"
42 NL, "}"
43 NL, ""
44 NL, "kernel void enqueue_block_first_kernel(uint num, __global int* res)"
45 NL, "{"
46 NL, " void (^kernelBlock)(void) = ^{ block_fn(num, res); };"
47 NL, ""
48 NL, " ndrange_t ndrange = ndrange_1D(num, 1);"
49 NL, ""
50 NL, " int enq_res = enqueue_kernel(get_default_queue(), CLK_ENQUEUE_FLAGS_NO_WAIT, ndrange, kernelBlock);"
51 NL, " if(enq_res != CLK_SUCCESS) { res[0] = -1; return; }"
52 NL, ""
53 NL, "}"
54 NL
55 };
56
57 static const char* enqueue_block_second_kernel[] =
58 {
59 NL, "void block_fn(uint num, __global int* res)"
60 NL, "{"
61 NL, " for(int i = 2 ; i < num ; i++)"
62 NL, " {"
63 NL, " res[i] = res[i]/num - (i-1);"
64 NL, " }"
65 NL, "}"
66 NL, ""
67 NL, "kernel void enqueue_block_second_kernel(uint num, __global int* res)"
68 NL, "{"
69 NL, " void (^kernelBlock)(void) = ^{ block_fn(num, res); };"
70 NL, ""
71 NL, " ndrange_t ndrange = ndrange_1D(1);"
72 NL, ""
73 NL, " int enq_res = enqueue_kernel(get_default_queue(), CLK_ENQUEUE_FLAGS_WAIT_KERNEL, ndrange, kernelBlock);"
74 NL, " if(enq_res != CLK_SUCCESS) { res[0] = -1; return; }"
75 NL, ""
76 NL, "}"
77 NL
78 };
79
check_kernel_results(cl_int * results,cl_int len)80 static int check_kernel_results(cl_int* results, cl_int len)
81 {
82 for(cl_int i = 0; i < len; ++i)
83 {
84 if(results[i] != 0) return i;
85 }
86 return -1;
87 }
88
89 /*
90 Test checks kernel block execution order in case of two different kernels with enqueue block submitted to one ordered host queue.
91 */
test_host_queue_order(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)92 int test_host_queue_order(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
93 {
94 cl_int k, err_ret, res = 0;
95 clCommandQueueWrapper dev_queue;
96 cl_int kernel_results[MAX_GWS] = {0};
97
98 size_t ret_len;
99 cl_uint max_queues = 1;
100 cl_uint maxQueueSize = 0;
101 err_ret = clGetDeviceInfo(device, CL_DEVICE_QUEUE_ON_DEVICE_MAX_SIZE, sizeof(maxQueueSize), &maxQueueSize, 0);
102 test_error(err_ret, "clGetDeviceInfo(CL_DEVICE_QUEUE_ON_DEVICE_MAX_SIZE) failed");
103
104 err_ret = clGetDeviceInfo(device, CL_DEVICE_MAX_ON_DEVICE_QUEUES, sizeof(max_queues), &max_queues, &ret_len);
105 test_error(err_ret, "clGetDeviceInfo(CL_DEVICE_MAX_ON_DEVICE_QUEUES) failed");
106
107 size_t max_local_size = 1;
108 err_ret = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(max_local_size), &max_local_size, &ret_len);
109 test_error(err_ret, "clGetDeviceInfo(CL_DEVICE_MAX_WORK_GROUP_SIZE) failed");
110
111 cl_queue_properties queue_prop_def[] =
112 {
113 CL_QUEUE_PROPERTIES, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE|CL_QUEUE_ON_DEVICE|CL_QUEUE_ON_DEVICE_DEFAULT,
114 CL_QUEUE_SIZE, maxQueueSize,
115 0
116 };
117
118 dev_queue = clCreateCommandQueueWithProperties(context, device, queue_prop_def, &err_ret);
119 test_error(err_ret, "clCreateCommandQueueWithProperties(CL_QUEUE_DEVICE|CL_QUEUE_DEFAULT) failed");
120
121 cl_int status;
122 size_t size = 1;
123 cl_int result[MAX_GWS] = { 0 };
124 cl_uint num = arr_size(result);
125 if( gWimpyMode )
126 {
127 num = MAX(num / 16, 4);
128 }
129
130 clMemWrapper res_mem;
131 clProgramWrapper program1, program2;
132 clKernelWrapper kernel1, kernel2;
133
134 cl_event kernel_event;
135
136 err_ret = create_single_kernel_helper(
137 context, &program1, &kernel1, arr_size(enqueue_block_first_kernel),
138 enqueue_block_first_kernel, "enqueue_block_first_kernel");
139 if(check_error(err_ret, "Create single kernel failed")) return -1;
140
141 err_ret = create_single_kernel_helper(
142 context, &program2, &kernel2, arr_size(enqueue_block_second_kernel),
143 enqueue_block_second_kernel, "enqueue_block_second_kernel");
144 if(check_error(err_ret, "Create single kernel failed")) return -1;
145
146 res_mem = clCreateBuffer(context, CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR, sizeof(kernel_results), kernel_results, &err_ret);
147 test_error(err_ret, "clCreateBuffer() failed");
148
149 // Enqueue first kernel
150 err_ret = clSetKernelArg(kernel1, 0, sizeof(num), &num);
151 test_error(err_ret, "clSetKernelArg(0) failed");
152 err_ret = clSetKernelArg(kernel1, 1, sizeof(cl_mem), &res_mem);
153 test_error(err_ret, "clSetKernelArg(1) failed");
154
155 cl_event event1 = clCreateUserEvent(context, &err_ret);
156 if(check_error(err_ret, "Create user event failed")) return -1;
157
158 err_ret = clEnqueueNDRangeKernel(queue, kernel1, 1, NULL, &size, &size, 1, &event1, NULL);
159 test_error(err_ret, "clEnqueueNDRangeKernel('enqueue_block_first_kernel') failed");
160
161 // Enqueue second kernel
162 err_ret = clSetKernelArg(kernel2, 0, sizeof(num), &num);
163 test_error(err_ret, "clSetKernelArg(0) failed");
164 err_ret = clSetKernelArg(kernel2, 1, sizeof(cl_mem), &res_mem);
165 test_error(err_ret, "clSetKernelArg(1) failed");
166
167 err_ret = clEnqueueNDRangeKernel(queue, kernel2, 1, NULL, &size, &size, 0, NULL, &kernel_event);
168 test_error(err_ret, "clEnqueueNDRangeKernel('enqueue_block_second_kernel') failed");
169
170 //Triger execution of first kernel
171 err_ret = clSetUserEventStatus(event1, CL_COMPLETE);
172 test_error(err_ret, "clSetUserEventStatus() failed");
173
174 // Collect resulsts
175 err_ret = clEnqueueReadBuffer(queue, res_mem, CL_TRUE, 0, sizeof(result), result, 0, NULL, NULL);
176 test_error(err_ret, "clEnqueueReadBuffer() failed");
177
178 err_ret = clGetEventInfo(kernel_event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(status), &status, &ret_len);
179 test_error(err_ret, "clGetEventInfo() failed");
180
181 if(check_error(status, "Kernel execution status %d", status)) return status;
182
183 if((k = check_kernel_results(result, num)) >= 0 && check_error(-1, "'%s' results validation failed: [%d] returned %d expected 0", "test_host_queue_order", k, result[k])) res = -1;
184
185 clReleaseEvent(kernel_event);
186 clReleaseEvent(event1);
187
188 return res;
189 }
190
191 #endif
192
193