• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 
29 
30 #ifdef CL_VERSION_2_0
31 extern int gWimpyMode;
32 static const char enqueue_block_multi_queue[] =
33     NL "#define BLOCK_COMPLETED 0"
34     NL "#define BLOCK_SUBMITTED 1"
35     NL ""
36     NL "kernel void enqueue_block_multi_queue(__global int* res, __global int* buff %s)"
37     NL "{"
38     NL "  uint i, n = %d;"
39     NL "  clk_event_t block_evt[%d];"
40     NL "  queue_t q[] = { %s };"
41     NL "  queue_t *queue = q;"
42     NL ""
43     NL "  clk_event_t user_evt = create_user_event();"
44     NL "  queue_t def_q = get_default_queue();"
45     NL "  size_t tid = get_global_id(0);"
46     NL "  res[tid] = -1;"
47     NL "  __global int* b = buff + tid*n;"
48     NL "  for(i=0; i<n; ++i) b[i] = -1;"
49     NL ""
50     NL "  ndrange_t ndrange = ndrange_1D(1);"
51     NL "  for(i = 0; i < n; ++i)"
52     NL "  {"
53     NL "    b[i] = BLOCK_SUBMITTED;"
54     NL "    int enq_res = enqueue_kernel(queue[i], CLK_ENQUEUE_FLAGS_NO_WAIT, ndrange, 1, &user_evt, &block_evt[i], "
55     NL "    ^{"
56     NL "       b[i] = BLOCK_COMPLETED;"
57     NL "     });"
58     NL "    if(enq_res != CLK_SUCCESS) { res[tid] = -2; return; }"
59     NL "  }"
60     NL ""
61     NL "  // check blocks are not started"
62     NL "  for(i = 0; i < n; ++i)"
63     NL "  {"
64     NL "    if(b[i] != BLOCK_SUBMITTED) { res[tid] = -5; return; }"
65     NL "  }"
66     NL ""
67     NL "  res[tid] = BLOCK_SUBMITTED;"
68     NL "  int enq_res = enqueue_kernel(def_q, CLK_ENQUEUE_FLAGS_NO_WAIT, ndrange, n, block_evt, NULL, "
69     NL "  ^{"
70     NL "     uint k;"
71     NL "     // check blocks are finished"
72     NL "     for(k = 0; k < n; ++k)"
73     NL "     {"
74     NL "       if(b[k] != BLOCK_COMPLETED) { res[tid] = -3; return; }"
75     NL "     }"
76     NL "     res[tid] = BLOCK_COMPLETED;"
77     NL "   });"
78     NL "  for(i = 0; i < n; ++i)"
79     NL "  {"
80     NL "    release_event(block_evt[i]);"
81     NL "  }"
82     NL "  if(enq_res != CLK_SUCCESS) { res[tid] = -4; return; }"
83     NL ""
84     NL "  set_user_event_status(user_evt, CL_COMPLETE);"
85     NL "  release_event(user_evt);"
86     NL "}";
87 
88 
check_kernel_results(cl_int * results,cl_int len)89 static int check_kernel_results(cl_int* results, cl_int len)
90 {
91     for(cl_int i = 0; i < len; ++i)
92     {
93         if(results[i] != 0) return i;
94     }
95     return -1;
96 }
97 
test_enqueue_multi_queue(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)98 int test_enqueue_multi_queue(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
99 {
100     cl_uint i;
101     cl_int k, err_ret, res = 0;
102     clCommandQueueWrapper dev_queue;
103     cl_int kernel_results[MAX_GWS] = {0};
104 
105     size_t ret_len;
106     cl_uint n, max_queues = 1;
107     cl_uint maxQueueSize = 0;
108     err_ret = clGetDeviceInfo(device, CL_DEVICE_QUEUE_ON_DEVICE_MAX_SIZE, sizeof(maxQueueSize), &maxQueueSize, 0);
109     test_error(err_ret, "clGetDeviceInfo(CL_DEVICE_QUEUE_ON_DEVICE_MAX_SIZE) failed");
110 
111     err_ret = clGetDeviceInfo(device, CL_DEVICE_MAX_ON_DEVICE_QUEUES, sizeof(max_queues), &max_queues, &ret_len);
112     test_error(err_ret, "clGetDeviceInfo(CL_DEVICE_MAX_ON_DEVICE_QUEUES) failed");
113 
114     size_t max_local_size = 1;
115     err_ret = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(max_local_size), &max_local_size, &ret_len);
116     test_error(err_ret, "clGetDeviceInfo(CL_DEVICE_MAX_WORK_GROUP_SIZE) failed");
117 
118     cl_queue_properties queue_prop_def[] =
119     {
120         CL_QUEUE_PROPERTIES, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE|CL_QUEUE_ON_DEVICE|CL_QUEUE_ON_DEVICE_DEFAULT,
121         CL_QUEUE_SIZE, maxQueueSize,
122         0
123     };
124 
125     dev_queue = clCreateCommandQueueWithProperties(context, device, queue_prop_def, &err_ret);
126     test_error(err_ret, "clCreateCommandQueueWithProperties(CL_QUEUE_DEVICE|CL_QUEUE_DEFAULT) failed");
127 
128     if(max_queues > 1)
129     {
130         n = (max_queues > MAX_QUEUES) ? MAX_QUEUES : max_queues-1;
131         clMemWrapper mem, buff, evt;
132         std::vector<clCommandQueueWrapper> queues(n);
133         std::vector<cl_command_queue> q(n);
134         cl_queue_properties queue_prop[] =
135         {
136             CL_QUEUE_PROPERTIES, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE|CL_QUEUE_ON_DEVICE,
137             CL_QUEUE_SIZE, maxQueueSize,
138             0
139         };
140 
141         for(i = 0; i < n; ++i)
142         {
143             queues[i] = clCreateCommandQueueWithProperties(context, device, queue_prop, &err_ret);
144             test_error(err_ret, "clCreateCommandQueueWithProperties(CL_QUEUE_DEVICE) failed");
145             q[i] = queues[i];
146         }
147 
148         size_t global_size = MAX_GWS;
149         size_t local_size = (max_local_size > global_size/16) ? global_size/16 : max_local_size;
150         if(gWimpyMode)
151         {
152             global_size = 4;
153             local_size = 2;
154         }
155 
156         evt = clCreateBuffer(context, CL_MEM_READ_WRITE, n * sizeof(cl_event), NULL, &err_ret);
157         test_error(err_ret, "clCreateBuffer() failed");
158 
159         mem = clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, n * sizeof(cl_command_queue), &q[0], &err_ret);
160         test_error(err_ret, "clCreateBuffer() failed");
161 
162         buff = clCreateBuffer(context, CL_MEM_READ_WRITE, global_size * n * sizeof(cl_int), NULL, &err_ret);
163         test_error(err_ret, "clCreateBuffer() failed");
164 
165         // Prepare CL source
166         char cl[65536] = { 0 };
167         char q_args[16384] = { 0 };
168         char q_list[8192] = { 0 };
169 
170         kernel_arg arg_res = { sizeof(cl_mem), &buff };
171 
172         std::vector<kernel_arg> args(n+1);
173         args[0] = arg_res;
174 
175         for(i = 0; i < n; ++i)
176         {
177             snprintf(q_args+strlen(q_args), sizeof(q_args)-strlen(q_args)-1, ", queue_t q%d", i);
178             snprintf(q_list+strlen(q_list), sizeof(q_list)-strlen(q_list)-1, "q%d, ", i);
179             kernel_arg arg_q = { sizeof(cl_command_queue), &q[i] };
180             args[i+1] = arg_q;
181         }
182 
183         snprintf(cl, sizeof(cl)-1, enqueue_block_multi_queue, q_args, n, n, q_list);
184         const char *source = cl;
185 
186         err_ret = run_n_kernel_args(context, queue, &source, 1, "enqueue_block_multi_queue", local_size, global_size, kernel_results, sizeof(kernel_results), args.size(), &args[0]);
187         if(check_error(err_ret, "'%s' kernel execution failed", "enqueue_block_multi_queue")) res = -1;
188         else if((k = check_kernel_results(kernel_results, arr_size(kernel_results))) >= 0 && check_error(-1, "'%s' kernel results validation failed: [%d] returned %d expected 0", "enqueue_block_multi_queue", k, kernel_results[k])) res = -1;
189         else log_info("'%s' kernel is OK.\n", "enqueue_block_multi_queue");
190     }
191     return res;
192 }
193 
194 
195 
196 #endif
197 
198 
199