#include "stdafx.h"
#include <CL/cl.h>
#include <memory>
#include <vector>
#include <iostream>
void displayPlatformInfo(cl_platform_id id, cl_platform_info param_name, const char* paramNameAsStr)
{
cl_int error = 0;
size_t paramSize = 0;
error = clGetPlatformInfo(id, param_name, 0, NULL, ¶mSize);
std::unique_ptr<char> moreInfo(new char[paramSize]);
error = clGetPlatformInfo(id, param_name, paramSize, moreInfo.get(), NULL);
if (error == CL_SUCCESS) {
std::cout << paramNameAsStr << " : " << moreInfo.get() << std::endl;
}
}
void displayDeviceDetails(cl_device_id id, cl_device_info param_name, const char* paramNameAsStr) {
cl_int error = 0;
size_t paramSize = 0;
error = clGetDeviceInfo(id, param_name, 0, NULL, ¶mSize);
if (error != CL_SUCCESS) {
perror("Unable to obtain device info for param\n");
return;
}
/* the cl_device_info are preprocessor directives defined in cl.h */
switch (param_name) {
case CL_DEVICE_TYPE: {
std::unique_ptr<cl_device_type> devType(new cl_device_type[paramSize]);
error = clGetDeviceInfo(id, param_name, paramSize, devType.get(), NULL);
if (error != CL_SUCCESS) {
perror("Unable to obtain device info for param\n");
return;
}
switch (*devType) {
case CL_DEVICE_TYPE_CPU: printf("CPU detected\n"); break;
case CL_DEVICE_TYPE_GPU: printf("GPU detected\n"); break;
case CL_DEVICE_TYPE_ACCELERATOR: printf("Accelerator detected\n"); break;
case CL_DEVICE_TYPE_DEFAULT: printf("default detected\n"); break;
}
} break;
case CL_DEVICE_VENDOR_ID:
case CL_DEVICE_MAX_COMPUTE_UNITS:
case CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS: {
std::unique_ptr<cl_uint> ret(new cl_uint[paramSize]);
error = clGetDeviceInfo(id, param_name, paramSize, ret.get(), NULL);
if (error != CL_SUCCESS) {
perror("Unable to obtain device info for param\n");
return;
}
switch (param_name) {
case CL_DEVICE_VENDOR_ID: printf("\tVENDOR ID: 0x%x\n", *ret); break;
case CL_DEVICE_MAX_COMPUTE_UNITS: printf("\tMaximum number of parallel compute units: %d\n", *ret); break;
case CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS: printf("\tMaximum dimensions for global/local work-item IDs: %d\n", *ret); break;
}
} break;
case CL_DEVICE_MAX_WORK_ITEM_SIZES: {
cl_uint maxWIDimensions;
std::unique_ptr<size_t> ret(new size_t[paramSize]);
error = clGetDeviceInfo(id, param_name, paramSize, ret.get(), NULL);
error = clGetDeviceInfo(id, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(cl_uint), &maxWIDimensions, NULL);
if (error != CL_SUCCESS) {
perror("Unable to obtain device info for param\n");
return;
}
printf("\tMaximum number of work-items in each dimension: ( ");
for (cl_uint i = 0; i < maxWIDimensions; ++i) {
printf("%d ", ret.get()[i]);
}
printf(" )\n");
} break;
case CL_DEVICE_MAX_WORK_GROUP_SIZE: {
std::unique_ptr<size_t> ret(new size_t[paramSize]);
error = clGetDeviceInfo(id, param_name, paramSize, ret.get(), NULL);
if (error != CL_SUCCESS) {
perror("Unable to obtain device info for param\n");
return;
}
printf("\tMaximum number of work-items in a work-group: %d\n", *ret);
} break;
case CL_DEVICE_NAME:
case CL_DEVICE_VENDOR: {
std::unique_ptr<char> data(new char[48]);
error = clGetDeviceInfo(id, param_name, paramSize, data.get(), NULL);
if (error != CL_SUCCESS) {
perror("Unable to obtain device name/vendor info for param\n");
return;
}
switch (param_name) {
case CL_DEVICE_NAME: printf("\tDevice name is %s\n", data.get()); break;
case CL_DEVICE_VENDOR: printf("\tDevice vendor is %s\n", data.get()); break;
}
} break;
case CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE: {
std::unique_ptr<cl_uint> size(new cl_uint[paramSize]);
error = clGetDeviceInfo(id, param_name, paramSize, size.get(), NULL);
if (error != CL_SUCCESS) {
perror("Unable to obtain device name/vendor info for param\n");
return;
}
printf("\tDevice global cacheline size: %d bytes\n", (*size)); break;
} break;
case CL_DEVICE_GLOBAL_MEM_SIZE:
case CL_DEVICE_MAX_MEM_ALLOC_SIZE: {
std::unique_ptr<cl_ulong> size(new cl_ulong[paramSize]);
error = clGetDeviceInfo(id, param_name, paramSize, size.get(), NULL);
if (error != CL_SUCCESS) {
perror("Unable to obtain device name/vendor info for param\n");
return;
}
switch (param_name) {
case CL_DEVICE_GLOBAL_MEM_SIZE: printf("\tDevice global mem: %ld mega-bytes\n", (*size) >> 20); break;
case CL_DEVICE_MAX_MEM_ALLOC_SIZE: printf("\tDevice max memory allocation: %ld mega-bytes\n", (*size) >> 20); break;
}
} break;
} //end of switch
}
void displayDeviceInfo(cl_platform_id id, cl_device_type dev_type) {
/* OpenCL 1.1 device types */
cl_int error = 0;
cl_uint numOfDevices = 0;
/* Determine how many devices are connected to your platform */
error = clGetDeviceIDs(id, dev_type, 0, NULL, &numOfDevices);
if (error != CL_SUCCESS) {
perror("Unable to obtain any OpenCL compliant device info");
exit(1);
}
std::vector<cl_device_id> devices(numOfDevices, nullptr);
/* Load the information about your devices into the variable 'devices' */
error = clGetDeviceIDs(id, dev_type, numOfDevices, devices.data(), NULL);
if (error != CL_SUCCESS) {
perror("Unable to obtain any OpenCL compliant device info");
exit(1);
}
printf("Number of detected OpenCL devices: %d\n", numOfDevices);
/* We attempt to retrieve some information about the devices. */
for (auto device : devices) {
displayDeviceDetails(device, CL_DEVICE_TYPE, "CL_DEVICE_TYPE");
displayDeviceDetails(device, CL_DEVICE_NAME, "CL_DEVICE_NAME");
displayDeviceDetails(device, CL_DEVICE_VENDOR, "CL_DEVICE_VENDOR");
displayDeviceDetails(device, CL_DEVICE_VENDOR_ID, "CL_DEVICE_VENDOR_ID");
displayDeviceDetails(device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, "CL_DEVICE_MAX_MEM_ALLOC_SIZE");
displayDeviceDetails(device, CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE, "CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE");
displayDeviceDetails(device, CL_DEVICE_GLOBAL_MEM_SIZE, "CL_DEVICE_GLOBAL_MEM_SIZE");
displayDeviceDetails(device, CL_DEVICE_MAX_COMPUTE_UNITS, "CL_DEVICE_MAX_COMPUTE_UNITS");
displayDeviceDetails(device, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, "CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS");
displayDeviceDetails(device, CL_DEVICE_MAX_WORK_ITEM_SIZES, "CL_DEVICE_MAX_WORK_ITEM_SIZES");
displayDeviceDetails(device, CL_DEVICE_MAX_WORK_GROUP_SIZE, "CL_DEVICE_MAX_WORK_GROUP_SIZE");
}
}
int _tmain(int argc, _TCHAR* argv[])
{
/* OpenCL 1.1 scalar data types */
cl_uint numOfPlatforms;
cl_int error;
/*
Get the number of platforms
*/
error = clGetPlatformIDs(0, NULL, &numOfPlatforms);
if (error != CL_SUCCESS) {
perror("Unable to find any OpenCL platforms");
return(1);
}
// Allocate memory for the number of installed platforms.
std::vector<cl_platform_id> platforms(numOfPlatforms, nullptr);
printf("Number of OpenCL platforms found: %d\n", numOfPlatforms);
error = clGetPlatformIDs(numOfPlatforms, platforms.data(), NULL);
if (error != CL_SUCCESS) {
perror("Unable to find any OpenCL platforms");
return(1);
}
for (auto platform : platforms) {
displayPlatformInfo(platform, CL_PLATFORM_PROFILE, "CL_PLATFORM_PROFILE");
displayPlatformInfo(platform, CL_PLATFORM_VERSION, "CL_PLATFORM_VERSION");
displayPlatformInfo(platform, CL_PLATFORM_NAME, "CL_PLATFORM_NAME");
displayPlatformInfo(platform, CL_PLATFORM_VENDOR, "CL_PLATFORM_VENDOR");
displayPlatformInfo(platform, CL_PLATFORM_EXTENSIONS, "CL_PLATFORM_EXTENSIONS");
// Assume that we don't know how many devices are OpenCL compliant, we locate everything !
displayDeviceInfo(platform, CL_DEVICE_TYPE_ALL);
}
return 0;
}
Comments