• 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 <string.h>
20 #include <time.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 
24 #include "procs.h"
25 #include "harness/testHarness.h"
26 #include "harness/errorHelpers.h"
27 
28 static const char *read3d_kernel_code =
29 "\n"
30 "__kernel void read3d(read_only image3d_t srcimg, __global unsigned char *dst, sampler_t sampler)\n"
31 "{\n"
32 "    int    tid_x = get_global_id(0);\n"
33 "    int    tid_y = get_global_id(1);\n"
34 "    int    tid_z = get_global_id(2);\n"
35 "    int    indx = (tid_z * get_image_height(srcimg) + tid_y) * get_image_width(srcimg) + tid_x;\n"
36 "    float4 color;\n"
37 "\n"
38 "    color = read_imagef(srcimg, sampler, (int4)(tid_x, tid_y, tid_z, 0));\n"
39 "    indx *= 4;\n"
40 "    dst[indx+0] = (unsigned char)(color.x * 255.0f);\n"
41 "    dst[indx+1] = (unsigned char)(color.y * 255.0f);\n"
42 "    dst[indx+2] = (unsigned char)(color.z * 255.0f);\n"
43 "    dst[indx+3] = (unsigned char)(color.w * 255.0f);\n"
44 "\n"
45 "}\n";
46 
47 
createImage(int elements,MTdata d)48 static cl_uchar *createImage( int elements, MTdata d )
49 {
50     int i;
51     cl_uchar *ptr = (cl_uchar *)malloc( elements * sizeof( cl_uchar ) );
52     if( ! ptr )
53         return NULL;
54 
55     for( i = 0; i < elements; i++ ){
56         ptr[i] = (cl_uchar)genrand_int32(d);
57     }
58 
59     return ptr;
60 
61 }    // end createImage()
62 
63 
verifyImages(cl_uchar * ptr0,cl_uchar * ptr1,cl_uchar tolerance,int xsize,int ysize,int zsize,int nChannels)64 static int verifyImages( cl_uchar *ptr0, cl_uchar *ptr1, cl_uchar tolerance, int xsize, int ysize, int zsize, int nChannels )
65 {
66     int x, y, z, c;
67     cl_uchar *p0 = ptr0;
68     cl_uchar *p1 = ptr1;
69 
70     for( z = 0; z < zsize; z++ ){
71         for( y = 0; y < ysize; y++ ){
72             for( x = 0; x < xsize; x++ ){
73                 for( c = 0; c < nChannels; c++ ){
74                     if( (cl_uchar)abs( (int)( *p0++ - *p1++ ) ) > tolerance ){
75                         log_error( "  images differ at x,y,z = %d,%d,%d channel = %d, %d to %d\n",
76                                   x, y, z, c, (int)p0[-1], (int)p1[-1] );
77                         return -1;
78                     }
79                 }
80             }
81         }
82     }
83 
84     return 0;
85 
86 }    // end verifyImages()
87 
88 
run_kernel(cl_device_id device,cl_context context,cl_command_queue queue,int w,int h,int d,int nChannels,cl_uchar * inptr,cl_uchar * outptr)89 static int run_kernel( cl_device_id device, cl_context context, cl_command_queue queue,
90                       int w, int h, int d, int nChannels, cl_uchar *inptr, cl_uchar *outptr )
91 {
92     cl_program program[1];
93     cl_kernel kernel[1];
94     cl_mem memobjs[2];
95     cl_image_format image_format_desc = { CL_RGBA, CL_UNORM_INT8 };
96     cl_event executeEvent = NULL;
97     cl_ulong queueStart, submitStart, writeStart, writeEnd;
98     size_t threads[3];
99     size_t localThreads[3];
100     int err = 0;
101 
102     // set thread dimensions
103     threads[0] = w;
104     threads[1] = h;
105     threads[2] = d;
106 
107     err = clGetDeviceInfo( device, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof( cl_uint ), (size_t*)localThreads, NULL );
108     if (err)
109     {
110         localThreads[0] = 256; localThreads[1] = 1; localThreads[2] = 1;
111         err = 0;
112     }
113     if( localThreads[0] > threads[0] )
114         localThreads[0] = threads[0];
115     if( localThreads[1] > threads[1] )
116         localThreads[1] = threads[1];
117 
118     cl_sampler sampler = clCreateSampler( context, CL_FALSE, CL_ADDRESS_CLAMP_TO_EDGE, CL_FILTER_NEAREST, &err );
119     if( err ){
120         log_error( " clCreateSampler failed.\n" );
121         return -1;
122     }
123 
124     // allocate the input and output image memory objects
125     memobjs[0] = create_image_3d( context, (cl_mem_flags)(CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR), &image_format_desc, w, h, d, 0, 0, inptr, &err );
126     if( memobjs[0] == (cl_mem)0 ){
127         log_error( " unable to create 2D image using create_image_2d\n" );
128         return -1;
129     }
130 
131     // allocate an array memory object to load the filter weights
132     memobjs[1] = clCreateBuffer( context, (cl_mem_flags)( CL_MEM_READ_WRITE ), sizeof( cl_float ) * w*h*d*nChannels, NULL, &err );
133     if( memobjs[1] == (cl_mem)0 ){
134         log_error( " unable to create array using clCreateBuffer\n" );
135         clReleaseMemObject( memobjs[0] );
136         return -1;
137     }
138 
139     // create the compute program
140     err = create_single_kernel_helper( context, &program[0], &kernel[0], 1, &read3d_kernel_code, "read3d" );
141     if( err ){
142         clReleaseMemObject( memobjs[1] );
143         clReleaseMemObject( memobjs[0] );
144         return -1;
145     }
146 
147 
148     // create kernel args object and set arg values.
149     // set the args values
150     err |= clSetKernelArg( kernel[0], 0, sizeof( cl_mem ), (void *)&memobjs[0] );
151     err |= clSetKernelArg( kernel[0], 1, sizeof( cl_mem ), (void *)&memobjs[1] );
152     err |= clSetKernelArg(kernel[0], 2, sizeof sampler, &sampler);
153 
154     if( err != CL_SUCCESS ){
155         print_error( err, "clSetKernelArg failed\n" );
156         clReleaseKernel( kernel[0] );
157         clReleaseProgram( program[0] );
158         clReleaseMemObject( memobjs[1] );
159         clReleaseMemObject( memobjs[0] );
160         return -1;
161     }
162 
163     err = clEnqueueNDRangeKernel( queue, kernel[0], 3, NULL, threads, localThreads, 0, NULL, &executeEvent );
164 
165     if( err != CL_SUCCESS ){
166         print_error( err, "clEnqueueNDRangeKernel failed\n" );
167         clReleaseKernel( kernel[0] );
168         clReleaseProgram( program[0] );
169         clReleaseMemObject( memobjs[1] );
170         clReleaseMemObject( memobjs[0] );
171         return -1;
172     }
173 
174     if (executeEvent) {
175 
176         // This synchronization point is needed in order to assume the data is valid.
177         // Getting profiling information is not a synchronization point.
178         err = clWaitForEvents( 1, &executeEvent );
179         if( err != CL_SUCCESS )
180         {
181             print_error( err, "clWaitForEvents failed\n" );
182             clReleaseKernel( kernel[0] );
183             clReleaseProgram( program[0] );
184             clReleaseMemObject( memobjs[1] );
185             clReleaseMemObject( memobjs[0] );
186             return -1;
187         }
188 
189         // test profiling
190         while( ( err = clGetEventProfilingInfo( executeEvent, CL_PROFILING_COMMAND_QUEUED, sizeof( cl_ulong ), &queueStart, NULL ) ) == CL_PROFILING_INFO_NOT_AVAILABLE );
191         if( err != CL_SUCCESS ){
192             print_error( err, "clGetEventProfilingInfo failed" );
193             clReleaseKernel( kernel[0] );
194             clReleaseProgram( program[0] );
195             clReleaseMemObject( memobjs[1] );
196             clReleaseMemObject( memobjs[0] );
197             return -1;
198         }
199 
200         while( ( err = clGetEventProfilingInfo( executeEvent, CL_PROFILING_COMMAND_SUBMIT, sizeof( cl_ulong ), &submitStart, NULL ) ) == CL_PROFILING_INFO_NOT_AVAILABLE );
201         if( err != CL_SUCCESS ){
202             print_error( err, "clGetEventProfilingInfo failed" );
203             clReleaseKernel( kernel[0] );
204             clReleaseProgram( program[0] );
205             clReleaseMemObject( memobjs[1] );
206             clReleaseMemObject( memobjs[0] );
207             return -1;
208         }
209 
210         err = clGetEventProfilingInfo( executeEvent, CL_PROFILING_COMMAND_START, sizeof( cl_ulong ), &writeStart, NULL );
211         if( err != CL_SUCCESS ){
212             print_error( err, "clGetEventProfilingInfo failed" );
213             clReleaseKernel( kernel[0] );
214             clReleaseProgram( program[0] );
215             clReleaseMemObject( memobjs[1] );
216             clReleaseMemObject( memobjs[0] );
217             return -1;
218         }
219 
220         err = clGetEventProfilingInfo( executeEvent, CL_PROFILING_COMMAND_END, sizeof( cl_ulong ), &writeEnd, NULL );
221         if( err != CL_SUCCESS ){
222             print_error( err, "clGetEventProfilingInfo failed" );
223             clReleaseKernel( kernel[0] );
224             clReleaseProgram( program[0] );
225             clReleaseMemObject( memobjs[1] );
226             clReleaseMemObject( memobjs[0] );
227             return -1;
228         }
229 
230         log_info( "Profiling info:\n" );
231         log_info( "Time from queue to start of clEnqueueNDRangeKernel: %f seconds\n", (double)(writeStart - queueStart) / 1000000000000.f );
232         log_info( "Time from start of clEnqueueNDRangeKernel to end: %f seconds\n", (double)(writeEnd - writeStart) / 1000000000000.f );
233     }
234 
235     // read output image
236     err = clEnqueueReadBuffer(queue, memobjs[1], CL_TRUE, 0, w*h*d*nChannels*4, outptr, 0, NULL, NULL);
237     if( err != CL_SUCCESS ){
238         print_error( err, "clReadImage failed\n" );
239         clReleaseKernel( kernel[0] );
240         clReleaseProgram( program[0] );
241         clReleaseMemObject( memobjs[1] );
242         clReleaseMemObject( memobjs[0] );
243         return -1;
244     }
245 
246     // release kernel, program, and memory objects
247     clReleaseKernel( kernel[0] );
248     clReleaseProgram( program[0] );
249     clReleaseMemObject( memobjs[1] );
250     clReleaseMemObject( memobjs[0] );
251 
252     return err;
253 
254 }    // end run_kernel()
255 
256 
257 // The main point of this test is to exercise code that causes a multipass cld launch for a single
258 // kernel exec at the cl level. This is done on the gpu for 3d launches, and it's also done
259 // to handle gdims that excede the maximums allowed by the hardware. In this case we
260 // use 3d to exercise the multipass events. In the future 3d may not be multpass, in which
261 // case we will need to ensure that we use gdims large enough to force multipass.
262 
execute_multipass(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)263 int execute_multipass( cl_device_id device, cl_context context, cl_command_queue queue, int num_elements )
264 {
265     cl_uchar *inptr;
266     cl_uchar *outptr;
267     int w = 256, h = 128, d = 32;
268     int nChannels = 4;
269     int nElements = w * h * d * nChannels;
270     int err = 0;
271     MTdata mtData;
272 
273     PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
274 
275     mtData = init_genrand( gRandomSeed );
276     inptr = createImage( nElements, mtData );
277     free_mtdata( mtData); mtData = NULL;
278     if( ! inptr ){
279         log_error( " unable to allocate %d bytes of memory for image\n", nElements );
280         return -1;
281     }
282 
283     outptr = (cl_uchar *)malloc( nElements * sizeof( cl_uchar ) );
284     if( ! outptr ){
285         log_error( " unable to allocate %d bytes of memory for output image #1\n", nElements );
286         free( (void *)inptr );
287         return -1;
288     }
289 
290 
291     err = run_kernel( device, context, queue, w, h, d, nChannels, inptr, outptr );
292 
293     if( ! err ){
294         // verify that the images are the same
295         err = verifyImages( outptr, inptr, (cl_uchar)0x1, w, h, d, nChannels );
296         if( err )
297             log_error( " images do not match\n" );
298     }
299 
300     // clean up
301     free( (void *)outptr );
302     free( (void *)inptr );
303 
304     return err;
305 
306 }    // end execute()
307 
308 
309 
310