forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_selector.cpp
More file actions
162 lines (137 loc) · 4.86 KB
/
device_selector.cpp
File metadata and controls
162 lines (137 loc) · 4.86 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//==------ device_selector.cpp - SYCL device selector ----------------------==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <CL/sycl/backend_types.hpp>
#include <CL/sycl/device.hpp>
#include <CL/sycl/device_selector.hpp>
#include <CL/sycl/exception.hpp>
#include <CL/sycl/stl.hpp>
#include <detail/device_impl.hpp>
#include <detail/force_device.hpp>
// 4.6.1 Device selection class
__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
// Utility function to check if device is of the preferred backend.
// Currently preference is given to the opencl backend.
static bool isDeviceOfPreferredSyclBe(const device &Device) {
if (Device.is_host())
return false;
return detail::getSyclObjImpl(Device)->getPlugin().getBackend() ==
backend::opencl;
}
device device_selector::select_device() const {
vector_class<device> devices = device::get_devices();
int score = REJECT_DEVICE_SCORE;
const device *res = nullptr;
for (const auto &dev : devices) {
int dev_score = (*this)(dev);
if (detail::pi::trace(detail::pi::TraceLevel::PI_TRACE_ALL)) {
string_class PlatformVersion = dev.get_info<info::device::platform>()
.get_info<info::platform::version>();
string_class DeviceName = dev.get_info<info::device::name>();
std::cout << "SYCL_PI_TRACE[all]: "
<< "select_device(): -> score = " << score
<< ((score == REJECT_DEVICE_SCORE) ? "(REJECTED)" : " ")
<< std::endl
<< "SYCL_PI_TRACE[all]: "
<< " platform: " << PlatformVersion << std::endl
<< "SYCL_PI_TRACE[all]: "
<< " device: " << DeviceName << std::endl;
}
// Device is discarded if is marked with REJECT_DEVICE_SCORE
if (dev_score == REJECT_DEVICE_SCORE)
continue;
// SYCL spec says: "If more than one device receives the high score then
// one of those tied devices will be returned, but which of the devices
// from the tied set is to be returned is not defined". Here we give a
// preference to the device of the preferred BE.
//
if ((score < dev_score) ||
(score == dev_score && isDeviceOfPreferredSyclBe(dev))) {
res = &dev;
score = dev_score;
}
}
if (res != nullptr) {
if (detail::pi::trace(detail::pi::TraceLevel::PI_TRACE_BASIC)) {
string_class PlatformVersion = res->get_info<info::device::platform>()
.get_info<info::platform::version>();
string_class DeviceName = res->get_info<info::device::name>();
std::cout << "SYCL_PI_TRACE[all]: "
<< "Selected device ->" << std::endl
<< "SYCL_PI_TRACE[all]: "
<< " platform: " << PlatformVersion << std::endl
<< "SYCL_PI_TRACE[all]: "
<< " device: " << DeviceName << std::endl;
}
return *res;
}
throw cl::sycl::runtime_error("No device of requested type available.",
PI_DEVICE_NOT_FOUND);
}
/// Devices of different kinds are prioritized in the following order:
/// 1. GPU
/// 2. CPU
/// 3. Host
int default_selector::operator()(const device &dev) const {
int Score = REJECT_DEVICE_SCORE;
// Give preference to device of SYCL BE.
if (isDeviceOfPreferredSyclBe(dev))
Score = 50;
// override always wins
if (dev.get_info<info::device::device_type>() == detail::get_forced_type())
Score += 1000;
if (dev.is_gpu())
Score += 500;
if (dev.is_cpu())
Score += 300;
if (dev.is_host())
Score += 100;
return Score;
}
int gpu_selector::operator()(const device &dev) const {
int Score = REJECT_DEVICE_SCORE;
if (dev.is_gpu()) {
Score = 1000;
// Give preference to device of SYCL BE.
if (isDeviceOfPreferredSyclBe(dev))
Score += 50;
}
return Score;
}
int cpu_selector::operator()(const device &dev) const {
int Score = REJECT_DEVICE_SCORE;
if (dev.is_cpu()) {
Score = 1000;
// Give preference to device of SYCL BE.
if (isDeviceOfPreferredSyclBe(dev))
Score += 50;
}
return Score;
}
int accelerator_selector::operator()(const device &dev) const {
int Score = REJECT_DEVICE_SCORE;
if (dev.is_accelerator()) {
Score = 1000;
// Give preference to device of SYCL BE.
if (isDeviceOfPreferredSyclBe(dev))
Score += 50;
}
return Score;
}
int host_selector::operator()(const device &dev) const {
int Score = REJECT_DEVICE_SCORE;
if (dev.is_host()) {
Score = 1000;
// Give preference to device of SYCL BE.
if (isDeviceOfPreferredSyclBe(dev))
Score += 50;
}
return Score;
}
} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)