xref: /aosp_15_r20/external/OpenCL-CTS/test_conformance/profiling/execute_multipass.cpp (revision 6467f958c7de8070b317fc65bcb0f6472e388d82)
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] =
126         create_image_3d(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
127                         &image_format_desc, w, h, d, 0, 0, inptr, &err);
128     if( memobjs[0] == (cl_mem)0 ){
129         log_error( " unable to create 2D image using create_image_2d\n" );
130         return -1;
131     }
132 
133     // allocate an array memory object to load the filter weights
134     memobjs[1] =
135         clCreateBuffer(context, CL_MEM_READ_WRITE,
136                        sizeof(cl_float) * w * h * d * nChannels, NULL, &err);
137     if( memobjs[1] == (cl_mem)0 ){
138         log_error( " unable to create array using clCreateBuffer\n" );
139         clReleaseMemObject( memobjs[0] );
140         return -1;
141     }
142 
143     // create the compute program
144     err = create_single_kernel_helper( context, &program[0], &kernel[0], 1, &read3d_kernel_code, "read3d" );
145     if( err ){
146         clReleaseMemObject( memobjs[1] );
147         clReleaseMemObject( memobjs[0] );
148         return -1;
149     }
150 
151 
152     // create kernel args object and set arg values.
153     // set the args values
154     err |= clSetKernelArg( kernel[0], 0, sizeof( cl_mem ), (void *)&memobjs[0] );
155     err |= clSetKernelArg( kernel[0], 1, sizeof( cl_mem ), (void *)&memobjs[1] );
156     err |= clSetKernelArg(kernel[0], 2, sizeof sampler, &sampler);
157 
158     if( err != CL_SUCCESS ){
159         print_error( err, "clSetKernelArg failed\n" );
160         clReleaseKernel( kernel[0] );
161         clReleaseProgram( program[0] );
162         clReleaseMemObject( memobjs[1] );
163         clReleaseMemObject( memobjs[0] );
164         return -1;
165     }
166 
167     err = clEnqueueNDRangeKernel( queue, kernel[0], 3, NULL, threads, localThreads, 0, NULL, &executeEvent );
168 
169     if( err != CL_SUCCESS ){
170         print_error( err, "clEnqueueNDRangeKernel failed\n" );
171         clReleaseKernel( kernel[0] );
172         clReleaseProgram( program[0] );
173         clReleaseMemObject( memobjs[1] );
174         clReleaseMemObject( memobjs[0] );
175         return -1;
176     }
177 
178     if (executeEvent) {
179 
180         // This synchronization point is needed in order to assume the data is valid.
181         // Getting profiling information is not a synchronization point.
182         err = clWaitForEvents( 1, &executeEvent );
183         if( err != CL_SUCCESS )
184         {
185             print_error( err, "clWaitForEvents failed\n" );
186             clReleaseKernel( kernel[0] );
187             clReleaseProgram( program[0] );
188             clReleaseMemObject( memobjs[1] );
189             clReleaseMemObject( memobjs[0] );
190             return -1;
191         }
192 
193         // test profiling
194         while( ( err = clGetEventProfilingInfo( executeEvent, CL_PROFILING_COMMAND_QUEUED, sizeof( cl_ulong ), &queueStart, NULL ) ) == CL_PROFILING_INFO_NOT_AVAILABLE );
195         if( err != CL_SUCCESS ){
196             print_error( err, "clGetEventProfilingInfo failed" );
197             clReleaseKernel( kernel[0] );
198             clReleaseProgram( program[0] );
199             clReleaseMemObject( memobjs[1] );
200             clReleaseMemObject( memobjs[0] );
201             return -1;
202         }
203 
204         while( ( err = clGetEventProfilingInfo( executeEvent, CL_PROFILING_COMMAND_SUBMIT, sizeof( cl_ulong ), &submitStart, NULL ) ) == CL_PROFILING_INFO_NOT_AVAILABLE );
205         if( err != CL_SUCCESS ){
206             print_error( err, "clGetEventProfilingInfo failed" );
207             clReleaseKernel( kernel[0] );
208             clReleaseProgram( program[0] );
209             clReleaseMemObject( memobjs[1] );
210             clReleaseMemObject( memobjs[0] );
211             return -1;
212         }
213 
214         err = clGetEventProfilingInfo( executeEvent, CL_PROFILING_COMMAND_START, sizeof( cl_ulong ), &writeStart, NULL );
215         if( err != CL_SUCCESS ){
216             print_error( err, "clGetEventProfilingInfo failed" );
217             clReleaseKernel( kernel[0] );
218             clReleaseProgram( program[0] );
219             clReleaseMemObject( memobjs[1] );
220             clReleaseMemObject( memobjs[0] );
221             return -1;
222         }
223 
224         err = clGetEventProfilingInfo( executeEvent, CL_PROFILING_COMMAND_END, sizeof( cl_ulong ), &writeEnd, NULL );
225         if( err != CL_SUCCESS ){
226             print_error( err, "clGetEventProfilingInfo failed" );
227             clReleaseKernel( kernel[0] );
228             clReleaseProgram( program[0] );
229             clReleaseMemObject( memobjs[1] );
230             clReleaseMemObject( memobjs[0] );
231             return -1;
232         }
233 
234         log_info( "Profiling info:\n" );
235         log_info( "Time from queue to start of clEnqueueNDRangeKernel: %f seconds\n", (double)(writeStart - queueStart) / 1000000000000.f );
236         log_info( "Time from start of clEnqueueNDRangeKernel to end: %f seconds\n", (double)(writeEnd - writeStart) / 1000000000000.f );
237     }
238 
239     // read output image
240     err = clEnqueueReadBuffer(queue, memobjs[1], CL_TRUE, 0, w*h*d*nChannels*4, outptr, 0, NULL, NULL);
241     if( err != CL_SUCCESS ){
242         print_error( err, "clReadImage failed\n" );
243         clReleaseKernel( kernel[0] );
244         clReleaseProgram( program[0] );
245         clReleaseMemObject( memobjs[1] );
246         clReleaseMemObject( memobjs[0] );
247         return -1;
248     }
249 
250     // release kernel, program, and memory objects
251     clReleaseKernel( kernel[0] );
252     clReleaseProgram( program[0] );
253     clReleaseMemObject( memobjs[1] );
254     clReleaseMemObject( memobjs[0] );
255 
256     return err;
257 
258 }    // end run_kernel()
259 
260 
261 // The main point of this test is to exercise code that causes a multipass cld launch for a single
262 // kernel exec at the cl level. This is done on the gpu for 3d launches, and it's also done
263 // to handle gdims that excede the maximums allowed by the hardware. In this case we
264 // use 3d to exercise the multipass events. In the future 3d may not be multpass, in which
265 // case we will need to ensure that we use gdims large enough to force multipass.
266 
execute_multipass(cl_device_id device,cl_context context,cl_command_queue queue,int num_elements)267 int execute_multipass( cl_device_id device, cl_context context, cl_command_queue queue, int num_elements )
268 {
269     cl_uchar *inptr;
270     cl_uchar *outptr;
271     int w = 256, h = 128, d = 32;
272     int nChannels = 4;
273     int nElements = w * h * d * nChannels;
274     int err = 0;
275     MTdata mtData;
276 
277     PASSIVE_REQUIRE_IMAGE_SUPPORT( device )
278 
279     mtData = init_genrand( gRandomSeed );
280     inptr = createImage( nElements, mtData );
281     free_mtdata( mtData); mtData = NULL;
282     if( ! inptr ){
283         log_error( " unable to allocate %d bytes of memory for image\n", nElements );
284         return -1;
285     }
286 
287     outptr = (cl_uchar *)malloc( nElements * sizeof( cl_uchar ) );
288     if( ! outptr ){
289         log_error( " unable to allocate %d bytes of memory for output image #1\n", nElements );
290         free( (void *)inptr );
291         return -1;
292     }
293 
294 
295     err = run_kernel( device, context, queue, w, h, d, nChannels, inptr, outptr );
296 
297     if( ! err ){
298         // verify that the images are the same
299         err = verifyImages( outptr, inptr, (cl_uchar)0x1, w, h, d, nChannels );
300         if( err )
301             log_error( " images do not match\n" );
302     }
303 
304     // clean up
305     free( (void *)outptr );
306     free( (void *)inptr );
307 
308     return err;
309 
310 }    // end execute()
311 
312 
313 
314