Last active
June 11, 2018 18:37
-
-
Save CptFoobar/bcb513d87e574e69c2db to your computer and use it in GitHub Desktop.
Quick command line utility to list OpenCL compatible devices, along with their important attributes. (OpenCL must be previously installed)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Program to list all attached devices that support OpenCL, along with their key | |
* attributes. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#ifdef __APPLE__ | |
#include <OpenCL/opencl.h> | |
#else | |
#include <CL/cl.h> | |
#endif | |
/* Get the type string of device, given cl_device_type */ | |
char* getType(cl_device_type type) { | |
if(type == 1) return "CL_DEVICE_TYPE_DEFAULT"; | |
else if(type == 2) return "CL_DEVICE_TYPE_CPU"; | |
else if(type == 4) return "CL_DEVICE_TYPE_GPU"; | |
else if(type == 8) return "CL_DEVICE_TYPE_ACCELERATOR"; | |
} | |
void printUsage() { | |
printf("Usage: lsgpu [OPTION]\n"); | |
printf("Options: \n\t-gpu : Display GPU device only"); | |
printf("\n\t-h : Display this help message\n"); | |
} | |
/* | |
* http://dhruba.name/2012/08/14/opencl-cookbook-listing-all-devices-and-their-critical-attributes/ | |
*/ | |
int main(int argc, char* argv[]) { | |
int i, j, ret, gpuOnly = 0; | |
char* value; | |
size_t valueSize; | |
cl_uint platformCount; | |
cl_platform_id* platforms; | |
cl_uint deviceCount; | |
cl_device_id* devices; | |
cl_uint maxComputeUnits; | |
cl_uint maxClockFreq; | |
cl_bool boolean; | |
cl_device_type type; | |
cl_ulong memSize; | |
if (argc > 2) { | |
printUsage(); | |
return 0; | |
} | |
if (argc == 2) { | |
if (strcmp(argv[1], "-h") == 0) { | |
printUsage(); | |
return 0; | |
} | |
} | |
if (argc == 2) | |
if (strcmp(argv[1], "-gpu") == 0) | |
gpuOnly= 1; | |
// get all platforms | |
ret = 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 type | |
clGetDeviceInfo(devices[j], CL_DEVICE_TYPE, | |
sizeof(type), &type, NULL); | |
if (type != 4 & gpuOnly) continue; | |
// 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 vendor name | |
clGetDeviceInfo(devices[j], CL_DEVICE_VENDOR, 0, NULL, &valueSize); | |
value = (char*) malloc(valueSize); | |
clGetDeviceInfo(devices[j], CL_DEVICE_VENDOR, valueSize, value, NULL); | |
printf(" %d.%d Vendor: %s\n", j+1, 1, value); | |
free(value); | |
printf(" %d.%d Type: %s\n", j + 1, 2, getType(type)); | |
// 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, 3, 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, 4, value); | |
free(value); | |
// print opencl 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 version: %s\n", j+1, 5, value); | |
free(value); | |
// print if device is little endian | |
clGetDeviceInfo(devices[j], CL_DEVICE_ENDIAN_LITTLE, | |
sizeof(boolean), &boolean, NULL); | |
printf(" %d.%d Little Endian: %s\n", j+1, 6, boolean ? "Yes" : "No"); | |
// print max frequency | |
clGetDeviceInfo(devices[j], CL_DEVICE_MAX_CLOCK_FREQUENCY, | |
sizeof(maxClockFreq), &maxClockFreq, NULL); | |
printf(" %d.%d Max Clock frequency: %dMHz\n", j+1, 7, maxClockFreq); | |
// print if device is little endian | |
clGetDeviceInfo(devices[j], CL_DEVICE_IMAGE_SUPPORT, | |
sizeof(boolean), &boolean, NULL); | |
printf(" %d.%d Image support available: %s\n", j+1, 8, boolean ? "Yes" : "No"); | |
// 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, 9, maxComputeUnits); | |
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
To compile, use:
gcc -I/opt/AMDAPPSDK-3.0/include -L/opt/AMDAPPSDK-3.0/lib/x86_64 lsgpu.o -o lsgpu -lOpenCL