Skip to content

Instantly share code, notes, and snippets.

@Foadsf
Last active June 21, 2020 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Foadsf/3ae03a58da81abfaaef4a0fde606701f to your computer and use it in GitHub Desktop.
Save Foadsf/3ae03a58da81abfaaef4a0fde606701f to your computer and use it in GitHub Desktop.
small clinfo, fetching OpenCL platfroms and theri device's infomation
cmake_minimum_required(VERSION 3.1)
project(OpenCL_Example)
find_package(OpenCL REQUIRED)
include_directories(${OpenCL_INCLUDE_DIRS})
link_directories(${OpenCL_LIBRARY})
add_executable(main main.c)
target_include_directories(main PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(main ${OpenCL_LIBRARY})
#define CL_TARGET_OPENCL_VERSION 120
#include <stdio.h>
#include <stdlib.h> // exit, EXIT_FAILURE
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
int main(int argc, char const *argv[]) {
cl_int err;
cl_uint numPlatforms = 0;
cl_platform_id *platforms;
err = clGetPlatformIDs(0, NULL, &numPlatforms);
if (err != CL_SUCCESS || numPlatforms == 0) {
printf("Couldn't find any platforms");
return EXIT_FAILURE;
}
printf("Number of platforms: %d\n", numPlatforms);
printf("----------------------\n");
platforms = (cl_platform_id *)malloc(sizeof(cl_platform_id) * numPlatforms);
err = clGetPlatformIDs(numPlatforms, platforms, NULL);
if (err != CL_SUCCESS) {
printf("Couldn't get platform's IDs");
return EXIT_FAILURE;
}
cl_uint *numDevices = (cl_uint *)malloc(sizeof(cl_uint) * numPlatforms);
cl_device_id **devices =
(cl_device_id **)malloc(sizeof(cl_device_id *) * numPlatforms);
cl_platform_info platformParams[5] = {
CL_PLATFORM_VENDOR, CL_PLATFORM_NAME, CL_PLATFORM_VERSION,
CL_PLATFORM_PROFILE, CL_PLATFORM_EXTENSIONS};
char *platfromParamNames[5] = {"Vendor", "Name", "Version", "", ""};
for (size_t i = 0; i < numPlatforms; i++) {
printf("Platform %zu:\n", (i + 1));
char *paramValue;
size_t paramValueSize;
for (size_t j = 0; j < 3; j++) {
err = clGetPlatformInfo(platforms[i], platformParams[j], 0, NULL,
&paramValueSize);
if (err != CL_SUCCESS) {
printf("Couldn't get platform's information size");
return EXIT_FAILURE;
}
paramValue = (char *)malloc(sizeof(char) * paramValueSize);
err = clGetPlatformInfo(platforms[i], platformParams[j], paramValueSize,
paramValue, NULL);
if (err != CL_SUCCESS) {
printf("Couldn't get platform's information");
return EXIT_FAILURE;
}
printf("%s: %s\n", platfromParamNames[j], paramValue);
free(paramValue);
}
err = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL,
&numDevices[i]);
if (err != CL_SUCCESS || numDevices[i] == 0) {
printf("Couldn't find any devices in %zu-th platform", i);
return EXIT_FAILURE;
}
printf("platfrom %lu-th has %u devices\n", (i + 1), numDevices[i]);
printf("----------------------\n");
devices[i] = (cl_device_id *)malloc(sizeof(cl_device_id) * numDevices[i]);
err = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, numDevices[i],
devices[i], NULL);
if (err != CL_SUCCESS) {
printf("Couldn't fetch device IDs in %zu-th platform", i);
return EXIT_FAILURE;
}
cl_device_type deviceType;
char *deviceTypes[] = {"CPU", "GPU", "ACCELERATOR"};
cl_uint deviceUintQuery;
cl_ulong deviceUlonglongQuery;
size_t *deviceSizetArrayQuery;
size_t deviceSizetQuery;
for (size_t j = 0; j < numDevices[i]; j++) {
printf("\t Device %lu:\n", (j + 1));
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_TYPE,
sizeof(cl_device_type), &deviceType, NULL);
printf("\t Type: %s \n", deviceTypes[(deviceType >> 1) - 1]);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_VENDOR, 0, NULL,
&paramValueSize);
paramValue = (char *)malloc(sizeof(char) * paramValueSize);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_VENDOR, paramValueSize,
paramValue, NULL);
printf("\t Vendor: %s \n", paramValue);
free(paramValue);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_VENDOR_ID, sizeof(cl_uint),
&deviceUintQuery, NULL);
printf("\t Vendor identifier: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_MAX_COMPUTE_UNITS,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t Max compute units: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t Max work item dimensions: %u \n", deviceUintQuery);
deviceSizetArrayQuery =
(size_t *)malloc(sizeof(size_t) * deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_MAX_WORK_ITEM_SIZES,
deviceUintQuery * sizeof(size_t),
deviceSizetArrayQuery, NULL);
printf("\t Max work item sizes: %zu ", deviceSizetArrayQuery[0]);
for (size_t k = 1; k < deviceUintQuery; k++) {
printf("X %zu ", deviceSizetArrayQuery[k]);
}
printf("\n");
free(deviceSizetArrayQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_MAX_WORK_GROUP_SIZE,
sizeof(size_t), &deviceSizetQuery, NULL);
printf("\t Max work group size: %zu \n", deviceSizetQuery);
printf("\t Preffered vector width:\n");
err =
clGetDeviceInfo(devices[i][j], CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - char: %u \n", deviceUintQuery);
err =
clGetDeviceInfo(devices[i][j], CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - short: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - int: %u \n", deviceUintQuery);
err =
clGetDeviceInfo(devices[i][j], CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - long: %u \n", deviceUintQuery);
err =
clGetDeviceInfo(devices[i][j], CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - float: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j],
CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - double: %u \n", deviceUintQuery);
err =
clGetDeviceInfo(devices[i][j], CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - half: %u \n", deviceUintQuery);
printf("\t Native vector width:\n");
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - char: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - short: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_NATIVE_VECTOR_WIDTH_INT,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - int: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - long: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - float: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - double: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t \t - half: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_MAX_CLOCK_FREQUENCY,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t Max clock frequency: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_ADDRESS_BITS,
sizeof(cl_uint), &deviceUintQuery, NULL);
printf("\t Address bits: %u \n", deviceUintQuery);
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_MAX_MEM_ALLOC_SIZE,
sizeof(cl_ulong), &deviceUlonglongQuery, NULL);
printf("\t Max memory allocation size: %u MByte\n",
(unsigned int)(deviceUlonglongQuery / (1024 * 1024)));
err = clGetDeviceInfo(devices[i][j], CL_DEVICE_GLOBAL_MEM_SIZE,
sizeof(cl_ulong), &deviceUlonglongQuery, NULL);
printf("\t Global memory size: %u MByte\n",
(unsigned int)(deviceUlonglongQuery / (1024 * 1024)));
printf("\t ----------------------\n");
}
// free(devices[i]);
}
for (size_t i = 0; i < numPlatforms; i++) {
free(devices[i]);
}
free(devices);
free(numDevices);
free(platforms);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment