Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 9d35612

Browse files
[SYCL] new tests for SYCL 2020 callable device selector interfaces (#1119)
1 parent 23e8f46 commit 9d35612

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

SYCL/Basic/device-selectors.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
2+
// RUN: %t.out
3+
4+
#include <sycl/sycl.hpp>
5+
using namespace sycl;
6+
7+
auto exception_handler_lambda = [](exception_list elist) {
8+
// nop
9+
};
10+
11+
void handle_exceptions_f(exception_list elist) {
12+
// nop
13+
}
14+
15+
auto refuse_any_device_lambda = [](const device &d) { return -1; };
16+
17+
int refuse_any_device_f(const device &d) { return -1; }
18+
19+
int main() {
20+
// Ensure these compile correctly. That exception handler does not
21+
// end up specialized against device selector.
22+
queue exception_queue_01(exception_handler_lambda);
23+
queue exception_queue_02(handle_exceptions_f);
24+
25+
// Set up callable device selector.
26+
std::vector<device> deviceList = device::get_devices();
27+
device &lastDevice = deviceList.back();
28+
auto select_last_device = [&lastDevice](const device &d) {
29+
return d == lastDevice;
30+
};
31+
32+
// Instantiate platform via callable Device Selector.
33+
platform lastPlatform(select_last_device);
34+
35+
// Test each of the four queue constructors that take callable Device
36+
// Selectors: q(device selector) q(dev selector , async handler)
37+
// q(context , dev selector) q(context, dev selector , async handler)
38+
queue lastQueue(select_last_device);
39+
assert(lastQueue.get_device().get_platform() == lastPlatform &&
40+
"Queue and platform created by selecting same device, should result "
41+
"in matching platforms.");
42+
43+
queue lastQueueWithHandler(select_last_device, exception_handler_lambda);
44+
assert(lastQueueWithHandler.get_device() == lastDevice &&
45+
"Queue created by selecting a device should have that same device.");
46+
47+
// Create a context.
48+
platform plt;
49+
std::vector<device> platformDevices = plt.get_devices();
50+
context ctx(platformDevices);
51+
// Set up a callable device selector to select the last device from the
52+
// context.
53+
device &lastPlatformDevice = platformDevices.back();
54+
auto select_last_platform_device = [&lastPlatformDevice](const device &d) {
55+
return d == lastPlatformDevice;
56+
};
57+
// Test queue constructors that use devices from a context.
58+
queue lastQueueViaContext(ctx, select_last_platform_device);
59+
assert(lastQueueViaContext.get_device() == lastPlatformDevice &&
60+
"Queue created by selecting a device should have that same device.");
61+
62+
queue lastQueueViaContextWithHandler(ctx, select_last_platform_device,
63+
handle_exceptions_f);
64+
assert(lastQueueViaContextWithHandler.get_device() == lastPlatformDevice &&
65+
"Queue created by selecting a device should have that same device.");
66+
67+
// Device constructor.
68+
device matchingDevice(select_last_device);
69+
assert(matchingDevice == lastDevice && "Incorrect selected device.");
70+
71+
// Check exceptions and failures.
72+
try {
73+
platform refusedPlatform(refuse_any_device_lambda);
74+
assert(false &&
75+
"Declined device selection should have resulted in an exception.");
76+
} catch (sycl::exception &e) {
77+
assert(e.code() == sycl::errc::runtime && "Incorrect error code.");
78+
}
79+
80+
try {
81+
queue refusedQueue(refuse_any_device_f);
82+
assert(false &&
83+
"Declined device selection should have resulted in an exception.");
84+
} catch (sycl::exception &e) {
85+
assert(e.code() == sycl::errc::runtime && "Incorrect error code.");
86+
}
87+
88+
try {
89+
device refusedDevice(refuse_any_device_f);
90+
assert(false &&
91+
"Declined device selection should have resulted in an exception.");
92+
} catch (sycl::exception &e) {
93+
assert(e.code() == sycl::errc::runtime && "Incorrect error code.");
94+
}
95+
96+
return 0;
97+
}

0 commit comments

Comments
 (0)