Skip to content

Instantly share code, notes, and snippets.

@stephanlachnit
Forked from dogukancagatay/Makefile
Last active November 17, 2018 21:51
Show Gist options
  • Save stephanlachnit/4e1ba6526f3a9d7f8772f936f2d7b8f5 to your computer and use it in GitHub Desktop.
Save stephanlachnit/4e1ba6526f3a9d7f8772f936f2d7b8f5 to your computer and use it in GitHub Desktop.
List OpenCL devices and platforms using C++
#include <iostream>
#include <CL/cl.hpp>
int main(int argc, char *argv[]) {
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
int platform_id = 0;
int device_id = 0;
std::cout << "Number of Platforms: " << platforms.size() << std::endl;
for(std::vector<cl::Platform>::iterator it = platforms.begin(); it != platforms.end(); ++it) {
cl::Platform platform(*it);
std::cout << "Platform ID: " << platform_id++ << std::endl;
std::cout << "Platform Name: " << platform.getInfo<CL_PLATFORM_NAME>() << std::endl;
std::cout << "Platform Vendor: " << platform.getInfo<CL_PLATFORM_VENDOR>() << std::endl;
std::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_CPU, &devices);
for(std::vector<cl::Device>::iterator it2 = devices.begin(); it2 != devices.end(); ++it2) {
cl::Device device(*it2);
std::cout << std::endl << "\tDevice " << device_id++ << ": " << std::endl;
std::cout << "\t\tDevice Name: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
std::cout << "\t\tDevice Vendor: " << device.getInfo<CL_DEVICE_VENDOR>() << std::endl;
std::cout << "\t\tDevice Version: " << device.getInfo<CL_DEVICE_VERSION>() << std::endl;
switch (device.getInfo<CL_DEVICE_TYPE>()) {
case 4:
std::cout << "\t\tDevice Type: GPU" << std::endl;
break;
case 2:
std::cout << "\t\tDevice Type: CPU" << std::endl;
break;
default:
std::cout << "\t\tDevice Type: unknown" << std::endl;
}
std::cout << "\t\tDevice Max Compute Units: " << device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>() << std::endl;
std::cout << "\t\tDevice Global Memory: " << device.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>() << std::endl;
std::cout << "\t\tDevice Max Clock Frequency: " << device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>() << std::endl;
std::cout << "\t\tDevice Max Memory Allocation: " << device.getInfo<CL_DEVICE_MAX_MEM_ALLOC_SIZE>() << std::endl;
std::cout << "\t\tDevice Local Memory: " << device.getInfo<CL_DEVICE_LOCAL_MEM_SIZE>() << std::endl;
std::cout << "\t\tDevice Available: " << device.getInfo<CL_DEVICE_AVAILABLE>() << std::endl;
}
std::cout << std::endl;
}
}
@stephanlachnit
Copy link
Author

Compile with g++ opencl_devices.cpp -lOpenCL

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