|
1 | 1 | // RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
|
2 |
| -// RUN: env SYCL_DEVICE_FILTER=0 %t.out |
3 |
| -// RUN: env SYCL_DEVICE_FILTER=1 %t.out |
4 |
| -// RUN: env SYCL_DEVICE_FILTER=2 %t.out |
5 |
| -// RUN: env SYCL_DEVICE_FILTER=3 %t.out |
6 |
| -// RUN: env SYCL_DEVICE_FILTER=4 %t.out |
| 2 | +// RUN: env PRINT_FULL_DEVICE_INFO=1 %t.out > %t1.conf |
| 3 | +// RUN: env SYCL_DEVICE_FILTER=0 env TEST_DEV_CONFIG_FILE_NAME=%t1.conf %t.out |
| 4 | +// RUN: env SYCL_DEVICE_FILTER=1 env TEST_DEV_CONFIG_FILE_NAME=%t1.conf %t.out |
| 5 | +// RUN: env SYCL_DEVICE_FILTER=2 env TEST_DEV_CONFIG_FILE_NAME=%t1.conf %t.out |
| 6 | +// RUN: env SYCL_DEVICE_FILTER=3 env TEST_DEV_CONFIG_FILE_NAME=%t1.conf %t.out |
7 | 7 |
|
8 | 8 | // The test is using all available BEs but CUDA machine in CI does not have
|
9 | 9 | // functional OpenCL RT
|
10 | 10 | // UNSUPPORTED: cuda || hip
|
11 | 11 |
|
12 | 12 | #include <CL/sycl.hpp>
|
13 | 13 | #include <iostream>
|
| 14 | +#include <map> |
14 | 15 |
|
15 | 16 | using namespace cl::sycl;
|
16 | 17 | using namespace std;
|
17 | 18 |
|
| 19 | +const std::map<info::device_type, std::string> DeviceTypeStringMap = { |
| 20 | + {info::device_type::cpu, "cpu"}, |
| 21 | + {info::device_type::gpu, "gpu"}, |
| 22 | + {info::device_type::host, "host"}, |
| 23 | + {info::device_type::accelerator, "acc"}}; |
| 24 | + |
| 25 | +const std::map<backend, std::string> BackendStringMap = { |
| 26 | + {backend::opencl, "opencl"}, |
| 27 | + {backend::host, "host"}, |
| 28 | + {backend::ext_oneapi_level_zero, "ext_oneapi_level_zero"}, |
| 29 | + {backend::ext_intel_esimd_emulator, "ext_intel_esimd_emulator"}}; |
| 30 | + |
| 31 | +std::string getDeviceTypeName(const device &d) { |
| 32 | + auto DeviceType = d.get_info<info::device::device_type>(); |
| 33 | + std::string DeviceTypeName = "unknown"; |
| 34 | + auto it = DeviceTypeStringMap.find(DeviceType); |
| 35 | + if (it != DeviceTypeStringMap.end()) |
| 36 | + DeviceTypeName = it->second; |
| 37 | + return DeviceTypeName; |
| 38 | +} |
| 39 | + |
18 | 40 | void printDeviceType(const device &d) {
|
19 | 41 | string name = d.get_platform().get_info<info::platform::name>();
|
20 |
| - auto DeviceType = d.get_info<info::device::device_type>(); |
21 |
| - std::string DeviceTypeName; |
22 |
| - |
23 |
| - switch (DeviceType) { |
24 |
| - case info::device_type::cpu: |
25 |
| - DeviceTypeName = "CPU "; |
26 |
| - break; |
27 |
| - case info::device_type::gpu: |
28 |
| - DeviceTypeName = "GPU "; |
29 |
| - break; |
30 |
| - case info::device_type::host: |
31 |
| - DeviceTypeName = "HOST "; |
32 |
| - break; |
33 |
| - case info::device_type::accelerator: |
34 |
| - DeviceTypeName = "ACCELERATOR "; |
35 |
| - break; |
36 |
| - default: |
37 |
| - DeviceTypeName = "UNKNOWN "; |
38 |
| - break; |
| 42 | + std::cout << getDeviceTypeName(d) << ", " << name << std::endl; |
| 43 | +} |
| 44 | + |
| 45 | +// Applicable for backend and device info. |
| 46 | +template <typename DevInfoEntry> |
| 47 | +DevInfoEntry |
| 48 | +getDeviceInfoByName(const std::string &name, |
| 49 | + const std::map<DevInfoEntry, std::string> &mapWithNames) { |
| 50 | + auto it = std::find_if( |
| 51 | + mapWithNames.begin(), mapWithNames.end(), |
| 52 | + [&name](auto &entry) { return entry.second.compare(name) == 0; }); |
| 53 | + // Invalid or unknown configuration if not found. |
| 54 | + assert(it != mapWithNames.end()); |
| 55 | + return it->first; |
| 56 | +} |
| 57 | + |
| 58 | +void PrintSystemConfiguration() { |
| 59 | + const auto &Platforms = platform::get_platforms(); |
| 60 | + |
| 61 | + // Keep track of the number of devices per backend. |
| 62 | + std::map<backend, size_t> DeviceNums; |
| 63 | + |
| 64 | + for (const auto &Platform : Platforms) { |
| 65 | + backend Backend = Platform.get_backend(); |
| 66 | + auto PlatformName = Platform.get_info<info::platform::name>(); |
| 67 | + const auto &Devices = Platform.get_devices(); |
| 68 | + for (const auto &Device : Devices) { |
| 69 | + std::cout << Backend << ":" << getDeviceTypeName(Device) << ":" |
| 70 | + << DeviceNums[Backend] << std::endl; |
| 71 | + ++DeviceNums[Backend]; |
| 72 | + } |
39 | 73 | }
|
40 |
| - std::cout << DeviceTypeName << name << std::endl; |
| 74 | +} |
| 75 | + |
| 76 | +using DevInfo = std::pair<info::device_type, backend>; |
| 77 | +using DevInfoMap = std::map<int, std::vector<DevInfo>>; |
| 78 | +bool ReadInitialSystemConfiguration(char *fileName, DevInfoMap &devices) { |
| 79 | + fstream confFile; |
| 80 | + confFile.open(fileName, ios::in); |
| 81 | + if (!confFile.is_open()) |
| 82 | + return false; |
| 83 | + char linebuf[64]; |
| 84 | + while (confFile.getline(linebuf, 64)) { |
| 85 | + std::istringstream entry(linebuf); |
| 86 | + std::string type, backend, devNum; |
| 87 | + if (!std::getline(entry, backend, ':')) |
| 88 | + return false; |
| 89 | + if (!std::getline(entry, type, ':')) |
| 90 | + return false; |
| 91 | + if (!std::getline(entry, devNum)) |
| 92 | + return false; |
| 93 | + devices[std::atoi(devNum.data())].push_back( |
| 94 | + make_pair(getDeviceInfoByName(type, DeviceTypeStringMap), |
| 95 | + getDeviceInfoByName(backend, BackendStringMap))); |
| 96 | + } |
| 97 | + if (devices.size() == 0) |
| 98 | + return false; |
| 99 | + return true; |
| 100 | +} |
| 101 | + |
| 102 | +int GetPreferredDeviceIndex(const std::vector<device> &devices, |
| 103 | + info::device_type type) { |
| 104 | + // Note: scores can be not the same as in runtime, just keep the same |
| 105 | + // preference. |
| 106 | + // gpu L0, opencl |
| 107 | + // cpu |
| 108 | + // acc |
| 109 | + // host |
| 110 | + const std::map<info::device_type, int> scoreByType = { |
| 111 | + {info::device_type::cpu, 300}, |
| 112 | + {info::device_type::gpu, 500}, |
| 113 | + {info::device_type::accelerator, 75}, |
| 114 | + {info::device_type::host, 100}}; |
| 115 | + int score = -1; |
| 116 | + int index = -1; |
| 117 | + int devCount = devices.size(); |
| 118 | + for (int i = 0; i < devCount; i++) { |
| 119 | + int dev_score = 0; |
| 120 | + auto deviceType = devices[i].get_info<info::device::device_type>(); |
| 121 | + auto backend = devices[i].get_backend(); |
| 122 | + if ((type != info::device_type::all) && (deviceType != type)) |
| 123 | + continue; |
| 124 | + dev_score = scoreByType.at(deviceType); |
| 125 | + if (backend == backend::ext_oneapi_level_zero) |
| 126 | + dev_score += 100; |
| 127 | + if (dev_score > score) { |
| 128 | + score = dev_score; |
| 129 | + index = i; |
| 130 | + } |
| 131 | + } |
| 132 | + return index; |
41 | 133 | }
|
42 | 134 |
|
43 | 135 | int main() {
|
| 136 | + // Expected that the sycl device filter is not set |
| 137 | + if (getenv("PRINT_FULL_DEVICE_INFO")) { |
| 138 | + PrintSystemConfiguration(); |
| 139 | + return 0; |
| 140 | + } |
| 141 | + |
| 142 | + DevInfoMap unfilteredDevices; |
| 143 | + assert(ReadInitialSystemConfiguration(getenv("TEST_DEV_CONFIG_FILE_NAME"), |
| 144 | + unfilteredDevices) && |
| 145 | + "Failed to parse file with initial system configuration data"); |
| 146 | + |
44 | 147 | const char *envVal = std::getenv("SYCL_DEVICE_FILTER");
|
45 | 148 | int deviceNum;
|
46 | 149 | std::cout << "SYCL_DEVICE_FILTER=" << envVal << std::endl;
|
47 | 150 | deviceNum = std::atoi(envVal);
|
48 | 151 |
|
49 | 152 | auto devices = device::get_devices();
|
50 |
| - if (devices.size() > deviceNum) { |
51 |
| - device targetDevice = devices[deviceNum]; |
52 |
| - std::cout << "Target Device: "; |
53 |
| - printDeviceType(targetDevice); |
54 |
| - |
55 |
| - { |
56 |
| - default_selector ds; |
57 |
| - device d = ds.select_device(); |
58 |
| - std::cout << "default_selector selected "; |
59 |
| - printDeviceType(d); |
60 |
| - assert(targetDevice == d && |
61 |
| - "The selected device is not the target device specified."); |
62 |
| - } |
| 153 | + std::cout << "Device count to analyze =" << devices.size() << std::endl; |
63 | 154 |
|
64 |
| - if (targetDevice.is_gpu()) { |
65 |
| - gpu_selector gs; |
66 |
| - device d = gs.select_device(); |
67 |
| - std::cout << "gpu_selector selected "; |
68 |
| - printDeviceType(d); |
69 |
| - assert(targetDevice == d && |
70 |
| - "The selected device is not the target device specified."); |
71 |
| - } else if (targetDevice.is_cpu()) { |
72 |
| - cpu_selector cs; |
73 |
| - device d = cs.select_device(); |
74 |
| - std::cout << "cpu_selector selected "; |
75 |
| - printDeviceType(d); |
76 |
| - assert(targetDevice == d && |
77 |
| - "The selected device is not the target device specified."); |
78 |
| - } else if (targetDevice.is_accelerator()) { |
79 |
| - accelerator_selector as; |
80 |
| - device d = as.select_device(); |
81 |
| - std::cout << "accelerator_selector selected "; |
82 |
| - printDeviceType(d); |
83 |
| - assert(targetDevice == d && |
84 |
| - "The selected device is not the target device specified."); |
85 |
| - } else if (targetDevice.is_host()) { |
86 |
| - host_selector hs; |
87 |
| - device d = hs.select_device(); |
88 |
| - std::cout << "host_selector selected "; |
89 |
| - printDeviceType(d); |
90 |
| - assert(targetDevice == d && "The selected device is not a host device."); |
91 |
| - } |
| 155 | + auto expectedDevices = unfilteredDevices[deviceNum]; |
| 156 | + size_t devCount = expectedDevices.size(); |
| 157 | + assert(devices.size() == devCount && |
| 158 | + "Devices seems to be filtered in a wrong way. Count of devices is " |
| 159 | + "unexpected."); |
| 160 | + |
| 161 | + if (devices.size() == 0) { |
| 162 | + std::cout << "No devices with such filter, skipping test." << std::endl; |
| 163 | + return 0; |
92 | 164 | }
|
| 165 | + |
| 166 | + for (int i = 0; i < devCount; i++) { |
| 167 | + auto deviceType = devices[i].get_info<info::device::device_type>(); |
| 168 | + assert(deviceType == std::get<0>(expectedDevices[i]) && |
| 169 | + "Device type or device order is not expected."); |
| 170 | + assert(devices[i].get_backend() == std::get<1>(expectedDevices[i]) && |
| 171 | + "Device backend or device order is not expected."); |
| 172 | + } |
| 173 | + |
| 174 | + int targetDevIndex = -1; |
| 175 | + { |
| 176 | + targetDevIndex = GetPreferredDeviceIndex(devices, info::device_type::all); |
| 177 | + assert(targetDevIndex >= 0 && |
| 178 | + "Failed to find target device for default selector."); |
| 179 | + default_selector ds; |
| 180 | + device d = ds.select_device(); |
| 181 | + std::cout << "default_selector selected "; |
| 182 | + printDeviceType(d); |
| 183 | + assert(devices[targetDevIndex] == d && |
| 184 | + "The selected device is not the target device specified."); |
| 185 | + } |
| 186 | + targetDevIndex = GetPreferredDeviceIndex(devices, info::device_type::gpu); |
| 187 | + if (targetDevIndex >= 0) { |
| 188 | + gpu_selector gs; |
| 189 | + device d = gs.select_device(); |
| 190 | + std::cout << "gpu_selector selected "; |
| 191 | + printDeviceType(d); |
| 192 | + assert(devices[targetDevIndex] == d && |
| 193 | + "The selected device is not the target device specified."); |
| 194 | + } |
| 195 | + targetDevIndex = GetPreferredDeviceIndex(devices, info::device_type::cpu); |
| 196 | + if (targetDevIndex >= 0) { |
| 197 | + cpu_selector cs; |
| 198 | + device d = cs.select_device(); |
| 199 | + std::cout << "cpu_selector selected "; |
| 200 | + printDeviceType(d); |
| 201 | + assert(devices[targetDevIndex] == d && |
| 202 | + "The selected device is not the target device specified."); |
| 203 | + } |
| 204 | + targetDevIndex = |
| 205 | + GetPreferredDeviceIndex(devices, info::device_type::accelerator); |
| 206 | + if (targetDevIndex >= 0) { |
| 207 | + accelerator_selector as; |
| 208 | + device d = as.select_device(); |
| 209 | + std::cout << "accelerator_selector selected "; |
| 210 | + printDeviceType(d); |
| 211 | + assert(devices[targetDevIndex] == d && |
| 212 | + "The selected device is not the target device specified."); |
| 213 | + } |
| 214 | + targetDevIndex = GetPreferredDeviceIndex(devices, info::device_type::host); |
| 215 | + assert((targetDevIndex >= 0 || deviceNum != 0) && |
| 216 | + "Failed to find host device."); |
| 217 | + if (targetDevIndex >= 0) { |
| 218 | + host_selector hs; |
| 219 | + device d = hs.select_device(); |
| 220 | + std::cout << "host_selector selected "; |
| 221 | + printDeviceType(d); |
| 222 | + assert(devices[targetDevIndex] == d && |
| 223 | + "The selected device is not a host device."); |
| 224 | + } |
| 225 | + |
93 | 226 | return 0;
|
94 | 227 | }
|
0 commit comments