Skip to content

Commit 42e41ea

Browse files
authored
Add support for OCL_ICD_FILENAMES. (#42)
* Add support for OCL_ICD_FILENAMES. * Added tests.
1 parent e5b32d5 commit 42e41ea

3 files changed

Lines changed: 77 additions & 14 deletions

File tree

doc/libOpenCL.7.txt.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ b. else, if *$OCL_ICD_VENDORS* ends with '`.icd`', libOpenCL.so will only load
6767
c. else libOpenCL.so will try to load *$OCL_ICD_VENDORS* as the ICD shared
6868
library itself (i.e. to load it directly with **dlopen**(3)).
6969

70+
*OCL_ICD_FILENAMES*::
71+
This variable allows one to specify a colon separated list of ICD to load,
72+
specifying their path.
73+
7074
*OPENCL_LAYERS*::
7175
This variable allows one to specify a colon separated list of layers to load,
7276
specifying their path.

ocl_icd_loader.c

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,16 @@ static int compare_path(const void *a, const void *b)
212212
}
213213

214214
static inline unsigned int _load_icd(int num_icds, const char* lib_path) {
215-
unsigned int ret=0;
216215
debug(D_LOG, "Loading ICD '%s'", lib_path);
217216

218217
_icds[num_icds].dl_handle = dlopen(lib_path, RTLD_LAZY|RTLD_LOCAL);//|RTLD_DEEPBIND);
219218
if(_icds[num_icds].dl_handle != NULL) {
220219
debug(D_LOG, "ICD[%i] loaded", num_icds);
221-
ret=1;
220+
num_icds += 1;
222221
} else {
223222
debug(D_WARN, "error while dlopening the IDL: '%s',\n => skipping ICD", dlerror());
224223
}
225-
return ret;
224+
return num_icds;
226225
}
227226

228227
static inline unsigned int _open_driver(unsigned int num_icds,
@@ -268,14 +267,13 @@ static inline unsigned int _open_driver(unsigned int num_icds,
268267
if( lib_path[lib_path_length-1] == '\n' )
269268
lib_path[lib_path_length-1] = '\0';
270269

271-
num_icds += _load_icd(num_icds, lib_path);
270+
num_icds = _load_icd(num_icds, lib_path);
272271

273272
free(lib_path);
274273
RETURN(num_icds);
275274
}
276275

277-
static inline unsigned int _open_drivers(DIR *dir, const char* dir_path) {
278-
unsigned int num_icds = 0;
276+
static inline unsigned int _open_drivers(unsigned int num_icds, DIR *dir, const char* dir_path) {
279277
struct dirent *ent;
280278
while( (ent=readdir(dir)) != NULL ){
281279
if(! _string_end_with_icd(ent->d_name)) {
@@ -852,6 +850,22 @@ static void __initClIcd( void ) {
852850
DIR *dir = NULL;
853851
const char* dir_path=getenv("OCL_ICD_VENDORS");
854852
const char* vendor_path=getenv("OPENCL_VENDOR_PATH");
853+
char* icd_filenames=getenv("OCL_ICD_FILENAMES");
854+
855+
if (icd_filenames) {
856+
debug(D_DUMP, "OCL_ICD_FILENAMES set to '%s', using it", icd_filenames);
857+
if (*icd_filenames) {
858+
num_icds++;
859+
char* next_icd_filenames = strchr(icd_filenames, ':');
860+
while (next_icd_filenames) {
861+
next_icd_filenames++;
862+
num_icds++;
863+
next_icd_filenames = strchr(next_icd_filenames, ':');
864+
}
865+
}
866+
debug(D_LOG, "found %ld potential ICDs in OCL_ICD_FILENAMES", (long)num_icds);
867+
}
868+
855869
if (! vendor_path || vendor_path[0]==0) {
856870
vendor_path=ETC_OPENCL_VENDORS;
857871
debug(D_DUMP, "OPENCL_VENDOR_PATH unset or empty. Using hard-coded path '%s'", vendor_path);
@@ -876,7 +890,7 @@ static void __initClIcd( void ) {
876890

877891
if (!is_dir) {
878892
debug(D_LOG,"Only loading '%s' as an ICD", dir_path);
879-
num_icds = 1;
893+
num_icds += 1;
880894
dir=NULL;
881895
} else {
882896
debug(D_LOG,"Reading icd list from '%s'", dir_path);
@@ -889,7 +903,7 @@ static void __initClIcd( void ) {
889903
goto abort;
890904
}
891905

892-
num_icds = _find_num_icds(dir);
906+
num_icds += _find_num_icds(dir);
893907
if(num_icds == 0) {
894908
goto abort;
895909
}
@@ -900,20 +914,36 @@ static void __initClIcd( void ) {
900914
goto abort;
901915
}
902916

917+
num_icds = 0;
918+
919+
if (icd_filenames) {
920+
char* icd_filename = icd_filenames;
921+
char* next_icd_filename = strchr(icd_filenames, ':');
922+
while (icd_filename) {
923+
if (next_icd_filename)
924+
*next_icd_filename++ = '\0';
925+
num_icds = _load_icd(num_icds, icd_filename);
926+
icd_filename = next_icd_filename;
927+
if (next_icd_filename)
928+
next_icd_filename = strchr(next_icd_filename, ':');
929+
}
930+
}
931+
903932
if (!is_dir) {
904933
if (_string_end_with_icd(dir_path)) {
905-
num_icds = 0;
934+
cl_uint n_icds = 0;
906935
if (! _string_with_slash(dir_path)) {
907-
num_icds = _open_driver(0, vendor_path, dir_path);
936+
n_icds = _open_driver(num_icds, vendor_path, dir_path);
908937
}
909-
if (num_icds == 0) {
910-
num_icds = _open_driver(0, NULL, dir_path);
938+
if (n_icds == num_icds) {
939+
n_icds = _open_driver(num_icds, NULL, dir_path);
911940
}
941+
num_icds = n_icds;
912942
} else {
913-
num_icds = _load_icd(0, dir_path);
943+
num_icds = _load_icd(num_icds, dir_path);
914944
}
915945
} else {
916-
num_icds = _open_drivers(dir, dir_path);
946+
num_icds = _open_drivers(num_icds, dir, dir_path);
917947
}
918948
if(num_icds == 0) {
919949
goto abort;

tests/testsuite-standard.at

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,32 @@ AT_CHECK_UNQUOTED([cllayerinfo], 0,
109109
CL_LAYER_NAME: dummylayer
110110
], [])
111111
AT_CLEANUP
112+
113+
AT_SETUP([OCL_ICD_FILENAMES as library file])
114+
AT_EXPORT([OCL_ICD_DEBUG],[7],
115+
[OCL_ICD_VENDORS],[unexisting-vendors-dir],
116+
[OCL_ICD_FILENAMES],[$abs_top_builddir/.libs/libdummycl.so])
117+
AT_CHECK([ocl_test], 0, [Found 1 platforms!
118+
ocl-icd ICD test
119+
], [stderr])
120+
AT_CLEANUP
121+
122+
AT_SETUP([OCL_ICD_FILENAMES as library file list])
123+
AT_EXPORT([OCL_ICD_DEBUG],[7],
124+
[OCL_ICD_VENDORS],[unexisting-vendors-dir],
125+
[OCL_ICD_FILENAMES],[$abs_top_builddir/.libs/libdummycl.so:$abs_top_builddir/.libs/libdummycl2.so])
126+
AT_CHECK([ocl_test | env LC_ALL=C sort], 0, [Found 2 platforms!
127+
ocl-icd ICD test
128+
ocl-icd ICD test2
129+
], [stderr])
130+
AT_CLEANUP
131+
132+
AT_SETUP([OCL_ICD_FILENAMES and OCL_ICD_VENDORS as library files])
133+
AT_EXPORT([OCL_ICD_DEBUG],[7],
134+
[OCL_ICD_VENDORS],[$abs_top_builddir/.libs/libdummycl.so],
135+
[OCL_ICD_FILENAMES],[$abs_top_builddir/.libs/libdummycl2.so])
136+
AT_CHECK([ocl_test | env LC_ALL=C sort], 0, [Found 2 platforms!
137+
ocl-icd ICD test
138+
ocl-icd ICD test2
139+
], [stderr])
140+
AT_CLEANUP

0 commit comments

Comments
 (0)