Skip to content

Instantly share code, notes, and snippets.

@courtneyfaulkner
Created December 11, 2013 22:20
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save courtneyfaulkner/7919509 to your computer and use it in GitHub Desktop.
Save courtneyfaulkner/7919509 to your computer and use it in GitHub Desktop.
List OpenCL platforms and devices
#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
int main() {
int i, j;
char* value;
size_t valueSize;
cl_uint platformCount;
cl_platform_id* platforms;
cl_uint deviceCount;
cl_device_id* devices;
cl_uint maxComputeUnits;
// get all platforms
clGetPlatformIDs(0, NULL, &platformCount);
platforms = (cl_platform_id*) malloc(sizeof(cl_platform_id) * platformCount);
clGetPlatformIDs(platformCount, platforms, NULL);
for (i = 0; i < platformCount; i++) {
// get all devices
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &deviceCount);
devices = (cl_device_id*) malloc(sizeof(cl_device_id) * deviceCount);
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, deviceCount, devices, NULL);
// for each device print critical attributes
for (j = 0; j < deviceCount; j++) {
// print device name
clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 0, NULL, &valueSize);
value = (char*) malloc(valueSize);
clGetDeviceInfo(devices[j], CL_DEVICE_NAME, valueSize, value, NULL);
printf("%d. Device: %s\n", j+1, value);
free(value);
// print hardware device version
clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, 0, NULL, &valueSize);
value = (char*) malloc(valueSize);
clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, valueSize, value, NULL);
printf(" %d.%d Hardware version: %s\n", j+1, 1, value);
free(value);
// print software driver version
clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, 0, NULL, &valueSize);
value = (char*) malloc(valueSize);
clGetDeviceInfo(devices[j], CL_DRIVER_VERSION, valueSize, value, NULL);
printf(" %d.%d Software version: %s\n", j+1, 2, value);
free(value);
// print c version supported by compiler for device
clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, 0, NULL, &valueSize);
value = (char*) malloc(valueSize);
clGetDeviceInfo(devices[j], CL_DEVICE_OPENCL_C_VERSION, valueSize, value, NULL);
printf(" %d.%d OpenCL C version: %s\n", j+1, 3, value);
free(value);
// print parallel compute units
clGetDeviceInfo(devices[j], CL_DEVICE_MAX_COMPUTE_UNITS,
sizeof(maxComputeUnits), &maxComputeUnits, NULL);
printf(" %d.%d Parallel compute units: %d\n", j+1, 4, maxComputeUnits);
}
free(devices);
}
free(platforms);
return 0;
}
#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: %s\n", i+1, j+1, attributeNames[j], info);
free(info);
}
printf("\n");
}
free(platforms);
return 0;
}
@courtneyfaulkner
Copy link
Author

List OpenCL platforms and devices:

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

(From http://dhruba.name/2012/08/14/opencl-cookbook-listing-all-devices-and-their-critical-attributes/ and http://dhruba.name/2012/08/13/opencl-cookbook-listing-all-platforms-and-their-attributes/)

@aasten
Copy link

aasten commented Feb 20, 2017

Thank you for this!

@sinetek
Copy link

sinetek commented Jan 15, 2018

Thank you for this!

@bw2012
Copy link

bw2012 commented Apr 7, 2018

thanks

@Foadsf
Copy link

Foadsf commented May 11, 2018

@courtneyfaulkner may I ask why you have used size_t type for valueSize but cl_uint for the rest of variables. Aren't they kind of the same (unsigned 32-bit integer)?

@CHEWKOKWEI
Copy link

thank you very much!

@tholu
Copy link

tholu commented Oct 23, 2018

Any idea why this happens?

$ clang platforms.c -o platforms -framework OpenCL && ./platforms
/usr/bin/ld: -f may not be used without -shared
clang: error: linker command failed with exit code 1 (use -v to see invocation)

$ clang -v
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)

@eromero-vlc
Copy link

For Linux, try this:

clang devices.c -o devices -lOpenCL
clang platforms.c -o platforms -lOpenCL

@bw2012
Copy link

bw2012 commented Feb 3, 2019

without clang:

gcc -o devices devices.c -lOpenCL
gcc -o platforms platforms.c -lOpenCL

@harieamjari
Copy link

For ARM, I compiled it as:

$  gcc platforms.c -L/system/lib/egl -lGLES_mali -o platforms

@dnandz
Copy link

dnandz commented Jan 6, 2021

For Linux, try this:

clang devices.c -o devices -lOpenCL
clang platforms.c -o platforms -lOpenCL

must be install sudo apt install clang

@TianZhao-007
Copy link

Thanks, pretty useful!

@vmiheer
Copy link

vmiheer commented Sep 16, 2021

Thank you for this small improvement would be error checking :).

@dyd1234
Copy link

dyd1234 commented Mar 11, 2023

Thanks in advance, it is really helpful!

@sathisceg
Copy link

Hi @courtneyfaulkner
Thank you for sharing.

@mkohlhaas
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment