核心對象與屬性
對象/屬性 | 描述 | 示例 |
---|
cl::Platform | 表示OpenCL平臺 | cl::Platform::get(&platforms) |
cl::Device | 表示計算設備 | cl::Device::getDefault() |
cl::Context | 管理設備、內存和命令隊列的上下文 | cl::Context(contextDevices) |
cl::CommandQueue | 命令隊列,用于提交命令 | cl::CommandQueue(context, device) |
cl::Program | OpenCL程序對象 | cl::Program(context, sources) |
cl::Kernel | 內核函數對象 | cl::Kernel(program, "kernel_name") |
cl::Buffer | 內存緩沖區對象 | cl::Buffer(context, flags, size) |
cl::Image1D/Image2D等 | 圖像對象 | cl::Image1D(context, flags, format, width, host_ptr) |
cl::Pipe | 管道對象 | cl::Pipe(context, flags, packet_size, max_packets) |
Platform 屬性與函數
Platform 對象代表 OpenCL 實現平臺,通常對應不同的硬件供應商(如 NVIDIA、AMD、Intel 等)。選擇適當的平臺對應用性能有重要影響。
Platform 對象創建與獲取
對象/函數 | 描述 | 示例 |
---|
cl::Platform | Platform 對象 | cl::Platform::get(&platforms) |
cl::Platform::get(vector<Platform>*) | 獲取所有可用平臺 | cl::Platform::get(&platformList) |
cl::Platform::getDefault() | 獲取默認平臺 | cl::Platform platform = cl::Platform::getDefault() |
Platform 信息查詢函數
函數 | 描述 | 返回類型 |
---|
cl::Platform::getInfo<info_type>(param_name) | 獲取平臺信息 | 取決于查詢參數 |
cl::Platform::getDevices(device_type, vector<Device>*) | 獲取平臺關聯的設備 | vector<cl::Device> |
Platform 查詢參數
查詢參數 | 描述 | 返回類型 |
---|
CL_PLATFORM_PROFILE | 平臺支持的OpenCL規范 | string |
CL_PLATFORM_VERSION | OpenCL版本信息 | string |
CL_PLATFORM_NAME | 平臺名稱 | string |
CL_PLATFORM_VENDOR | 平臺供應商 | string |
CL_PLATFORM_EXTENSIONS | 支持的擴展 | string |
CL_PLATFORM_ICD_SUFFIX_KHR | ICD后綴(需要擴展) | string |
Platform 擴展函數
函數 | 描述 | 備注 |
---|
cl::Platform::getHostTimerResolution() | 獲取主機計時器分辨率(OpenCL 2.1+) | 需要 cl_khr_host_timer_resolution 擴展 |
cl::Platform::unloadCompiler() | 卸載編譯器資源 | |
Platform 屬性常量
常量 | 值 | 描述 |
---|
CL_PLATFORM_PROFILE_FULL | "FULL_PROFILE" | 完整規范支持 |
CL_PLATFORM_PROFILE_EMBEDDED | "EMBEDDED_PROFILE" | 嵌入式規范支持 |
示例代碼
cpp
// 1. 獲取所有平臺
vector<cl::Platform> platforms;
cl::Platform::get(&platforms);// 2. 遍歷平臺信息
for (auto &platform : platforms) {string name = platform.getInfo<CL_PLATFORM_NAME>();string vendor = platform.getInfo<CL_PLATFORM_VENDOR>();string version = platform.getInfo<CL_PLATFORM_VERSION>();cout << "Platform: " << name << "\n"<< "Vendor: " << vendor << "\n"<< "Version: " << version << endl;// 3. 獲取設備列表vector<cl::Device> devices;platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);// 4. 檢查擴展支持string extensions = platform.getInfo<CL_PLATFORM_EXTENSIONS>();if (extensions.find("cl_khr_icd") != string::npos) {cout << "ICD extension supported" << endl;}
}// 5. 獲取默認平臺
cl::Platform defaultPlatform = cl::Platform::getDefault();// 6. 卸載編譯器(減少資源占用)
defaultPlatform.unloadCompiler();
平臺選擇實用技巧
cpp
// 1. 按供應商選擇平臺
cl::Platform selectPlatform(const string &vendor) {vector<cl::Platform> platforms;cl::Platform::get(&platforms);for (auto &platform : platforms) {string platformVendor = platform.getInfo<CL_PLATFORM_VENDOR>();if (platformVendor.find(vendor) != string::npos) {return platform;}}throw runtime_error("No platform found for vendor: " + vendor);
}// 2. 檢查平臺版本
bool checkPlatformVersion(cl::Platform &platform, int major, int minor) {string version = platform.getInfo<CL_PLATFORM_VERSION>();int platMajor, platMinor;sscanf(version.c_str(), "OpenCL %d.%d", &platMajor, &platMinor);return (platMajor > major) || (platMajor == major && platMinor >= minor);
}
錯誤處理
錯誤碼 | 描述 |
---|
CL_INVALID_PLATFORM | 無效的平臺對象 |
CL_INVALID_VALUE | 無效的查詢參數 |
CL_OUT_OF_HOST_MEMORY | 主機內存不足 |
Device
屬性與函數
Device 對象代表 OpenCL 可用的計算設備,了解設備屬性和能力對于優化 OpenCL 程序性能至關重要。實際編程中應根據設備特性調整內核參數和工作組大小。
Device 對象獲取
對象/函數 | 描述 | 示例 |
---|
cl::Device | Device 對象 | cl::Device::getDefault() |
cl::Platform::getDevices(device_type, devices) | 獲取平臺關聯的設備 | platform.getDevices(CL_DEVICE_TYPE_GPU, &devices) |
cl::Device::getDefault() | 獲取默認設備 | cl::Device device = cl::Device::getDefault() |
設備信息查詢函數
函數 | 描述 |
---|
cl::Device::getInfo<info_type>(param_name) | 獲取設備信息 |
cl::Device::getSupportedImageFormats(context, flags, image_type) | 獲取支持的圖像格式 |
設備類型常量
常量 | 描述 |
---|
CL_DEVICE_TYPE_CPU | CPU 設備 |
CL_DEVICE_TYPE_GPU | GPU 設備 |
CL_DEVICE_TYPE_ACCELERATOR | 加速器設備 |
CL_DEVICE_TYPE_CUSTOM | 自定義設備 |
CL_DEVICE_TYPE_ALL | 所有設備類型 |
設備基本信息查詢
查詢參數 | 描述 | 返回類型 |
---|
CL_DEVICE_NAME | 設備名稱 | string |
CL_DEVICE_VENDOR | 設備供應商 | string |
CL_DEVICE_VERSION | OpenCL 版本 | string |
CL_DRIVER_VERSION | 驅動版本 | string |
CL_DEVICE_PROFILE | 支持規范(FULL/EMBEDDED) | string |
CL_DEVICE_TYPE | 設備類型 | cl_device_type |
設備能力信息
查詢參數 | 描述 | 返回類型 |
---|
CL_DEVICE_MAX_COMPUTE_UNITS | 計算單元數量 | cl_uint |
CL_DEVICE_MAX_CLOCK_FREQUENCY | 最大時鐘頻率(MHz) | cl_uint |
CL_DEVICE_ADDRESS_BITS | 地址寬度(位) | cl_uint |
CL_DEVICE_AVAILABLE | 設備是否可用 | cl_bool |
CL_DEVICE_COMPILER_AVAILABLE | 編譯器是否可用 | cl_bool |
內存信息
查詢參數 | 描述 | 返回類型 |
---|
CL_DEVICE_GLOBAL_MEM_SIZE | 全局內存大小(字節) | cl_ulong |
CL_DEVICE_LOCAL_MEM_SIZE | 局部內存大小(字節) | cl_ulong |
CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE | 常量內存大小(字節) | cl_ulong |
CL_DEVICE_GLOBAL_MEM_CACHE_SIZE | 全局內存緩存大小 | cl_ulong |
CL_DEVICE_MAX_MEM_ALLOC_SIZE | 最大內存分配大小 | cl_ulong |
執行限制
查詢參數 | 描述 | 返回類型 |
---|
CL_DEVICE_MAX_WORK_GROUP_SIZE | 最大工作組大小 | size_t |
CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS | 最大工作項維度 | cl_uint |
CL_DEVICE_MAX_WORK_ITEM_SIZES | 各維度最大工作項大小 | vector<size_t> |
圖像支持
查詢參數 | 描述 | 返回類型 |
---|
CL_DEVICE_IMAGE_SUPPORT | 是否支持圖像 | cl_bool |
CL_DEVICE_IMAGE2D_MAX_WIDTH | 2D圖像最大寬度 | size_t |
CL_DEVICE_IMAGE2D_MAX_HEIGHT | 2D圖像最大高度 | size_t |
CL_DEVICE_IMAGE3D_MAX_WIDTH | 3D圖像最大寬度 | size_t |
CL_DEVICE_IMAGE3D_MAX_HEIGHT | 3D圖像最大高度 | size_t |
CL_DEVICE_IMAGE3D_MAX_DEPTH | 3D圖像最大深度 | size_t |
擴展功能
查詢參數 | 描述 | 返回類型 |
---|
CL_DEVICE_EXTENSIONS | 支持的擴展 | string |
CL_DEVICE_PREFERRED_VECTOR_WIDTH_<type> | 各種類型的向量寬度 | cl_uint |
設備分區相關(OpenCL 2.0+)
查詢參數 | 描述 |
---|
CL_DEVICE_PARTITION_MAX_SUB_DEVICES | 最大子設備數量 |
CL_DEVICE_PARTITION_PROPERTIES | 支持的分區類型 |
CL_DEVICE_PARTITION_AFFINITY_DOMAIN | 支持的親和域 |
實用函數示例
cpp
// 1. 獲取設備基本信息
void printDeviceInfo(cl::Device &device) {cout << "Device: " << device.getInfo<CL_DEVICE_NAME>() << "\n"<< "Vendor: " << device.getInfo<CL_DEVICE_VENDOR>() << "\n"<< "Version: "