From 140666d2c474c595507744cafc47bfde3d23152c Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Wed, 30 Nov 2022 17:55:43 -0500 Subject: [PATCH 1/5] ENH: Update opencl_error() adding support for OpenCL 2.0 and 2.2 --- libautoscoper/src/gpu/opencl/OpenCL.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libautoscoper/src/gpu/opencl/OpenCL.cpp b/libautoscoper/src/gpu/opencl/OpenCL.cpp index 3e635c4d..b97118d1 100644 --- a/libautoscoper/src/gpu/opencl/OpenCL.cpp +++ b/libautoscoper/src/gpu/opencl/OpenCL.cpp @@ -177,6 +177,14 @@ static const char* opencl_error(cl_int err) case CL_INVALID_COMPILER_OPTIONS: return "CL_INVALID_COMPILER_OPTIONS"; case CL_INVALID_LINKER_OPTIONS: return "CL_INVALID_LINKER_OPTIONS"; case CL_INVALID_DEVICE_PARTITION_COUNT: return "CL_INVALID_DEVICE_PARTITION_COUNT"; +#endif +#ifdef CL_VERSION_2_0 + case CL_INVALID_PIPE_SIZE: return "CL_INVALID_PIPE_SIZE"; + case CL_INVALID_DEVICE_QUEUE: return "CL_INVALID_DEVICE_QUEUE"; +#endif +#ifdef CL_VERSION_2_2 + case CL_INVALID_SPEC_ID: return "CL_INVALID_SPEC_ID"; + case CL_MAX_SIZE_RESTRICTION_EXCEEDED: return "CL_MAX_SIZE_RESTRICTION_EXCEEDED"; #endif default: return "Unknown"; } From ed9f376e166d92af6952fc5cefc9ab7f21d7373c Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Wed, 30 Nov 2022 15:50:22 -0500 Subject: [PATCH 2/5] COMP: Update opencl::GLBuffer to use cl_GLuint instead of GLuint --- libautoscoper/src/gpu/opencl/OpenCL.cpp | 2 +- libautoscoper/src/gpu/opencl/OpenCL.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libautoscoper/src/gpu/opencl/OpenCL.cpp b/libautoscoper/src/gpu/opencl/OpenCL.cpp index b97118d1..f4ef517f 100644 --- a/libautoscoper/src/gpu/opencl/OpenCL.cpp +++ b/libautoscoper/src/gpu/opencl/OpenCL.cpp @@ -994,7 +994,7 @@ void Buffer::fill(const float val) const #endif } -GLBuffer::GLBuffer(GLuint pbo, cl_mem_flags access) +GLBuffer::GLBuffer(cl_GLuint pbo, cl_mem_flags access) { err_ = opencl_global_context(); CHECK_CL diff --git a/libautoscoper/src/gpu/opencl/OpenCL.hpp b/libautoscoper/src/gpu/opencl/OpenCL.hpp index c14cd6a9..ad8efc0a 100644 --- a/libautoscoper/src/gpu/opencl/OpenCL.hpp +++ b/libautoscoper/src/gpu/opencl/OpenCL.hpp @@ -140,7 +140,7 @@ class Buffer class GLBuffer { public: - GLBuffer(GLuint pbo, cl_mem_flags access=CL_MEM_READ_WRITE); + GLBuffer(cl_GLuint pbo, cl_mem_flags access=CL_MEM_READ_WRITE); ~GLBuffer(); friend class Kernel; protected: From 0a588b66ca44061b018c6d4ebec6b258a63c750f Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Wed, 30 Nov 2022 15:43:33 -0500 Subject: [PATCH 3/5] COMP: Add OpenCL-(CLHPP|Headers|ICD-Loader} external projects --- SuperBuild.cmake | 6 ++ Superbuild/External_OpenCL-CLHPP.cmake | 68 +++++++++++++++++++++ Superbuild/External_OpenCL-Headers.cmake | 60 ++++++++++++++++++ Superbuild/External_OpenCL-ICD-Loader.cmake | 64 +++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 Superbuild/External_OpenCL-CLHPP.cmake create mode 100644 Superbuild/External_OpenCL-Headers.cmake create mode 100644 Superbuild/External_OpenCL-ICD-Loader.cmake diff --git a/SuperBuild.cmake b/SuperBuild.cmake index 62c52fc8..07f18beb 100644 --- a/SuperBuild.cmake +++ b/SuperBuild.cmake @@ -3,6 +3,12 @@ set(Autoscoper_DEPENDENCIES GLEW TIFF ) +if(Autoscoper_RENDERING_BACKEND STREQUAL "OpenCL") + list(APPEND Autoscoper_DEPENDENCIES + OpenCL-CLHPP + OpenCL-ICD-Loader + ) +endif() set(proj ${SUPERBUILD_TOPLEVEL_PROJECT}) diff --git a/Superbuild/External_OpenCL-CLHPP.cmake b/Superbuild/External_OpenCL-CLHPP.cmake new file mode 100644 index 00000000..99e9750c --- /dev/null +++ b/Superbuild/External_OpenCL-CLHPP.cmake @@ -0,0 +1,68 @@ + +set(proj OpenCL-CLHPP) + +set(${proj}_DEPENDENCIES "OpenCL-Headers") + +# Include dependent projects if any +ExternalProject_Include_Dependencies(${proj} PROJECT_VAR proj DEPENDS_VAR ${proj}_DEPENDENCIES) + +if(Autoscoper_USE_SYSTEM_${proj}) + message(FATAL_ERROR "Enabling Autoscoper_USE_SYSTEM_${proj} is not supported !") +endif() + +# Sanity checks +if(DEFINED OpenCLHeadersCpp_DIR AND NOT EXISTS ${OpenCLHeadersCpp_DIR}) + message(FATAL_ERROR "OpenCLHeadersCpp_DIR variable is defined but corresponds to nonexistent directory") +endif() + +if(NOT DEFINED OpenCLHeadersCpp_DIR AND NOT Autoscoper_USE_SYSTEM_${proj}) + + set(EP_SOURCE_DIR ${CMAKE_BINARY_DIR}/${proj}) + set(EP_BINARY_DIR ${CMAKE_BINARY_DIR}/${proj}-build) + + set(EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS) + + if(NOT CMAKE_CONFIGURATION_TYPES) + list(APPEND EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS + -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} + ) + endif() + + ExternalProject_Add(${proj} + ${${proj}_EP_ARGS} + GIT_REPOSITORY https://github.com/KhronosGroup/OpenCL-CLHPP.git + GIT_TAG v2022.09.30 + SOURCE_DIR ${EP_SOURCE_DIR} + BINARY_DIR ${EP_BINARY_DIR} + INSTALL_DIR ${EP_INSTALL_DIR} + CMAKE_CACHE_ARGS + # Compiler settings + -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER} + -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER} + -DCMAKE_CXX_EXTENSIONS:BOOL=${CMAKE_CXX_EXTENSIONS} + -DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD} + -DCMAKE_CXX_STANDARD_REQUIRED:BOOL=${CMAKE_CXX_STANDARD_REQUIRED} + # Options + -DBUILD_DOCS:BOOL=OFF + -DBUILD_EXAMPLES:BOOL=OFF + -DBUILD_TESTING:BOOL=OFF + -DOPENCL_CLHPP_BUILD_TESTING:BOOL=OFF + # Install directories + -DCMAKE_INSTALL_PREFIX:PATH= + # Dependencies + -DOpenCLHeaders_DIR:PATH=${OpenCLHeaders_DIR} + ${EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS} + INSTALL_COMMAND "" + DEPENDS + ${${proj}_DEPENDENCIES} + ) + set(OpenCLHeadersCpp_DIR ${EP_BINARY_DIR}/OpenCLHeadersCpp) + +endif() + +mark_as_superbuild( + VARS + OpenCLHeadersCpp_DIR:PATH + ) + +ExternalProject_Message(${proj} "OpenCLHeadersCpp_DIR:${OpenCLHeadersCpp_DIR}") diff --git a/Superbuild/External_OpenCL-Headers.cmake b/Superbuild/External_OpenCL-Headers.cmake new file mode 100644 index 00000000..47955e71 --- /dev/null +++ b/Superbuild/External_OpenCL-Headers.cmake @@ -0,0 +1,60 @@ + +set(proj OpenCL-Headers) + +set(${proj}_DEPENDENCIES "") + +# Include dependent projects if any +ExternalProject_Include_Dependencies(${proj} PROJECT_VAR proj DEPENDS_VAR ${proj}_DEPENDENCIES) + +if(Autoscoper_USE_SYSTEM_${proj}) + message(FATAL_ERROR "Enabling Autoscoper_USE_SYSTEM_${proj} is not supported !") +endif() + +# Sanity checks +if(DEFINED OpenCLHeaders_DIR AND NOT EXISTS ${OpenCLHeaders_DIR}) + message(FATAL_ERROR "OpenCLHeaders_DIR variable is defined but corresponds to nonexistent directory") +endif() + +if(NOT DEFINED OpenCLHeaders_DIR AND NOT Autoscoper_USE_SYSTEM_${proj}) + + set(EP_SOURCE_DIR ${CMAKE_BINARY_DIR}/${proj}) + set(EP_BINARY_DIR ${CMAKE_BINARY_DIR}/${proj}-build) + + set(EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS) + + if(NOT CMAKE_CONFIGURATION_TYPES) + list(APPEND EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS + -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} + ) + endif() + + ExternalProject_Add(${proj} + ${${proj}_EP_ARGS} + GIT_REPOSITORY https://github.com/KhronosGroup/OpenCL-Headers.git + GIT_TAG v2022.09.30 + SOURCE_DIR ${EP_SOURCE_DIR} + BINARY_DIR ${EP_BINARY_DIR} + INSTALL_DIR ${EP_INSTALL_DIR} + CMAKE_CACHE_ARGS + # Compiler settings + -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER} + # Options + -DBUILD_TESTING:BOOL=OFF + -DOPENCL_HEADERS_BUILD_TESTING:BOOL=OFF + # Install directories + -DCMAKE_INSTALL_PREFIX:PATH= + ${EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS} + INSTALL_COMMAND "" + DEPENDS + ${${proj}_DEPENDENCIES} + ) + set(OpenCLHeaders_DIR ${EP_BINARY_DIR}/OpenCLHeaders) + +endif() + +mark_as_superbuild( + VARS + OpenCLHeaders_DIR:PATH + ) + +ExternalProject_Message(${proj} "OpenCLHeaders_DIR:${OpenCLHeaders_DIR}") diff --git a/Superbuild/External_OpenCL-ICD-Loader.cmake b/Superbuild/External_OpenCL-ICD-Loader.cmake new file mode 100644 index 00000000..e9002e26 --- /dev/null +++ b/Superbuild/External_OpenCL-ICD-Loader.cmake @@ -0,0 +1,64 @@ + +set(proj OpenCL-ICD-Loader) + +set(${proj}_DEPENDENCIES + OpenCL-Headers + ) + +# Include dependent projects if any +ExternalProject_Include_Dependencies(${proj} PROJECT_VAR proj DEPENDS_VAR ${proj}_DEPENDENCIES) + +if(Autoscoper_USE_SYSTEM_${proj}) + message(FATAL_ERROR "Enabling Autoscoper_USE_SYSTEM_${proj} is not supported !") +endif() + +# Sanity checks +if(DEFINED OpenCLICDLoader_DIR AND NOT EXISTS ${OpenCLICDLoader_DIR}) + message(FATAL_ERROR "OpenCLICDLoader_DIR variable is defined but corresponds to nonexistent directory") +endif() + +if(NOT DEFINED OpenCLICDLoader_DIR AND NOT Autoscoper_USE_SYSTEM_${proj}) + + set(EP_SOURCE_DIR ${CMAKE_BINARY_DIR}/${proj}) + set(EP_BINARY_DIR ${CMAKE_BINARY_DIR}/${proj}-build) + + set(EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS) + + if(NOT CMAKE_CONFIGURATION_TYPES) + list(APPEND EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS + -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} + ) + endif() + + ExternalProject_Add(${proj} + ${${proj}_EP_ARGS} + GIT_REPOSITORY https://github.com/KhronosGroup/OpenCL-ICD-Loader.git + GIT_TAG v2022.09.30 + SOURCE_DIR ${EP_SOURCE_DIR} + BINARY_DIR ${EP_BINARY_DIR} + INSTALL_DIR ${EP_INSTALL_DIR} + CMAKE_CACHE_ARGS + # Compiler settings + -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER} + # Options + -DBUILD_TESTING:BOOL=OFF + -DOPENCL_ICD_LOADER_BUILD_TESTING:BOOL=OFF + # Install directories + -DCMAKE_INSTALL_PREFIX:PATH= + # Depdendencies + -DOpenCLHeaders_DIR:PATH=${OpenCLHeaders_DIR} + ${EXTERNAL_PROJECT_OPTIONAL_CMAKE_CACHE_ARGS} + INSTALL_COMMAND "" + DEPENDS + ${${proj}_DEPENDENCIES} + ) + set(OpenCLICDLoader_DIR ${EP_BINARY_DIR}/OpenCLHeaders) + +endif() + +mark_as_superbuild( + VARS + OpenCLICDLoader_DIR:PATH + ) + +ExternalProject_Message(${proj} "OpenCLICDLoader_DIR:${OpenCLICDLoader_DIR}") From b670ea5aeb92a4d5df6f05766ff2822fa975db0c Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Wed, 30 Nov 2022 15:55:01 -0500 Subject: [PATCH 4/5] WIP --- autoscoper/CMakeLists.txt | 12 ++++++++++-- libautoscoper/CMakeLists.txt | 14 ++++++++++++-- libautoscoper/src/gpu/opencl/OpenCL.hpp | 13 ++----------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/autoscoper/CMakeLists.txt b/autoscoper/CMakeLists.txt index 9a2d5e75..3ddfbb0b 100644 --- a/autoscoper/CMakeLists.txt +++ b/autoscoper/CMakeLists.txt @@ -116,11 +116,19 @@ if(Autoscoper_RENDERING_BACKEND STREQUAL "CUDA") ) elseif(Autoscoper_RENDERING_BACKEND STREQUAL "OpenCL") find_package(OpenCL ${Autoscoper_OpenCL_MINIMUM_REQUIRED_VERSION} REQUIRED) - target_include_directories(autoscoper PUBLIC ${OPENCL_INCLUDE_DIRS}) + find_package(OpenCLHeaders REQUIRED) + find_package(OpenCLHeadersCpp REQUIRED) + #target_include_directories(autoscoper PUBLIC ${OPENCL_INCLUDE_DIRS}) set(GPU_LIBRARIES ${OpenCL_LIBRARIES} + OpenCL::HeadersCpp + ) + target_compile_definitions(autoscoper PRIVATE + CL_TARGET_OPENCL_VERSION=${Autoscoper_CL_TARGET_OPENCL_VERSION} + CL_HPP_TARGET_OPENCL_VERSION=${Autoscoper_CL_TARGET_OPENCL_VERSION} + CL_HPP_MINIMUM_OPENCL_VERSION=${Autoscoper_CL_TARGET_OPENCL_VERSION} + CL_HPP_ENABLE_EXCEPTIONS ) - target_compile_definitions(autoscoper PRIVATE CL_TARGET_OPENCL_VERSION=${Autoscoper_CL_TARGET_OPENCL_VERSION}) else() message(FATAL_ERROR "Setting Autoscoper_RENDERING_BACKEND to '${Autoscoper_RENDERING_BACKEND}' is not supported") endif() diff --git a/libautoscoper/CMakeLists.txt b/libautoscoper/CMakeLists.txt index 33ecf98e..4ca0345e 100644 --- a/libautoscoper/CMakeLists.txt +++ b/libautoscoper/CMakeLists.txt @@ -40,11 +40,21 @@ if(Autoscoper_RENDERING_BACKEND STREQUAL "CUDA") target_include_directories(libautoscoper PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/gpu/cuda/cutil) elseif(Autoscoper_RENDERING_BACKEND STREQUAL "OpenCL") find_package(OpenCL ${Autoscoper_OpenCL_MINIMUM_REQUIRED_VERSION} REQUIRED) + find_package(OpenCLHeaders REQUIRED) + find_package(OpenCLHeadersCpp REQUIRED) include(${CMAKE_CURRENT_SOURCE_DIR}/src/gpu/opencl/CMakeLists.txt) add_library(libautoscoper STATIC ${libautoscoper_SOURCES} ${libautoscoper_HEADERS} ${opencl_SOURCES} ${opencl_HEADERS}) - target_include_directories(libautoscoper PUBLIC ${OpenCL_INCLUDE_DIRS}) + #target_include_directories(libautoscoper PUBLIC ${OpenCL_INCLUDE_DIRS}) add_dependencies(libautoscoper ${SHADER_TO_HEADER}) - target_compile_definitions(libautoscoper PRIVATE CL_TARGET_OPENCL_VERSION=${Autoscoper_CL_TARGET_OPENCL_VERSION}) + target_link_libraries(libautoscoper PUBLIC + OpenCL::HeadersCpp + ) + target_compile_definitions(libautoscoper PRIVATE + CL_TARGET_OPENCL_VERSION=${Autoscoper_CL_TARGET_OPENCL_VERSION} + CL_HPP_TARGET_OPENCL_VERSION=${Autoscoper_CL_TARGET_OPENCL_VERSION} + CL_HPP_MINIMUM_OPENCL_VERSION=${Autoscoper_CL_TARGET_OPENCL_VERSION} + CL_HPP_ENABLE_EXCEPTIONS + ) else() message(FATAL_ERROR "Setting Autoscoper_RENDERING_BACKEND to '${Autoscoper_RENDERING_BACKEND}' is not supported") endif() diff --git a/libautoscoper/src/gpu/opencl/OpenCL.hpp b/libautoscoper/src/gpu/opencl/OpenCL.hpp index ad8efc0a..c7b03753 100644 --- a/libautoscoper/src/gpu/opencl/OpenCL.hpp +++ b/libautoscoper/src/gpu/opencl/OpenCL.hpp @@ -42,20 +42,11 @@ #ifndef XROMM_HPP #define XROMM_HPP +#include + #include #include -#if defined(__APPLE__) || defined(__MACOSX) -#include -#include -#else -#if defined(_WIN32) -#include -#endif -#include -#include -#endif - namespace xromm { namespace gpu { void opencl_global_gl_context(); From 7ddf514b037f0a0bed912e3b342e5f7b2794abc8 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Thu, 1 Dec 2022 10:37:40 -0500 Subject: [PATCH 5/5] WIP: Refactor Program class to use cl::Program --- libautoscoper/src/gpu/opencl/OpenCL.cpp | 34 +++---------------------- libautoscoper/src/gpu/opencl/OpenCL.hpp | 4 +-- 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/libautoscoper/src/gpu/opencl/OpenCL.cpp b/libautoscoper/src/gpu/opencl/OpenCL.cpp index f4ef517f..d2cebba0 100644 --- a/libautoscoper/src/gpu/opencl/OpenCL.cpp +++ b/libautoscoper/src/gpu/opencl/OpenCL.cpp @@ -701,7 +701,7 @@ cl_int opencl_global_context() return CL_SUCCESS; } -Kernel::Kernel(cl_program program, const char* func) +Kernel::Kernel(const cl::Program& program, const char* func) { err_ = opencl_global_context(); CHECK_CL @@ -881,44 +881,18 @@ void Kernel::setArg(cl_uint i, size_t size, const void* value) CHECK_CL } -Program::Program() { compiled_ = false; } - Kernel* Program::compile(const char* code, const char* func) { if (!compiled_) { - err_ = opencl_global_context(); - CHECK_CL - - size_t len = strlen(code); - program_ = clCreateProgramWithSource(context_, 1, &code, &len, &err_); - CHECK_CL - - err_ = clBuildProgram(program_, 1, devices_, NULL, NULL, NULL); - if (err_ == CL_BUILD_PROGRAM_FAILURE) { - size_t log_size; - err_ = clGetProgramBuildInfo( - program_, devices_[0], CL_PROGRAM_BUILD_LOG, - 0, NULL, &log_size); - CHECK_CL - char* build_log = (char*)malloc(log_size+1); - if (!build_log) ERROR("malloc for build log"); - err_ = clGetProgramBuildInfo( - program_, devices_[0], CL_PROGRAM_BUILD_LOG, - log_size, build_log, NULL); - CHECK_CL - build_log[log_size] = '\0'; + if (program_.compile(code) != CL_SUCCESS) + { cerr << "OpenCL build failure for kernel function '" << func - << "':\n" << build_log << endl; - free(build_log); + << "':\n" << program_.getBuildInfo() << endl; exit(1); - } else { - CHECK_CL } - compiled_ = true; } - return new Kernel(program_, func); } diff --git a/libautoscoper/src/gpu/opencl/OpenCL.hpp b/libautoscoper/src/gpu/opencl/OpenCL.hpp index c7b03753..59c32ff1 100644 --- a/libautoscoper/src/gpu/opencl/OpenCL.hpp +++ b/libautoscoper/src/gpu/opencl/OpenCL.hpp @@ -65,7 +65,7 @@ class Image; class Kernel { public: - Kernel(cl_program program, const char* func); + Kernel(const cl::Program& program, const char* func); void reset(); static size_t getLocalMemSize(); @@ -105,7 +105,7 @@ class Program Program(); Kernel* compile(const char* code, const char* func); protected: - cl_program program_; + cl::Program program_; bool compiled_; };