• 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 "../testBase.h"
17 
18 
19 struct image_kernel_data
20 {
21     cl_int width;
22     cl_int channelType;
23     cl_int channelOrder;
24     cl_int expectedChannelType;
25     cl_int expectedChannelOrder;
26 };
27 
28 static const char *methodTest1DImageKernelPattern =
29     "typedef struct {\n"
30     "    int width;\n"
31     "    int channelType;\n"
32     "    int channelOrder;\n"
33     "    int expectedChannelType;\n"
34     "    int expectedChannelOrder;\n"
35     " } image_kernel_data;\n"
36     "__kernel void sample_kernel( %s image1d_t input, __global "
37     "image_kernel_data *outData )\n"
38     "{\n"
39     "   outData->width = get_image_width( input );\n"
40     "   outData->channelType = get_image_channel_data_type( input );\n"
41     "   outData->channelOrder = get_image_channel_order( input );\n"
42     "\n"
43     "   outData->expectedChannelType = %s;\n"
44     "   outData->expectedChannelOrder = %s;\n"
45     "}";
46 
test_get_1Dimage_info_single(cl_context context,cl_command_queue queue,image_descriptor * imageInfo,MTdata d,cl_mem_flags flags)47 static int test_get_1Dimage_info_single(cl_context context,
48                                         cl_command_queue queue,
49                                         image_descriptor *imageInfo, MTdata d,
50                                         cl_mem_flags flags)
51 {
52     int error = 0;
53 
54     clProgramWrapper program;
55     clKernelWrapper kernel;
56     clMemWrapper image, outDataBuffer;
57     char programSrc[ 10240 ];
58 
59     image_kernel_data    outKernelData;
60 
61     // Generate some data to test against
62     BufferOwningPtr<char> imageValues;
63     generate_random_image_data( imageInfo, imageValues, d );
64 
65     // Construct testing source
66     if( gDebugTrace )
67         log_info( " - Creating 1D image %d ...\n", (int)imageInfo->width );
68 
69     image = create_image_1d(context, flags, imageInfo->format, imageInfo->width,
70                             0, NULL, NULL, &error);
71     if( image == NULL )
72     {
73         log_error( "ERROR: Unable to create 1D image of size %d (%s)", (int)imageInfo->width, IGetErrorString( error ) );
74         return -1;
75     }
76 
77     char channelTypeConstantString[256] = {0};
78     char channelOrderConstantString[256] = {0};
79 
80     const char* channelTypeName = GetChannelTypeName( imageInfo->format->image_channel_data_type );
81     const char* channelOrderName = GetChannelOrderName( imageInfo->format->image_channel_order );
82     const char *image_access_qualifier =
83         (flags == CL_MEM_READ_ONLY) ? "read_only" : "write_only";
84 
85     if(channelTypeName && strlen(channelTypeName))
86         sprintf(channelTypeConstantString, "CLK_%s", &channelTypeName[3]);  // replace CL_* with CLK_*
87 
88     if(channelOrderName && strlen(channelOrderName))
89         sprintf(channelOrderConstantString, "CLK_%s", &channelOrderName[3]); // replace CL_* with CLK_*
90 
91     // Create a program to run against
92     sprintf(programSrc, methodTest1DImageKernelPattern, image_access_qualifier,
93             channelTypeConstantString, channelOrderConstantString);
94 
95     //log_info("-----------------------------------\n%s\n", programSrc);
96     error = clFinish(queue);
97     if (error)
98         print_error(error, "clFinish failed.\n");
99     const char *ptr = programSrc;
100     error = create_single_kernel_helper(context, &program, &kernel, 1, &ptr,
101                                         "sample_kernel");
102     test_error( error, "Unable to create kernel to test against" );
103 
104     // Create an output buffer
105     outDataBuffer = clCreateBuffer(context, CL_MEM_READ_WRITE,
106                                    sizeof(outKernelData), NULL, &error);
107     test_error( error, "Unable to create output buffer" );
108 
109     // Set up arguments and run
110     error = clSetKernelArg( kernel, 0, sizeof( image ), &image );
111     test_error( error, "Unable to set kernel argument" );
112     error = clSetKernelArg( kernel, 1, sizeof( outDataBuffer ), &outDataBuffer );
113     test_error( error, "Unable to set kernel argument" );
114 
115     size_t threads[1] = { 1 }, localThreads[1] = { 1 };
116 
117     error = clEnqueueNDRangeKernel( queue, kernel, 1, NULL, threads, localThreads, 0, NULL, NULL );
118     test_error( error, "Unable to run kernel" );
119 
120     error = clEnqueueReadBuffer( queue, outDataBuffer, CL_TRUE, 0, sizeof( outKernelData ), &outKernelData, 0, NULL, NULL );
121     test_error( error, "Unable to read data buffer" );
122 
123 
124     // Verify the results now
125     if( outKernelData.width != (cl_int)imageInfo->width )
126     {
127         log_error( "ERROR: Returned width did not validate (expected %d, got %d)\n", (int)imageInfo->width, (int)outKernelData.width );
128         error = -1;
129     }
130     if( outKernelData.channelType != (cl_int)outKernelData.expectedChannelType )
131     {
132         log_error( "ERROR: Returned channel type did not validate (expected %s (%d), got %d)\n", GetChannelTypeName( imageInfo->format->image_channel_data_type ),
133                                                                                               (int)outKernelData.expectedChannelType, (int)outKernelData.channelType );
134         error = -1;
135     }
136     if( outKernelData.channelOrder != (cl_int)outKernelData.expectedChannelOrder )
137     {
138         log_error( "ERROR: Returned channel order did not validate (expected %s (%d), got %d)\n", GetChannelOrderName( imageInfo->format->image_channel_order ),
139                                                                                               (int)outKernelData.expectedChannelOrder, (int)outKernelData.channelOrder );
140         error = -1;
141     }
142 
143      if( clFinish(queue) != CL_SUCCESS )
144      {
145          log_error( "ERROR: CL Finished failed in %s \n", __FUNCTION__);
146          error = -1;
147      }
148 
149     return error;
150 }
151 
test_get_image_info_1D(cl_device_id device,cl_context context,cl_command_queue queue,cl_image_format * format,cl_mem_flags flags)152 int test_get_image_info_1D(cl_device_id device, cl_context context,
153                            cl_command_queue queue, cl_image_format *format,
154                            cl_mem_flags flags)
155 {
156     size_t maxWidth;
157     cl_ulong maxAllocSize, memSize;
158     image_descriptor imageInfo = { 0 };
159     RandomSeed seed( gRandomSeed );
160     size_t pixelSize;
161 
162     imageInfo.type = CL_MEM_OBJECT_IMAGE1D;
163     imageInfo.format = format;
164     imageInfo.height = imageInfo.depth = imageInfo.slicePitch = 0;
165     pixelSize = get_pixel_size( imageInfo.format );
166 
167     int error = clGetDeviceInfo( device, CL_DEVICE_IMAGE2D_MAX_WIDTH, sizeof( maxWidth ), &maxWidth, NULL );
168     error |= clGetDeviceInfo( device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof( maxAllocSize ), &maxAllocSize, NULL );
169     error |= clGetDeviceInfo( device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof( memSize ), &memSize, NULL );
170     test_error( error, "Unable to get max image 1D size from device" );
171 
172   if (memSize > (cl_ulong)SIZE_MAX) {
173     memSize = (cl_ulong)SIZE_MAX;
174   }
175 
176     if( gTestSmallImages )
177     {
178         for( imageInfo.width = 1; imageInfo.width < 13; imageInfo.width++ )
179         {
180             imageInfo.rowPitch = imageInfo.width * pixelSize;
181             if( gDebugTrace )
182                 log_info( "   at size %d\n", (int)imageInfo.width );
183 
184             int ret = test_get_1Dimage_info_single(context, queue, &imageInfo,
185                                                    seed, flags);
186             if( ret )
187                 return -1;
188         }
189     }
190     else if( gTestMaxImages )
191     {
192         // Try a specific set of maximum sizes
193         size_t numbeOfSizes;
194         size_t sizes[100][3];
195 
196         get_max_sizes(&numbeOfSizes, 100, sizes, maxWidth, 1, 1, 1, maxAllocSize, memSize, CL_MEM_OBJECT_IMAGE1D, imageInfo.format);
197 
198         for( size_t idx = 0; idx < numbeOfSizes; idx++ )
199         {
200             imageInfo.width = sizes[ idx ][ 0 ];
201             imageInfo.rowPitch = imageInfo.width * pixelSize;
202 
203             log_info( "Testing %d\n", (int)sizes[ idx ][ 0 ]);
204             if( gDebugTrace )
205                 log_info( "   at max size %d\n", (int)sizes[ idx ][ 0 ] );
206             if (test_get_1Dimage_info_single(context, queue, &imageInfo, seed,
207                                              flags))
208                 return -1;
209         }
210     }
211     else
212     {
213         for( int i = 0; i < NUM_IMAGE_ITERATIONS; i++ )
214         {
215             cl_ulong size;
216             // Loop until we get a size that a) will fit in the max alloc size and b) that an allocation of that
217             // image, the result array, plus offset arrays, will fit in the global ram space
218             do
219             {
220                 imageInfo.width = (size_t)random_log_in_range( 16, (int)maxWidth / 32, seed );
221 
222                 imageInfo.rowPitch = imageInfo.width * pixelSize;
223                 size_t extraWidth = (int)random_log_in_range( 0, 64, seed );
224                 imageInfo.rowPitch += extraWidth;
225 
226                 do {
227                     extraWidth++;
228                     imageInfo.rowPitch += extraWidth;
229                 } while ((imageInfo.rowPitch % pixelSize) != 0);
230 
231                 size = (cl_ulong)imageInfo.rowPitch * (cl_ulong)imageInfo.height * 4;
232             } while(  size > maxAllocSize || ( size * 3 ) > memSize );
233 
234             if( gDebugTrace )
235                 log_info( "   at size %d (row pitch %d) out of %d\n", (int)imageInfo.width, (int)imageInfo.rowPitch, (int)maxWidth );
236             int ret = test_get_1Dimage_info_single(context, queue, &imageInfo,
237                                                    seed, flags);
238             if( ret )
239                 return -1;
240         }
241     }
242 
243     return 0;
244 }
245