Tag Archives: gpu

OpenCL Cookbook: Listing all platforms and their attributes

The first article in the OpenCL Cookbook series looks at how to list all platforms and their attributes in OpenCL using C as a host language on an OpenCL supported system.

For those new to OpenCL (like me) a platform is a top level entity in the OpenCL API and represents an SDK. You have to get a platform before you can delve deeper into what a platform provides access to such as devices (cpu, gpu). Depending on the hardware/GPU of a system may find an AMD, NVidia, Intel or Apple OpenCL SDK. You may even find multiple SDKs, for instance, if you have multiple GPUs of different makes.

#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif

int main() {

    int i, j;
    char* info;
    size_t infoSize;
    cl_uint platformCount;
    cl_platform_id *platforms;
    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 };
    const int attributeCount = sizeof(attributeNames) / sizeof(char*);

    // get platform count
    clGetPlatformIDs(5, NULL, &platformCount);

    // get all platforms
    platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
    clGetPlatformIDs(platformCount, platforms, NULL);

    // for each platform print all attributes
    for (i = 0; i < platformCount; i++) {

        printf("n %d. Platform 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: %sn", i+1, j+1, attributeNames[j], info);
            free(info);

        }

        printf("n");

    }

    free(platforms);
    return 0;

}

Compile and run on the Mac as follows. If you don’t have the clang, g++ or gcc commands install them. Any of those commands should work.

clang -framework OpenCL platforms.c -o platforms && ./platforms

The output produced on my Macbook Air is as follows but may differ for your system.

 1. Platform 
  1.1 Name       : Apple
  1.2 Vendor     : Apple
  1.3 Version    : OpenCL 1.2 (Jun 20 2012 14:18:19)
  1.4 Profile    : FULL_PROFILE
  1.5 Extensions : cl_APPLE_SetMemObjectDestructor cl_APPLE_ContextLoggingFunctions cl_APPLE_clut cl_APPLE_query_kernel_names cl_APPLE_gl_sharing cl_khr_gl_event

The code has been tested only on my Macbook Air but should work on Windows and Linux too though the way you compile and run will differ from above slightly. If you find any issues or would like to suggest improvements to the program (given that I’m not a C programmer) then please let me know in the comments. If you would like a step by step dissected guide to the above program explaining what it’s doing let me know and if there’s enough demand I’ll do a breakdown in another post.

Intel Sandy Bridge announced

The eagerly awaited Intel Sandy Bridge processors have finally been announced (yesterday) and have received superb reviews. Read more at Macrumors, Engadget, TechReport, Intel, Intel Blogs (an older link). They feature, amongst overall improvements on all fronts, vastly improved graphics performance and battery life. These really can’t be found on the mac line soon enough. No doubt Apple will be touting 15 hours battery life with these if they’re touting 10 hours now. Mid year release, I reckon, along with Lion though I’d like to see those on the MacBook Air more than any other model as they are, without a doubt, best of breed now. To quote Intel on the most significant feature of this release in my opinion:

Improved Cores with Innovative Ring Interconnect: The 2nd generation Intel Core Processor family microarchitecture features vastly improved cores that are better connected with an innovative ring interconnect for improved data bandwidth, performance and power efficiency. The ring interconnect is a high bandwidth, low latency modular on-die system for connection between processor components for improved performance. The ring interconnect enables high speed and low latency communication between the upgraded processor cores, processor graphics, and other integrated components such as memory controller and display.