### Run Python OpenCL Example Source: https://github.com/khronosgroup/opencl-sdk/blob/main/python/README.md This command shows how to execute a Python OpenCL example file. Ensure PyOpenCL is installed. ```sh python3 example-file.py ``` -------------------------------- ### Build and Install OpenCL SDK Source: https://github.com/khronosgroup/opencl-sdk/blob/main/README.md Build the OpenCL SDK using the generated CMake files and install it to a specified directory. The '--target install' option copies the built files. ```bash cmake --build . --target install --config Release ``` -------------------------------- ### Set Default Install Prefix Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/src/Extensions/CMakeLists.txt Sets the default installation prefix to './install' if it has not been explicitly set by the user. This ensures a predictable installation location for the SDK. ```cmake if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/install" CACHE PATH "Install Path" FORCE) endif() ``` -------------------------------- ### Build and Install GLEW Utility Executables Source: https://github.com/khronosgroup/opencl-sdk/blob/main/cmake/Dependencies/GLEW/CMakeLists.txt Builds and installs utility executables 'glewinfo' and 'visualinfo' if 'BUILD_UTILS' is enabled, linking them against the GLEW library. ```cmake if (BUILD_UTILS) set (GLEWINFO_SRC_FILES ${GLEW_DIR}/src/glewinfo.c) if (WIN32) list (APPEND GLEWINFO_SRC_FILES ${GLEW_DIR}/build/glewinfo.rc) endif () add_executable (glewinfo ${GLEWINFO_SRC_FILES}) if(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS) target_link_libraries (glewinfo glew) else() target_link_libraries (glewinfo glew_s) endif() if (NOT WIN32) target_link_libraries(glewinfo ${X11_LIBRARIES}) endif () set (VISUALINFO_SRC_FILES ${GLEW_DIR}/src/visualinfo.c) if (WIN32) list (APPEND VISUALINFO_SRC_FILES ${GLEW_DIR}/build/visualinfo.rc) endif () add_executable (visualinfo ${VISUALINFO_SRC_FILES}) if(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS) target_link_libraries (visualinfo glew) else() target_link_libraries (visualinfo glew_s) endif() if (NOT WIN32) target_link_libraries(visualinfo ${X11_LIBRARIES}) endif () install ( TARGETS glewinfo visualinfo DESTINATION ${CMAKE_INSTALL_BINDIR}) endif () ``` -------------------------------- ### Install opencl_ruby_ffi Gem Source: https://github.com/khronosgroup/opencl-sdk/blob/main/ruby/README.md Install the necessary Ruby bindings for OpenCL. This command should be run in your terminal. ```bash gem install --user-install opencl_ruby_ffi ``` -------------------------------- ### C++ Example: Custom SAXPY Options Parsing Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/SDK.md Demonstrates how to define and integrate custom command-line options for an OpenCL sample using the SDK's parsing utilities. This example shows how to add a 'length' option for vector operands. ```c++ // Sample-specific option struct SaxpyOptions { size_t length; }; // Add option to CLI parsing SDK utility template <> auto cl::sdk::parse(){ return std::make_tuple( std::make_shared>("l", "length", "Length of input", false, 1'048'576, "positive integral") ); } template <> SaxpyOptions cl::sdk::comprehend( std::shared_ptr> length_arg){ return SaxpyOptions{ length_arg->getValue() }; } int main(int argc, char* argv[]) { try { // Parse command-line options auto opts = cl::sdk::parse_cli< cl::sdk::options::Diagnostic, cl::sdk::options::SingleDevice, SaxpyOptions>(argc, argv); const auto& diag_opts = std::get<0>(opts); const auto& dev_opts = std::get<1>(opts); const auto& saxpy_opts = std::get<2>(opts); ... } // catch clauses } ``` -------------------------------- ### Set Installation Path Variables Source: https://github.com/khronosgroup/opencl-sdk/blob/main/cmake/Dependencies/GLEW/CMakeLists.txt Defines variables for installation paths and library information, typically used for generating pkg-config files or other configuration outputs. ```cmake set (prefix ${CMAKE_INSTALL_PREFIX}) set (exec_prefix ${CMAKE_INSTALL_PREFIX}) set (libdir ${CMAKE_INSTALL_FULL_LIBDIR}) set (includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR}) set (version ${GLEW_VERSION}) set (libname ${GLEW_LIB_NAME}) set (cflags) set (requireslib glu) ``` -------------------------------- ### OpenCL/OpenGL Interop Context Setup Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/extensions/khr/conway/README.md Demonstrates key APIs used for setting up an OpenCL-OpenGL interop context and related objects. Requires utility functions for context and program retrieval. ```c++ cl::util::get_context(cl::util::Triplet) cl::Context::getInfo() cl::CommandQueue(cl::Context, cl::Device) cl::Device::getInfo() cl::util::get_program(cl::Context, cl::string) cl::KernelFunctor<...>(cl::Program, const char*) cl::sdk::fill_with_random(...) cl::Buffer(cl::CommandQueue, Iter, Iter, bool) cl::copy(cl::CommandQueue, cl::Buffer, Iter, Iter) ``` -------------------------------- ### Install OpenCLConfigVersion.cmake Source: https://github.com/khronosgroup/opencl-sdk/blob/main/CMakeLists.txt Installs the generated `OpenCLConfigVersion.cmake` file. This complements `OpenCLConfig.cmake` by providing version information for package management. ```cmake install( FILES ${CMAKE_CURRENT_BINARY_DIR}/OpenCL/OpenCLConfigVersion.cmake DESTINATION ${config_package_location} COMPONENT binary ) ``` -------------------------------- ### Install OpenCLConfig.cmake Source: https://github.com/khronosgroup/opencl-sdk/blob/main/CMakeLists.txt Installs the generated `OpenCLConfig.cmake` file to the appropriate CMake package configuration directory. This makes the SDK discoverable by other projects using `find_package`. ```cmake set(config_package_location ${CMAKE_INSTALL_DATADIR}/cmake/OpenCL) install( FILES ${PROJECT_BINARY_DIR}/OpenCL/OpenCLConfig.cmake DESTINATION ${config_package_location} COMPONENT binary ) ``` -------------------------------- ### Install GLEW Targets Source: https://github.com/khronosgroup/opencl-sdk/blob/main/cmake/Dependencies/GLEW/CMakeLists.txt Installs the GLEW targets (shared or static libraries) to the appropriate directories based on the build configuration. ```cmake set(targets_to_install "") if(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS) list(APPEND targets_to_install glew) endif() if(NOT DEFINED BUILD_SHARED_LIBS OR NOT BUILD_SHARED_LIBS) list(APPEND targets_to_install glew_s) endif() install ( TARGETS ${targets_to_install} ${MAYBE_EXPORT} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} FRAMEWORK DESTINATION ${CMAKE_INSTALL_PREFIX} ) ``` -------------------------------- ### Install OpenCL Extension Loader Targets Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/src/Extensions/CMakeLists.txt Installs the OpenCL Extension Loader targets, including runtime, archive, and library components. This ensures the extension loader is correctly placed in the installation directory. ```cmake set(OPENCL_EXTENSION_LOADER_CONFIG_PATH "${CMAKE_INSTALL_DATADIR}/cmake/OpenCLExtensionLoader") install(TARGETS OpenCLExt EXPORT OpenCLExtensionLoaderTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT binary ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT binary LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT binary ) ``` -------------------------------- ### Enumerate OpenCL Platforms and Devices Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/enumopencl/README.md This snippet shows the core OpenCL C APIs used for discovering platforms and devices. Ensure OpenCL is correctly installed and your build environment is set up. ```c clGetPlatformIDs clGetDeviceIDs clGetPlatformInfo clGetDeviceInfo ``` -------------------------------- ### Set Install Prefix Source: https://github.com/khronosgroup/opencl-sdk/blob/main/CMakeLists.txt Configures the installation prefix to be within the project's source directory if it's currently at its default value. This is useful for development builds. ```cmake if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/install" CACHE PATH "Install Path" FORCE) endif() ``` -------------------------------- ### Include GNUInstallDirs Source: https://github.com/khronosgroup/opencl-sdk/blob/main/cmake/Dependencies/GLEW/CMakeLists.txt Includes the GNUInstallDirs module to standardize installation directory variables. ```cmake include(GNUInstallDirs) ``` -------------------------------- ### Include CPack Source: https://github.com/khronosgroup/opencl-sdk/blob/main/CMakeLists.txt Includes the CPack module, which is CMake's packaging utility. This enables the creation of installers and packages for the SDK. ```cmake include(CPack) ``` -------------------------------- ### Get Executable Folder (C) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Returns the path to the folder containing the currently running executable. The filename buffer must be provided by the caller. ```c cl_int cl_util_executable_folder( char* filename, size_t* const length); ``` -------------------------------- ### Add OpenCL Sample Macro Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/CMakeLists.txt Defines a CMake macro to add an OpenCL sample executable. It handles source files, include directories, libraries, compile definitions, and installation rules. Use this macro to define new samples within the project. ```cmake macro(add_sample) set(options TEST) set(one_value_args TARGET VERSION CATEGORY) set(multi_value_args SOURCES KERNELS SHADERS INCLUDES LIBS DEFINITIONS) cmake_parse_argument(OPENCL_SAMPLE "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN} ) if(NOT OPENCL_SAMPLE_VERSION) message(STATUS "No OpenCL version specified for sample ${OPENCL_SAMPLE_TARGET}, using OpenCL 3.0.") set(OPENCL_SAMPLE_VERSION 300) endif() add_executable(${OPENCL_SAMPLE_TARGET} ${OPENCL_SAMPLE_SOURCES}) target_include_directories(${OPENCL_SAMPLE_TARGET} PRIVATE ${OPENCL_SDK_INCLUDE_DIRS} ${OPENCL_SAMPLE_INCLUDES} ) target_link_libraries(${OPENCL_SAMPLE_TARGET} PRIVATE cargs OpenCL::Headers OpenCL::HeadersCpp OpenCL::OpenCL OpenCL::Utils OpenCL::UtilsCpp OpenCL::SDK OpenCL::SDKCpp $<$:m> ${OPENCL_SAMPLE_LIBS} ) target_compile_definitions(${OPENCL_SAMPLE_TARGET} PRIVATE CL_TARGET_OPENCL_VERSION=${OPENCL_SAMPLE_VERSION} CL_HPP_TARGET_OPENCL_VERSION=${OPENCL_SAMPLE_VERSION} CL_HPP_MINIMUM_OPENCL_VERSION=${OPENCL_SAMPLE_VERSION} CL_HPP_ENABLE_EXCEPTIONS $<$:_CRT_SECURE_NO_WARNINGS> # TODO: remove ${OPENCL_SAMPLE_DEFINITIONS} ) set_target_properties(${OPENCL_SAMPLE_TARGET} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}" INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}" FOLDER "Samples/${OPENCL_SAMPLE_CATEGORY}/${OPENCL_SAMPLE_TARGET}" ) # We register utility targets that copy kernel/shader code to build tree so that samples can be run from there. # We can't use OPENCL_SAMPLE_TARGET-device-code as a target name, because we often have two samples (C and CXX) # refering to the same device source code. Instead we use CURRENT_FOLDER_NAME and check for the utility target # already existing. If it does, we don't register redundant custom_commands and targets. (They would only cause # conflicting jobs in parallel MSBuild builds. Ninja is smart enough.) Either way, we add a convenience dependence # of OPENCL_SAMPLE_TARGET on the utility to make sure building the host code copies kernels too. get_filename_component(CURRENT_FOLDER_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) if(NOT TARGET ${CURRENT_FOLDER_NAME}-device-code) set(DEVICE_CODE_OUTPUTS) foreach(DEVICE_CODE_SOURCE IN LISTS OPENCL_SAMPLE_KERNELS OPENCL_SAMPLE_SHADERS) # NOTE: if() and foreach() could be omitted if CMake ver > 3.20 (COMMAND and OUTPUT needs genexpr) if(CMAKE_CONFIGURATION_TYPES) foreach(CONFIG_TYPE IN LISTS CMAKE_CONFIGURATION_TYPES) add_custom_command( OUTPUT "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/${CONFIG_TYPE}/${DEVICE_CODE_SOURCE}" COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different "${CMAKE_CURRENT_LIST_DIR}/${DEVICE_CODE_SOURCE}" "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/${CONFIG_TYPE}/${DEVICE_CODE_SOURCE}" DEPENDS ${DEVICE_CODE_SOURCE} COMMENT "Copying ${DEVICE_CODE_SOURCE}" ) list(APPEND DEVICE_CODE_OUTPUTS "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/${CONFIG_TYPE}/${DEVICE_CODE_SOURCE}") endforeach() else() add_custom_command( OUTPUT "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/${DEVICE_CODE_SOURCE}" COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different "${CMAKE_CURRENT_LIST_DIR}/${DEVICE_CODE_SOURCE}" "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/${DEVICE_CODE_SOURCE}" DEPENDS ${DEVICE_CODE_SOURCE} COMMENT "Copying ${DEVICE_CODE_SOURCE}" ) list(APPEND DEVICE_CODE_OUTPUTS "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/${DEVICE_CODE_SOURCE}") endif() endforeach() add_custom_target(${CURRENT_FOLDER_NAME}-device-code DEPENDS ${DEVICE_CODE_OUTPUTS} ) endif() add_dependencies(${OPENCL_SAMPLE_TARGET} ${CURRENT_FOLDER_NAME}-device-code ) foreach(CONFIG ${OPENCL_SAMPLE_CONFIGS}) install(TARGETS ${OPENCL_SAMPLE_TARGET} CONFIGURATIONS ${CONFIG} DESTINATION ${CMAKE_INSTALL_BINDIR}) install(FILES ${OPENCL_SAMPLE_KERNELS} CONFIGURATIONS ${CONFIG} DESTINATION ${CMAKE_INSTALL_BINDIR}) install(FILES ${OPENCL_SAMPLE_SHADERS} CONFIGURATIONS ${CONFIG} DESTINATION ${CMAKE_INSTALL_BINDIR}) endforeach() if(OPENCL_SDK_TEST_SAMPLES AND OPENCL_SAMPLE_TEST) add_test( NAME ${OPENCL_SAMPLE_TARGET} COMMAND ${OPENCL_SAMPLE_TARGET} WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} ) endif() endmacro() ``` -------------------------------- ### Get Executable Folder (C++) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Returns the path to the folder containing the currently running executable. This is typically useful for locating assets. ```cpp std::string executable_folder( cl_int* const error = nullptr); ``` -------------------------------- ### Static Library Export Definition Source: https://github.com/khronosgroup/opencl-sdk/blob/main/cmake/Dependencies/GLEW/CMakeLists.txt Conditionally defines the 'GLEW_STATIC' preprocessor macro for static library builds and sets up include directories for installation. ```cmake if(CMAKE_VERSION VERSION_LESS 2.8.12) set(MAYBE_EXPORT "") else() target_compile_definitions(glew_s INTERFACE "GLEW_STATIC") foreach(t glew glew_s) target_include_directories(${t} PUBLIC $) endforeach() set(MAYBE_EXPORT EXPORT glew-targets) endif() ``` -------------------------------- ### Get Interop Context Properties (C++) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/SDK.md Retrieves context properties for setting up an OpenCL-OpenGL interop context. Returns a null-terminated list and may report OS-specific errors. ```c++ cl::vector cl::sdk::get_interop_context_properties(const cl::Device& plat, cl_int* error = nullptr); ``` -------------------------------- ### Clone OpenCL SDK and Initialize Submodules Source: https://github.com/khronosgroup/opencl-sdk/blob/main/README.md Clone the OpenCL SDK repository first, then initialize and update only the necessary sub-modules for the SDK. This is an alternative to the recursive clone. ```bash git clone https://github.com/KhronosGroup/OpenCL-SDK.git git submodule init git submodule update ``` -------------------------------- ### Project and Minimum Version Source: https://github.com/khronosgroup/opencl-sdk/blob/main/cmake/Dependencies/GLEW/CMakeLists.txt Initializes the project as 'glew' and sets the minimum required CMake version. ```cmake project (glew C) cmake_minimum_required (VERSION 2.8.12) ``` -------------------------------- ### Context Utilities Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Utilities for creating OpenCL contexts. ```APIDOC ## cl_util_get_context ### Description Creates a context on the platform with id `plat_id` with a single device of type `type` and id `dev_id`. ### Parameters - `plat_id` (int) - The platform ID. - `dev_id` (int) - The device ID. - `type` (cl_device_type) - The device type. - `error` (cl_int*) - Pointer to store the error code. ### Returns `cl_context` - The created OpenCL context. ### Error Codes - `CL_UTIL_INDEX_OUT_OF_RANGE` if the requested platform or device id is outside the range of available platforms or devices of the selected `type` on the platform. ``` ```APIDOC ## cl::util::get_context ### Description Creates a context on the platform with id `plat_id` with a single device of type `type` and id `dev_id`. ### Parameters - `plat_id` (int) - The platform ID. - `dev_id` (int) - The device ID. - `type` (cl_device_type) - The device type. - `error` (cl_int* = nullptr) - Pointer to store the error code (optional). ### Returns `cl::Context` - The created OpenCL context. ### Error Codes - `CL_UTIL_INDEX_OUT_OF_RANGE` if the requested platform or device id is outside the range of available platforms or devices of the selected `type` on the platform. ``` -------------------------------- ### Tag and Push SDK Release Source: https://github.com/khronosgroup/opencl-sdk/blob/main/docs/RELEASE.md Use these git commands to tag a new release version and push it to the remote repository. Ensure all dependencies are published to the PPA before executing. ```bash git tag vYYYY.MM.DD ``` ```bash git push vYYYY.MM.DD ``` -------------------------------- ### Write Program Binaries (C) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Writes all device binaries of a program to persistent storage. The program_file_name is a pattern that is completed for each device. ```c cl_int cl_util_write_binaries( const cl_program program, const char* const program_file_name); ``` -------------------------------- ### Project Definition Source: https://github.com/khronosgroup/opencl-sdk/blob/main/CMakeLists.txt Defines the project name, version, and supported languages. This is a standard CMake command to initialize a project. ```cmake project(OpenCL-SDK VERSION 2026.05.29 LANGUAGES C CXX ) ``` -------------------------------- ### Write Image File (C) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/SDK.md Writes an cl_sdk_image structure to a file in BMP/JPEG/PNG format. Requires a file name, the image data, and an optional error pointer. ```c void cl_sdk_write_image(const char * file_name, const cl_sdk_image * im, cl_int * err); ``` -------------------------------- ### Get Event Duration (C++) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Queries an OpenCL event for the duration between two state transitions, returning it in user-specified units. Defaults to nanoseconds. ```c++ template auto cl::util::get_duration(cl::Event& ev); ``` -------------------------------- ### macOS Framework Target Properties Source: https://github.com/khronosgroup/opencl-sdk/blob/main/cmake/Dependencies/GLEW/CMakeLists.txt Sets target properties for building GLEW as a macOS framework, including versioning, bundle identifier, and installation path. ```cmake if (BUILD_FRAMEWORK) set_target_properties(glew PROPERTIES FRAMEWORK TRUE FRAMEWORK_VERSION ${GLEW_VERSION} MACOSX_FRAMEWORK_IDENTIFIER net.sourceforge.glew MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${GLEW_VERSION} MACOSX_FRAMEWORK_BUNDLE_VERSION ${GLEW_VERSION} XCODE_ATTRIBUTE_INSTALL_PATH "@rpath" PUBLIC_HEADER "${GLEW_PUBLIC_HEADERS_FILES}" OUTPUT_NAME GLEW ) endif() ``` -------------------------------- ### Program Building and Event Handling Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/blur/README.md Functions for building OpenCL programs and retrieving event durations. ```APIDOC ## cl_util_build_program ### Description Builds an OpenCL program for a specified device with optional build options. ### Signature ```c cl_util_build_program(const cl_program pr, const cl_device_id dev, const char * const opt) ``` ## cl_util_get_event_duration ### Description Calculates the duration of an OpenCL event between specified start and end profiling points. ### Signature ```c cl_util_get_event_duration(const cl_event event, const cl_profiling_info start, const cl_profiling_info end, cl_int * const error) ``` ``` -------------------------------- ### Generate OpenCLConfig.cmake Source: https://github.com/khronosgroup/opencl-sdk/blob/main/CMakeLists.txt Writes a CMake configuration file (`OpenCLConfig.cmake`) to the build directory. This file is used by downstream projects to find and use the installed OpenCL SDK. ```cmake file( WRITE ${PROJECT_BINARY_DIR}/OpenCL/OpenCLConfig.cmake [[ get_filename_component(PARENT_DIR ${CMAKE_CURRENT_LIST_DIR} PATH) include("${PARENT_DIR}/OpenCLHeaders/OpenCLHeadersConfig.cmake") include("${PARENT_DIR}/OpenCLICDLoader/OpenCLICDLoaderConfig.cmake") include("${PARENT_DIR}/OpenCLHeadersCpp/OpenCLHeadersCppConfig.cmake") include("${PARENT_DIR}/OpenCLUtils/OpenCLUtilsConfig.cmake") include("${PARENT_DIR}/OpenCLUtilsCpp/OpenCLUtilsCppConfig.cmake") ]] ) ``` -------------------------------- ### Create OpenCL Context (C) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Creates an OpenCL context on a specified platform and device type. Returns a cl_context. Error codes are returned via the 'error' pointer. ```c cl_context cl_util_get_context(int plat_id, int dev_id, cl_device_type type, cl_int* error); ``` -------------------------------- ### Define Kernel Functor for Reduction Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/reduce/README.md Example of creating a type-checked C++ kernel functor for a reduction operation. This utility simplifies kernel invocation and argument handling. ```c++ auto reduce = cl::KernelFunctor(program, "reduce"); ``` -------------------------------- ### Enable Event Profiling in Command Queue Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/reduce/README.md Create a command queue with profiling enabled. This is a prerequisite for recording event profiling data. ```c++ cl::CommandQueue queue{ context, device, cl::QueueProperties::Profiling }; ``` -------------------------------- ### Utility Functions for OpenCL Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/binaries/README.md A list of utility functions used within the sample for device management, file operations, and error handling. ```c cl_util_get_device(const cl_uint plat_id, const cl_uint dev_id, const cl_device_type type, cl_int * const error) ``` ```c cl_util_print_device_info(const cl_device_id device) ``` ```c cl_util_read_binaries(const cl_context context, const cl_device_id * const devices, const cl_uint num_devices, const char * const program_file_name, cl_int * const error) ``` ```c cl_util_build_program(const cl_program pr, const cl_device_id dev, const char * const opt) ``` ```c cl_util_read_text_file(const char * const filename, size_t * const length, cl_int * const error) ``` ```c cl_util_write_binaries(const cl_program program, const char * const program_file_name) ``` ```c cl_util_print_error(const cl_int error) ``` -------------------------------- ### Checkout Tip of Main Branch Source: https://github.com/khronosgroup/opencl-sdk/blob/main/docs/RELEASE.md Fetches the latest changes from the remote repository and checks out the main branch. Ensure your workspace is clean and the stage is empty before running. ```bash git pull git checkout /main ``` -------------------------------- ### Conditional Build Options Source: https://github.com/khronosgroup/opencl-sdk/blob/main/CMakeLists.txt Configures build options that depend on other options. For example, building samples requires utility libraries, and specific interop samples depend on the general samples build. ```cmake option(OPENCL_SDK_BUILD_UTILITY_LIBRARIES "Build utility libraries" ON) cmake_dependent_option(OPENCL_SDK_BUILD_SAMPLES "Build sample code" ON OPENCL_SDK_BUILD_UTILITY_LIBRARIES OFF) cmake_dependent_option(OPENCL_SDK_BUILD_OPENGL_SAMPLES "Build OpenCL-OpenGL interop sample code" OFF OPENCL_SDK_BUILD_SAMPLES OFF) cmake_dependent_option(OPENCL_SDK_BUILD_VULKAN_SAMPLES "Build OpenCL-Vulkan interop sample code" OFF OPENCL_SDK_BUILD_SAMPLES OFF) cmake_dependent_option(OPENCL_SDK_TEST_SAMPLES "Add CTest to samples (where applicable)" ON OPENCL_SDK_BUILD_SAMPLES OFF) ``` -------------------------------- ### Export OpenCL Extension Loader Targets Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/src/Extensions/CMakeLists.txt Exports the OpenCL Extension Loader targets for use by other CMake projects. This generates a CMake file that allows consumers to find and use the installed targets. ```cmake export(EXPORT OpenCLExtensionLoaderTargets FILE ${CMAKE_CURRENT_BINARY_DIR}/OpenCLExtensionLoader/OpenCLExtensionLoaderTargets.cmake NAMESPACE OpenCL:: ) install(EXPORT OpenCLExtensionLoaderTargets FILE OpenCLExtensionLoaderTargets.cmake NAMESPACE OpenCL:: DESTINATION ${OPENCL_EXTENSION_LOADER_CONFIG_PATH} COMPONENT binary ) ``` -------------------------------- ### Set Output Directories Source: https://github.com/khronosgroup/opencl-sdk/blob/main/cmake/Dependencies/GLEW/CMakeLists.txt Configures the output directories for archives, libraries, and runtime binaries. ```cmake set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) ``` -------------------------------- ### Write Program Binaries (C++) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Writes all device binaries of a program to persistent storage. The program_file_name is a pattern that is completed for each device. ```cpp write_binaries( const cl::Program::Binaries& binaries, const std::vector& devices, const char* const program_file_name); ``` -------------------------------- ### Add Sample Project Function (CMake) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/CMakeLists.txt Defines a CMake function `add_sample` to streamline the addition of OpenCL samples. It supports options for testing, target naming, versioning, categorization, source and kernel files, include directories, libraries, and compile definitions. ```cmake # Usage: # add_sample( # TEST # optional, adds a ctest for the sample # TARGET # specifies the name of the sample # VERSION # specifies the OpenCL version for the sample # CATEGORY # optional, specifies a category for the sample # SOURCES ... # specifies source files for the sample # KERNELS ... # optional, specifies kernel files for the sample # INCLUDES ... # optional, specifies additional include directories for the sample # LIBS ... # optional, specifies additional libraries for the sample # DEFINITIONS # optional, specifies additional compile definitions for the sample # ) ``` -------------------------------- ### Add C Callback Sample Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/callback/CMakeLists.txt Adds the C callback sample target if C11 threads and atomics are supported. It links against the Threads library if available. ```cmake add_sample( TEST TARGET callback VERSION 300 SOURCES main.c KERNELS reaction_diffusion.cl) target_link_libraries(callback PRIVATE $) ``` -------------------------------- ### Create and Copy Input Buffer Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/reduce/README.md Creates an input buffer from host memory and a temporary buffer for reduction. The input buffer is copied to the device using a cl::copy overload that internally maps the buffer. ```c++ cl::Buffer front{ queue, std::begin(arr), std::end(arr), false }, back{ context, CL_MEM_READ_WRITE, new_size(arr.size()) * sizeof(cl_int) }; ``` -------------------------------- ### Clone OpenCL SDK with Recursive Submodules Source: https://github.com/khronosgroup/opencl-sdk/blob/main/README.md Use this command to clone the OpenCL SDK repository and all its sub-modules, including transitive dependencies, in a single step. ```bash git clone --recursive https://github.com/KhronosGroup/OpenCL-SDK.git ``` -------------------------------- ### Platform Utilities Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Utilities for querying OpenCL platform capabilities and version information. ```APIDOC ## cl::util::supports_extension (Platform) ### Description Tells whether a platform supports a given extension. ### Parameters - `platform` (cl::Platform&) - The platform to query. - `extension` (cl::string&) - The extension string being searched for. ### Returns `bool` - True if the platform supports the extension, false otherwise. ``` ```APIDOC ## cl::util::platform_version_contains ### Description Tells whether the platform version string contains a specific fragment. ### Parameters - `platform` (cl::Platform&) - The platform to query. - `version_fragment` (cl::string&) - The version string fragment to search for. ### Returns `bool` - True if the version string contains the fragment, false otherwise. ``` -------------------------------- ### OpenCL Buffer Copy APIs Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/copybuffer/README.md Key OpenCL APIs used for creating buffers, command queues, mapping memory, and enqueuing copy operations. ```c clCreateCommandQueue / clCreateCommandQueueWithProperties ``` ```c clCreateBuffer ``` ```c clEnqueueMapBuffer ``` ```c clEnqueueUnmapMemObject ``` ```c clEnqueueCopyBuffer ``` -------------------------------- ### Set OpenCL Sample Configurations (CMake) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/CMakeLists.txt Determines the build configurations for OpenCL samples based on CMAKE_CONFIGURATION_TYPES or CMAKE_BUILD_TYPE. ```cmake if(CMAKE_CONFIGURATION_TYPES) set(OPENCL_SAMPLE_CONFIGS ${CMAKE_CONFIGURATION_TYPES}) else() set(OPENCL_SAMPLE_CONFIGS ${CMAKE_BUILD_TYPE}) endif() ``` -------------------------------- ### Generate CMake Build Files Source: https://github.com/khronosgroup/opencl-sdk/blob/main/README.md Navigate to the build directory and use CMake to generate the build files for the OpenCL SDK. Specify the build type, such as 'Release'. ```bash mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Release ``` -------------------------------- ### Extract Version from File Source: https://github.com/khronosgroup/opencl-sdk/blob/main/cmake/Dependencies/GLEW/CMakeLists.txt Reads version components (major, minor, patch) from a 'version' file using regular expressions and sets them for packaging. ```cmake file (STRINGS ${GLEW_DIR}/config/version _VERSION_MAJOR_STRING REGEX "GLEW_MAJOR[ ]*=[ ]*[0-9]+.*") string (REGEX REPLACE "GLEW_MAJOR[ ]*=[ ]*([0-9]+)" "\1" CPACK_PACKAGE_VERSION_MAJOR ${_VERSION_MAJOR_STRING}) file (STRINGS ${GLEW_DIR}/config/version _VERSION_MINOR_STRING REGEX "GLEW_MINOR[ ]*=[ ]*[0-9]+.*") string (REGEX REPLACE "GLEW_MINOR[ ]*=[ ]*([0-9]+)" "\1" CPACK_PACKAGE_VERSION_MINOR ${_VERSION_MINOR_STRING}) file (STRINGS ${GLEW_DIR}/config/version _VERSION_PATCH_STRING REGEX "GLEW_MICRO[ ]*=[ ]*[0-9]+.*") string (REGEX REPLACE "GLEW_MICRO[ ]*=[ ]*([0-9]+)" "\1" CPACK_PACKAGE_VERSION_PATCH ${_VERSION_PATCH_STRING}) set (GLEW_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}) ``` -------------------------------- ### OpenCL-OpenGL Interop Context Creation Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/SDK.md Creates an OpenCL context capable of sharing resources with OpenGL. ```APIDOC ## cl::sdk::get_interop_context ### Description Creates an interop context on the platform with id `plat_id` with a single device of type `type` and id `dev_id` which is able to share resources with the currently active OpenGL context. ### Parameters - `plat_id` (int) - The ID of the OpenCL platform. - `dev_id` (int) - The ID of the OpenCL device on the specified platform. - `type` (cl_device_type) - The type of the OpenCL device. - `error` (cl_int*) - An optional pointer to capture error codes. Ordinary OpenCL error codes or `CL_UTIL_INDEX_OUT_OF_RANGE`, `CL_UTIL_OS_GL_QUERY_ERROR` may be returned. ### Returns A `cl::Context` instance configured for OpenCL-OpenGL interoperation. ``` -------------------------------- ### Add C++ Callback Sample Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/callback/CMakeLists.txt Adds the C++ callback sample target. This sample is always added and links against the Threads library if available. ```cmake add_sample( TEST TARGET callbackcpp VERSION 300 SOURCES main.cpp KERNELS reaction_diffusion.cl) target_link_libraries(callbackcpp PRIVATE $) ``` -------------------------------- ### Clinfo Utility Build Option Source: https://github.com/khronosgroup/opencl-sdk/blob/main/CMakeLists.txt An option to control the build of the 'clinfo' utility. This is a common diagnostic tool for OpenCL. ```cmake option(OPENCL_SDK_BUILD_CLINFO "Build clinfo utility" ON) ``` -------------------------------- ### Write Image File (C++) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/SDK.md Writes a cl::sdk::Image object to a file (BMP/JPEG/PNG). Takes a file name, the image data, and an optional error pointer for error reporting. ```c++ void cl::sdk::write_image(const char* file_name, const Image& image, cl_int* err); ``` -------------------------------- ### Write Basic Package Version File Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/src/Extensions/CMakeLists.txt Generates a CMake package version file for the OpenCL Extension Loader. This file specifies the project version and compatibility, aiding in package management. ```cmake write_basic_package_version_file( ${CMAKE_CURRENT_BINARY_DIR}/OpenCLExtensionLoader/OpenCLExtensionLoaderConfigVersion.cmake VERSION ${PROJECT_VERSION} COMPATIBILITY AnyNewerVersion ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/OpenCLExtensionLoader/OpenCLExtensionLoaderConfigVersion.cmake DESTINATION ${OPENCL_EXTENSION_LOADER_CONFIG_PATH} COMPONENT binary ) ``` -------------------------------- ### Check Device Extension Support (C++) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Verifies if a specific OpenCL device supports a given extension. Requires the cl::Device object and the extension name string. ```c++ bool cl::util::supports_extension(const cl::Device& device, const cl::string& extension); ``` -------------------------------- ### Create OpenCL Context (C++) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Creates an OpenCL context using C++ bindings. Returns a cl::Context object. Exceptions are thrown on error if CL_HPP_ENABLE_EXCEPTIONS is defined. ```c++ cl::Context cl::util::get_context(int plat_id, int dev_id, cl_device_type type, cl_int* error = nullptr); ``` -------------------------------- ### File and Image Handling Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/blur/README.md Functions for reading text files and OpenCL images, and writing images. ```APIDOC ## cl_util_read_text_file ### Description Reads the content of a text file into a buffer. ### Signature ```c cl_util_read_text_file(const char * const filename, size_t * const length, cl_int * const error) ``` ## cl_sdk_read_image ### Description Reads an image from a file. ### Signature ```c cl_sdk_read_image(const char * const file_name, cl_int * const error) ``` ## cl_sdk_write_image ### Description Writes an image to a file. ### Signature ```c cl_sdk_write_image(const char * const file_name, const cl_sdk_image * const im) ``` ``` -------------------------------- ### Enable Event Profiling in Command Queue Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/binaries/README.md Enable profiling capabilities for a command queue. This is a prerequisite for measuring event durations. ```c cl_command_queue_properties props[] = { CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0 }; queue = clCreateCommandQueueWithProperties(context, device, props, &error); ``` -------------------------------- ### Check Platform Extension Support (C++) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Verifies if a given OpenCL platform supports a specific extension. Requires the cl::Platform object and the extension name string. ```c++ bool cl::util::supports_extension(const cl::Platform& platform, const cl::string& extension); ``` -------------------------------- ### Build Options Source: https://github.com/khronosgroup/opencl-sdk/blob/main/cmake/Dependencies/GLEW/CMakeLists.txt Defines boolean options for configuring the build, such as utilities, Regal mode, OSMesa mode, and macOS framework builds. ```cmake option (BUILD_UTILS "utilities" ON) option (GLEW_REGAL "Regal mode" OFF) option (GLEW_OSMESA "OSMesa mode" OFF) if (APPLE) option (BUILD_FRAMEWORK "Build Framework bundle for OSX" OFF) endif () ``` -------------------------------- ### Add Sample Build Target Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/copybufferkernel/CMakeLists.txt This CMake command adds a new sample target named 'copybufferkernel'. It specifies the OpenCL version required (120) and lists the source files for the sample. ```cmake add_sample( TEST TARGET copybufferkernel VERSION 120 SOURCES main.cpp) ``` -------------------------------- ### Device Utilities Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Utilities for querying OpenCL device capabilities, extensions, and OpenCL C versions. ```APIDOC ## cl::util::opencl_c_version_contains ### Description Tells whether the device OpenCL C version string contains a specific fragment. ### Parameters - `device` (cl::Device&) - The device to query. - `version_fragment` (cl::string&) - The version string fragment to search for. ### Returns `bool` - True if the OpenCL C version string contains the fragment, false otherwise. ``` ```APIDOC ## cl::util::supports_extension (Device) ### Description Tells whether a device supports a given extension. ### Parameters - `device` (cl::Device&) - The device to query. - `extension` (cl::string&) - The extension string being searched for. ### Returns `bool` - True if the device supports the extension, false otherwise. ``` ```APIDOC ## cl::util::supports_feature ### Description Tells whether a device supports any version of a feature. ### Parameters - `device` (cl::Device&) - The device to query. - `feature_name` (cl::string&) - The feature name string being searched for. ### Returns `bool` - True if the device supports the feature, false otherwise. _(Note: this function is only available when both the Utility library and the using code defines minimally `CL_VERSION_3_0`.)_ ``` -------------------------------- ### C Structs for Command-line Options Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/SDK.md Defines C structures used for parsing command-line arguments related to device selection and diagnostic options. ```c struct cl_sdk_options_DeviceTriplet { int plat_index; int dev_index; cl_device_type dev_type; }; struct cl_sdk_options_Diagnostic { bool verbose; bool quiet; }; struct cl_sdk_options_SingleDevice { struct cl_sdk_options_DeviceTriplet triplet; }; struct cl_sdk_options_MultiDevice { struct cl_sdk_options_DeviceTriplet * triplets; size_t number; }; ``` -------------------------------- ### OpenCL-OpenGL Interop API Surface Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/extensions/khr/nbody/README.md Lists key APIs and concepts used for OpenCL-OpenGL interoperability, including context creation, command queues, program building, kernel functors, buffer management, and synchronization primitives. ```c++ cl::sdk::InteropWindow cl::Context::getInfo() cl::CommandQueue(cl::Context, cl::Device) cl::util::get_program(cl::Context, cl::string) cl::Program::build(cl::Device) cl::KernelFunctor<...>(cl::Program, const char*) cl::Buffer(cl::CommandQueue, Iter, Iter, bool) cl::BufferGL::BufferGL(cl::Context, cl_mem_flags, cl_GLuint) cl::copy(cl::CommandQueue, cl::Buffer, Iter, Iter) cl::CommandQueue::enqueueAcquireGLObjects(const cl::vector*, const cl::vector*, cl::Event*) cl::CommandQueue::enqueueReleaseGLObjects(const cl::vector*, const cl::vector*, cl::Event*) cl::finish() cl::Event::wait() ``` -------------------------------- ### Read Program Binaries (C) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Reads a set of binary files into memory for OpenCL program compilation. The program_file_name is a pattern that is completed for each device. ```c cl_program cl_util_read_binaries( const cl_context context, const cl_device_id* const devices, const cl_uint num_devices, const char* const program_file_name, cl_int* const error ); ``` -------------------------------- ### Create Interop Context (C++) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/SDK.md Creates an OpenCL interop context capable of sharing resources with the active OpenGL context. Handles potential index out-of-range or OS-specific errors. ```c++ cl::Context cl::sdk::get_interop_context(int plat_id, int dev_id, cl_device_type type, cl_int* error = nullptr); ``` -------------------------------- ### Read Image File (C) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/SDK.md Reads an image file (BMP/JPEG/PNG) from disk into an cl_sdk_image structure. Handles potential errors via an error pointer. ```c cl_sdk_image cl_sdk_read_image(const char* file_name, cl_int* err); ``` -------------------------------- ### Vanilla Work-Group Reduction in OpenCL Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/reduce/README.md Implements a textbook tree-like reduction using local memory and barriers. Ensures data uniformity with `read_local` and handles uneven work-group loads. ```opencl int read_local(local int* shared, size_t count, int zero, size_t i) { return i < count ? shared[i] : zero; } kernel void reduce( global int* front, global int* back, local int* shared, unsigned int length, int zero_elem ) { const size_t lid = get_local_id(0), lsi = get_local_size(0), wid = get_group_id(0), wsi = get_num_groups(0); const size_t wg_stride = lsi * 2, valid_count = wid != wsi - 1 ? // If not last group wg_stride : // as much as possible length - wid * wg_stride; // only the remaining // Copy real data to local event_t read; async_work_group_copy( shared, front + wid * wg_stride, valid_count, read); wait_group_events(1, &read); barrier(CLK_LOCAL_MEM_FENCE); for (int i = lsi; i != 0; i /= 2) { if (lid < i) shared[lid] = op( read_local(shared, valid_count, zero_elem, lid), read_local(shared, valid_count, zero_elem, lid + i) ); barrier(CLK_LOCAL_MEM_FENCE); } if (lid == 0) back[wid] = shared[0]; } ``` -------------------------------- ### Check Device Feature Support (C++) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Determines if a device supports any version of a specified feature. Note: Requires CL_VERSION_3_0 to be defined. ```c++ bool cl::util::supports_feature(const cl::Device& device, const cl::string& feature_name); ``` -------------------------------- ### OpenCL C++ API Wrappers Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/core/multi-device/README.md This snippet showcases C++ wrappers for OpenCL functionalities, offering a more object-oriented interface. It includes constructors for core OpenCL objects like Buffer, CommandQueue, and Context, as well as methods for device partitioning, kernel execution, and synchronization. ```c++ cl::Buffer::Buffer(const Context&, cl_mem_flags, size_type, void*, cl_int*=NULL) cl::Buffer::createSubBuffer(cl_mem_flags, cl_buffer_create_type, const void*, cl_int*=NULL) cl::BuildError cl::CommandQueue::CommandQueue(const cl::Context&, cl::QueueProperties, cl_int*=NULL) cl::CommandQueue::enqueueReadBuffer(const Buffer&, cl_bool, size_type, size_type, void*, const std::vector*=nullptr, cl::Event*=nullptr) cl::Context cl::Device::Device() cl::Device::createSubDevices(const cl_device_partition_property*, std::vector*) cl::EnqueueArgs::EnqueueArgs(cl::CommandQueue&, cl::NDRange, cl::NDRange) cl::Error cl::Event cl::Kernel cl::KernelFunctor::KernelFunctor(const Program&, const string, cl_int*=NULL) cl::NDRange::NDRange(size_t, size_t) cl::NullRange cl::Platform::Platform() cl::Platform::Platform(cl::Platform) cl::Platform::get(vector*) cl::Program::Program() cl::Program::Program(cl::Program) cl::WaitForEvents(const vector&) cl::copy(const CommandQueue&, const cl::Buffer&, IteratorType, IteratorType) cl::sdk::comprehend() cl::sdk::fill_with_random() cl::sdk::get_context(cl_uint, cl_uint, cl_device_type, cl_int*) cl::sdk::options::SingleDevice cl::sdk::parse() cl::sdk::parse_cli() cl::sdk::options::DeviceTriplet cl::sdk::options::Diagnostic cl::sdk::options::SingleDevice cl::string::string(cl::string) cl::util::Error cl::util::get_duration(cl::Event&) cl::util::opencl_c_version_contains(const cl::Device&, const cl::string&) ``` -------------------------------- ### Event Utilities Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Utilities for querying event durations between state transitions. ```APIDOC ## cl::util::get_duration ### Description Queries an event for the duration of time measured in user-provided units between two state transitions. ### Template Parameters - `From` (cl_int) - The starting state transition. - `To` (cl_int) - The ending state transition. - `Dur` (typename = std::chrono::nanoseconds) - The desired duration unit. ### Parameters - `ev` (cl::Event&) - The OpenCL event to query. ### Returns `auto` - The duration in the specified units (defaults to `std::chrono::nanoseconds`). ``` -------------------------------- ### Create OpenCL Extension Loader Config File Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/src/Extensions/CMakeLists.txt Writes a CMake configuration file for the OpenCL Extension Loader. This file includes the exported targets, making the extension loader discoverable by package management. ```cmake file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/OpenCLExtensionLoader/OpenCLExtensionLoaderConfig.cmake "include(\"${CMAKE_CURRENT_LIST_DIR}/OpenCLExtensionLoaderTargets.cmake\")" ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/OpenCLExtensionLoader/OpenCLExtensionLoaderConfig.cmake DESTINATION ${OPENCL_EXTENSION_LOADER_CONFIG_PATH} COMPONENT binary ) ``` -------------------------------- ### Error Handling Utilities Source: https://github.com/khronosgroup/opencl-sdk/blob/main/lib/Utils.md Custom exception class for OpenCL utility errors. ```APIDOC ## class cl::util::Error ### Description Custom exception type used by OpenCL utilities when an error occurs and `CL_HPP_ENABLE_EXCEPTIONS` is defined. ### Methods - `Error(cl_int err, const char * errStr = NULL)`: Constructor to create a new error exception. - `~Error() throw()`: Destructor. - `const char * what() const throw()`: Returns the error message string. - `cl_int err(void) const`: Returns the error code. ``` -------------------------------- ### Check for Math Library Existence (CMake) Source: https://github.com/khronosgroup/opencl-sdk/blob/main/samples/CMakeLists.txt Checks if the math library (libm) is available on the system. This is often needed for standard math functions. ```cmake # add math library to link if needed # method by Michael Ambrus, https://stackoverflow.com/questions/34625627 include(CheckLibraryExists) CHECK_LIBRARY_EXISTS(m sin "" HAVE_LIB_M) ```