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

[SYCL] new tests for SYCL 2020 callable device selector interfaces #1119

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions SYCL/Basic/device-selectors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUN: %t.out

#include <sycl/sycl.hpp>
using namespace sycl;

auto exception_handler_lambda = [](exception_list elist) {
// nop
};

void handle_exceptions_f(exception_list elist) {
// nop
}

auto refuse_any_device_lambda = [](const device &d) { return -1; };

int refuse_any_device_f(const device &d) { return -1; }

int main() {
// Ensure these compile correctly. That exception handler does not
// end up specialized against device selector.
queue exception_queue_01(exception_handler_lambda);
queue exception_queue_02(handle_exceptions_f);

// Set up callable device selector.
std::vector<device> deviceList = device::get_devices();
device &lastDevice = deviceList.back();
auto select_last_device = [&lastDevice](const device &d) {
return d == lastDevice;
};

// Instantiate platform via callable Device Selector.
platform lastPlatform(select_last_device);

// Test each of the four queue constructors that take callable Device
// Selectors: q(device selector) q(dev selector , async handler)
// q(context , dev selector) q(context, dev selector , async handler)
queue lastQueue(select_last_device);
assert(lastQueue.get_device().get_platform() == lastPlatform &&
"Queue and platform created by selecting same device, should result "
"in matching platforms.");

queue lastQueueWithHandler(select_last_device, exception_handler_lambda);
assert(lastQueueWithHandler.get_device() == lastDevice &&
"Queue created by selecting a device should have that same device.");

// Create a context.
platform plt;
std::vector<device> platformDevices = plt.get_devices();
context ctx(platformDevices);
// Set up a callable device selector to select the last device from the
// context.
device &lastPlatformDevice = platformDevices.back();
auto select_last_platform_device = [&lastPlatformDevice](const device &d) {
return d == lastPlatformDevice;
};
// Test queue constructors that use devices from a context.
queue lastQueueViaContext(ctx, select_last_platform_device);
assert(lastQueueViaContext.get_device() == lastPlatformDevice &&
"Queue created by selecting a device should have that same device.");

queue lastQueueViaContextWithHandler(ctx, select_last_platform_device,
handle_exceptions_f);
assert(lastQueueViaContextWithHandler.get_device() == lastPlatformDevice &&
"Queue created by selecting a device should have that same device.");

// Device constructor.
device matchingDevice(select_last_device);
assert(matchingDevice == lastDevice && "Incorrect selected device.");

// Check exceptions and failures.
try {
platform refusedPlatform(refuse_any_device_lambda);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the above suggestion is implemented, then we can use p/q/d as the variables' names.

assert(false &&
"Declined device selection should have resulted in an exception.");
} catch (sycl::exception &e) {
assert(e.code() == sycl::errc::runtime && "Incorrect error code.");
}

try {
queue refusedQueue(refuse_any_device_f);
assert(false &&
"Declined device selection should have resulted in an exception.");
} catch (sycl::exception &e) {
assert(e.code() == sycl::errc::runtime && "Incorrect error code.");
}

try {
device refusedDevice(refuse_any_device_f);
assert(false &&
"Declined device selection should have resulted in an exception.");
} catch (sycl::exception &e) {
assert(e.code() == sycl::errc::runtime && "Incorrect error code.");
}

return 0;
}