• 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 #include <string.h>
19 
20 #define EXTENSION_NAME_BUF_SIZE 4096
21 
22 #define PRINT_EXTENSION_INFO 0
23 
test_platform_extensions(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)24 int test_platform_extensions(cl_device_id deviceID, cl_context context,
25                  cl_command_queue queue, int num_elements)
26 {
27     const char * extensions[] = {
28     "cl_khr_byte_addressable_store",
29 //    "cl_APPLE_SetMemObjectDestructor",
30     "cl_khr_global_int32_base_atomics",
31     "cl_khr_global_int32_extended_atomics",
32     "cl_khr_local_int32_base_atomics",
33     "cl_khr_local_int32_extended_atomics",
34     "cl_khr_int64_base_atomics",
35     "cl_khr_int64_extended_atomics",
36 // need to put in entires for various atomics
37     "cl_khr_3d_image_writes",
38     "cl_khr_fp16",
39     "cl_khr_fp64",
40     NULL
41     };
42 
43     bool extensionsSupported[] = {
44     false, //"cl_khr_byte_addressable_store",
45     false, // need to put in entires for various atomics
46     false, // "cl_khr_global_int32_base_atomics",
47     false, // "cl_khr_global_int32_extended_atomics",
48     false, // "cl_khr_local_int32_base_atomics",
49     false, // "cl_khr_local_int32_extended_atomics",
50     false, // "cl_khr_int64_base_atomics",
51     false, // "cl_khr_int64_extended_atomics",
52     false, //"cl_khr_3d_image_writes",
53     false, //"cl_khr_fp16",
54     false, //"cl_khr_fp64",
55     false //NULL
56     };
57 
58     int extensionIndex;
59 
60     cl_platform_id platformID;
61     cl_int err;
62 
63     char platform_extensions[EXTENSION_NAME_BUF_SIZE];
64     char device_extensions[EXTENSION_NAME_BUF_SIZE];
65 
66     // Okay, so what we're going to do is just check the device indicated by
67     // deviceID against the platform that includes this device
68 
69 
70     // pass CL_DEVICE_PLATFORM to clGetDeviceInfo
71     // to get a result of type cl_platform_id
72 
73     err = clGetDeviceInfo(deviceID,
74               CL_DEVICE_PLATFORM,
75               sizeof(cl_platform_id),
76               (void *)(&platformID),
77               NULL);
78 
79     if(err != CL_SUCCESS)
80     {
81     vlog_error("test_platform_extensions : could not get platformID from device\n");
82     return -1;
83     }
84 
85 
86     // now we grab the set of extensions specified by the platform
87     err = clGetPlatformInfo(platformID,
88                 CL_PLATFORM_EXTENSIONS,
89                 sizeof(platform_extensions),
90                 (void *)(&platform_extensions[0]),
91                 NULL);
92     if(err != CL_SUCCESS)
93     {
94     vlog_error("test_platform_extensions : could not get extension string from platform\n");
95     return -1;
96     }
97 
98 #if PRINT_EXTENSION_INFO
99     log_info("Platform extensions include \"%s\"\n\n", platform_extensions);
100 #endif
101 
102     // here we parse the platform extensions, to look for the "important" ones
103     for(extensionIndex=0; extensions[extensionIndex] != NULL; ++extensionIndex)
104     {
105     if(strstr(platform_extensions, extensions[extensionIndex]) != NULL)
106     {
107         // we found it
108 #if PRINT_EXTENSION_INFO
109         log_info("Found \"%s\" in platform extensions\n",
110         extensions[extensionIndex]);
111 #endif
112         extensionsSupported[extensionIndex] = true;
113     }
114     }
115 
116     // and then we grab the set of extensions specified by the device
117     // (this can be turned into a "loop over all devices in this platform")
118     err = clGetDeviceInfo(deviceID,
119               CL_DEVICE_EXTENSIONS,
120               sizeof(device_extensions),
121               (void *)(&device_extensions[0]),
122               NULL);
123     if(err != CL_SUCCESS)
124     {
125     vlog_error("test_platform_extensions : could not get extension string from device\n");
126     return -1;
127     }
128 
129 
130 #if PRINT_EXTENSION_INFO
131     log_info("Device extensions include \"%s\"\n\n", device_extensions);
132 #endif
133 
134     for(extensionIndex=0; extensions[extensionIndex] != NULL; ++extensionIndex)
135     {
136     if(extensionsSupported[extensionIndex] == false)
137     {
138         continue; // skip this one
139     }
140 
141     if(strstr(device_extensions, extensions[extensionIndex]) == NULL)
142     {
143         // device does not support it
144         vlog_error("Platform supports extension \"%s\" but device does not\n",
145                extensions[extensionIndex]);
146         return -1;
147     }
148     }
149     return 0;
150 }
151 
test_get_platform_ids(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)152 int test_get_platform_ids(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements) {
153   cl_platform_id platforms[16];
154   cl_uint num_platforms;
155   char *string_returned;
156 
157   string_returned = (char*)malloc(8192);
158 
159   int total_errors = 0;
160   int err = CL_SUCCESS;
161 
162 
163   err = clGetPlatformIDs(16, platforms, &num_platforms);
164   test_error(err, "clGetPlatformIDs failed");
165 
166   if (num_platforms <= 16) {
167     // Try with NULL
168     err = clGetPlatformIDs(num_platforms, platforms, NULL);
169     test_error(err, "clGetPlatformIDs failed with NULL for return size");
170   }
171 
172   if (num_platforms < 1) {
173     log_error("Found 0 platforms.\n");
174     return -1;
175   }
176   log_info("Found %d platforms.\n", num_platforms);
177 
178 
179   for (int p=0; p<(int)num_platforms; p++) {
180     cl_device_id *devices;
181     cl_uint num_devices;
182     size_t size;
183 
184 
185     log_info("Platform %d (%p):\n", p, platforms[p]);
186 
187     memset(string_returned, 0, 8192);
188     err = clGetPlatformInfo(platforms[p], CL_PLATFORM_PROFILE, 8192, string_returned, &size);
189     test_error(err, "clGetPlatformInfo for CL_PLATFORM_PROFILE failed");
190     log_info("\tCL_PLATFORM_PROFILE: %s\n", string_returned);
191     if (strlen(string_returned)+1 != size) {
192       log_error("Returned string length %ld does not equal reported one %ld.\n", strlen(string_returned)+1, size);
193       total_errors++;
194     }
195 
196     memset(string_returned, 0, 8192);
197     err = clGetPlatformInfo(platforms[p], CL_PLATFORM_VERSION, 8192, string_returned, &size);
198     test_error(err, "clGetPlatformInfo for CL_PLATFORM_VERSION failed");
199     log_info("\tCL_PLATFORM_VERSION: %s\n", string_returned);
200     if (strlen(string_returned)+1 != size) {
201       log_error("Returned string length %ld does not equal reported one %ld.\n", strlen(string_returned)+1, size);
202       total_errors++;
203     }
204 
205     memset(string_returned, 0, 8192);
206     err = clGetPlatformInfo(platforms[p], CL_PLATFORM_NAME, 8192, string_returned, &size);
207     test_error(err, "clGetPlatformInfo for CL_PLATFORM_NAME failed");
208     log_info("\tCL_PLATFORM_NAME: %s\n", string_returned);
209     if (strlen(string_returned)+1 != size) {
210       log_error("Returned string length %ld does not equal reported one %ld.\n", strlen(string_returned)+1, size);
211       total_errors++;
212     }
213 
214     memset(string_returned, 0, 8192);
215     err = clGetPlatformInfo(platforms[p], CL_PLATFORM_VENDOR, 8192, string_returned, &size);
216     test_error(err, "clGetPlatformInfo for CL_PLATFORM_VENDOR failed");
217     log_info("\tCL_PLATFORM_VENDOR: %s\n", string_returned);
218     if (strlen(string_returned)+1 != size) {
219       log_error("Returned string length %ld does not equal reported one %ld.\n", strlen(string_returned)+1, size);
220       total_errors++;
221     }
222 
223     memset(string_returned, 0, 8192);
224     err = clGetPlatformInfo(platforms[p], CL_PLATFORM_EXTENSIONS, 8192, string_returned, &size);
225     test_error(err, "clGetPlatformInfo for CL_PLATFORM_EXTENSIONS failed");
226     log_info("\tCL_PLATFORM_EXTENSIONS: %s\n", string_returned);
227     if (strlen(string_returned)+1 != size) {
228       log_error("Returned string length %ld does not equal reported one %ld.\n", strlen(string_returned)+1, size);
229       total_errors++;
230     }
231 
232     err = clGetDeviceIDs(platforms[p], CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
233     test_error(err, "clGetDeviceIDs size failed.\n");
234     devices = (cl_device_id *)malloc(num_devices*sizeof(cl_device_id));
235     memset(devices, 0, sizeof(cl_device_id)*num_devices);
236     err = clGetDeviceIDs(platforms[p], CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
237     test_error(err, "clGetDeviceIDs failed.\n");
238 
239     log_info("\tPlatform has %d devices.\n", (int)num_devices);
240     for (int d=0; d<(int)num_devices; d++) {
241       size_t returned_size;
242       cl_platform_id returned_platform;
243       cl_context context;
244       cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platforms[p], 0 };
245 
246       err = clGetDeviceInfo(devices[d], CL_DEVICE_PLATFORM, sizeof(cl_platform_id), &returned_platform, &returned_size);
247       test_error(err, "clGetDeviceInfo failed for CL_DEVICE_PLATFORM\n");
248       if (returned_size != sizeof(cl_platform_id)) {
249         log_error("Reported return size (%ld) does not match expected size (%ld).\n", returned_size, sizeof(cl_platform_id));
250         total_errors++;
251       }
252 
253       memset(string_returned, 0, 8192);
254       err = clGetDeviceInfo(devices[d], CL_DEVICE_NAME, 8192, string_returned, NULL);
255       test_error(err, "clGetDeviceInfo failed for CL_DEVICE_NAME\n");
256 
257       log_info("\t\tPlatform for device %d (%s) is %p.\n", d, string_returned, returned_platform);
258 
259       log_info("\t\t\tTesting clCreateContext for the platform/device...\n");
260       // Try creating a context for the platform
261       context = clCreateContext(properties, 1, &devices[d], NULL, NULL, &err);
262       test_error(err, "\t\tclCreateContext failed for device with platform properties\n");
263 
264       memset(properties, 0, sizeof(cl_context_properties)*3);
265 
266       err = clGetContextInfo(context, CL_CONTEXT_PROPERTIES, sizeof(cl_context_properties)*3, properties, &returned_size);
267       test_error(err, "clGetContextInfo for CL_CONTEXT_PROPERTIES failed");
268       if (returned_size != sizeof(cl_context_properties)*3) {
269         log_error("Invalid size returned from clGetContextInfo for CL_CONTEXT_PROPERTIES. Got %ld, expected %ld.\n",
270                   returned_size, sizeof(cl_context_properties)*3);
271         total_errors++;
272       }
273 
274       if (properties[0] != (cl_context_properties)CL_CONTEXT_PLATFORM || properties[1] != (cl_context_properties)platforms[p]) {
275         log_error("Wrong properties returned. Expected: [%p %p], got [%p %p]\n",
276                   (void*)CL_CONTEXT_PLATFORM, platforms[p], (void*)properties[0], (void*)properties[1]);
277         total_errors++;
278       }
279 
280       err = clReleaseContext(context);
281       test_error(err, "clReleaseContext failed");
282     }
283     free(devices);
284   }
285 
286   free(string_returned);
287 
288   return total_errors;
289 }
290