|
| 1 | +//==------------------- device_filter.cpp ----------------------------------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include <CL/sycl/detail/device_filter.hpp> |
| 10 | +#include <CL/sycl/info/info_desc.hpp> |
| 11 | +#include <detail/config.hpp> |
| 12 | +#include <detail/device_impl.hpp> |
| 13 | + |
| 14 | +#include <cstring> |
| 15 | + |
| 16 | +__SYCL_INLINE_NAMESPACE(cl) { |
| 17 | +namespace sycl { |
| 18 | +namespace detail { |
| 19 | + |
| 20 | +device_filter::device_filter(const std::string &FilterString) { |
| 21 | + const std::array<std::pair<std::string, info::device_type>, 5> |
| 22 | + SyclDeviceTypeMap = {{{"host", info::device_type::host}, |
| 23 | + {"cpu", info::device_type::cpu}, |
| 24 | + {"gpu", info::device_type::gpu}, |
| 25 | + {"acc", info::device_type::accelerator}, |
| 26 | + {"*", info::device_type::all}}}; |
| 27 | + const std::array<std::pair<std::string, backend>, 5> SyclBeMap = { |
| 28 | + {{"host", backend::host}, |
| 29 | + {"opencl", backend::opencl}, |
| 30 | + {"level_zero", backend::level_zero}, |
| 31 | + {"cuda", backend::cuda}, |
| 32 | + {"*", backend::all}}}; |
| 33 | + |
| 34 | + size_t Cursor = 0; |
| 35 | + size_t ColonPos = 0; |
| 36 | + auto findElement = [&](auto Element) { |
| 37 | + size_t Found = FilterString.find(Element.first, Cursor); |
| 38 | + if (Found == std::string::npos) |
| 39 | + return false; |
| 40 | + Cursor = Found; |
| 41 | + return true; |
| 42 | + }; |
| 43 | + auto selectElement = [&](auto It, auto Map, auto EltIfNotFound) { |
| 44 | + if (It == Map.end()) |
| 45 | + return EltIfNotFound; |
| 46 | + ColonPos = FilterString.find(":", Cursor); |
| 47 | + if (ColonPos != std::string::npos) |
| 48 | + Cursor = ColonPos + 1; |
| 49 | + else |
| 50 | + Cursor = Cursor + It->first.size(); |
| 51 | + return It->second; |
| 52 | + }; |
| 53 | + |
| 54 | + // Handle the optional 1st field of the filter, backend |
| 55 | + // Check if the first entry matches with a known backend type |
| 56 | + auto It = |
| 57 | + std::find_if(std::begin(SyclBeMap), std::end(SyclBeMap), findElement); |
| 58 | + // If no match is found, set the backend type backend::all |
| 59 | + // which actually means 'any backend' will be a match. |
| 60 | + Backend = selectElement(It, SyclBeMap, backend::all); |
| 61 | + |
| 62 | + // Handle the optional 2nd field of the filter - device type. |
| 63 | + // Check if the 2nd entry matches with any known device type. |
| 64 | + if (Cursor >= FilterString.size()) { |
| 65 | + DeviceType = info::device_type::all; |
| 66 | + } else { |
| 67 | + auto Iter = std::find_if(std::begin(SyclDeviceTypeMap), |
| 68 | + std::end(SyclDeviceTypeMap), findElement); |
| 69 | + // If no match is found, set device_type 'all', |
| 70 | + // which actually means 'any device_type' will be a match. |
| 71 | + DeviceType = selectElement(Iter, SyclDeviceTypeMap, info::device_type::all); |
| 72 | + } |
| 73 | + |
| 74 | + // Handle the optional 3rd field of the filter, device number |
| 75 | + // Try to convert the remaining string to an integer. |
| 76 | + // If succeessful, the converted integer is the desired device num. |
| 77 | + if (Cursor < FilterString.size()) { |
| 78 | + try { |
| 79 | + DeviceNum = stoi(FilterString.substr(ColonPos + 1)); |
| 80 | + HasDeviceNum = true; |
| 81 | + } catch (...) { |
| 82 | + std::string Message = |
| 83 | + std::string("Invalid device filter: ") + FilterString + |
| 84 | + "\nPossible backend values are {host,opencl,level_zero,cuda,*}.\n" |
| 85 | + "Possible device types are {host,cpu,gpu,acc,*}.\n" |
| 86 | + "Device number should be an non-negative integer.\n"; |
| 87 | + throw cl::sycl::invalid_parameter_error(Message, PI_INVALID_VALUE); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +device_filter_list::device_filter_list(const std::string &FilterStr) { |
| 93 | + // First, change the string in all lowercase. |
| 94 | + // This means we allow the user to use both uppercase and lowercase strings. |
| 95 | + std::string FilterString = FilterStr; |
| 96 | + std::transform(FilterString.begin(), FilterString.end(), FilterString.begin(), |
| 97 | + ::tolower); |
| 98 | + // SYCL_DEVICE_FILTER can set multiple filters separated by commas. |
| 99 | + // convert each filter triple string into an istance of device_filter class. |
| 100 | + size_t Pos = 0; |
| 101 | + while (Pos < FilterString.size()) { |
| 102 | + size_t CommaPos = FilterString.find(",", Pos); |
| 103 | + if (CommaPos == std::string::npos) { |
| 104 | + CommaPos = FilterString.size(); |
| 105 | + } |
| 106 | + std::string SubString = FilterString.substr(Pos, CommaPos - Pos); |
| 107 | + FilterList.push_back(device_filter(SubString)); |
| 108 | + Pos = CommaPos + 1; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +device_filter_list::device_filter_list(device_filter &Filter) { |
| 113 | + FilterList.push_back(Filter); |
| 114 | +} |
| 115 | + |
| 116 | +void device_filter_list::addFilter(device_filter &Filter) { |
| 117 | + FilterList.push_back(Filter); |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace detail |
| 121 | +} // namespace sycl |
| 122 | +} // __SYCL_INLINE_NAMESPACE(cl) |
0 commit comments