Skip to content

Instantly share code, notes, and snippets.

@darcyliu darcyliu/clinfo.m
Created Aug 28, 2017

Embed
What would you like to do?
Retrieve OpenCL Information on OSX
//
// clinfo.m
// clinfo
//
// Created by Darcy Liu on 26/08/2017.
// Copyright © 2017 Darcy Liu. All rights reserved.
//
// clang -fobjc-arc -fmodules clinfo.m -o clinfo
#import <Foundation/Foundation.h>
#import <OpenCL/OpenCL.h>
void print_device_info(cl_device_id device) {
char cBuffer[1024];
clGetDeviceInfo(device, CL_DEVICE_VENDOR, sizeof(cBuffer), &cBuffer, NULL);
printf("\t Vendor : %s\n", cBuffer);
clGetDeviceInfo(device, CL_DRIVER_VERSION, sizeof(cBuffer), &cBuffer, NULL);
printf("\t Version : %s\n", cBuffer);
cl_device_type type;
clGetDeviceInfo(device, CL_DEVICE_TYPE, sizeof(type), &type, NULL);
switch (type) {
case CL_DEVICE_TYPE_CPU:
strcpy(cBuffer, "CPU");
break;
case CL_DEVICE_TYPE_GPU:
strcpy(cBuffer, "GPU");
break;
case CL_DEVICE_TYPE_ACCELERATOR:
strcpy(cBuffer, "Accelerator");
break;
case CL_DEVICE_TYPE_CUSTOM:
strcpy(cBuffer, "Custom");
break;
case CL_DEVICE_TYPE_ALL:
strcpy(cBuffer, "All");
break;
default:
strcpy(cBuffer, "Default");
break;
}
printf("\t Type : %s\n", cBuffer);
cl_uint compute_units;
clGetDeviceInfo(device, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(compute_units), &compute_units, NULL);
printf("\t Compute Units : %u\n", compute_units);
size_t workitem_dims;
clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(workitem_dims), &workitem_dims, NULL);
printf("\t Workitem Dims : %zu\n", workitem_dims);
size_t workitem_size[3];
clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(workitem_size), &workitem_size, NULL);
printf("\t Workitem Size : %zu / %zu / %zu\n", workitem_size[0], workitem_size[1], workitem_size[2]);
size_t workgroup_size;
clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(workgroup_size), &workgroup_size, NULL);
printf("\t Workgroup Size : %zu\n", workgroup_size);
cl_uint clock_frequency;
clGetDeviceInfo(device, CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(clock_frequency), &clock_frequency, NULL);
printf("\t Clock Frequency : %u MHz\n", clock_frequency);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
char cBuffer[1024];
cl_uint num_platforms;
cl_int err = clGetPlatformIDs(0, NULL, &num_platforms);
if (err != CL_SUCCESS) {
return -1;
}
cl_platform_id* clPlatformIDs = (cl_platform_id*)malloc(num_platforms * sizeof(cl_platform_id));
err = clGetPlatformIDs(num_platforms, clPlatformIDs, NULL);
if (err != CL_SUCCESS) {
return -1;
}
for (int i=0; i < num_platforms; i++) {
err = clGetPlatformInfo (clPlatformIDs[i], CL_PLATFORM_NAME, 1024, &cBuffer, NULL);
if (err != CL_SUCCESS) {
continue;
}
printf("Platform Name: %s\n", cBuffer);
cl_uint ciDeviceCount;
err = clGetDeviceIDs(clPlatformIDs[i], CL_DEVICE_TYPE_ALL, 0, NULL, &ciDeviceCount);
if (err != CL_SUCCESS) {
continue;
}
cl_device_id *devices = (cl_device_id*)malloc(sizeof(cl_device_id) * ciDeviceCount);
err = clGetDeviceIDs (clPlatformIDs[i], CL_DEVICE_TYPE_ALL, ciDeviceCount, devices, &ciDeviceCount);
if (err != CL_SUCCESS) {
continue;
}
printf("OpenCL Device Info: %u devices found.\n",ciDeviceCount);
for(int j=0; j<ciDeviceCount; j++) {
cl_device_id device = devices[j];
err = clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(cBuffer), &cBuffer, NULL);
if (err != CL_SUCCESS) {
continue;
}
printf("\nDevice %d: %s\n", j, cBuffer);
print_device_info(device);
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.