-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcl_selector.hpp
58 lines (49 loc) · 1.78 KB
/
cl_selector.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//-----------------------------------------------------------------------------
//
// Selector class to reuse code
//
// Please include it after MYVERSION for cl_defs defined
// Compilation with C++20 only for basic_string::starts_with
//
//-----------------------------------------------------------------------------
//
// This file is licensed after LGPL v3
// Look at: https://www.gnu.org/licenses/lgpl-3.0.en.html for details
//
//------------------------------------------------------------------------------
#pragma once
#ifndef MYVERSION
#error "Please include it after MYVERSION for cl_defs defined"
#endif
#if __cplusplus <= 201703L
#error "Please compile with C++2a/C++20 for basic_string::starts_with"
#endif
// clang-format off
#include "cl_defs.h"
#include "CL/opencl.hpp"
// clang-format on
// intended to be private base only => no virtual dtor
struct ocl_selector_t {
cl::Platform platform;
cl::vector<cl::Device> devices;
ocl_selector_t() = default;
ocl_selector_t(std::string prefix) {
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
std::cout << "Platforms:" << std::endl;
for (auto p : platforms) {
std::string vendor = p.getInfo<CL_PLATFORM_VENDOR>();
std::string version = p.getInfo<CL_PLATFORM_VERSION>();
std::cout << "\t* " << vendor << "\t" << version << std::endl;
if (version.starts_with(prefix) || vendor.starts_with(prefix))
cl::Platform::setDefault(p);
}
platform = cl::Platform::getDefault();
std::cout << "selected: " << platform.getInfo<CL_PLATFORM_VENDOR>()
<< std::endl;
platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
std::cout << "Devices:" << std::endl;
for (auto d : devices)
std::cout << "\t* " << d.getInfo<CL_DEVICE_NAME>() << std::endl;
}
};