forked from IntelPython/dpctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdpctl_utils_helper.cpp
More file actions
384 lines (372 loc) · 11.9 KB
/
dpctl_utils_helper.cpp
File metadata and controls
384 lines (372 loc) · 11.9 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//===- dpctl_utils_helper.cpp - Implementation of enum to string helpers ===//
//
// Data Parallel Control (dpCtl)
//
// Copyright 2020-2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements the helper functions defined in dpctl_utils_helper.h.
///
//===----------------------------------------------------------------------===//
#include "dpctl_utils_helper.h"
#include <sstream>
#include <string>
using namespace cl::sycl;
/*!
* Transforms enum info::device_type to string.
*/
std::string DPCTL_DeviceTypeToStr(info::device_type devTy)
{
std::stringstream ss;
switch (devTy) {
case info::device_type::cpu:
ss << "cpu" << '\n';
break;
case info::device_type::gpu:
ss << "gpu" << '\n';
break;
case info::device_type::accelerator:
ss << "accelerator" << '\n';
break;
case info::device_type::custom:
ss << "custom" << '\n';
break;
case info::device_type::host:
ss << "host" << '\n';
break;
default:
ss << "unknown" << '\n';
}
return ss.str();
}
/*!
* Transforms string to enum info::device_type.
*/
info::device_type DPCTL_StrToDeviceType(const std::string &devTyStr)
{
info::device_type devTy;
if (devTyStr == "cpu") {
devTy = info::device_type::cpu;
}
else if (devTyStr == "gpu") {
devTy = info::device_type::gpu;
}
else if (devTyStr == "accelerator") {
devTy = info::device_type::accelerator;
}
else if (devTyStr == "custom") {
devTy = info::device_type::custom;
}
else if (devTyStr == "host") {
devTy = info::device_type::host;
}
else {
// \todo handle the error
throw std::runtime_error("Unknown device type.");
}
return devTy;
}
backend DPCTL_DPCTLBackendTypeToSyclBackend(DPCTLSyclBackendType BeTy)
{
switch (BeTy) {
case DPCTLSyclBackendType::DPCTL_CUDA:
return backend::cuda;
case DPCTLSyclBackendType::DPCTL_HOST:
return backend::host;
case DPCTLSyclBackendType::DPCTL_LEVEL_ZERO:
return backend::level_zero;
case DPCTLSyclBackendType::DPCTL_OPENCL:
return backend::opencl;
default:
throw runtime_error("Unsupported backend type", -1);
}
}
DPCTLSyclBackendType DPCTL_SyclBackendToDPCTLBackendType(backend B)
{
switch (B) {
case backend::cuda:
return DPCTLSyclBackendType::DPCTL_CUDA;
case backend::host:
return DPCTLSyclBackendType::DPCTL_HOST;
case backend::level_zero:
return DPCTLSyclBackendType::DPCTL_LEVEL_ZERO;
case backend::opencl:
return DPCTLSyclBackendType::DPCTL_OPENCL;
default:
return DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND;
}
}
info::device_type DPCTL_DPCTLDeviceTypeToSyclDeviceType(DPCTLSyclDeviceType DTy)
{
switch (DTy) {
case DPCTLSyclDeviceType::DPCTL_ACCELERATOR:
return info::device_type::accelerator;
case DPCTLSyclDeviceType::DPCTL_ALL:
return info::device_type::all;
case DPCTLSyclDeviceType::DPCTL_AUTOMATIC:
return info::device_type::automatic;
case DPCTLSyclDeviceType::DPCTL_CPU:
return info::device_type::cpu;
case DPCTLSyclDeviceType::DPCTL_CUSTOM:
return info::device_type::custom;
case DPCTLSyclDeviceType::DPCTL_GPU:
return info::device_type::gpu;
case DPCTLSyclDeviceType::DPCTL_HOST_DEVICE:
return info::device_type::host;
default:
throw runtime_error("Unsupported device type", -1);
}
}
DPCTLSyclDeviceType DPCTL_SyclDeviceTypeToDPCTLDeviceType(info::device_type D)
{
switch (D) {
case info::device_type::accelerator:
return DPCTLSyclDeviceType::DPCTL_ACCELERATOR;
case info::device_type::all:
return DPCTLSyclDeviceType::DPCTL_ALL;
case info::device_type::automatic:
return DPCTLSyclDeviceType::DPCTL_AUTOMATIC;
case info::device_type::cpu:
return DPCTLSyclDeviceType::DPCTL_CPU;
case info::device_type::custom:
return DPCTLSyclDeviceType::DPCTL_CUSTOM;
case info::device_type::gpu:
return DPCTLSyclDeviceType::DPCTL_GPU;
case info::device_type::host:
return DPCTLSyclDeviceType::DPCTL_HOST_DEVICE;
default:
return DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE;
}
}
/*!
* Transforms cl::sycl::aspect to string.
*/
std::string DPCTL_AspectToStr(aspect aspectTy)
{
std::stringstream ss;
switch (aspectTy) {
case aspect::host:
ss << "host" << '\n';
break;
case aspect::cpu:
ss << "cpu" << '\n';
break;
case aspect::gpu:
ss << "gpu" << '\n';
break;
case aspect::accelerator:
ss << "accelerator" << '\n';
break;
case aspect::custom:
ss << "custom" << '\n';
break;
case aspect::fp16:
ss << "fp16" << '\n';
break;
case aspect::fp64:
ss << "fp64" << '\n';
break;
case aspect::int64_base_atomics:
ss << "int64_base_atomics" << '\n';
break;
case aspect::int64_extended_atomics:
ss << "int64_extended_atomics" << '\n';
break;
case aspect::image:
ss << "image" << '\n';
break;
case aspect::online_compiler:
ss << "online_compiler" << '\n';
break;
case aspect::online_linker:
ss << "online_linker" << '\n';
break;
case aspect::queue_profiling:
ss << "queue_profiling" << '\n';
break;
case aspect::usm_device_allocations:
ss << "usm_device_allocations" << '\n';
break;
case aspect::usm_host_allocations:
ss << "usm_host_allocations" << '\n';
break;
case aspect::usm_shared_allocations:
ss << "usm_shared_allocations" << '\n';
break;
case aspect::usm_restricted_shared_allocations:
ss << "usm_restricted_shared_allocations" << '\n';
break;
case aspect::usm_system_allocator:
ss << "usm_system_allocator" << '\n';
break;
default:
throw runtime_error("Unsupported aspect type", -1);
}
return ss.str();
}
/*!
* Transforms string to cl::sycl::aspect.
*/
aspect DPCTL_StrToAspectType(const std::string &aspectTyStr)
{
aspect aspectTy;
if (aspectTyStr == "host") {
aspectTy = aspect::host;
}
else if (aspectTyStr == "cpu") {
aspectTy = aspect::cpu;
}
else if (aspectTyStr == "gpu") {
aspectTy = aspect::gpu;
}
else if (aspectTyStr == "accelerator") {
aspectTy = aspect::accelerator;
}
else if (aspectTyStr == "custom") {
aspectTy = aspect::custom;
}
else if (aspectTyStr == "fp16") {
aspectTy = aspect::fp16;
}
else if (aspectTyStr == "fp64") {
aspectTy = aspect::fp64;
}
else if (aspectTyStr == "int64_base_atomics") {
aspectTy = aspect::int64_base_atomics;
}
else if (aspectTyStr == "int64_extended_atomics") {
aspectTy = aspect::int64_extended_atomics;
}
else if (aspectTyStr == "image") {
aspectTy = aspect::image;
}
else if (aspectTyStr == "online_compiler") {
aspectTy = aspect::online_compiler;
}
else if (aspectTyStr == "online_linker") {
aspectTy = aspect::online_linker;
}
else if (aspectTyStr == "queue_profiling") {
aspectTy = aspect::queue_profiling;
}
else if (aspectTyStr == "usm_device_allocations") {
aspectTy = aspect::usm_device_allocations;
}
else if (aspectTyStr == "usm_host_allocations") {
aspectTy = aspect::usm_host_allocations;
}
else if (aspectTyStr == "usm_shared_allocations") {
aspectTy = aspect::usm_shared_allocations;
}
else if (aspectTyStr == "usm_restricted_shared_allocations") {
aspectTy = aspect::usm_restricted_shared_allocations;
}
else if (aspectTyStr == "usm_system_allocator") {
aspectTy = aspect::usm_system_allocator;
}
else {
// \todo handle the error
throw runtime_error("Unsupported aspect type", -1);
}
return aspectTy;
}
aspect DPCTL_DPCTLAspectTypeToSyclAspect(DPCTLSyclAspectType AspectTy)
{
switch (AspectTy) {
case DPCTLSyclAspectType::host:
return aspect::host;
case DPCTLSyclAspectType::cpu:
return aspect::cpu;
case DPCTLSyclAspectType::gpu:
return aspect::gpu;
case DPCTLSyclAspectType::accelerator:
return aspect::accelerator;
case DPCTLSyclAspectType::custom:
return aspect::custom;
case DPCTLSyclAspectType::fp16:
return aspect::fp16;
case DPCTLSyclAspectType::fp64:
return aspect::fp64;
case DPCTLSyclAspectType::int64_base_atomics:
return aspect::int64_base_atomics;
case DPCTLSyclAspectType::int64_extended_atomics:
return aspect::int64_extended_atomics;
case DPCTLSyclAspectType::image:
return aspect::image;
case DPCTLSyclAspectType::online_compiler:
return aspect::online_compiler;
case DPCTLSyclAspectType::online_linker:
return aspect::online_linker;
case DPCTLSyclAspectType::queue_profiling:
return aspect::queue_profiling;
case DPCTLSyclAspectType::usm_device_allocations:
return aspect::usm_device_allocations;
case DPCTLSyclAspectType::usm_host_allocations:
return aspect::usm_host_allocations;
case DPCTLSyclAspectType::usm_shared_allocations:
return aspect::usm_shared_allocations;
case DPCTLSyclAspectType::usm_restricted_shared_allocations:
return aspect::usm_restricted_shared_allocations;
case DPCTLSyclAspectType::usm_system_allocator:
return aspect::usm_system_allocator;
default:
throw runtime_error("Unsupported aspect type", -1);
}
}
DPCTLSyclAspectType DPCTL_SyclAspectToDPCTLAspectType(aspect Aspect)
{
switch (Aspect) {
case aspect::host:
return DPCTLSyclAspectType::host;
case aspect::cpu:
return DPCTLSyclAspectType::cpu;
case aspect::gpu:
return DPCTLSyclAspectType::gpu;
case aspect::accelerator:
return DPCTLSyclAspectType::accelerator;
case aspect::custom:
return DPCTLSyclAspectType::custom;
case aspect::fp16:
return DPCTLSyclAspectType::fp16;
case aspect::fp64:
return DPCTLSyclAspectType::fp64;
case aspect::int64_base_atomics:
return DPCTLSyclAspectType::int64_base_atomics;
case aspect::int64_extended_atomics:
return DPCTLSyclAspectType::int64_extended_atomics;
case aspect::image:
return DPCTLSyclAspectType::image;
case aspect::online_compiler:
return DPCTLSyclAspectType::online_compiler;
case aspect::online_linker:
return DPCTLSyclAspectType::online_linker;
case aspect::queue_profiling:
return DPCTLSyclAspectType::queue_profiling;
case aspect::usm_device_allocations:
return DPCTLSyclAspectType::usm_device_allocations;
case aspect::usm_host_allocations:
return DPCTLSyclAspectType::usm_host_allocations;
case aspect::usm_shared_allocations:
return DPCTLSyclAspectType::usm_shared_allocations;
case aspect::usm_restricted_shared_allocations:
return DPCTLSyclAspectType::usm_restricted_shared_allocations;
case aspect::usm_system_allocator:
return DPCTLSyclAspectType::usm_system_allocator;
default:
throw runtime_error("Unsupported aspect type", -1);
}
}