-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathsettings.cpp
More file actions
232 lines (200 loc) · 7.35 KB
/
Copy pathsettings.cpp
File metadata and controls
232 lines (200 loc) · 7.35 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* This file is part of AdaptiveCpp, an implementation of SYCL and C++ standard
* parallelism for CPUs and GPUs.
*
* Copyright The AdaptiveCpp Contributors
*
* AdaptiveCpp is released under the BSD 2-Clause "Simplified" License.
* See file LICENSE in the project root for full license details.
*/
// SPDX-License-Identifier: BSD-2-Clause
#include "hipSYCL/common/debug.hpp"
#include "hipSYCL/common/filesystem.hpp"
#include "hipSYCL/common/string_utils.hpp"
#include "hipSYCL/runtime/device_id.hpp"
#include "hipSYCL/runtime/settings.hpp"
#include <string>
#include <fstream>
#include <optional>
namespace hipsycl {
namespace rt {
std::istream &operator>>(std::istream &istr, scheduler_type &out) {
std::string str;
istr >> str;
if (str == "direct")
out = scheduler_type::direct;
else if (str == "unbound")
out = scheduler_type::unbound;
else
istr.setstate(std::ios_base::failbit);
return istr;
}
namespace {
void trim(std::string& str) {
str.erase(0, str.find_first_not_of("\t\n\v\f\r "));
str.erase(str.find_last_not_of("\t\n\v\f\r ") + 1);
}
bool is_number(const std::string& str){
return str.find_first_not_of("0123456789") == std::string::npos;
}
}
visibility_mask_t::mapped_type parse_device_visibility_mask(const std::string& str) {
visibility_mask_t::mapped_type device_visibility_conditions;
std::vector<std::string> condition_substrings = common::split_by_delimiter(str, ',', false);
for(auto& s : condition_substrings) {
trim(s);
device_visibility_condition current;
auto components = common::split_by_delimiter(s, '.', true);
for(auto& c : components)
trim(c);
if (components.size() == 1) {
if(is_number(components[0])) {
current.device_index_equality = std::stoi(components[0]);
} else if(components[0] != "*") {
current.device_name_match = components[0];
}
} else if (components.size() > 1) {
if(is_number(components[0])) {
current.platform_index_equality = std::stoi(components[0]);
} else if(components[0] != "*") {
current.platform_name_match = components[0];
}
if(is_number(components[1])) {
current.device_index_equality = std::stoi(components[1]);
} else if(components[1] != "*") {
current.device_name_match = components[1];
}
}
device_visibility_conditions.push_back(current);
}
return device_visibility_conditions;
}
bool device_matches(const visibility_mask_t::mapped_type &conditions,
int global_device_index, int platform_device_index,
int platform_index, const std::string &dev_name,
const std::string &platform_name) {
if(conditions.empty())
return true;
// The logic is: All individual device visibility conditions are connected by or,
// but the conditions within each condition are connected by and.
for(const auto& c : conditions) {
// If we are given conditions about the platform, use a device index relative to the
// platform.
int device_index =
(c.platform_index_equality >= 0 || !c.platform_name_match.empty())
? platform_device_index
: global_device_index;
bool all_true = true;
if(c.platform_index_equality >= 0 && (platform_index != c.platform_index_equality))
all_true = false;
if(c.device_index_equality >= 0 && (device_index != c.device_index_equality))
all_true = false;
if(!c.device_name_match.empty() && (dev_name.find(c.device_name_match) == std::string::npos))
all_true = false;
if(!c.platform_name_match.empty() && (platform_name.find(c.platform_name_match) == std::string::npos))
all_true = false;
if(all_true)
return true;
}
return false;
}
bool device_matches(const visibility_mask_t &mask, backend_id backend,
int global_device_index, int platform_device_index,
int platform_index, const std::string &dev_name,
const std::string &platform_name) {
auto it = mask.find(backend);
if(it == mask.end())
return true;
return device_matches(it->second, global_device_index, platform_device_index,
platform_index, dev_name, platform_name);
}
bool has_device_visibility_mask(const visibility_mask_t& mask, backend_id backend) {
auto it = mask.find(backend);
if(it != mask.end()) {
return it->second.size() > 0;
}
return false;
}
std::istream &operator>>(std::istream &istr, visibility_mask_t &out) {
std::string str;
istr >> str;
// have to copy, as otherwise might be interpreted as failing, although everything is fine.
std::istringstream istream{str};
std::string backend_specific_substring;
while(std::getline(istream, backend_specific_substring, ';')) {
if(backend_specific_substring.empty())
continue;
std::size_t delimiter = backend_specific_substring.find(':');
std::string name;
if(delimiter != std::string::npos) {
name = backend_specific_substring.substr(0, delimiter);
} else {
name = backend_specific_substring;
}
std::transform(name.cbegin(), name.cend(), name.begin(), ::tolower);
rt::backend_id backend;
if (name == "cuda") {
backend = rt::backend_id::cuda;
} else if (name == "hip") {
backend = rt::backend_id::hip;
} else if (name == "ze") {
backend = rt::backend_id::level_zero;
} else if (name == "omp") {
// looking for this, even though we have to allow it always.
backend = rt::backend_id::omp;
} else if (name == "ocl" || name == "opencl") {
backend = rt::backend_id::ocl;
} else if (name == "metal") {
backend = rt::backend_id::metal;
} else if (name == "vk" || name == "vulkan") {
backend = rt::backend_id::vk;
} else {
istr.setstate(std::ios_base::failbit);
// Don't use HIPSYCL_DEBUG_WARNING, it will cause recursive init error.
std::cout << "'" << name << "' is not a known backend name." << std::endl;
break;
}
if(delimiter != std::string::npos) {
auto device_visibility_mask = parse_device_visibility_mask(
backend_specific_substring.substr(delimiter + 1));
out[backend] = device_visibility_mask;
} else
out[backend] = {};
}
return istr;
}
std::istream &operator>>(std::istream &istr, default_selector_behavior& out) {
std::string str;
istr >> str;
if (str == "strict")
out = default_selector_behavior::strict;
else if (str == "multigpu")
out = default_selector_behavior::multigpu;
else if (str == "system")
out = default_selector_behavior::system;
else
istr.setstate(std::ios_base::failbit);
return istr;
}
std::istream &operator>>(std::istream &istr, std::optional<jitopt_host_vector_math_library>& out) {
std::string str;
istr >> str;
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
if (str == "none")
out = jitopt_host_vector_math_library::none;
else if (str == "svml")
out = jitopt_host_vector_math_library::svml;
else if (str == "armpl")
out = jitopt_host_vector_math_library::armpl;
else if (str == "sleef")
out = jitopt_host_vector_math_library::sleef;
else if (str == "libmvec")
out = jitopt_host_vector_math_library::libmvec;
else{
istr.setstate(std::ios_base::failbit);
std::cout << "'" << str << "' is not a valid vector math library option. Valid option are: none, svml, armpl, sleef, libmvec." << std::endl;
}
return istr;
}
}
}