调用clGetPlatformInfo的正确方法是什么?

时间:2013-06-21 16:28:15

标签: opencl

现在我两次调用clGetPlatformInfo。第一次获得结果的大小,第二次实际获得结果。如果我想获得20条信息意味着我必须拨打40次(80行代码)。有没有更好的方法呢?

clGetPlatformInfo示例

    char *profile = NULL;
    size_t size;
    clGetPlatformInfo(platforms[0], CL_PLATFORM_PROFILE, NULL, profile, &size); // get size of profile char array
    profile = (char*)malloc(size);
    clGetPlatformInfo(platforms[0], CL_PLATFORM_PROFILE,size, profile, NULL); // get profile char array
    cout << profile << endl;

clGetDeviceInfo示例

size_t size;
char *vendor = NULL;
clGetDeviceInfo(devices[0], CL_DEVICE_VENDOR, NULL, NULL, &size);
vendor = (char*)malloc(sizeof(char)*size);
clGetDeviceInfo(devices[0], CL_DEVICE_VENDOR, size, vendor, NULL);
cout << vendor << endl;

2 个答案:

答案 0 :(得分:7)

也许有点晚了但是...我建议像...这样的东西。

const char* attributeNames[5] = { "Name", "Vendor", "Version", "Profile", "Extensions" };
const cl_platform_info attributeTypes[5] = { 
    CL_PLATFORM_NAME, 
                                            CL_PLATFORM_VENDOR,
                                            CL_PLATFORM_VERSION, 
                                            CL_PLATFORM_PROFILE, 
                                            CL_PLATFORM_EXTENSIONS };
    // ..then loop thru...

    // for each platform print all attributes
    printf("\nAttribute Count = %d ",attributeCount);
    for (i = 0; i < platformCount; i++) {


    printf("\nPlatform - %d\n ", i+1);

    for (j = 0; j < attributeCount; j++) {

        // get platform attribute value size
        clGetPlatformInfo(platforms[i], attributeTypes[j], 0, NULL, &infoSize);
        info = (char*) malloc(infoSize);

        // get platform attribute value
        clGetPlatformInfo(platforms[i], attributeTypes[j], infoSize, info, NULL);

        printf("  %d.%d %-11s: %s\n", i+1, j+1, attributeNames[j], info); 
    } 
    printf("\n\n"); 
} 

答案 1 :(得分:5)

不,这是使用clGetPlatformInfo()功能的正确方法。返回字符串的大小仅在运行时才知道。

对于其他人(例如带有clGetDeviceInfo()的{​​{1}}),您只需要调用该函数一次(在编译时)输出的大小(CL_DEVICE_MAX_COMPUTE_UNITS )。