Skip to content

Commit baeb83f

Browse files
committed
Supported an OPENCL_VISIBLE_DEVICES environment variable inspired from CUDA_VISIBLE_DEVICES.
OPENCL_VISIBLE_DEVICES=descriptor_list descriptor_list -> descriptor | descriptor::descriptor_list descriptor -> driver,platform | driver,platform,type | driver,platform,type,device_list device_list -> device_id | device_id,device_list driver: client driver (foo.icd or foo.so) platform: platform ID type: gpu, cpu, accelerator, custom, or any device_id: device ID in the platform This patch is to land KhronosGroup#45 again. Most of the code is from pull request 45. e.g., OPENCL_VISIBLE_DEVICES=foo.icd,0,gpu,0,1::bar.icd,1,cpu,0 only shows - GPU 0, 1 in the first platform of foo.icd - CPU 0 in the second platform of bar.icd
1 parent 4e65bd5 commit baeb83f

7 files changed

Lines changed: 581 additions & 14 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ set (OPENCL_ICD_LOADER_SOURCES
5454
loader/icd_dispatch.h
5555
loader/icd_dispatch_generated.c
5656
loader/icd_envvars.h
57+
loader/icd_envvars.c
5758
loader/icd_platform.h)
5859

5960
if (WIN32)

loader/icd.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2020 The Khronos Group Inc.
2+
* Copyright (c) 2016-2021 The Khronos Group Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ struct KHRLayer *khrFirstLayer = NULL;
3333
// entrypoint to initialize the ICD and add all vendors
3434
void khrIcdInitialize(void)
3535
{
36+
khrIcdOsGetOpenCLVisibleDevicesOnce();
3637
// enumerate vendors present on the system
3738
khrIcdOsVendorsEnumerateOnce();
3839
}
@@ -122,6 +123,12 @@ void khrIcdVendorAdd(const char *libraryName)
122123
{
123124
continue;
124125
}
126+
127+
if (!khrIcdCheckPlatformVisible(libraryName, i))
128+
{
129+
continue;
130+
}
131+
125132
result = platforms[i]->dispatch->clGetPlatformInfo(
126133
platforms[i],
127134
CL_PLATFORM_ICD_SUFFIX_KHR,
@@ -179,8 +186,9 @@ void khrIcdVendorAdd(const char *libraryName)
179186
*prevNextPointer = vendor;
180187
}
181188

182-
KHR_ICD_TRACE("successfully added vendor %s with suffix %s\n", libraryName, suffix);
189+
khrIcdVisibilitySetPlatform(libraryName, i, platforms[i]);
183190

191+
KHR_ICD_TRACE("successfully added vendor %s with suffix %s\n", libraryName, suffix);
184192
}
185193

186194
Done:

loader/icd_dispatch_generated.c

Lines changed: 125 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012-2020 The Khronos Group Inc.
2+
* Copyright (c) 2012-2021 The Khronos Group Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include "icd_dispatch.h"
20+
#include "icd_envvars.h"
2021
#include "icd.h"
2122

2223
#ifdef __cplusplus
@@ -96,12 +97,81 @@ CL_API_ENTRY cl_int CL_API_CALL clGetDeviceIDs(
9697
num_devices);
9798
#endif // defined(CL_ENABLE_LAYERS)
9899
KHR_ICD_VALIDATE_HANDLE_RETURN_ERROR(platform, CL_INVALID_PLATFORM);
99-
return platform->dispatch->clGetDeviceIDs(
100+
cl_uint num_all_devices = 0;
101+
cl_device_id *all_devices = NULL;
102+
cl_uint i;
103+
cl_int result;
104+
105+
if (!num_entries && devices)
106+
{
107+
return CL_INVALID_VALUE;
108+
}
109+
if (!devices && !num_devices)
110+
{
111+
return CL_INVALID_VALUE;
112+
}
113+
114+
if (num_devices)
115+
{
116+
*num_devices = 0;
117+
}
118+
for (i = 0; i < num_entries && devices; ++i)
119+
{
120+
devices[i] = NULL;
121+
}
122+
123+
124+
result = platform->dispatch->clGetDeviceIDs(
100125
platform,
101126
device_type,
102-
num_entries,
103-
devices,
104-
num_devices);
127+
0,
128+
NULL,
129+
&num_all_devices);
130+
131+
if (CL_SUCCESS != result)
132+
{
133+
return result;
134+
}
135+
if (!num_all_devices)
136+
{
137+
return CL_DEVICE_NOT_FOUND;
138+
}
139+
all_devices = (cl_device_id*)malloc(sizeof(cl_device_id) * num_all_devices);
140+
if (!all_devices)
141+
{
142+
return CL_OUT_OF_HOST_MEMORY;
143+
}
144+
result = platform->dispatch->clGetDeviceIDs(
145+
platform,
146+
device_type,
147+
num_all_devices,
148+
all_devices,
149+
NULL);
150+
if (CL_SUCCESS != result)
151+
{
152+
free(all_devices);
153+
return result;
154+
}
155+
156+
result = CL_DEVICE_NOT_FOUND;
157+
for (i = 0; i < num_all_devices; ++i)
158+
{
159+
if (khrIcdCheckDeviceVisible(platform, all_devices[i]))
160+
{
161+
result = CL_SUCCESS;
162+
if (num_entries && devices)
163+
{
164+
*(devices++) = all_devices[i];
165+
--num_entries;
166+
}
167+
if (num_devices)
168+
{
169+
++(*num_devices);
170+
}
171+
}
172+
}
173+
free(all_devices);
174+
return result;
105175
}
106176

107177
///////////////////////////////////////////////////////////////////////////////
@@ -235,6 +305,12 @@ CL_API_ENTRY cl_context CL_API_CALL clCreateContextFromType(
235305
void* user_data,
236306
cl_int* errcode_ret)
237307
{
308+
cl_platform_id platform = NULL;
309+
cl_uint num_devices = 0;
310+
cl_device_id *devices = NULL;
311+
cl_context context = NULL;
312+
cl_int result;
313+
238314
khrIcdInitialize();
239315
#if defined(CL_ENABLE_LAYERS)
240316
if (khrFirstLayer)
@@ -245,15 +321,56 @@ CL_API_ENTRY cl_context CL_API_CALL clCreateContextFromType(
245321
user_data,
246322
errcode_ret);
247323
#endif // defined(CL_ENABLE_LAYERS)
248-
cl_platform_id platform = NULL;
249324
khrIcdContextPropertiesGetPlatform(properties, &platform);
250325
KHR_ICD_VALIDATE_HANDLE_RETURN_HANDLE(platform, CL_INVALID_PLATFORM);
251-
return platform->dispatch->clCreateContextFromType(
326+
327+
result = clGetDeviceIDs(platform, device_type, 0, NULL, &num_devices);
328+
if (CL_SUCCESS != result)
329+
{
330+
if (*errcode_ret)
331+
{
332+
*errcode_ret = result;
333+
}
334+
return NULL;
335+
}
336+
if (!num_devices)
337+
{
338+
if (*errcode_ret)
339+
{
340+
*errcode_ret = CL_DEVICE_NOT_FOUND;
341+
}
342+
return NULL;
343+
}
344+
devices = (cl_device_id*)malloc(sizeof(cl_device_id) * num_devices);
345+
if (!devices)
346+
{
347+
if (*errcode_ret)
348+
{
349+
*errcode_ret = CL_OUT_OF_HOST_MEMORY;
350+
}
351+
return NULL;
352+
}
353+
result = clGetDeviceIDs(platform, device_type, num_devices, devices, NULL);
354+
if (CL_SUCCESS != result)
355+
{
356+
free(devices);
357+
if (*errcode_ret)
358+
{
359+
*errcode_ret = result;
360+
}
361+
return NULL;
362+
}
363+
364+
context = platform->dispatch->clCreateContext(
252365
properties,
253-
device_type,
366+
num_devices,
367+
devices,
254368
pfn_notify,
255369
user_data,
256370
errcode_ret);
371+
free(devices);
372+
return context;
373+
257374
}
258375

259376
///////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)