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 "utils.h"
22
run_single_kernel(cl_context context,cl_command_queue queue,const char ** source,unsigned int num_lines,const char * kernel_name,void * results,size_t res_size)23 int run_single_kernel(cl_context context, cl_command_queue queue, const char** source, unsigned int num_lines, const char* kernel_name, void* results, size_t res_size)
24 {
25 return run_single_kernel_args(context, queue, source, num_lines, kernel_name, results, res_size, 0, NULL);
26 }
27
run_single_kernel_args(cl_context context,cl_command_queue queue,const char ** source,unsigned int num_lines,const char * kernel_name,void * results,size_t res_size,cl_uint num_args,kernel_arg * args)28 int run_single_kernel_args(cl_context context, cl_command_queue queue, const char** source, unsigned int num_lines, const char* kernel_name, void* results, size_t res_size, cl_uint num_args, kernel_arg* args)
29 {
30 return run_n_kernel_args(context, queue, source, num_lines, kernel_name, 1, 1, results, res_size, num_args, args);
31 }
32
run_n_kernel_args(cl_context context,cl_command_queue queue,const char ** source,unsigned int num_lines,const char * kernel_name,size_t local,size_t global,void * results,size_t res_size,cl_uint num_args,kernel_arg * args)33 int run_n_kernel_args(cl_context context, cl_command_queue queue, const char** source, unsigned int num_lines, const char* kernel_name, size_t local, size_t global, void* results, size_t res_size, cl_uint num_args, kernel_arg* args)
34 {
35 cl_int err_ret, status;
36 clProgramWrapper program;
37 clKernelWrapper kernel;
38 clMemWrapper mem;
39 clEventWrapper event;
40 cl_uint i;
41 size_t ret_len;
42
43 err_ret = create_single_kernel_helper_with_build_options(context, &program, &kernel, num_lines, source, kernel_name, "-cl-std=CL2.0");
44 if(check_error(err_ret, "Create single kernel failed")) return -1;
45
46 mem = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, res_size, results, &err_ret);
47 test_error(err_ret, "clCreateBuffer() failed");
48
49 err_ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), &mem);
50 if(check_error(err_ret, "clSetKernelArg(%d, %d, %p) for kernel: '%s' failed: %d", 0, (int)sizeof(cl_mem), &mem, kernel_name, err_ret)) return err_ret;
51
52 for(i = 0; i < num_args; ++i)
53 {
54 err_ret = clSetKernelArg(kernel, i+1, args[i].size, args[i].ptr);
55 if(check_error(err_ret, "clSetKernelArg(%d, %d, %p) for kernel: '%s' failed: %d", (int)(i+1), (int)args[i].size, args[i].ptr, kernel_name, err_ret)) return err_ret;
56 }
57
58 err_ret = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, (local ? &local : NULL), 0, NULL, &event);
59 if(check_error(err_ret, "clEnqueueNDRangeKernel('%s', gws=%d, lws=%d) failed", kernel_name, (int)global, (int)local)) return err_ret;
60
61 err_ret = clEnqueueReadBuffer(queue, mem, CL_TRUE, 0, res_size, results, 0, NULL, NULL);
62 test_error(err_ret, "clEnqueueReadBuffer() failed");
63
64 err_ret = clGetEventInfo(event, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof(status), &status, &ret_len);
65 test_error(err_ret, "clGetEventInfo() failed");
66
67 #if CL_COMPLETE != CL_SUCCESS
68 #error Fix me!
69 #endif
70
71 // This hack is possible because CL_COMPLETE and CL_SUCCESS defined as 0x0
72 if(check_error(status, "Kernel execution status %d", status)) return status;
73
74 return 0;
75 }
76
77