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

use*_*073 4 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;
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

小智 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"); 
} 
Run Code Online (Sandbox Code Playgroud)


Kyl*_*utz 5

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

对于其他人(例如clGetDeviceInfo()with CL_DEVICE_MAX_COMPUTE_UNITS),您只需要调用函数一次,因为您已经知道(在编译时)输出的大小(sizeof(cl_uint)).