Skip to content

Commit 84f5760

Browse files
committed
add a sample that unloads the OpenCL ICD loader and then reloads it
This sample currently requires a special branch of the OpenCL runtime loader that includes the unload function.
1 parent d56380b commit 84f5760

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
2525
include_directories(${OpenCL_INCLUDE_DIR})
2626

2727
add_subdirectory(external/OpenCL-Headers)
28+
29+
option(SAMPLES_USE_RUNTIME_LOADER "Build with the OpenCL Runtime Loader" ON)
30+
if (SAMPLES_USE_RUNTIME_LOADER)
31+
add_subdirectory(external/opencl-runtime-loader)
32+
set(OpenCL_LIBRARIES OpenCLRuntimeLoader ${CMAKE_DL_LIBS})
33+
else()
2834
add_subdirectory(external/opencl-icd-loader)
2935
set_target_properties(OpenCL PROPERTIES FOLDER "OpenCL-ICD-Loader")
3036
set_target_properties(cllayerinfo PROPERTIES FOLDER "OpenCL-ICD-Loader")
3137
set(OpenCL_LIBRARIES OpenCL)
38+
endif()
3239

3340
if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/external/opencl-extension-loader)
3441
add_subdirectory(external/opencl-extension-loader)

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ Many samples that use extensions additionally require the OpenCL Extension Loade
3939

4040
git clone https://github.com/bashbaug/opencl-extension-loader external/opencl-extension-loader
4141

42+
The loading and unloading sample requires the OpenCL Runtime Loader:
43+
44+
git clone https://github.com/bashbaug/opencl-runtime-loader external/opencl-runtime-loader
45+
4246
After satisfying the external dependencies create build files using CMake. For example:
4347

4448
mkdir build && cd build
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Ben Ashbaugh
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
add_opencl_sample(
6+
TEST
7+
NUMBER 99
8+
TARGET loaderunload
9+
VERSION 120
10+
SOURCES main.cpp)

samples/99_loaderunload/main.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
// Copyright (c) 2019-2025 Ben Ashbaugh
3+
//
4+
// SPDX-License-Identifier: MIT
5+
*/
6+
7+
#include <stdio.h>
8+
#include <vector>
9+
#include <popl/popl.hpp>
10+
11+
#include <CL/opencl.hpp>
12+
13+
static cl_int PrintPlatformInfoSummary(
14+
cl::Platform platform )
15+
{
16+
printf("\tName: %s\n", platform.getInfo<CL_PLATFORM_NAME>().c_str() );
17+
printf("\tVendor: %s\n", platform.getInfo<CL_PLATFORM_VENDOR>().c_str() );
18+
printf("\tDriver Version: %s\n", platform.getInfo<CL_PLATFORM_VERSION>().c_str() );
19+
20+
return CL_SUCCESS;
21+
}
22+
23+
static void PrintDeviceType(
24+
const char* label,
25+
cl_device_type type )
26+
{
27+
printf("%s%s%s%s%s%s\n",
28+
label,
29+
( type & CL_DEVICE_TYPE_DEFAULT ) ? "DEFAULT " : "",
30+
( type & CL_DEVICE_TYPE_CPU ) ? "CPU " : "",
31+
( type & CL_DEVICE_TYPE_GPU ) ? "GPU " : "",
32+
( type & CL_DEVICE_TYPE_ACCELERATOR ) ? "ACCELERATOR " : "",
33+
( type & CL_DEVICE_TYPE_CUSTOM ) ? "CUSTOM " : "");
34+
}
35+
36+
static cl_int PrintDeviceInfoSummary(
37+
const std::vector<cl::Device>& devices )
38+
{
39+
for( size_t i = 0; i < devices.size(); i++ )
40+
{
41+
printf("Device[%zu]:\n", i );
42+
43+
cl_device_type deviceType = devices[i].getInfo<CL_DEVICE_TYPE>();
44+
PrintDeviceType("\tType: ", deviceType);
45+
46+
printf("\tName: %s\n", devices[i].getInfo<CL_DEVICE_NAME>().c_str() );
47+
printf("\tVendor: %s\n", devices[i].getInfo<CL_DEVICE_VENDOR>().c_str() );
48+
printf("\tDevice Version: %s\n", devices[i].getInfo<CL_DEVICE_VERSION>().c_str() );
49+
printf("\tDriver Version: %s\n", devices[i].getInfo<CL_DRIVER_VERSION>().c_str() );
50+
}
51+
52+
return CL_SUCCESS;
53+
}
54+
55+
static void PrintPlatformInfo()
56+
{
57+
std::vector<cl::Platform> platforms;
58+
cl::Platform::get(&platforms);
59+
60+
for( size_t i = 0; i < platforms.size(); i++ )
61+
{
62+
printf( "Platform[%zu]:\n", i );
63+
PrintPlatformInfoSummary( platforms[i] );
64+
65+
std::vector<cl::Device> devices;
66+
platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &devices);
67+
68+
PrintDeviceInfoSummary( devices );
69+
printf( "\n" );
70+
}
71+
}
72+
73+
typedef cl_int (CL_API_CALL *_sclpfn_clTeardownOCLRT)(void);
74+
75+
int main(
76+
int argc,
77+
char** argv )
78+
{
79+
{
80+
popl::OptionParser op("Supported Options");
81+
82+
bool printUsage = false;
83+
try {
84+
op.parse(argc, argv);
85+
} catch (std::exception& e) {
86+
fprintf(stderr, "Error: %s\n\n", e.what());
87+
printUsage = true;
88+
}
89+
90+
if (printUsage || !op.unknown_options().empty() || !op.non_option_args().empty()) {
91+
fprintf(stderr,
92+
"Usage: loaderunload [options]\n"
93+
"%s", op.help().c_str());
94+
return -1;
95+
}
96+
}
97+
98+
printf("Initially:\n");
99+
PrintPlatformInfo();
100+
101+
auto clTeardownOCLRT = (_sclpfn_clTeardownOCLRT)
102+
clGetExtensionFunctionAddress("clTeardownOCLRT");
103+
if (clTeardownOCLRT == nullptr) {
104+
printf("Couldn't get a pointer to clTeardownOCLRT()...\n");
105+
} else {
106+
printf("Initiating teardown...\n");
107+
clTeardownOCLRT();
108+
109+
printf("Now:\n");
110+
PrintPlatformInfo();
111+
}
112+
113+
printf("Done.\n");
114+
115+
return 0;
116+
}

samples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ if(BUILD_EXTENSION_SAMPLES)
9191
add_subdirectory( 14_ooqcommandbuffers )
9292
add_subdirectory( 15_mutablecommandbufferasserts )
9393
endif()
94+
95+
add_subdirectory( 99_loaderunload )

0 commit comments

Comments
 (0)