This commit is contained in:
xsl
2025-09-04 10:54:47 +08:00
commit 6bc8f61b18
1808 changed files with 208268 additions and 0 deletions
@@ -0,0 +1,22 @@
////
- Copyright (c) 2023, Arm Limited and Contributors
-
- SPDX-License-Identifier: Apache-2.0
-
- Licensed under the Apache License, Version 2.0 the "License";
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-
////
= OpenCL common source files
This folder does not contain a sample, but only OpenCL related units shared by the OpenCL interoperability samples in this repository.
@@ -0,0 +1,58 @@
/* Copyright (c) 2021-2023, Arm Limited and Contributors
* Copyright (c) 2023, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Core OpenCL functions, loaded from libOpenCL.so
#if !defined(OPENCL_EXPORTED_FUNCTION)
#define OPENCL_EXPORTED_FUNCTION(fun)
#endif
OPENCL_EXPORTED_FUNCTION(clCreateContext);
OPENCL_EXPORTED_FUNCTION(clGetDeviceIDs);
OPENCL_EXPORTED_FUNCTION(clGetPlatformIDs);
OPENCL_EXPORTED_FUNCTION(clCreateBuffer);
OPENCL_EXPORTED_FUNCTION(clReleaseMemObject);
OPENCL_EXPORTED_FUNCTION(clCreateProgramWithSource);
OPENCL_EXPORTED_FUNCTION(clBuildProgram);
OPENCL_EXPORTED_FUNCTION(clCreateKernel);
OPENCL_EXPORTED_FUNCTION(clSetKernelArg);
OPENCL_EXPORTED_FUNCTION(clEnqueueNDRangeKernel);
OPENCL_EXPORTED_FUNCTION(clFlush);
OPENCL_EXPORTED_FUNCTION(clFinish);
OPENCL_EXPORTED_FUNCTION(clCreateCommandQueue);
OPENCL_EXPORTED_FUNCTION(clReleaseContext);
OPENCL_EXPORTED_FUNCTION(clGetPlatformInfo);
OPENCL_EXPORTED_FUNCTION(clGetExtensionFunctionAddressForPlatform);
OPENCL_EXPORTED_FUNCTION(clCreateImageWithProperties);
OPENCL_EXPORTED_FUNCTION(clGetDeviceInfo);
#undef OPENCL_EXPORTED_FUNCTION
// Extension functions, loaded using clGetExtensionFunctionAddressForPlatform
#if !defined(OPENCL_EXPORTED_EXTENSION_FUNCTION)
# define OPENCL_EXPORTED_EXTENSION_FUNCTION(fun)
#endif
OPENCL_EXPORTED_EXTENSION_FUNCTION(clImportMemoryARM);
OPENCL_EXPORTED_EXTENSION_FUNCTION(clCreateSemaphoreWithPropertiesKHR);
OPENCL_EXPORTED_EXTENSION_FUNCTION(clEnqueueWaitSemaphoresKHR);
OPENCL_EXPORTED_EXTENSION_FUNCTION(clEnqueueSignalSemaphoresKHR);
OPENCL_EXPORTED_EXTENSION_FUNCTION(clEnqueueAcquireExternalMemObjectsKHR);
OPENCL_EXPORTED_EXTENSION_FUNCTION(clEnqueueReleaseExternalMemObjectsKHR);
OPENCL_EXPORTED_EXTENSION_FUNCTION(clReleaseSemaphoreKHR);
#undef OPENCL_EXPORTED_EXTENSION_FUNCTION
@@ -0,0 +1,126 @@
/* Copyright (c) 2021-2023, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "open_cl_utils.h"
#if defined(__linux__)
# include <dlfcn.h>
#elif defined(_WIN32)
# include <windows.h>
#endif
#include <string>
#include <vector>
#define OPENCL_EXPORTED_FUNCTION(func_name) std::function<decltype(func_name)> func_name##_ptr = nullptr
#define OPENCL_EXPORTED_EXTENSION_FUNCTION(func_name) std::function<decltype(func_name)> func_name##_ptr = nullptr
#include "open_cl_functions.inl"
static void *handle = nullptr;
cl_platform_id load_opencl()
{
// Check if OpenCL is supported by trying to load the OpenCL library and getting a valid function pointer
bool openCLAvailable = false;
#if defined(__ANDROID__)
// Try to find the OpenCL library on one of the following paths
static const char *libraryPaths[] = {
// Generic
"/system/vendor/lib64/libOpenCL.so",
"/system/lib64/libOpenCL.so",
"/system/vendor/lib/libOpenCL.so",
"/system/lib/libOpenCL.so",
// ARM Mali
"/system/vendor/lib64/egl/libGLES_mali.so",
"/system/lib64/egl/libGLES_mali.so",
"/system/vendor/lib/egl/libGLES_mali.so",
"/system/lib/egl/libGLES_mali.so",
// PowerVR
"/system/vendor/lib64/libPVROCL.so",
"/system/lib64/libPVROCL.so",
"/system/vendor/lib/libPVROCL.so",
"/system/lib/libPVROCL.so"};
for (auto libraryPath : libraryPaths)
{
handle = dlopen(libraryPath, RTLD_LAZY);
if (handle)
{
break;
}
}
#elif defined(__linux__)
// Try to find the OpenCL library on one of the following paths
static const char *libraryPaths[] = {
"libOpenCL.so",
"/usr/lib/libOpenCL.so",
"/usr/local/lib/libOpenCL.so",
"/usr/local/lib/libpocl.so",
"/usr/lib64/libOpenCL.so",
"/usr/lib32/libOpenCL.so",
"libOpenCL.so.1",
"/usr/lib/libOpenCL.so.1",
"/usr/local/lib/libOpenCL.so.1",
"/usr/local/lib/libpocl.so.1",
"/usr/lib64/libOpenCL.so.1",
"/usr/lib32/libOpenCL.so.1"};
for (auto libraryPath : libraryPaths)
{
handle = dlopen(libraryPath, RTLD_LAZY);
if (handle)
{
break;
}
}
#elif defined(_WIN32)
handle = LoadLibraryA("OpenCL.dll");
#endif
if (handle == nullptr)
{
return nullptr;
}
#if defined(_WIN32)
# define OPENCL_EXPORTED_FUNCTION(func_name) func_name##_ptr = reinterpret_cast<decltype(func_name) *>(GetProcAddress((HMODULE) handle, #func_name))
#else
# define OPENCL_EXPORTED_FUNCTION(func_name) func_name##_ptr = reinterpret_cast<decltype(func_name) *>(dlsym(handle, #func_name))
#endif
#include "open_cl_functions.inl"
cl_platform_id platform_id = nullptr;
cl_uint num_platforms;
clGetPlatformIDs_ptr(1, &platform_id, &num_platforms);
#define OPENCL_EXPORTED_EXTENSION_FUNCTION(func_name) func_name##_ptr = \
reinterpret_cast<decltype(func_name) *>(clGetExtensionFunctionAddressForPlatform_ptr(platform_id, #func_name))
#include "open_cl_functions.inl"
return platform_id;
}
void unload_opencl()
{
if (handle)
{
#if defined(_WIN32)
FreeLibrary((HMODULE) handle);
#else
dlclose(handle);
#endif
handle = nullptr;
}
}
@@ -0,0 +1,64 @@
/* Copyright (c) 2021-2023, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#define CL_TARGET_OPENCL_VERSION 300
#include <CL/cl_ext.h>
#include <CL/opencl.h>
#include <functional>
#define OPENCL_EXPORTED_FUNCTION(func_name) extern std::function<decltype(func_name)> func_name##_ptr
#define OPENCL_EXPORTED_EXTENSION_FUNCTION(func_name) extern std::function<decltype(func_name)> func_name##_ptr
#include "open_cl_functions.inl"
#undef OPENCL_EXPORTED_FUNCTION
#undef OPENCL_EXPORTED_EXTENSION_FUNCTION
cl_platform_id load_opencl();
void unload_opencl();
#ifdef CL_FUNCTION_DEFINITIONS
# define clCreateContext clCreateContext_ptr
# define clGetDeviceIDs clGetDeviceIDs_ptr
# define clGetPlatformIDs clGetPlatformIDs_ptr
# define clCreateBuffer clCreateBuffer_ptr
# define clReleaseMemObject clReleaseMemObject_ptr
# define clCreateProgramWithSource clCreateProgramWithSource_ptr
# define clBuildProgram clBuildProgram_ptr
# define clCreateKernel clCreateKernel_ptr
# define clSetKernelArg clSetKernelArg_ptr
# define clEnqueueNDRangeKernel clEnqueueNDRangeKernel_ptr
# define clFlush clFlush_ptr
# define clFinish clFinish_ptr
# define clCreateCommandQueue clCreateCommandQueue_ptr
# define clReleaseContext clReleaseContext_ptr
# define clGetPlatformInfo clGetPlatformInfo_ptr
# define clGetExtensionFunctionAddressForPlatform clGetExtensionFunctionAddressForPlatform_ptr
# define clImportMemoryARM clImportMemoryARM_ptr
# define clCreateImageWithProperties clCreateImageWithProperties_ptr
# define clEnqueueWaitSemaphoresKHR clEnqueueWaitSemaphoresKHR_ptr
# define clEnqueueSignalSemaphoresKHR clEnqueueSignalSemaphoresKHR_ptr
# define clEnqueueAcquireExternalMemObjectsKHR clEnqueueAcquireExternalMemObjectsKHR_ptr
# define clEnqueueReleaseExternalMemObjectsKHR clEnqueueReleaseExternalMemObjectsKHR_ptr
# define clCreateSemaphoreWithPropertiesKHR clCreateSemaphoreWithPropertiesKHR_ptr
# define clReleaseSemaphoreKHR clReleaseSemaphoreKHR_ptr
# define clGetDeviceInfo clGetDeviceInfo_ptr
#endif