From d3cd6d290846a79539fbc18e10ca69ad6ab55bd9 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Tue, 5 Apr 2016 21:46:19 -0500 Subject: [PATCH] Dynamically binding to posix opencl library. --- CMakeLists.txt | 4 +++ rai/plat/posix/openclapi.cpp | 47 ++++++++++++++++++++++++++++++++++ rai/plat/windows/openclapi.cpp | 0 3 files changed, 51 insertions(+) create mode 100644 rai/plat/posix/openclapi.cpp create mode 100644 rai/plat/windows/openclapi.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b40476e..9d6f1496 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,12 +101,15 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") set (PLATFORM_WALLET_SOURCE rai/plat/default/icon.cpp) elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows") set (PLATFORM_SECURE_SOURCE rai/plat/windows/working.cpp rai/plat/windows/priority.cpp) + set (PLATFORM_NODE_SOURCE rai/plat/windows/openclapi.cpp) set (PLATFORM_WALLET_SOURCE rai/plat/windows/icon.cpp RaiBlocks.rc) elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux") set (PLATFORM_SECURE_SOURCE rai/plat/posix/working.cpp rai/plat/linux/priority.cpp) + set (PLATFORM_NODE_SOURCE rai/plat/posix/openclapi.cpp) set (PLATFORM_WALLET_SOURCE rai/plat/default/icon.cpp) elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") set (PLATFORM_SECURE_SOURCE rai/plat/posix/working.cpp rai/plat/default/priority.cpp) + set (PLATFORM_NODE_SOURCE rai/plat/posix/openclapi.cpp) set (PLATFORM_WALLET_SOURCE rai/plat/default/icon.cpp) else () error ("Unknown platform: ${CMAKE_SYSTEM_NAME}") @@ -123,6 +126,7 @@ add_library (secure rai/versioning.cpp) add_library (node + ${PLATFORM_NODE_SOURCE} rai/node/bootstrap.cpp rai/node/bootstrap.hpp rai/node/common.cpp diff --git a/rai/plat/posix/openclapi.cpp b/rai/plat/posix/openclapi.cpp new file mode 100644 index 00000000..3cf841b2 --- /dev/null +++ b/rai/plat/posix/openclapi.cpp @@ -0,0 +1,47 @@ +#include + +#include + +namespace +{ +class opencl_initializer +{ +public: + opencl_initializer () + { + opencl_library = dlopen ("libOpenCL.so", RTLD_NOW); + if (opencl_library != nullptr) + { + clGetPlatformIDs = reinterpret_cast (dlsym (opencl_library, "clGetPlatformIDs")); + } + } + ~opencl_initializer () + { + dlclose (opencl_library); + } + void * opencl_library; + cl_int (* clGetPlatformIDs) (cl_uint, cl_platform_id *, cl_uint *); + cl_int (* clGetDeviceIDs) (cl_platform_id, cl_device_type, cl_uint, cl_device_id *, cl_uint *); + static opencl_initializer initializer; +}; +} + +cl_int clGetPlatformIDs (cl_uint num_entries, cl_platform_id * platforms, cl_uint * num_platforms) +{ + cl_int result; + if (opencl_initializer::initializer.opencl_library != nullptr) + { + result = opencl_initializer::initializer.clGetPlatformIDs (num_entries, platforms, num_platforms); + } + else + { + result = CL_SUCCESS; + *num_platforms = 0; + } + return result; +} + +cl_int clGetDeviceIDs (cl_platform_id platform, cl_device_type device_type, cl_uint num_entries, cl_device_id * devices, cl_uint * num_devices) +{ + return clGetDeviceIDs (platform, device_type, num_entries, devices, num_devices); +} \ No newline at end of file diff --git a/rai/plat/windows/openclapi.cpp b/rai/plat/windows/openclapi.cpp new file mode 100644 index 00000000..e69de29b