2 Copyright (c) 2012, Brice Videau <brice.videau@imag.fr>
3 Copyright (c) 2012, Vincent Danjean <Vincent.Danjean@ens-lyon.org>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice, this
10 list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS
33 #include <CL/opencl.h>
35 #pragma GCC visibility push(hidden)
37 #include "ocl_icd_loader.h"
38 #include "ocl_icd_loader_debug.h"
40 int debug_ocl_icd_mask=0;
42 typedef __typeof__(clGetExtensionFunctionAddress) *clGetExtensionFunctionAddress_fn;
43 typedef __typeof__(clGetPlatformInfo) *clGetPlatformInfo_fn;
47 cl_uint num_platforms;
48 cl_uint first_platform;
50 clGetExtensionFunctionAddress_fn ext_fn_ptr;
54 char * extension_suffix;
55 struct vendor_icd *vicd;
59 struct vendor_icd *_icds=NULL;
60 struct platform_icd *_picds=NULL;
61 static cl_uint _num_icds = 0;
62 static cl_uint _num_picds = 0;
64 static cl_uint _initialized = 0;
66 static const char *_dir_path="/etc/OpenCL/vendors/";
68 static inline cl_uint _find_num_icds(DIR *dir) {
71 while( (ent=readdir(dir)) != NULL ){
72 if( strcmp(ent->d_name,".") == 0 || strcmp(ent->d_name,"..") == 0 )
74 cl_uint d_name_len = strlen(ent->d_name);
75 if( d_name_len>4 && strcmp(ent->d_name + d_name_len - 4, ".icd" ) != 0 )
77 // printf("%s%s\n", _dir_path, ent->d_name);
84 static inline cl_uint _open_drivers(DIR *dir) {
87 while( (ent=readdir(dir)) != NULL ){
88 if( strcmp(ent->d_name,".") == 0 || strcmp(ent->d_name,"..") == 0 )
90 cl_uint d_name_len = strlen(ent->d_name);
91 if( d_name_len>4 && strcmp(ent->d_name + d_name_len - 4, ".icd" ) != 0 )
95 unsigned int lib_path_length = strlen(_dir_path) + strlen(ent->d_name) + 1;
96 lib_path = malloc(lib_path_length*sizeof(char));
97 sprintf(lib_path,"%s%s", _dir_path, ent->d_name);
98 FILE *f = fopen(lib_path,"r");
101 fseek(f, 0, SEEK_END);
102 lib_path_length = ftell(f)+1;
103 fseek(f, 0, SEEK_SET);
104 if(lib_path_length == 1) {
108 lib_path = malloc(lib_path_length*sizeof(char));
109 err = fgets(lib_path, lib_path_length, f);
116 lib_path_length = strlen(lib_path);
118 if( lib_path[lib_path_length-1] == '\n' )
119 lib_path[lib_path_length-1] = '\0';
121 _icds[num_icds].dl_handle = dlopen(lib_path, RTLD_LAZY|RTLD_LOCAL);//|RTLD_DEEPBIND);
122 if(_icds[num_icds].dl_handle != NULL) {
123 debug(D_LOG, "Loading ICD[%i] -> '%s'", num_icds, lib_path);
131 static void* _get_function_addr(void* dlh, clGetExtensionFunctionAddress_fn fn, const char*name) {
133 addr1=dlsym(dlh, name);
135 debug(D_WARN, "Missing global symbol '%s' in ICD, should be skipped", name);
141 debug(D_WARN, "Missing function '%s' in ICD, should be skipped", name);
144 if (addr1 && addr2 && addr1!=addr2) {
145 debug(D_WARN, "Function and symbol '%s' have different addresses!", name);
149 if (!addr2) addr2=addr1;
153 static int _allocate_platforms(int req) {
154 static cl_uint allocated=0;
155 debug(D_LOG,"Requesting allocation for %d platforms",req);
156 if (allocated - _num_picds < req) {
158 _picds=(struct platform_icd*)malloc(req*sizeof(struct platform_icd));
160 req = req - (allocated - _num_picds);
161 _picds=(struct platform_icd*)realloc(_picds, (allocated+req)*sizeof(struct platform_icd));
165 RETURN(allocated - _num_picds);
168 static inline void _find_and_check_platforms(cl_uint num_icds) {
171 for( i=0; i<num_icds; i++){
172 debug(D_LOG, "Checking ICD %i", i);
173 struct vendor_icd *picd = &_icds[_num_icds];
174 void* dlh = _icds[i].dl_handle;
175 picd->ext_fn_ptr = _get_function_addr(dlh, NULL, "clGetExtensionFunctionAddress");
176 clIcdGetPlatformIDsKHR_fn plt_fn_ptr =
177 _get_function_addr(dlh, picd->ext_fn_ptr, "clIcdGetPlatformIDsKHR");
178 clGetPlatformInfo_fn plt_info_ptr =
179 _get_function_addr(dlh, picd->ext_fn_ptr, "clGetPlatformInfo");
180 if( picd->ext_fn_ptr == NULL
181 || plt_fn_ptr == NULL
182 || plt_info_ptr == NULL) {
183 debug(D_WARN, "Missing symbols in ICD, skipping it");
186 cl_uint num_platforms=0;
188 error = (*plt_fn_ptr)(0, NULL, &num_platforms);
189 if( error != CL_SUCCESS || num_platforms == 0) {
190 debug(D_LOG, "No platform in ICD, skipping it");
193 cl_platform_id *platforms = (cl_platform_id *) malloc( sizeof(cl_platform_id) * num_platforms);
194 error = (*plt_fn_ptr)(num_platforms, platforms, NULL);
195 if( error != CL_SUCCESS ){
197 debug(D_WARN, "Error in loading ICD platforms, skipping ICD");
200 cl_uint num_valid_platforms=0;
202 debug(D_LOG, "Try to load %d plateforms", num_platforms);
203 if (_allocate_platforms(num_platforms) < num_platforms) {
205 debug(D_WARN, "Not enought platform allocated. Skipping ICD");
208 for(j=0; j<num_platforms; j++) {
209 debug(D_LOG, "Checking platform %i", j);
210 size_t param_value_size_ret;
211 struct platform_icd *p=&_picds[_num_picds];
212 p->extension_suffix=NULL;
216 if (debug_ocl_icd_mask & D_DUMP) {
217 dump_platform(p->pid);
220 error = plt_info_ptr(p->pid, CL_PLATFORM_EXTENSIONS, 0, NULL, ¶m_value_size_ret);
221 if (error != CL_SUCCESS) {
222 debug(D_WARN, "Error while loading extensions in platform %i, skipping it",j);
225 char *param_value = (char *)malloc(sizeof(char)*param_value_size_ret);
226 error = plt_info_ptr(p->pid, CL_PLATFORM_EXTENSIONS, param_value_size_ret, param_value, NULL);
227 if (error != CL_SUCCESS){
229 debug(D_WARN, "Error while loading extensions in platform %i, skipping it", j);
232 debug(D_DUMP, "Supported extensions: %s", param_value);
233 if( strstr(param_value, "cl_khr_icd") == NULL){
235 debug(D_WARN, "Missing khr extension in platform %i, skipping it", j);
239 error = plt_info_ptr(p->pid, CL_PLATFORM_ICD_SUFFIX_KHR, 0, NULL, ¶m_value_size_ret);
240 if (error != CL_SUCCESS) {
241 debug(D_WARN, "Error while loading suffix in platform %i, skipping it", j);
244 param_value = (char *)malloc(sizeof(char)*param_value_size_ret);
245 error = plt_info_ptr(p->pid, CL_PLATFORM_ICD_SUFFIX_KHR, param_value_size_ret, param_value, NULL);
246 if (error != CL_SUCCESS){
247 debug(D_WARN, "Error while loading suffix in platform %i, skipping it", j);
251 p->extension_suffix = param_value;
252 debug(D_LOG, "Extension suffix: %s", param_value);
253 num_valid_platforms++;
256 if( num_valid_platforms != 0 ) {
257 if ( _num_icds != i ) {
258 picd->dl_handle = dlh;
261 picd->num_platforms = num_valid_platforms;
262 _icds[i].first_platform = _num_picds - num_valid_platforms;
270 static void _initClIcd( void ) {
274 char *debug=getenv("OCL_ICD_DEBUG");
276 debug_ocl_icd_mask=atoi(debug);
277 if (debug_ocl_icd_mask==0)
278 debug_ocl_icd_mask=1;
281 cl_uint num_icds = 0;
283 dir = opendir(_dir_path);
288 num_icds = _find_num_icds(dir);
293 _icds = (struct vendor_icd*)malloc(num_icds * sizeof(struct vendor_icd));
298 num_icds = _open_drivers(dir);
303 _find_and_check_platforms(num_icds);
308 if (_num_icds < num_icds) {
309 _icds = (struct vendor_icd*)realloc(_icds, _num_icds * sizeof(struct vendor_icd));
311 debug(D_WARN, "%d valid vendor(s)!", _num_icds);
324 #pragma GCC visibility pop
326 CL_API_ENTRY void * CL_API_CALL clGetExtensionFunctionAddress(const char * func_name) CL_API_SUFFIX__VERSION_1_0 {
329 if( func_name == NULL )
331 cl_uint suffix_length;
333 void * return_value=NULL;
334 struct func_desc const * fn=&function_description[0];
335 while (fn->name != NULL) {
336 if (strcmp(func_name, fn->name)==0)
340 for(i=0; i<_num_picds; i++) {
341 suffix_length = strlen(_picds[i].extension_suffix);
342 if( suffix_length > strlen(func_name) )
344 if(strcmp(_picds[i].extension_suffix, &func_name[strlen(func_name)-suffix_length]) == 0)
345 return (*_picds[i].vicd->ext_fn_ptr)(func_name);
349 typeof(clGetExtensionFunctionAddress) clGetExtensionFunctionAddress_hid __attribute__ ((alias ("clGetExtensionFunctionAddress"), visibility("hidden")));
351 CL_API_ENTRY cl_int CL_API_CALL
352 clGetPlatformIDs(cl_uint num_entries,
353 cl_platform_id * platforms,
354 cl_uint * num_platforms) CL_API_SUFFIX__VERSION_1_0 {
357 if( platforms == NULL && num_platforms == NULL )
358 return CL_INVALID_VALUE;
359 if( num_entries == 0 && platforms != NULL )
360 return CL_INVALID_VALUE;
362 return CL_PLATFORM_NOT_FOUND_KHR;
365 if( num_platforms != NULL ){
366 *num_platforms = _num_picds;
368 if( platforms != NULL ) {
369 cl_uint n_platforms = _num_picds < num_entries ? _num_picds : num_entries;
370 for( i=0; i<n_platforms; i++) {
371 *(platforms++) = _picds[i].pid;
376 typeof(clGetPlatformIDs) clGetPlatformIDs_hid __attribute__ ((alias ("clGetPlatformIDs"), visibility("hidden")));