呼叫ClgetPlatFormids时,OpenCl返回-64

OpenCL returning -64 upon calling clGetPlatformIDs

本文关键字:返回 OpenCl ClgetPlatFormids 呼叫      更新时间:2023-10-16

编辑:错误代码为以下

CL_INVALID_PROPERTY if context property name in properties is not a 
supported property name, if the value specified for a supported property 
name is not valid, or if the same property name is specified more than once.
However if the extension cl_khr_gl_sharing is enabled, then 
CL_INVALID_PROPERTY is returned if an attribute name other than those listed
in the table for properties above is specified in properties.

我正在使用mingw(x86)和x86 amd opencl libary,以下代码在最终检查之后返回null:

cl_context CreateContext()
{
     cl_int errNum;
     cl_uint numPlatforms;
     cl_platform_id firstPlatformID;
     cl_context context=NULL;
     //Select an OpenCL platform
     errNum=clGetPlatformIDs(1, &firstPlatformID, &numPlatforms);
     if (errNum!=CL_SUCCESS||numPlatforms<=0)
     {
        cerr<<"Failed to find any OpenCL platforms."<<endl;
        return NULL;
     }
     cl_context_properties contextProperties[]=
     {
        CL_CONTEXT_PLATFORM,
        (cl_context_properties) firstPlatformID,
     };
     context=clCreateContextFromType(contextProperties, CL_DEVICE_TYPE_GPU, NULL, NULL, &errNum);
     cout << errNum << endl;
     if (errNum!=CL_SUCCESS)
     {
         cerr<<"Failed to create an OpenCL GPU context, trying CPU."<<endl;
         context=clCreateContextFromType(contextProperties,CL_DEVICE_TYPE_CPU, NULL, NULL, &errNum);
         if (errNum!=CL_SUCCESS)
         {
            cerr<<"Failed to create an OpenCL GPU or CPU context."<<endl;
            return NULL;
         }
     }
     return context;
}

它可以检测到2个平台(Intel CPU,AMD GPU),但无法创建上下文。X64 Libary做同样的事情。有人知道如何修复它吗?

根据clcreatecontext的文档,您必须终止以零的cl_context_properties列表。

随着以下更改,代码对我有用:

cl_context_properties contextProperties[] =
{
    CL_CONTEXT_PLATFORM,
    (cl_context_properties)firstPlatformID, 
    0
};