Skip to content

Commit

Permalink
add a sample that unloads the OpenCL ICD loader and then reloads it
Browse files Browse the repository at this point in the history
This sample currently requires a special branch of the OpenCL runtime
loader that includes the unload function.
  • Loading branch information
bashbaug committed Jan 24, 2025
1 parent d56380b commit 84f5760
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${OpenCL_INCLUDE_DIR})

add_subdirectory(external/OpenCL-Headers)

option(SAMPLES_USE_RUNTIME_LOADER "Build with the OpenCL Runtime Loader" ON)
if (SAMPLES_USE_RUNTIME_LOADER)
add_subdirectory(external/opencl-runtime-loader)
set(OpenCL_LIBRARIES OpenCLRuntimeLoader ${CMAKE_DL_LIBS})
else()
add_subdirectory(external/opencl-icd-loader)
set_target_properties(OpenCL PROPERTIES FOLDER "OpenCL-ICD-Loader")
set_target_properties(cllayerinfo PROPERTIES FOLDER "OpenCL-ICD-Loader")
set(OpenCL_LIBRARIES OpenCL)
endif()

if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/external/opencl-extension-loader)
add_subdirectory(external/opencl-extension-loader)
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Many samples that use extensions additionally require the OpenCL Extension Loade

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

The loading and unloading sample requires the OpenCL Runtime Loader:

git clone https://github.com/bashbaug/opencl-runtime-loader external/opencl-runtime-loader

After satisfying the external dependencies create build files using CMake. For example:

mkdir build && cd build
Expand Down
10 changes: 10 additions & 0 deletions samples/99_loaderunload/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2025 Ben Ashbaugh
#
# SPDX-License-Identifier: MIT

add_opencl_sample(
TEST
NUMBER 99
TARGET loaderunload
VERSION 120
SOURCES main.cpp)
116 changes: 116 additions & 0 deletions samples/99_loaderunload/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
// Copyright (c) 2019-2025 Ben Ashbaugh
//
// SPDX-License-Identifier: MIT
*/

#include <stdio.h>
#include <vector>
#include <popl/popl.hpp>

#include <CL/opencl.hpp>

static cl_int PrintPlatformInfoSummary(
cl::Platform platform )
{
printf("\tName: %s\n", platform.getInfo<CL_PLATFORM_NAME>().c_str() );
printf("\tVendor: %s\n", platform.getInfo<CL_PLATFORM_VENDOR>().c_str() );
printf("\tDriver Version: %s\n", platform.getInfo<CL_PLATFORM_VERSION>().c_str() );

return CL_SUCCESS;
}

static void PrintDeviceType(
const char* label,
cl_device_type type )
{
printf("%s%s%s%s%s%s\n",
label,
( type & CL_DEVICE_TYPE_DEFAULT ) ? "DEFAULT " : "",
( type & CL_DEVICE_TYPE_CPU ) ? "CPU " : "",
( type & CL_DEVICE_TYPE_GPU ) ? "GPU " : "",
( type & CL_DEVICE_TYPE_ACCELERATOR ) ? "ACCELERATOR " : "",
( type & CL_DEVICE_TYPE_CUSTOM ) ? "CUSTOM " : "");
}

static cl_int PrintDeviceInfoSummary(
const std::vector<cl::Device>& devices )
{
for( size_t i = 0; i < devices.size(); i++ )
{
printf("Device[%zu]:\n", i );

cl_device_type deviceType = devices[i].getInfo<CL_DEVICE_TYPE>();
PrintDeviceType("\tType: ", deviceType);

printf("\tName: %s\n", devices[i].getInfo<CL_DEVICE_NAME>().c_str() );
printf("\tVendor: %s\n", devices[i].getInfo<CL_DEVICE_VENDOR>().c_str() );
printf("\tDevice Version: %s\n", devices[i].getInfo<CL_DEVICE_VERSION>().c_str() );
printf("\tDriver Version: %s\n", devices[i].getInfo<CL_DRIVER_VERSION>().c_str() );
}

return CL_SUCCESS;
}

static void PrintPlatformInfo()
{
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);

for( size_t i = 0; i < platforms.size(); i++ )
{
printf( "Platform[%zu]:\n", i );
PrintPlatformInfoSummary( platforms[i] );

std::vector<cl::Device> devices;
platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &devices);

PrintDeviceInfoSummary( devices );
printf( "\n" );
}
}

typedef cl_int (CL_API_CALL *_sclpfn_clTeardownOCLRT)(void);

int main(
int argc,
char** argv )
{
{
popl::OptionParser op("Supported Options");

bool printUsage = false;
try {
op.parse(argc, argv);
} catch (std::exception& e) {
fprintf(stderr, "Error: %s\n\n", e.what());
printUsage = true;
}

if (printUsage || !op.unknown_options().empty() || !op.non_option_args().empty()) {
fprintf(stderr,
"Usage: loaderunload [options]\n"
"%s", op.help().c_str());
return -1;
}
}

printf("Initially:\n");
PrintPlatformInfo();

auto clTeardownOCLRT = (_sclpfn_clTeardownOCLRT)
clGetExtensionFunctionAddress("clTeardownOCLRT");
if (clTeardownOCLRT == nullptr) {
printf("Couldn't get a pointer to clTeardownOCLRT()...\n");
} else {
printf("Initiating teardown...\n");
clTeardownOCLRT();

printf("Now:\n");
PrintPlatformInfo();
}

printf("Done.\n");

return 0;
}
2 changes: 2 additions & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ if(BUILD_EXTENSION_SAMPLES)
add_subdirectory( 14_ooqcommandbuffers )
add_subdirectory( 15_mutablecommandbufferasserts )
endif()

add_subdirectory( 99_loaderunload )

0 comments on commit 84f5760

Please sign in to comment.