4

现在我正在调用 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;
4

2 回答 2

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"); 
} 
于 2014-09-19T04:59:53.350 回答
5

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

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

于 2013-06-21T17:51:40.687 回答