forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernelBundle.cpp
More file actions
647 lines (532 loc) · 23.1 KB
/
Copy pathKernelBundle.cpp
File metadata and controls
647 lines (532 loc) · 23.1 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//==---- DefaultValues.cpp --- Spec constants default values unit test -----==//
//
// 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 <detail/device_impl.hpp>
#include <detail/kernel_bundle_impl.hpp>
#include <sycl/sycl.hpp>
#include <helpers/MockKernelInfo.hpp>
#include <helpers/MockDeviceImage.hpp>
#include <helpers/UrMock.hpp>
#include <gtest/gtest.h>
class TestKernel;
class TestKernelExeOnly;
class TestKernelWithAspects;
MOCK_INTEGRATION_HEADER(TestKernel)
MOCK_INTEGRATION_HEADER(TestKernelExeOnly)
MOCK_INTEGRATION_HEADER(TestKernelWithAspects)
static sycl::unittest::MockDeviceImage
generateDefaultImage(std::initializer_list<std::string> KernelNames,
sycl_device_binary_type BinaryType,
const char *DeviceTargetSpec,
const std::vector<sycl::aspect> &Aspects = {}) {
using namespace sycl::unittest;
MockPropertySet PropSet;
if (!Aspects.empty())
addDeviceRequirementsProps(PropSet, Aspects);
std::vector<unsigned char> Bin{0, 1, 2, 3, 4, 5}; // Random data
Array<MockOffloadEntry> Entries = makeEmptyKernels(KernelNames);
MockDeviceImage Img{BinaryType, // Format
DeviceTargetSpec,
"", // Compile options
"", // Link options
std::move(Bin),
std::move(Entries),
std::move(PropSet)};
return Img;
}
static sycl::unittest::MockDeviceImage Imgs[] = {
generateDefaultImage({"TestKernel"}, SYCL_DEVICE_BINARY_TYPE_SPIRV,
__SYCL_DEVICE_BINARY_TARGET_SPIRV64),
generateDefaultImage({"TestKernelExeOnly"}, SYCL_DEVICE_BINARY_TYPE_NATIVE,
__SYCL_DEVICE_BINARY_TARGET_SPIRV64_X86_64),
// A device image without entires
generateDefaultImage({}, SYCL_DEVICE_BINARY_TYPE_NATIVE,
__SYCL_DEVICE_BINARY_TARGET_SPIRV64_X86_64),
generateDefaultImage(
{"TestKernelWithAspects"}, SYCL_DEVICE_BINARY_TYPE_NATIVE,
__SYCL_DEVICE_BINARY_TARGET_SPIRV64, {sycl::aspect::gpu})};
static sycl::unittest::MockDeviceImageArray<std::size(Imgs)> ImgArray{Imgs};
static ur_result_t redefinedDeviceGetInfoCPU(void *pParams) {
auto params = *static_cast<ur_device_get_info_params_t *>(pParams);
if (*params.ppropName == UR_DEVICE_INFO_TYPE) {
auto *Result = reinterpret_cast<ur_device_type_t *>(*params.ppPropValue);
*Result = UR_DEVICE_TYPE_CPU;
}
return UR_RESULT_SUCCESS;
}
TEST(KernelBundle, GetKernelBundleFromKernel) {
sycl::unittest::UrMock<> Mock;
const sycl::device Dev = sycl::platform().get_devices()[0];
sycl::context Ctx{Dev};
sycl::queue Queue{Ctx, Dev};
sycl::kernel_bundle<sycl::bundle_state::executable> KernelBundle =
sycl::get_kernel_bundle<sycl::bundle_state::executable>(Ctx, {Dev});
sycl::kernel Kernel =
KernelBundle.get_kernel(sycl::get_kernel_id<TestKernel>());
sycl::kernel_bundle<sycl::bundle_state::executable> RetKernelBundle =
Kernel.get_kernel_bundle();
EXPECT_EQ(KernelBundle, RetKernelBundle);
}
TEST(KernelBundle, KernelBundleAndItsDevImageStateConsistency) {
sycl::unittest::UrMock<> Mock;
const sycl::device Dev = sycl::platform().get_devices()[0];
sycl::queue Queue{Dev};
const sycl::context Ctx = Queue.get_context();
sycl::kernel_bundle KernelBundle =
sycl::get_kernel_bundle<TestKernel, sycl::bundle_state::input>(Ctx,
{Dev});
auto ObjBundle = sycl::compile(KernelBundle, KernelBundle.get_devices());
EXPECT_FALSE(ObjBundle.empty()) << "Expect non-empty obj kernel bundle";
auto ObjBundleImpl = sycl::detail::getSyclObjImpl(ObjBundle);
EXPECT_EQ(ObjBundleImpl->get_bundle_state(), sycl::bundle_state::object)
<< "Expect object device image in bundle";
auto LinkBundle = sycl::link(ObjBundle, ObjBundle.get_devices());
EXPECT_FALSE(LinkBundle.empty()) << "Expect non-empty exec kernel bundle";
auto LinkBundleImpl = sycl::detail::getSyclObjImpl(LinkBundle);
EXPECT_EQ(LinkBundleImpl->get_bundle_state(), sycl::bundle_state::executable)
<< "Expect executable device image in bundle";
}
TEST(KernelBundle, EmptyKernelBundle) {
sycl::unittest::UrMock<> Mock;
const sycl::device Dev = sycl::platform().get_devices()[0];
sycl::queue Queue{Dev};
const sycl::context Ctx = Queue.get_context();
auto EmptyKernelBundle =
sycl::get_kernel_bundle<sycl::bundle_state::executable>(Ctx, {Dev}, {});
EXPECT_TRUE(EmptyKernelBundle.empty());
EXPECT_EQ(std::distance(EmptyKernelBundle.begin(), EmptyKernelBundle.end()),
0u);
}
TEST(KernelBundle, EmptyKernelBundleKernelLaunchException) {
sycl::unittest::UrMock<> Mock;
const sycl::device Dev = sycl::platform().get_devices()[0];
sycl::queue Queue{Dev};
const sycl::context Ctx = Queue.get_context();
auto EmptyKernelBundle =
sycl::get_kernel_bundle<sycl::bundle_state::executable>(Ctx, {Dev}, {});
class UnqiueException {};
try {
Queue.submit([&](sycl::handler &CGH) {
CGH.use_kernel_bundle(EmptyKernelBundle);
try {
CGH.single_task<TestKernel>([]() {});
FAIL() << "No exception was thrown.";
} catch (const sycl::exception &e) {
ASSERT_EQ(e.code().value(),
static_cast<int>(sycl::errc::kernel_not_supported))
<< "sycl::exception code was not the expected "
"sycl::errc::kernel_not_supported.";
// Throw uniquely identifiable exception to distinguish between that
// the sycl::exception originates from the correct level.
throw UnqiueException{};
} catch (...) {
FAIL()
<< "Unexpected exception was thrown in kernel invocation function.";
}
});
} catch (const UnqiueException &) {
// Expected path
} catch (const sycl::exception &) {
FAIL() << "sycl::exception thrown at the wrong level.";
} catch (...) {
FAIL() << "Unexpected exception was thrown in submit.";
}
}
TEST(KernelBundle, HasKernelBundle) {
sycl::unittest::UrMock<> Mock;
const sycl::device Dev = sycl::platform().get_devices()[0];
sycl::queue Queue{Dev};
const sycl::context Ctx = Queue.get_context();
bool HasKernelBundle =
sycl::has_kernel_bundle<sycl::bundle_state::input>(Ctx, {Dev});
EXPECT_TRUE(HasKernelBundle);
HasKernelBundle =
sycl::has_kernel_bundle<sycl::bundle_state::object>(Ctx, {Dev});
EXPECT_TRUE(HasKernelBundle);
HasKernelBundle =
sycl::has_kernel_bundle<sycl::bundle_state::executable>(Ctx, {Dev});
EXPECT_TRUE(HasKernelBundle);
HasKernelBundle =
sycl::has_kernel_bundle<TestKernel, sycl::bundle_state::input>(Ctx,
{Dev});
EXPECT_TRUE(HasKernelBundle);
HasKernelBundle =
sycl::has_kernel_bundle<TestKernel, sycl::bundle_state::object>(Ctx,
{Dev});
EXPECT_TRUE(HasKernelBundle);
HasKernelBundle =
sycl::has_kernel_bundle<TestKernel, sycl::bundle_state::executable>(
Ctx, {Dev});
EXPECT_TRUE(HasKernelBundle);
HasKernelBundle =
sycl::has_kernel_bundle<TestKernelExeOnly, sycl::bundle_state::input>(
Ctx, {Dev});
EXPECT_FALSE(HasKernelBundle);
HasKernelBundle =
sycl::has_kernel_bundle<TestKernelExeOnly, sycl::bundle_state::object>(
Ctx, {Dev});
EXPECT_FALSE(HasKernelBundle);
HasKernelBundle =
sycl::has_kernel_bundle<TestKernelExeOnly,
sycl::bundle_state::executable>(Ctx, {Dev});
EXPECT_TRUE(HasKernelBundle);
}
TEST(KernelBundle, UseKernelBundleWrongContextPrimaryQueueOnly) {
sycl::unittest::UrMock<> Mock;
const sycl::device Dev = sycl::platform().get_devices()[0];
sycl::queue Queue{Dev};
const sycl::context QueueCtx = Queue.get_context();
const sycl::context OtherCtx{Dev};
ASSERT_NE(QueueCtx, OtherCtx);
auto KernelBundle =
sycl::get_kernel_bundle<sycl::bundle_state::executable>(OtherCtx, {Dev});
class UnqiueException {};
try {
Queue.submit([&](sycl::handler &CGH) {
try {
CGH.use_kernel_bundle(KernelBundle);
FAIL() << "No exception was thrown.";
CGH.single_task<TestKernel>([]() {});
} catch (const sycl::exception &e) {
ASSERT_EQ(e.code().value(), static_cast<int>(sycl::errc::invalid))
<< "sycl::exception code was not the expected sycl::errc::invalid.";
// Throw uniquely identifiable exception to distinguish between that
// the sycl::exception originates from the correct level.
throw UnqiueException{};
} catch (...) {
FAIL()
<< "Unexpected exception was thrown in kernel invocation function.";
}
});
} catch (const UnqiueException &) {
// Expected path
} catch (const sycl::exception &) {
FAIL() << "sycl::exception thrown at the wrong level.";
} catch (...) {
FAIL() << "Unexpected exception was thrown in submit.";
}
}
TEST(KernelBundle, UseKernelBundleWrongContextPrimaryQueueValidSecondaryQueue) {
sycl::unittest::UrMock<> Mock;
const sycl::device Dev = sycl::platform().get_devices()[0];
const sycl::context PrimaryCtx{Dev};
const sycl::context SecondaryCtx{Dev};
ASSERT_NE(PrimaryCtx, SecondaryCtx);
auto KernelBundle = sycl::get_kernel_bundle<sycl::bundle_state::executable>(
SecondaryCtx, {Dev});
sycl::queue PrimaryQueue{PrimaryCtx, Dev};
sycl::queue SecondaryQueue{SecondaryCtx, Dev};
class UnqiueException {};
try {
PrimaryQueue.submit(
[&](sycl::handler &CGH) {
try {
CGH.use_kernel_bundle(KernelBundle);
FAIL() << "No exception was thrown.";
CGH.single_task<TestKernel>([]() {});
} catch (const sycl::exception &e) {
ASSERT_EQ(e.code().value(), static_cast<int>(sycl::errc::invalid))
<< "sycl::exception code was not the expected "
"sycl::errc::invalid.";
// Throw uniquely identifiable exception to distinguish between that
// the sycl::exception originates from the correct level.
throw UnqiueException{};
} catch (...) {
FAIL() << "Unexpected exception was thrown in kernel invocation "
"function.";
}
},
SecondaryQueue);
} catch (const UnqiueException &) {
// Expected path
} catch (const sycl::exception &) {
FAIL() << "sycl::exception thrown at the wrong level.";
} catch (...) {
FAIL() << "Unexpected exception was thrown in submit.";
}
}
TEST(KernelBundle, UseKernelBundleValidPrimaryQueueWrongContextSecondaryQueue) {
sycl::unittest::UrMock<> Mock;
const sycl::device Dev = sycl::platform().get_devices()[0];
const sycl::context PrimaryCtx{Dev};
const sycl::context SecondaryCtx{Dev};
ASSERT_NE(PrimaryCtx, SecondaryCtx);
auto KernelBundle = sycl::get_kernel_bundle<sycl::bundle_state::executable>(
PrimaryCtx, {Dev});
sycl::queue PrimaryQueue{PrimaryCtx, Dev};
sycl::queue SecondaryQueue{SecondaryCtx, Dev};
class UnqiueException {};
try {
PrimaryQueue.submit(
[&](sycl::handler &CGH) {
try {
CGH.use_kernel_bundle(KernelBundle);
FAIL() << "No exception was thrown.";
CGH.single_task<TestKernel>([]() {});
} catch (const sycl::exception &e) {
ASSERT_EQ(e.code().value(), static_cast<int>(sycl::errc::invalid))
<< "sycl::exception code was not the expected "
"sycl::errc::invalid.";
// Throw uniquely identifiable exception to distinguish between that
// the sycl::exception originates from the correct level.
throw UnqiueException{};
} catch (...) {
FAIL() << "Unexpected exception was thrown in kernel invocation "
"function.";
}
},
SecondaryQueue);
} catch (const UnqiueException &) {
// Expected path
} catch (const sycl::exception &) {
FAIL() << "sycl::exception thrown at the wrong level.";
} catch (...) {
FAIL() << "Unexpected exception was thrown in submit.";
}
}
TEST(KernelBundle, UseKernelBundleWrongContextPrimaryQueueAndSecondaryQueue) {
sycl::unittest::UrMock<> Mock;
const sycl::device Dev = sycl::platform().get_devices()[0];
const sycl::context PrimaryCtx{Dev};
const sycl::context SecondaryCtx{Dev};
const sycl::context OtherCtx{Dev};
ASSERT_NE(PrimaryCtx, SecondaryCtx);
ASSERT_NE(PrimaryCtx, OtherCtx);
ASSERT_NE(SecondaryCtx, OtherCtx);
auto KernelBundle =
sycl::get_kernel_bundle<sycl::bundle_state::executable>(OtherCtx, {Dev});
sycl::queue PrimaryQueue{PrimaryCtx, Dev};
sycl::queue SecondaryQueue{SecondaryCtx, Dev};
class UnqiueException {};
try {
PrimaryQueue.submit(
[&](sycl::handler &CGH) {
try {
CGH.use_kernel_bundle(KernelBundle);
FAIL() << "No exception was thrown.";
CGH.single_task<TestKernel>([]() {});
} catch (const sycl::exception &e) {
ASSERT_EQ(e.code().value(), static_cast<int>(sycl::errc::invalid))
<< "sycl::exception code was not the expected "
"sycl::errc::invalid.";
// Throw uniquely identifiable exception to distinguish between that
// the sycl::exception originates from the correct level.
throw UnqiueException{};
} catch (...) {
FAIL() << "Unexpected exception was thrown in kernel invocation "
"function.";
}
},
SecondaryQueue);
} catch (const UnqiueException &) {
// Expected path
} catch (const sycl::exception &) {
FAIL() << "sycl::exception thrown at the wrong level.";
} catch (...) {
FAIL() << "Unexpected exception was thrown in submit.";
}
}
TEST(KernelBundle, EmptyDevicesKernelBundleLinkException) {
sycl::unittest::UrMock<> Mock;
const sycl::device Dev = sycl::platform().get_devices()[0];
sycl::queue Queue{Dev};
const sycl::context Ctx = Queue.get_context();
auto EmptyKernelBundle =
sycl::get_kernel_bundle<sycl::bundle_state::object>(Ctx, {Dev}, {});
std::vector<sycl::device> EmptyDevices{};
// Case without an object bundle.
try {
auto LinkBundle = sycl::link(EmptyKernelBundle, EmptyDevices);
FAIL() << "No exception was thrown.";
} catch (const sycl::exception &e) {
ASSERT_EQ(e.code().value(), static_cast<int>(sycl::errc::invalid))
<< "sycl::exception code was not the expected "
"sycl::errc::invalid.";
// Expected path
} catch (...) {
FAIL() << "Unexpected exception was thrown in sycl::link.";
}
sycl::kernel_bundle KernelBundle =
sycl::get_kernel_bundle<TestKernel, sycl::bundle_state::input>(Ctx,
{Dev});
auto ObjBundle = sycl::compile(KernelBundle, KernelBundle.get_devices());
EXPECT_FALSE(ObjBundle.empty()) << "Expect non-empty obj kernel bundle";
try {
auto LinkBundle = sycl::link(ObjBundle, EmptyDevices);
FAIL() << "No exception was thrown.";
} catch (const sycl::exception &e) {
ASSERT_EQ(e.code().value(), static_cast<int>(sycl::errc::invalid))
<< "sycl::exception code was not the expected "
"sycl::errc::invalid.";
// Expected path
} catch (...) {
FAIL() << "Unexpected exception was thrown in sycl::link.";
}
}
ur_device_handle_t ParentDevice = nullptr;
ur_platform_handle_t UrPlatform = nullptr;
ur_result_t redefinedDeviceGetInfoAfter(void *pParams) {
auto params = *static_cast<ur_device_get_info_params_t *>(pParams);
if (*params.ppropName == UR_DEVICE_INFO_SUPPORTED_PARTITIONS) {
if (*params.ppPropValue) {
auto *Result =
reinterpret_cast<ur_device_partition_t *>(*params.ppPropValue);
*Result = UR_DEVICE_PARTITION_EQUALLY;
}
if (*params.ppPropSizeRet)
**params.ppPropSizeRet = sizeof(ur_device_partition_t);
} else if (*params.ppropName == UR_DEVICE_INFO_MAX_COMPUTE_UNITS) {
auto *Result = reinterpret_cast<uint32_t *>(*params.ppPropValue);
*Result = 2;
} else if (*params.ppropName == UR_DEVICE_INFO_PARENT_DEVICE) {
auto *Result = reinterpret_cast<ur_device_handle_t *>(*params.ppPropValue);
*Result = (*params.phDevice == ParentDevice) ? nullptr : ParentDevice;
} else if (*params.ppropName == UR_DEVICE_INFO_PLATFORM) {
auto *Result =
reinterpret_cast<ur_platform_handle_t *>(*params.ppPropValue);
*Result = UrPlatform;
}
return UR_RESULT_SUCCESS;
}
ur_result_t redefinedDevicePartitionAfter(void *pParams) {
auto params = *static_cast<ur_device_partition_params_t *>(pParams);
if (*params.pphSubDevices) {
for (size_t I = 0; I < *params.pNumDevices; ++I) {
*params.pphSubDevices[I] = reinterpret_cast<ur_device_handle_t>(1000 + I);
}
}
if (*params.ppNumDevicesRet)
**params.ppNumDevicesRet = *params.pNumDevices;
return UR_RESULT_SUCCESS;
}
TEST(KernelBundle, DescendentDevice) {
// Mock a non-OpenCL plugin since use of descendent devices of context members
// is not supported there yet.
sycl::unittest::UrMock<sycl::backend::ext_oneapi_level_zero> Mock;
sycl::platform Plt = sycl::platform();
UrPlatform = sycl::detail::getSyclObjImpl(Plt)->getHandleRef();
mock::getCallbacks().set_after_callback("urDeviceGetInfo",
&redefinedDeviceGetInfoAfter);
mock::getCallbacks().set_after_callback("urDevicePartition",
&redefinedDevicePartitionAfter);
const sycl::device Dev = sycl::platform().get_devices()[0];
ParentDevice = sycl::detail::getSyclObjImpl(Dev)->getHandleRef();
sycl::context Ctx{Dev};
sycl::device Subdev =
Dev.create_sub_devices<sycl::info::partition_property::partition_equally>(
2)[0];
sycl::queue Queue{Ctx, Subdev};
sycl::kernel_bundle<sycl::bundle_state::executable> KernelBundle =
sycl::get_kernel_bundle<sycl::bundle_state::executable>(Ctx, {Subdev});
sycl::kernel Kernel =
KernelBundle.get_kernel(sycl::get_kernel_id<TestKernel>());
sycl::kernel_bundle<sycl::bundle_state::executable> RetKernelBundle =
Kernel.get_kernel_bundle();
EXPECT_EQ(KernelBundle, RetKernelBundle);
}
TEST(KernelBundle, CheckIfBundleHasIncompatibleKernel) {
sycl::unittest::UrMock<> Mock;
// TestKernelWithAspects has GPU aspect, so it shouldn't be compatible with
// the CPU device and hence shouldn't be in the kernel bundle.
mock::getCallbacks().set_after_callback("urDeviceGetInfo",
&redefinedDeviceGetInfoCPU);
sycl::platform Plt = sycl::platform();
const sycl::device Dev = Plt.get_devices()[0];
EXPECT_TRUE(Dev.is_cpu());
auto Bundle = sycl::get_kernel_bundle<sycl::bundle_state::executable>(
sycl::context(Dev), {Dev});
auto KernelId1 = sycl::get_kernel_id<TestKernelWithAspects>();
auto KernelId2 = sycl::get_kernel_id<TestKernel>();
EXPECT_FALSE(Bundle.has_kernel(KernelId1));
EXPECT_TRUE(Bundle.has_kernel(KernelId2));
}
TEST(KernelBundle, CheckIfBundleHasCompatibleKernel) {
sycl::unittest::UrMock<> Mock;
sycl::platform Plt = sycl::platform();
// GPU by default.
const sycl::device Dev = Plt.get_devices()[0];
EXPECT_TRUE(Dev.is_gpu());
auto Bundle = sycl::get_kernel_bundle<sycl::bundle_state::executable>(
sycl::context(Dev), {Dev});
auto KernelId1 = sycl::get_kernel_id<TestKernelWithAspects>();
auto KernelId2 = sycl::get_kernel_id<TestKernel>();
EXPECT_TRUE(Bundle.has_kernel(KernelId1));
EXPECT_TRUE(Bundle.has_kernel(KernelId2));
}
TEST(KernelBundle, CheckIfIncompatibleBundleExists) {
sycl::unittest::UrMock<> Mock;
// TestKernelWithAspects has GPU aspect, so it shouldn't be compatible with
// the CPU device and hence shouldn't be in the kernel bundle.
mock::getCallbacks().set_after_callback("urDeviceGetInfo",
&redefinedDeviceGetInfoCPU);
sycl::platform Plt = sycl::platform();
const sycl::device Dev = Plt.get_devices()[0];
EXPECT_TRUE(Dev.is_cpu());
auto KernelId1 = sycl::get_kernel_id<TestKernelWithAspects>();
auto KernelId2 = sycl::get_kernel_id<TestKernel>();
EXPECT_FALSE(sycl::has_kernel_bundle<sycl::bundle_state::executable>(
sycl::context(Dev), {KernelId1, KernelId2}));
EXPECT_FALSE(sycl::has_kernel_bundle<sycl::bundle_state::executable>(
sycl::context(Dev), {KernelId1}));
EXPECT_TRUE(sycl::has_kernel_bundle<sycl::bundle_state::executable>(
sycl::context(Dev), {KernelId2}));
}
TEST(KernelBundle, CheckIfCompatibleBundleExists2) {
sycl::unittest::UrMock<> Mock;
sycl::platform Plt = sycl::platform();
// GPU by default.
const sycl::device Dev = Plt.get_devices()[0];
EXPECT_TRUE(Dev.is_gpu());
auto KernelId1 = sycl::get_kernel_id<TestKernelWithAspects>();
auto KernelId2 = sycl::get_kernel_id<TestKernel>();
EXPECT_TRUE(sycl::has_kernel_bundle<sycl::bundle_state::executable>(
sycl::context(Dev), {KernelId1, KernelId2}));
}
TEST(KernelBundle, CheckExceptionIfKernelIncompatible) {
sycl::unittest::UrMock<> Mock;
// TestKernelWithAspects has GPU aspect, so it shouldn't be compatible with
// the CPU device and hence shouldn't be in the kernel bundle.
mock::getCallbacks().set_after_callback("urDeviceGetInfo",
&redefinedDeviceGetInfoCPU);
sycl::platform Plt = sycl::platform();
const sycl::device Dev = Plt.get_devices()[0];
EXPECT_TRUE(Dev.is_cpu());
auto KernelId = sycl::get_kernel_id<TestKernelWithAspects>();
std::string msg = "";
try {
auto Bundle = sycl::get_kernel_bundle<sycl::bundle_state::executable>(
sycl::context(Dev), {Dev}, {KernelId});
} catch (sycl::exception &e) {
msg = e.what();
}
EXPECT_EQ(msg, "Kernel is incompatible with all devices in devs");
}
TEST(KernelBundle, HasKernelForSubDevice) {
sycl::unittest::UrMock<> Mock;
mock::getCallbacks().set_after_callback("urDeviceGetInfo",
&redefinedDeviceGetInfoAfter);
mock::getCallbacks().set_after_callback("urDevicePartition",
&redefinedDevicePartitionAfter);
sycl::platform Plt = sycl::platform();
const sycl::device Dev = Plt.get_devices()[0];
UrPlatform = sycl::detail::getSyclObjImpl(Plt)->getHandleRef();
ParentDevice = sycl::detail::getSyclObjImpl(Dev)->getHandleRef();
sycl::kernel_bundle<sycl::bundle_state::executable> Bundle =
sycl::get_kernel_bundle<sycl::bundle_state::executable>(
sycl::context(Dev), {Dev});
sycl::kernel_id KernelId = sycl::get_kernel_id<TestKernel>();
EXPECT_TRUE(Bundle.has_kernel(KernelId));
sycl::device SubDev =
Dev.create_sub_devices<sycl::info::partition_property::partition_equally>(
2)[0];
std::vector<sycl::device> BundleDevs = Bundle.get_devices();
EXPECT_EQ(std::find(BundleDevs.begin(), BundleDevs.end(), SubDev),
BundleDevs.end())
<< "Sub-device should not be in the devices of the kernel bundle.";
EXPECT_FALSE(getSyclObjImpl(SubDev)->isRootDevice());
EXPECT_TRUE(Bundle.has_kernel(KernelId, SubDev));
}