Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to call clGetPlatformInfo?

Tags:

opencl

Right now I am calling clGetPlatformInfo twice. The first time to get the size of the result and the second to actually get the result. If I want to get 20 pieces of information that means I have to call 40 times (80 lines of code). Is there a better way of doing this?

clGetPlatformInfo example

    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 example

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;
like image 567
user1873073 Avatar asked Dec 15 '22 09:12

user1873073


2 Answers

Maybe a tad bit late but...I suggest something like...

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"); 
} 
like image 64
beteljuse Avatar answered Apr 18 '23 06:04

beteljuse


Nope, that is the correct way to use the clGetPlatformInfo() function. The size of the returned string is only known at run-time.

For others (such as clGetDeviceInfo() with CL_DEVICE_MAX_COMPUTE_UNITS) you would only have to call the function once as you already know (at compile-time) the size of the output (sizeof(cl_uint)).

like image 41
Kyle Lutz Avatar answered Apr 18 '23 08:04

Kyle Lutz