Skip to content

Instantly share code, notes, and snippets.

@enkore
Last active November 5, 2016 15:59
Show Gist options
  • Save enkore/2830117 to your computer and use it in GitHub Desktop.
Save enkore/2830117 to your computer and use it in GitHub Desktop.
Simple OpenCL enumerator
// I place this in the public domain
// Compile with
// <gcc/clang> <thisfile>.c -lOpenCL-Wall -Wextra -Wpedantic -std=c99
// Created a long time ago
// Updated, some memory leaks fixed and so on 2014-05-28
// Changed to real C instead of some quirky C++ casts inbetween (2014-05-28)
#include <CL/cl.h>
#include <CL/cl_platform.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
cl_platform_id *platforms;
cl_uint num_platforms;
clGetPlatformIDs(0, NULL, &num_platforms);
if(num_platforms == 0) {
printf("No OpenCL platforms found.\n");
return 1;
}
platforms = (cl_platform_id*)malloc(num_platforms * sizeof(*platforms));
clGetPlatformIDs(num_platforms, platforms, NULL);
for(cl_uint i = 0; i < num_platforms; i++) {
char name[512];
clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 512, name, 0);
printf("%s\n", name);
cl_device_id *devices;
cl_uint num_devices;
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices);
if(num_devices == 0) {
printf("No devices found for platform %s.", name);
continue;
}
devices = (cl_device_id*)malloc(num_devices * sizeof(*devices));
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
for(cl_uint j = 0; j < num_devices; j++) {
char devname[512];
char devext[8192];
cl_bool available;
cl_uint addrsize;
cl_long mem;
cl_device_type type;
clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 512, devname, 0);
clGetDeviceInfo(devices[j], CL_DEVICE_EXTENSIONS, 8192, devext, 0);
clGetDeviceInfo(devices[j], CL_DEVICE_AVAILABLE, sizeof(cl_bool), &available, 0);
clGetDeviceInfo(devices[j], CL_DEVICE_ADDRESS_BITS, sizeof(cl_uint), &addrsize, 0);
clGetDeviceInfo(devices[j], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(cl_long), &mem, 0);
clGetDeviceInfo(devices[j], CL_DEVICE_TYPE, sizeof(cl_device_type), &type, 0);
mem /= 1048576;
const char *avail = available ? "yes" : "no";
const char *typestr;
switch(type) {
case CL_DEVICE_TYPE_CPU:
typestr = "CPU";
break;
case CL_DEVICE_TYPE_ACCELERATOR:
typestr = "accelerator";
break;
case CL_DEVICE_TYPE_GPU:
typestr = "GPU";
break;
default:
typestr = "<unknown>";
}
printf("\t%s (%s)\n", devname, typestr);
printf("\t\tavailable: %s\n\t\taddr: %u bits\n\t\tmemory: %ld MiB\n", avail, addrsize, mem);
printf("\t\t%s\n", devext);
printf("\n");
}
free(devices);
}
free(platforms);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment