Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // gpgpu
- //
- // Created by Jan Henrik Müller on 27.09.16.
- // Copyright © 2016 Jan Henrik Müller. All rights reserved.
- //
- #include <iostream>
- #include <stdexcept>
- #include <sstream>
- #include <vector>
- #include <OpenCL/OpenCL.h>
- static constexpr int SIZE = 100;
- void check_err(cl_uint err) {
- if(err != CL_SUCCESS) {
- std::stringstream ss;
- ss << "error: " << err;
- throw std::runtime_error(ss.str());
- }
- }
- class device {
- cl_device_id id;
- template<typename T, cl_device_type type, size_t size = SIZE>
- std::vector<T> get_device_info() {
- T t[size];
- size_t len;
- check_err(clGetDeviceInfo(id, type, size * sizeof(T), static_cast<void*>(t), &len));
- return std::vector<T>(t, t+len);
- }
- public:
- device(cl_device_id id) : id{id} {
- }
- std::string get_name() {
- auto info = get_device_info<char, CL_DEVICE_NAME>();
- if(info.size() == SIZE) {
- throw std::runtime_error("name buffer exceeded");
- }
- return std::string(info.begin(), info.end());
- }
- int get_max_compute_units() {
- auto info = get_device_info<int, CL_DEVICE_MAX_COMPUTE_UNITS, 1>();
- return info.front();
- }
- };
- class platform {
- cl_platform_id id;
- public:
- platform(cl_platform_id id) : id{id} {
- }
- std::string get_name() {
- char name[200];
- size_t name_len;
- check_err(clGetPlatformInfo(id, CL_PLATFORM_NAME, 200, static_cast<void*>(name), &name_len));
- if(name_len == 200) {
- throw std::runtime_error("name buffer exceeded");
- }
- return std::string(name, name+name_len);
- }
- std::vector<device> get_devices() {
- std::vector<device> devices;
- cl_device_id ids[100];
- cl_uint count;
- check_err(clGetDeviceIDs(id, CL_DEVICE_TYPE_ALL, 100, ids, &count));
- if(count == 100) {
- throw std::runtime_error("platform buffer exceeded");
- }
- for(int i = 0; i < count; i++) {
- devices.emplace_back(ids[i]);
- }
- return devices;
- }
- };
- std::vector<platform> get_platforms() {
- std::vector<platform> platforms;
- cl_platform_id ids[100];
- cl_uint count;
- check_err(clGetPlatformIDs(100, ids, &count));
- if(count == 100) {
- throw std::runtime_error("platform buffer exceeded");
- }
- for(int i = 0; i < count; i++) {
- platforms.emplace_back(ids[i]);
- }
- return platforms;
- }
- int main(int argc, const char * argv[]) {
- for(auto& p : get_platforms()) {
- std::cout << p.get_name() << ":" << std::endl;
- for(auto& d: p.get_devices()) {
- std::cout << std::string(4, ' ') << d.get_name() << " @ " << d.get_max_compute_units() << std::endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement