-
Notifications
You must be signed in to change notification settings - Fork 722
/
Copy pathdevicemanager.hpp
110 lines (93 loc) · 3.9 KB
/
devicemanager.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//==============================================================
// Copyright © 2021 Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
#pragma once
#include <sycl/sycl.hpp>
#include <iostream>
namespace devicemanager {
// Report all available SYCL devices
inline void GetDevices() {
std::cout << "Available devices: \n";
// Query all SYCL devices in the system
for (const auto &device : sycl::device::get_devices()) {
switch (device.get_info<sycl::info::device::device_type>()) {
case sycl::info::device_type::cpu:
std::cout << " CPU: " << device.get_info<sycl::info::device::name>() << "\n";
break;
case sycl::info::device_type::gpu:
std::cout << " GPU: " << device.get_info<sycl::info::device::name>() << "\n";
break;
case sycl::info::device_type::host:
std::cout << " Host (single-threaded CPU)\n";
break;
case sycl::info::device_type::accelerator:
// The kernels were not tested for accelerators, only for Host, CPU and GPU
std::cout << " Accelerator (not supported): " << device.get_info<sycl::info::device::name>() << "\n";
break;
default:
std::cout << " Unknown (not supported): " << device.get_info<sycl::info::device::name>() << "\n";
break;
}
}
}
// Singleton DeviceManager
// Ensures consistent use of same SYCL device and SYCL queue among all kernels and subroutines
// Allows user transparent device selection via command line
class DeviceManager {
public:
// get the currently active device
sycl::device &GetCurrentDevice() { return current_device_; }
// get the currently used device queue
sycl::queue &GetCurrentQueue() { return current_queue_; }
// select a new device and queue
// @return true on success, false otherwise
// @details currently only SYCL Host device, or SYCL CPU/GPU device are supported
bool SelectDevice(const sycl::info::device_type &device_type) {
// loop over all SYCL devices and choose the required one (if available)
for (const auto &device : sycl::device::get_devices()) {
if (device.get_info<sycl::info::device::device_type>() == device_type) {
current_device_ = device;
}
}
// if the desired device was not chosen, provide a warning
if (current_device_.get_info<sycl::info::device::device_type>() != device_type) {
std::cout << "Requested device not available \n";
GetDevices();
return false;
} else {
if (current_device_.is_cpu()) {
std::cout << "Using Host device (single-threaded CPU)\n";
} else {
std::cout << "Using " << current_device_.get_info<sycl::info::device::name>() << "\n";
}
current_queue_ = sycl::queue(current_device_);
return true;
}
}
// Returns the instance of device manager singleton.
static DeviceManager &instance() {
static DeviceManager device_manager;
return device_manager;
}
// DeviceManager is a singleton
// remove all constructors
DeviceManager(const DeviceManager &) = delete;
DeviceManager &operator=(const DeviceManager &) = delete;
DeviceManager(DeviceManager &&) = delete;
DeviceManager &operator=(DeviceManager &&) = delete;
private:
DeviceManager() { current_device_ = sycl::device(sycl::default_selector{}); }
sycl::device current_device_; // SYCL device used by all kernels/operations
sycl::queue current_queue_; // SYCL queue used by all kernels/operations
};
// Get current queue for current device
inline sycl::queue &GetCurrentQueue() { return DeviceManager::instance().GetCurrentQueue(); }
// Get current device
inline sycl::device &GetCurrentDevice() { return DeviceManager::instance().GetCurrentDevice(); }
// Select a different device
inline bool SelectDevice(const sycl::info::device_type &device_type) {
return DeviceManager::instance().SelectDevice(device_type);
}
} // namespace devicemanager