Skip to content

Commit 65874e0

Browse files
committed
Deinitialization support.
1 parent 5058890 commit 65874e0

17 files changed

+787
-619
lines changed

include/cl_khr_icd2.h

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
#if !defined(CL_PLATFORM_UNLOADABLE)
2+
#define CL_PLATFORM_UNLOADABLE_KHR 0x0921
3+
#endif
4+
5+
#if defined(CL_ENABLE_LAYERS) && !defined(CL_LAYER_API_VERSION_200)
6+
#define CL_LAYER_API_VERSION_200 200
7+
8+
typedef cl_int CL_API_CALL
9+
clDeinitLayer_t(void);
10+
11+
typedef clDeinitLayer_t *pfn_clDeinitLayer;
12+
#endif //defined(CL_ENABLE_LAYERS) && !defined(CL_LAYER_API_VERSION_200)
13+
114
#if !defined(CL_ICD2_TAG_KHR)
215
#if INTPTR_MAX == INT32_MAX
316
#define CL_ICD2_TAG_KHR ((intptr_t)0x434C3331)

loader/cllayerinfo.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "icd.h"
2020
#include <stdio.h>
2121
#include <stdlib.h>
22-
#include <CL/cl_layer.h>
2322
#if defined(_WIN32)
2423
#include <io.h>
2524
#include <share.h>
@@ -90,7 +89,7 @@ static void restore_outputs(void)
9089
void printLayerInfo(const struct KHRLayer *layer)
9190
{
9291
cl_layer_api_version api_version = 0;
93-
pfn_clGetLayerInfo p_clGetLayerInfo = (pfn_clGetLayerInfo)(size_t)layer->p_clGetLayerInfo;
92+
pfn_clGetLayerInfo p_clGetLayerInfo = layer->p_clGetLayerInfo;
9493
cl_int result = CL_SUCCESS;
9594
size_t sz;
9695

loader/icd.c

+68-11
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919
#include "icd.h"
2020
#include "icd_dispatch.h"
2121
#include "icd_envvars.h"
22-
#if defined(CL_ENABLE_LAYERS)
23-
#include <CL/cl_layer.h>
24-
#endif // defined(CL_ENABLE_LAYERS)
2522
#include <stdlib.h>
2623
#include <string.h>
2724

2825
KHRicdVendor *khrIcdVendors = NULL;
26+
static KHRicdVendor *lastVendor = NULL;
2927
int khrEnableTrace = 0;
3028

3129
#if defined(CL_ENABLE_LAYERS)
@@ -186,6 +184,14 @@ void khrIcdVendorAdd(const char *libraryName)
186184
#endif
187185

188186
// call clGetPlatformInfo on the returned platform to get the suffix
187+
188+
KHR_ICD2_DISPATCH(platforms[i])->clGetPlatformInfo(
189+
platforms[i],
190+
CL_PLATFORM_UNLOADABLE_KHR,
191+
sizeof(vendor->unloadable),
192+
&vendor->unloadable,
193+
NULL);
194+
189195
result = KHR_ICD2_DISPATCH(platforms[i])->clGetPlatformInfo(
190196
platforms[i],
191197
CL_PLATFORM_ICD_SUFFIX_KHR,
@@ -229,11 +235,13 @@ void khrIcdVendorAdd(const char *libraryName)
229235
vendor->suffix = suffix;
230236

231237
// add this vendor to the list of vendors at the tail
232-
{
233-
KHRicdVendor **prevNextPointer = NULL;
234-
for (prevNextPointer = &khrIcdVendors; *prevNextPointer; prevNextPointer = &( (*prevNextPointer)->next) );
235-
*prevNextPointer = vendor;
238+
if (lastVendor) {
239+
lastVendor->next = vendor;
240+
vendor->prev = lastVendor;
241+
} else {
242+
khrIcdVendors = vendor;
236243
}
244+
lastVendor = vendor;
237245

238246
KHR_ICD_TRACE("successfully added vendor %s with suffix %s\n", libraryName, suffix);
239247

@@ -258,6 +266,7 @@ void khrIcdLayerAdd(const char *libraryName)
258266
cl_int result = CL_SUCCESS;
259267
pfn_clGetLayerInfo p_clGetLayerInfo = NULL;
260268
pfn_clInitLayer p_clInitLayer = NULL;
269+
pfn_clDeinitLayer p_clDeinitLayer = NULL;
261270
struct KHRLayer *layerIterator = NULL;
262271
struct KHRLayer *layer = NULL;
263272
cl_layer_api_version api_version = 0;
@@ -307,14 +316,21 @@ void khrIcdLayerAdd(const char *libraryName)
307316
goto Done;
308317
}
309318

319+
p_clDeinitLayer = (pfn_clDeinitLayer)(size_t)khrIcdOsLibraryGetFunctionAddress(library, "clDeinitLayer");
320+
if (!p_clDeinitLayer)
321+
{
322+
KHR_ICD_TRACE("failed to get function address clDeinitLayer\n");
323+
goto Done;
324+
}
325+
310326
result = p_clGetLayerInfo(CL_LAYER_API_VERSION, sizeof(api_version), &api_version, NULL);
311327
if (CL_SUCCESS != result)
312328
{
313329
KHR_ICD_TRACE("failed to query layer version\n");
314330
goto Done;
315331
}
316332

317-
if (CL_LAYER_API_VERSION_100 != api_version)
333+
if (CL_LAYER_API_VERSION_200 != api_version)
318334
{
319335
KHR_ICD_TRACE("unsupported api version\n");
320336
goto Done;
@@ -337,17 +353,18 @@ void khrIcdLayerAdd(const char *libraryName)
337353
goto Done;
338354
}
339355
memcpy(layer->libraryName, libraryName, sz_name);
340-
layer->p_clGetLayerInfo = (void *)(size_t)p_clGetLayerInfo;
356+
layer->p_clGetLayerInfo = p_clGetLayerInfo;
341357
}
342358
#endif
359+
layer->p_clDeinitLayer = p_clDeinitLayer;
343360

344361
if (khrFirstLayer) {
345362
targetDispatch = &(khrFirstLayer->dispatch);
346363
} else {
347-
targetDispatch = &khrMasterDispatch;
364+
targetDispatch = &khrMainDispatch;
348365
}
349366

350-
loaderDispatchNumEntries = sizeof(khrMasterDispatch)/sizeof(void*);
367+
loaderDispatchNumEntries = sizeof(khrMainDispatch)/sizeof(void*);
351368
result = p_clInitLayer(
352369
loaderDispatchNumEntries,
353370
targetDispatch,
@@ -471,3 +488,43 @@ void khrIcdContextPropertiesGetPlatform(const cl_context_properties *properties,
471488
}
472489
}
473490

491+
#if defined(CL_ENABLE_LAYERS)
492+
static struct KHRLayer deinitLayer = {0};
493+
#endif
494+
495+
void khrIcdDeinitialize(void) {
496+
497+
KHR_ICD_TRACE("ICD Loader deinitialization\n");
498+
499+
#if defined(CL_ENABLE_LAYERS)
500+
// free layers first in reverse order of their creation (front to back)
501+
// they may still need to use vendors while terminating
502+
KHR_ICD_TRACE("Finalizing and unloading layers\n");
503+
struct KHRLayer *head = khrFirstLayer;
504+
deinitLayer.dispatch = khrDeinitDispatch;
505+
khrFirstLayer = &deinitLayer;
506+
507+
while(head) {
508+
struct KHRLayer *cur = head;
509+
#ifdef CL_LAYER_INFO
510+
free(cur->libraryName);
511+
#endif
512+
cur->p_clDeinitLayer();
513+
khrIcdOsLibraryUnload(cur->library);
514+
head = cur->next;
515+
free(cur);
516+
}
517+
#endif // defined(CL_ENABLE_LAYERS)
518+
519+
// free vendor in reverse order of their creation (back to front)
520+
KHR_ICD_TRACE("Finalizing and unloading vendors\n");
521+
while (lastVendor) {
522+
KHRicdVendor *cur = lastVendor;
523+
free(cur->suffix);
524+
if (cur->unloadable)
525+
khrIcdOsLibraryUnload(cur->library);
526+
lastVendor = cur->prev;
527+
free(cur);
528+
}
529+
khrIcdVendors = NULL;
530+
}

loader/icd.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
#include <CL/cl.h>
5050
#include <CL/cl_ext.h>
5151
#include <CL/cl_icd.h>
52+
#if defined(CL_ENABLE_LAYERS)
53+
#include <CL/cl_layer.h>
54+
#endif // defined(CL_ENABLE_LAYERS)
5255
#include <stdio.h>
5356

5457
/*
@@ -85,6 +88,9 @@ struct KHRicdVendorRec
8588
// the extension suffix for this platform
8689
char *suffix;
8790

91+
// can this vendor library be unloaded?
92+
cl_bool unloadable;
93+
8894
// function pointer to the ICD platform IDs extracted from the library
8995
pfn_clGetExtensionFunctionAddress clGetExtensionFunctionAddress;
9096

@@ -98,6 +104,7 @@ struct KHRicdVendorRec
98104

99105
// next vendor in the list vendors
100106
KHRicdVendor *next;
107+
KHRicdVendor *prev;
101108
};
102109

103110
// the global state
@@ -123,14 +130,17 @@ struct KHRLayer
123130
#ifdef CL_LAYER_INFO
124131
// The layer library name
125132
char *libraryName;
126-
// the pointer to the clGetLayerInfo funciton
127-
void *p_clGetLayerInfo;
133+
// the pointer to the clGetLayerInfo function
134+
pfn_clGetLayerInfo p_clGetLayerInfo;
128135
#endif
136+
// the pointer to the clDeinitLayer function
137+
pfn_clDeinitLayer p_clDeinitLayer;
129138
};
130139

131140
// the global layer state
132141
extern struct KHRLayer * khrFirstLayer;
133-
extern struct _cl_icd_dispatch khrMasterDispatch;
142+
extern const struct _cl_icd_dispatch khrMainDispatch;
143+
extern const struct _cl_icd_dispatch khrDeinitDispatch;
134144
#endif // defined(CL_ENABLE_LAYERS)
135145

136146
/*
@@ -147,6 +157,9 @@ void khrIcdInitialize(void);
147157
// entrypoint to check and initialize trace.
148158
void khrIcdInitializeTrace(void);
149159

160+
// entrypoint to release icd resources
161+
void khrIcdDeinitialize(void);
162+
150163
// go through the list of vendors (in /etc/OpenCL.conf or through
151164
// the registry) and call khrIcdVendorAdd for each vendor encountered
152165
// n.b, this call is OS-specific

0 commit comments

Comments
 (0)