You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenCL函数clGetPlatformInfo运行时故障求助(Win10+VS2012)

OpenCL程序调用clGetPlatformInfo时运行终止

环境配置

  • 硬件:Intel Core I7 6700、32GB内存、Nvidia GT1030
  • 软件:Windows 10 64位、Visual Studio 2012专业版
  • 参考书籍:《OpenCL Parallel Programming Development Cookbook》

问题现象

已将OpenCL.lib添加到链接器输入附加依赖项,程序编译成功,但运行时在首次调用clGetPlatformInfo的代码行终止:

error = clGetPlatformInfo(id, param_name, 0, NULL, &paramSize);

相关代码

#define CL_TARGET_OPENCL_VERSION 120
#include "windows.h"
#include <stdio.h>
#include <stdlib.h>
#include "CL/cl.h"
#include "CL/cl_platform.h"
#include "malloc.h"
#include "resconsts.h"

#include <iostream>
#include <fstream>
using namespace std;
static ofstream fout ("log_main_cpp.txt");

typedef unsigned char Byte;

void displayPlatformInfo(cl_platform_id id, cl_platform_info param_name,
  const char* paramNameAsStr) 
{
  cl_int error = 0;
  size_t paramSize = 0;
  error = clGetPlatformInfo(id, param_name, 0, NULL, &paramSize);  
  char* moreInfo = (char*) malloc(paramSize);
  error = clGetPlatformInfo(id, param_name, paramSize, moreInfo, NULL);
  if (error != CL_SUCCESS)
    fout << "Unable to find any OpenCL Platform information";
  fout << "paramNameAsStr = " << paramNameAsStr << endl 
    << "moreInfo = " << moreInfo << endl; 
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  LPSTR lpCmdLine, int nCmdShow) 
{
  cl_platform_id *platforms;
  cl_uint numOfPlatforms;
  cl_int error;
  error = clGetPlatformIDs(0, NULL, &numOfPlatforms);
  if (error < 0) {
    fout << "Unable to find any OpenCL platforms" << endl;
    exit(1);
  }
  // Allocate memory for the number of installed platforms.
  // alloca(...) occupies some stack space but is automatically freed on return.
  platforms = (cl_platform_id*) alloca (sizeof(cl_platform_id)*numOfPlatforms);
  fout << "Number of OpenCL platforms found: " << numOfPlatforms << endl;
  // We invoke the API 'clPlatformInfo' twice for each parameter we're trying 
  // to extract and we use the return value to create temporary data structures
  // (on the stack) to store the returned information on the second invocation.
  for (cl_uint i = 0; i < numOfPlatforms; i++) {
    displayPlatformInfo(platforms[i], CL_PLATFORM_PROFILE,
      "CL_PLATFORM_PROFILE");
    displayPlatformInfo(platforms[i], CL_PLATFORM_VERSION,
      "CL_PLATFORM_VERSION");
    displayPlatformInfo(platforms[i], CL_PLATFORM_NAME,
      "CL_PLATFORM_NAME");
    displayPlatformInfo(platforms[i], CL_PLATFORM_VENDOR,
      "CL_PLATFORM_VENDOR");
    displayPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS,
      "CL_PLATFORM_EXTENSIONS");
  }
  
  return 0;
}

解决方案
  1. 核心问题:未实际获取平台ID列表
    代码仅调用clGetPlatformIDs(0, NULL, &numOfPlatforms)获取了平台数量,但分配platforms内存后,没有调用clGetPlatformIDs(numOfPlatforms, platforms, NULL)填充平台ID数组。此时platforms内是未初始化的垃圾值,传入clGetPlatformInfo必然导致程序崩溃。

  2. 关键修复步骤
    在分配platforms内存后,添加代码获取实际的平台ID:

    platforms = (cl_platform_id*) alloca (sizeof(cl_platform_id)*numOfPlatforms);
    // 新增:获取平台ID列表
    error = clGetPlatformIDs(numOfPlatforms, platforms, NULL);
    if (error != CL_SUCCESS) {
        fout << "Failed to get OpenCL platform IDs" << endl;
        exit(1);
    }
    fout << "Number of OpenCL platforms found: " << numOfPlatforms << endl;
    
  3. 其他潜在问题修复

    • 检查malloc返回值:添加判断避免空指针访问:
      char* moreInfo = (char*) malloc(paramSize);
      if (!moreInfo) {
          fout << "Memory allocation failed for platform info" << endl;
          return;
      }
      
    • 释放内存:使用完moreInfo后调用free(moreInfo);,防止内存泄漏。
    • 完善错误处理:第一次调用clGetPlatformInfo后也需检查返回值,确保获取参数长度成功。

内容的提问来源于stack exchange,提问作者John Smit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 20:13:13