diff --git a/SYCL/Basic/device-selectors.cpp b/SYCL/Basic/device-selectors.cpp new file mode 100644 index 0000000000..d1323f2d8f --- /dev/null +++ b/SYCL/Basic/device-selectors.cpp @@ -0,0 +1,97 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RUN: %t.out + +#include +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 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 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); + 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; +}