|
| 1 | +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out |
| 2 | +// RUN: sycl-ls |
| 3 | +// RUN: env SYCL_DEVICE_FILTER=0 %t.out |
| 4 | +// RUN: env SYCL_DEVICE_FILTER=1 %t.out |
| 5 | +// RUN: env SYCL_DEVICE_FILTER=2 %t.out |
| 6 | +// RUN: env SYCL_DEVICE_FILTER=3 %t.out |
| 7 | +// RUN: env SYCL_DEVICE_FILTER=4 %t.out |
| 8 | + |
| 9 | +#include <CL/sycl.hpp> |
| 10 | +#include <iostream> |
| 11 | + |
| 12 | +using namespace cl::sycl; |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +void printDeviceType(const device &d) { |
| 16 | + string name = d.get_platform().get_info<info::platform::name>(); |
| 17 | + auto DeviceType = d.get_info<info::device::device_type>(); |
| 18 | + std::string DeviceTypeName; |
| 19 | + |
| 20 | + switch (DeviceType) { |
| 21 | + case info::device_type::cpu: |
| 22 | + DeviceTypeName = "CPU "; |
| 23 | + break; |
| 24 | + case info::device_type::gpu: |
| 25 | + DeviceTypeName = "GPU "; |
| 26 | + break; |
| 27 | + case info::device_type::host: |
| 28 | + DeviceTypeName = "HOST "; |
| 29 | + break; |
| 30 | + case info::device_type::accelerator: |
| 31 | + DeviceTypeName = "ACCELERATOR "; |
| 32 | + break; |
| 33 | + default: |
| 34 | + DeviceTypeName = "UNKNOWN "; |
| 35 | + break; |
| 36 | + } |
| 37 | + std::cout << DeviceTypeName << name << std::endl; |
| 38 | +} |
| 39 | + |
| 40 | +int main() { |
| 41 | + const char *envVal = std::getenv("SYCL_DEVICE_FILTER"); |
| 42 | + int deviceNum; |
| 43 | + std::cout << "SYCL_DEVICE_FILTER=" << envVal << std::endl; |
| 44 | + deviceNum = std::atoi(envVal); |
| 45 | + |
| 46 | + auto devices = device::get_devices(); |
| 47 | + if (devices.size() >= deviceNum) { |
| 48 | + device targetDevice = devices[deviceNum]; |
| 49 | + std::cout << "Target Device: "; |
| 50 | + printDeviceType(targetDevice); |
| 51 | + |
| 52 | + { |
| 53 | + default_selector ds; |
| 54 | + device d = ds.select_device(); |
| 55 | + std::cout << "default_selector selected "; |
| 56 | + printDeviceType(d); |
| 57 | + assert(targetDevice == d && |
| 58 | + "The selected device is not the target device specified."); |
| 59 | + } |
| 60 | + |
| 61 | + if (targetDevice.is_gpu()) { |
| 62 | + gpu_selector gs; |
| 63 | + device d = gs.select_device(); |
| 64 | + std::cout << "gpu_selector selected "; |
| 65 | + printDeviceType(d); |
| 66 | + assert(targetDevice == d && |
| 67 | + "The selected device is not the target device specified."); |
| 68 | + } else if (targetDevice.is_cpu()) { |
| 69 | + cpu_selector cs; |
| 70 | + device d = cs.select_device(); |
| 71 | + std::cout << "cpu_selector selected "; |
| 72 | + printDeviceType(d); |
| 73 | + assert(targetDevice == d && |
| 74 | + "The selected device is not the target device specified."); |
| 75 | + } else if (targetDevice.is_accelerator()) { |
| 76 | + accelerator_selector as; |
| 77 | + device d = as.select_device(); |
| 78 | + std::cout << "accelerator_selector selected "; |
| 79 | + printDeviceType(d); |
| 80 | + assert(targetDevice == d && |
| 81 | + "The selected device is not the target device specified."); |
| 82 | + } |
| 83 | + // HOST device is always available regardless of SYCL_DEVICE_FILTER |
| 84 | + { |
| 85 | + host_selector hs; |
| 86 | + device d = hs.select_device(); |
| 87 | + std::cout << "host_selector selected "; |
| 88 | + printDeviceType(d); |
| 89 | + assert(d.is_host() && "The selected device is not a host device."); |
| 90 | + } |
| 91 | + } |
| 92 | + return 0; |
| 93 | +} |
0 commit comments