Skip to content

Commit

Permalink
Deinitialization support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed Feb 15, 2024
1 parent 8dde4f0 commit 2c62cc3
Show file tree
Hide file tree
Showing 10 changed files with 580 additions and 227 deletions.
3 changes: 1 addition & 2 deletions loader/cllayerinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "icd.h"
#include <stdio.h>
#include <stdlib.h>
#include <CL/cl_layer.h>
#if defined(_WIN32)
#include <io.h>
#include <share.h>
Expand Down Expand Up @@ -90,7 +89,7 @@ static void restore_outputs(void)
void printLayerInfo(const struct KHRLayer *layer)
{
cl_layer_api_version api_version = 0;
pfn_clGetLayerInfo p_clGetLayerInfo = (pfn_clGetLayerInfo)(size_t)layer->p_clGetLayerInfo;
pfn_clGetLayerInfo p_clGetLayerInfo = layer->p_clGetLayerInfo;
cl_int result = CL_SUCCESS;
size_t sz;

Expand Down
79 changes: 68 additions & 11 deletions loader/icd.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
#include "icd.h"
#include "icd_dispatch.h"
#include "icd_envvars.h"
#if defined(CL_ENABLE_LAYERS)
#include <CL/cl_layer.h>
#endif // defined(CL_ENABLE_LAYERS)
#include <stdlib.h>
#include <string.h>

KHRicdVendor *khrIcdVendors = NULL;
static KHRicdVendor *lastVendor = NULL;
int khrEnableTrace = 0;

#if defined(CL_ENABLE_LAYERS)
Expand Down Expand Up @@ -181,6 +179,14 @@ void khrIcdVendorAdd(const char *libraryName)
#endif

// call clGetPlatformInfo on the returned platform to get the suffix

KHR_ICD2_DISPATCH(platforms[i])->clGetPlatformInfo(
platforms[i],
CL_PLATFORM_UNLOADABLE_KHR,
sizeof(vendor->unloadable),
&vendor->unloadable,
NULL);

result = KHR_ICD2_DISPATCH(platforms[i])->clGetPlatformInfo(
platforms[i],
CL_PLATFORM_ICD_SUFFIX_KHR,
Expand Down Expand Up @@ -224,11 +230,13 @@ void khrIcdVendorAdd(const char *libraryName)
vendor->suffix = suffix;

// add this vendor to the list of vendors at the tail
{
KHRicdVendor **prevNextPointer = NULL;
for (prevNextPointer = &khrIcdVendors; *prevNextPointer; prevNextPointer = &( (*prevNextPointer)->next) );
*prevNextPointer = vendor;
if (lastVendor) {
lastVendor->next = vendor;
vendor->prev = lastVendor;
} else {
khrIcdVendors = vendor;
}
lastVendor = vendor;

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

Expand All @@ -253,6 +261,7 @@ void khrIcdLayerAdd(const char *libraryName)
cl_int result = CL_SUCCESS;
pfn_clGetLayerInfo p_clGetLayerInfo = NULL;
pfn_clInitLayer p_clInitLayer = NULL;
pfn_clDeinitLayer p_clDeinitLayer = NULL;
struct KHRLayer *layerIterator = NULL;
struct KHRLayer *layer = NULL;
cl_layer_api_version api_version = 0;
Expand Down Expand Up @@ -302,14 +311,21 @@ void khrIcdLayerAdd(const char *libraryName)
goto Done;
}

p_clDeinitLayer = (pfn_clDeinitLayer)(size_t)khrIcdOsLibraryGetFunctionAddress(library, "clDeinitLayer");
if (!p_clDeinitLayer)
{
KHR_ICD_TRACE("failed to get function address clDeinitLayer\n");
goto Done;
}

result = p_clGetLayerInfo(CL_LAYER_API_VERSION, sizeof(api_version), &api_version, NULL);
if (CL_SUCCESS != result)
{
KHR_ICD_TRACE("failed to query layer version\n");
goto Done;
}

if (CL_LAYER_API_VERSION_100 != api_version)
if (CL_LAYER_API_VERSION_200 != api_version)
{
KHR_ICD_TRACE("unsupported api version\n");
goto Done;
Expand All @@ -332,17 +348,18 @@ void khrIcdLayerAdd(const char *libraryName)
goto Done;
}
memcpy(layer->libraryName, libraryName, sz_name);
layer->p_clGetLayerInfo = (void *)(size_t)p_clGetLayerInfo;
layer->p_clGetLayerInfo = p_clGetLayerInfo;
layer->p_clDeinitLayer = p_clDeinitLayer;
}
#endif

if (khrFirstLayer) {
targetDispatch = &(khrFirstLayer->dispatch);
} else {
targetDispatch = &khrMasterDispatch;
targetDispatch = &khrMainDispatch;
}

loaderDispatchNumEntries = sizeof(khrMasterDispatch)/sizeof(void*);
loaderDispatchNumEntries = sizeof(khrMainDispatch)/sizeof(void*);
result = p_clInitLayer(
loaderDispatchNumEntries,
targetDispatch,
Expand Down Expand Up @@ -466,3 +483,43 @@ void khrIcdContextPropertiesGetPlatform(const cl_context_properties *properties,
}
}

#if defined(CL_ENABLE_LAYERS)
static struct KHRLayer deinitLayer = {0};
#endif

void khrIcdDeinitialize(void) {

KHR_ICD_TRACE("ICD Loader deinitialization\n");

#if defined(CL_ENABLE_LAYERS)
// free layers first in reverse order of their creation (front to back)
// they may still need to use vendors while terminating
KHR_ICD_TRACE("Finalizing and unloading layers\n");
struct KHRLayer *head = khrFirstLayer;
deinitLayer.dispatch = khrDeinitDispatch;
khrFirstLayer = &deinitLayer;

while(head) {
struct KHRLayer *cur = head;
#ifdef CL_LAYER_INFO
free(cur->libraryName);
#endif
cur->p_clDeinitLayer();
khrIcdOsLibraryUnload(cur->library);
head = cur->next;
free(cur);
}
#endif // defined(CL_ENABLE_LAYERS)

// free vendor in reverse order of their creation (back to front)
KHR_ICD_TRACE("Finalizing and unloading vendors\n");
while (lastVendor) {
KHRicdVendor *cur = lastVendor;
free(cur->suffix);
if (cur->unloadable)
khrIcdOsLibraryUnload(cur->library);
lastVendor = cur->prev;
free(cur);
}
khrIcdVendors = NULL;
}
32 changes: 29 additions & 3 deletions loader/icd.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,24 @@
#include <CL/cl.h>
#include <CL/cl_ext.h>
#include <CL/cl_icd.h>
#if defined(CL_ENABLE_LAYERS)
#include <CL/cl_layer.h>
#endif // defined(CL_ENABLE_LAYERS)
#include <stdio.h>

#if defined(CL_ENABLE_LAYERS) && !defined(CL_LAYER_API_VERSION_200)
#define CL_LAYER_API_VERSION_200 200

typedef cl_int CL_API_CALL
clDeinitLayer_t(void);

typedef clDeinitLayer_t *pfn_clDeinitLayer;
#endif //defined(CL_ENABLE_LAYERS) && !defined(CL_LAYER_API_VERSION_200)

#if !defined(CL_PLATFORM_UNLOADABLE)
#define CL_PLATFORM_UNLOADABLE_KHR 0x0921
#endif

/*
* type definitions
*/
Expand Down Expand Up @@ -85,6 +101,9 @@ struct KHRicdVendorRec
// the extension suffix for this platform
char *suffix;

// can this vendor library be unloaded?
cl_bool unloadable;

// function pointer to the ICD platform IDs extracted from the library
pfn_clGetExtensionFunctionAddress clGetExtensionFunctionAddress;

Expand All @@ -98,6 +117,7 @@ struct KHRicdVendorRec

// next vendor in the list vendors
KHRicdVendor *next;
KHRicdVendor *prev;
};

// the global state
Expand All @@ -123,14 +143,17 @@ struct KHRLayer
#ifdef CL_LAYER_INFO
// The layer library name
char *libraryName;
// the pointer to the clGetLayerInfo funciton
void *p_clGetLayerInfo;
// the pointer to the clGetLayerInfo function
pfn_clGetLayerInfo p_clGetLayerInfo;
#endif
// the pointer to the clDeinitLayer function
pfn_clDeinitLayer p_clDeinitLayer;
};

// the global layer state
extern struct KHRLayer * khrFirstLayer;
extern struct _cl_icd_dispatch khrMasterDispatch;
extern const struct _cl_icd_dispatch khrMainDispatch;
extern const struct _cl_icd_dispatch khrDeinitDispatch;
#endif // defined(CL_ENABLE_LAYERS)

/*
Expand All @@ -147,6 +170,9 @@ void khrIcdInitialize(void);
// entrypoint to check and initialize trace.
void khrIcdInitializeTrace(void);

// entrypoint to release icd resources
void khrIcdDeinitialize(void);

// go through the list of vendors (in /etc/OpenCL.conf or through
// the registry) and call khrIcdVendorAdd for each vendor encountered
// n.b, this call is OS-specific
Expand Down
Loading

0 comments on commit 2c62cc3

Please sign in to comment.