• 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 "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 
24 
25 #include "procs.h"
26 
27 const char *loop_kernel_code =
28 "__kernel void test_loop(__global int *src, __global int *loopindx, __global int *loopcnt, __global int *dst)\n"
29 "{\n"
30 "    int  tid = get_global_id(0);\n"
31 "    int  n = get_global_size(0);\n"
32 "    int  i, j;\n"
33 "\n"
34 "    dst[tid] = 0;\n"
35 "    for (i=0,j=loopindx[tid]; i<loopcnt[tid]; i++,j++)\n"
36 "    {\n"
37 "        if (j >= n)\n"
38 "            j = 0;\n"
39 "        dst[tid] += src[j];\n"
40 "    }\n"
41 "\n"
42 "}\n";
43 
44 
45 int
verify_loop(int * inptr,int * loopindx,int * loopcnt,int * outptr,int n)46 verify_loop(int *inptr, int *loopindx, int *loopcnt, int *outptr, int n)
47 {
48     int     r, i, j, k;
49 
50     for (i=0; i<n; i++)
51     {
52         r = 0;
53         for (j=0,k=loopindx[i]; j<loopcnt[i]; j++,k++)
54         {
55             if (k >= n)
56                 k = 0;
57             r += inptr[k];
58         }
59 
60         if (r != outptr[i])
61         {
62             log_error("LOOP test failed: %d found, expected %d\n", outptr[i], r);
63             return -1;
64         }
65     }
66 
67     log_info("LOOP test passed\n");
68     return 0;
69 }
70 
test_loop(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)71 int test_loop(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements)
72 {
73     cl_mem streams[4];
74     cl_int *input_ptr, *loop_indx, *loop_cnt, *output_ptr;
75     cl_program program;
76     cl_kernel kernel;
77     size_t threads[1];
78     int err, i;
79 
80     size_t length = sizeof(cl_int) * num_elements;
81     input_ptr  = (cl_int*)malloc(length);
82     loop_indx  = (cl_int*)malloc(length);
83     loop_cnt   = (cl_int*)malloc(length);
84     output_ptr = (cl_int*)malloc(length);
85 
86     streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
87     if (!streams[0])
88     {
89         log_error("clCreateBuffer failed\n");
90         return -1;
91     }
92     streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
93     if (!streams[1])
94     {
95         log_error("clCreateBuffer failed\n");
96         return -1;
97     }
98     streams[2] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
99     if (!streams[2])
100     {
101         log_error("clCreateBuffer failed\n");
102         return -1;
103     }
104     streams[3] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, NULL);
105     if (!streams[3])
106     {
107         log_error("clCreateBuffer failed\n");
108         return -1;
109     }
110 
111     MTdata d = init_genrand( gRandomSeed );
112     for (i=0; i<num_elements; i++)
113     {
114         input_ptr[i] = (int)genrand_int32(d);
115         loop_indx[i] = (int)get_random_float(0, num_elements-1, d);
116         loop_cnt[i] = (int)get_random_float(0, num_elements/32, d);
117     }
118     free_mtdata(d); d = NULL;
119 
120   err = clEnqueueWriteBuffer(queue, streams[0], CL_TRUE, 0, length, input_ptr, 0, NULL, NULL);
121   if (err != CL_SUCCESS)
122   {
123     log_error("clEnqueueWriteBuffer failed\n");
124     return -1;
125   }
126   err = clEnqueueWriteBuffer(queue, streams[1], CL_TRUE, 0, length, loop_indx, 0, NULL, NULL);
127   if (err != CL_SUCCESS)
128   {
129     log_error("clEnqueueWriteBuffer failed\n");
130     return -1;
131   }
132   err = clEnqueueWriteBuffer(queue, streams[2], CL_TRUE, 0, length, loop_cnt, 0, NULL, NULL);
133   if (err != CL_SUCCESS)
134   {
135     log_error("clEnqueueWriteBuffer failed\n");
136     return -1;
137   }
138 
139   err = create_single_kernel_helper(context, &program, &kernel, 1, &loop_kernel_code, "test_loop" );
140   if (err)
141     return -1;
142 
143   err  = clSetKernelArg(kernel, 0, sizeof streams[0], &streams[0]);
144   err |= clSetKernelArg(kernel, 1, sizeof streams[1], &streams[1]);
145   err |= clSetKernelArg(kernel, 2, sizeof streams[2], &streams[2]);
146   err |= clSetKernelArg(kernel, 3, sizeof streams[3], &streams[3]);
147     if (err != CL_SUCCESS)
148     {
149         log_error("clSetKernelArgs failed\n");
150         return -1;
151     }
152 
153     threads[0] = (unsigned int)num_elements;
154   err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, threads, NULL, 0, NULL, NULL);
155   if (err != CL_SUCCESS)
156   {
157     log_error("clEnqueueNDRangeKernel failed\n");
158     return -1;
159   }
160 
161   err = clEnqueueReadBuffer(queue, streams[3], CL_TRUE, 0, length, output_ptr, 0, NULL, NULL);
162   if (err != CL_SUCCESS)
163   {
164     log_error("clReadArray failed\n");
165     return -1;
166   }
167 
168   err = verify_loop(input_ptr, loop_indx, loop_cnt, output_ptr, num_elements);
169 
170     // cleanup
171     clReleaseMemObject(streams[0]);
172     clReleaseMemObject(streams[1]);
173     clReleaseMemObject(streams[2]);
174     clReleaseMemObject(streams[3]);
175     clReleaseKernel(kernel);
176     clReleaseProgram(program);
177     free(input_ptr);
178     free(loop_indx);
179     free(loop_cnt);
180     free(output_ptr);
181 
182     return err;
183 }
184 
185 
186