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 "harness/compat.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include "harness/rounding_mode.h"
24
25 #include "procs.h"
26
27 static const char *local_linear_id_1d_code =
28 "__kernel void test_local_linear_id_1d(global int *dst)\n"
29 "{\n"
30 " int tid = get_global_id(0);\n"
31 "\n"
32 " int linear_id = get_local_id(0);\n"
33 " int result = (linear_id == (int)get_local_linear_id()) ? 0x1 : 0x0;\n"
34 " dst[tid] = result;\n"
35 "}\n";
36
37 static const char *local_linear_id_2d_code =
38 "__kernel void test_local_linear_id_2d(global int *dst)\n"
39 "{\n"
40 " int tid_x = get_global_id(0);\n"
41 " int tid_y = get_global_id(1);\n"
42 "\n"
43 " int linear_id = get_local_id(1) * get_local_size(0) + get_local_id(0);\n"
44 " int result = (linear_id == (int)get_local_linear_id()) ? 0x1 : 0x0;\n"
45 " dst[tid_y * get_global_size(0) + tid_x] = result;\n"
46 "}\n";
47
48
49 static int
verify_local_linear_id(int * result,int n)50 verify_local_linear_id(int *result, int n)
51 {
52 int i;
53 for (i=0; i<n; i++)
54 {
55 if (result[i] == 0)
56 {
57 log_error("get_local_linear_id failed\n");
58 return -1;
59 }
60 }
61 log_info("get_local_linear_id passed\n");
62 return 0;
63 }
64
65
66 int
test_local_linear_id(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)67 test_local_linear_id(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
68 {
69 cl_mem streams;
70 cl_program program[2];
71 cl_kernel kernel[2];
72
73 int *output_ptr;
74 size_t threads[2];
75 int err;
76 num_elements = (int)sqrt((float)num_elements);
77 int length = num_elements * num_elements;
78
79 output_ptr = (cl_int *)malloc(sizeof(int) * length);
80
81 streams = clCreateBuffer(context, CL_MEM_READ_WRITE, length * sizeof(int),
82 NULL, &err);
83 test_error(err, "clCreateBuffer failed.");
84
85 err = create_single_kernel_helper(context, &program[0], &kernel[0], 1,
86 &local_linear_id_1d_code,
87 "test_local_linear_id_1d");
88 test_error( err, "create_single_kernel_helper failed");
89 err = create_single_kernel_helper(context, &program[1], &kernel[1], 1,
90 &local_linear_id_2d_code,
91 "test_local_linear_id_2d");
92 test_error( err, "create_single_kernel_helper failed");
93
94 err = clSetKernelArg(kernel[0], 0, sizeof streams, &streams);
95 test_error( err, "clSetKernelArgs failed.");
96 err = clSetKernelArg(kernel[1], 0, sizeof streams, &streams);
97 test_error( err, "clSetKernelArgs failed.");
98
99 threads[0] = (size_t)num_elements;
100 threads[1] = (size_t)num_elements;
101 err = clEnqueueNDRangeKernel(queue, kernel[1], 2, NULL, threads, NULL, 0, NULL, NULL);
102 test_error( err, "clEnqueueNDRangeKernel failed.");
103
104 err = clEnqueueReadBuffer(queue, streams, CL_TRUE, 0, length*sizeof(int), output_ptr, 0, NULL, NULL);
105 test_error( err, "clEnqueueReadBuffer failed.");
106
107 err = verify_local_linear_id(output_ptr, length);
108
109 threads[0] = (size_t)num_elements;
110 err = clEnqueueNDRangeKernel(queue, kernel[0], 1, NULL, threads, NULL, 0, NULL, NULL);
111 test_error( err, "clEnqueueNDRangeKernel failed.");
112
113 err = clEnqueueReadBuffer(queue, streams, CL_TRUE, 0, num_elements*sizeof(int), output_ptr, 0, NULL, NULL);
114 test_error( err, "clEnqueueReadBuffer failed.");
115
116 err = verify_local_linear_id(output_ptr, num_elements);
117
118 // cleanup
119 clReleaseMemObject(streams);
120 clReleaseKernel(kernel[0]);
121 clReleaseKernel(kernel[1]);
122 clReleaseProgram(program[0]);
123 clReleaseProgram(program[1]);
124 free(output_ptr);
125
126 return err;
127 }
128