Skip to content
systemcoding.dev
GPU

List platforms with clGetPlatformIDs

Call clGetPlatformIDs twice, once for the count and once for the handles, then print each platform name with clGetPlatformInfo in OpenCL 3.0 C.

1 min read
Summarize in AI

This is how every OpenCL program starts: ask the installable client driver (ICD) loader which vendor implementations exist. Each driver on the machine appears as a platform, and the call is made twice, first to learn the count and then to fill an array of that size.

c
#define CL_TARGET_OPENCL_VERSION 300
#include <CL/cl.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    cl_uint n = 0;
    clGetPlatformIDs(0, NULL, &n);
    cl_platform_id *p = malloc(n * sizeof *p);
    clGetPlatformIDs(n, p, NULL);

    for (cl_uint i = 0; i < n; i++) {
        char name[128];
        clGetPlatformInfo(p[i], CL_PLATFORM_NAME, sizeof name, name, NULL);
        printf("platform %u: %s\n", i, name);
    }
    free(p);
}
Text
platform 0: NVIDIA CUDA

That output is from a machine with one NVIDIA driver installed; a laptop with an Intel and an AMD runtime prints two lines, and a machine with no ICDs prints nothing. Link with -lOpenCL.

CL_TARGET_OPENCL_VERSION matters. Without it the Khronos headers target the newest version and warn about deprecated calls, and with 300 the 3.0 entry points are available while the older deprecated ones such as clCreateCommandQueue still compile with a warning. Production code checks every return value; when the loader finds no driver the first call returns CL_PLATFORM_NOT_FOUND_KHR (-1001), which is the usual cause of "OpenCL works on my other machine".

Taggedcopencl
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.