clGetPlatformIDs返回两个平台,但它们是相同的

时间:2012-01-23 09:49:46

标签: c++ opencl

要创建opencl应用程序,第一步是使用

获取平台
clGetPlatformIDs 

我从功能返回的平台有问题;函数返回我有2个平台但是当我检查它时我发现我有一个平台但它是重复的!!

源代码

struct PLATFORM
{
    cl_platform_id _Platforms ;
    map <cl_platform_info , char*> _Platforms_info ;        
};

cl_int error ; 
cl_uint temp_num_platforms ;

error = clGetPlatformIDs (NULL , NULL , &temp_num_platforms );

if ( error != CL_SUCCESS )
{
    /* create error and debug function*/
    cout << " error detect platforms " << endl << endl ;
}
else
{
    cout << " we  detect " << temp_num_platforms << " platforms " << endl << endl ;

    _Platforms = std::unique_ptr < PLATFORM [] > ( new PLATFORM [temp_num_platforms] ) ;

    for ( unsigned int num_platforms = 1 ; num_platforms <= temp_num_platforms ; num_platforms++ )
    {   
        // get platforms    
        error = clGetPlatformIDs (num_platforms ,&_Platforms[num_platforms-1]._Platforms , NULL );

        if ( error != CL_SUCCESS || _Platforms[num_platforms-1]._Platforms == NULL )
        {
            cout << " error get platform "  <<  num_platforms - 1 << endl << endl;
        }
        else
        {
            cout << " OK ! we detect "<< num_platforms << " platform " << endl << endl ;
        }
    }
}

if ( _Platforms[0]._Platforms == _Platforms[1]._Platforms )
{
    cout << " we have two platforms" << endl << endl ;
}

1 个答案:

答案 0 :(得分:7)

您对安装平台的评价不多。我的猜测是你已经从某个供应商安装了多个版本的OpenCL SDK。那,或者你遇到了一个bug。尝试下面的程序,该程序打印出系统上报告的所有平台的供应商,名称和版本。它可以帮助您更好地理解您的问题。

// You might need to change this header based on your install:
#include <OpenCL/cl.h>
#include <stdio.h>
#include <stdlib.h>

static void check_error(cl_int error, char* name) {
    if (error != CL_SUCCESS) {
        fprintf(stderr, "Non-successful return code %d for %s.  Exiting.\n", error, name);
        exit(1);
    }
}

int main (int argc, char const *argv[])
{
    cl_uint i;
    cl_int err;

    // Discover the number of platforms:
    cl_uint nplatforms;
    err = clGetPlatformIDs(0, NULL, &nplatforms);
    check_error(err, "clGetPlatformIds");

    // Now ask OpenCL for the platform IDs:
    cl_platform_id* platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id) * nplatforms);
    err = clGetPlatformIDs(nplatforms, platforms, NULL);
    check_error(err, "clGetPlatformIds");

    // Ask OpenCL about each platform to understand the problem:
    char name[128];
    char vendor[128];
    char version[128];

    fprintf(stdout, "OpenCL reports %d platforms.\n\n", nplatforms);

    for (i = 0; i < nplatforms; i++) {
        err |= clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, 128, vendor, NULL);
        err |= clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 128, name, NULL);
        err |= clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION, 128, version, NULL);
        check_error(err, "clGetPlatformInfo");

        fprintf(stdout, "Platform %d: %s %s %s\n", i, vendor, name, version);
    }

    free(platforms);
    return 0;
}

如果您看到两个相同的供应商名称版本字符串,那么这是一个错误。将文件与您的OpenCL供应商一起提交,他们会感谢您的!