Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  gpgpu
  4. //
  5. //  Created by Jan Henrik Müller on 27.09.16.
  6. //  Copyright © 2016 Jan Henrik Müller. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <stdexcept>
  11. #include <sstream>
  12. #include <vector>
  13. #include <OpenCL/OpenCL.h>
  14.  
  15. static constexpr int SIZE = 100;
  16.  
  17. void check_err(cl_uint err) {
  18.     if(err != CL_SUCCESS) {
  19.         std::stringstream ss;
  20.         ss << "error: " << err;
  21.         throw std::runtime_error(ss.str());
  22.     }
  23. }
  24.  
  25. class device {
  26.     cl_device_id id;
  27.    
  28.     template<typename T, cl_device_type type, size_t size = SIZE>
  29.     std::vector<T> get_device_info() {
  30.         T t[size];
  31.         size_t len;
  32.         check_err(clGetDeviceInfo(id, type, size * sizeof(T), static_cast<void*>(t), &len));
  33.         return std::vector<T>(t, t+len);
  34.     }
  35. public:
  36.     device(cl_device_id id) : id{id} {
  37.     }
  38.    
  39.     std::string get_name() {
  40.         auto info = get_device_info<char, CL_DEVICE_NAME>();
  41.         if(info.size() == SIZE) {
  42.             throw std::runtime_error("name buffer exceeded");
  43.         }
  44.         return std::string(info.begin(), info.end());
  45.     }
  46.    
  47.     int get_max_compute_units() {
  48.         auto info = get_device_info<int, CL_DEVICE_MAX_COMPUTE_UNITS, 1>();
  49.         return info.front();
  50.     }
  51. };
  52.  
  53. class platform {
  54.     cl_platform_id id;
  55. public:
  56.     platform(cl_platform_id id) : id{id} {
  57.     }
  58.    
  59.     std::string get_name() {
  60.         char name[200];
  61.         size_t name_len;
  62.         check_err(clGetPlatformInfo(id, CL_PLATFORM_NAME, 200, static_cast<void*>(name), &name_len));
  63.         if(name_len == 200) {
  64.             throw std::runtime_error("name buffer exceeded");
  65.         }
  66.         return std::string(name, name+name_len);
  67.     }
  68.    
  69.     std::vector<device> get_devices() {
  70.         std::vector<device> devices;
  71.         cl_device_id ids[100];
  72.         cl_uint count;
  73.         check_err(clGetDeviceIDs(id, CL_DEVICE_TYPE_ALL, 100, ids, &count));
  74.         if(count == 100) {
  75.             throw std::runtime_error("platform buffer exceeded");
  76.         }
  77.         for(int i = 0; i < count; i++) {
  78.             devices.emplace_back(ids[i]);
  79.         }
  80.         return devices;
  81.     }
  82. };
  83.  
  84. std::vector<platform> get_platforms() {
  85.     std::vector<platform> platforms;
  86.     cl_platform_id ids[100];
  87.     cl_uint count;
  88.     check_err(clGetPlatformIDs(100, ids, &count));
  89.     if(count == 100) {
  90.         throw std::runtime_error("platform buffer exceeded");
  91.     }
  92.     for(int i = 0; i < count; i++) {
  93.         platforms.emplace_back(ids[i]);
  94.     }
  95.     return platforms;
  96. }
  97.  
  98. int main(int argc, const char * argv[]) {
  99.     for(auto& p : get_platforms()) {
  100.         std::cout << p.get_name() << ":" << std::endl;
  101.         for(auto& d: p.get_devices()) {
  102.             std::cout << std::string(4, ' ') << d.get_name() << " @ " << d.get_max_compute_units() << std::endl;
  103.         }
  104.     }
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement