From 16a24b0f61fae778db2e948f848332f88c1e8851 Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Fri, 29 Nov 2019 21:23:49 +0300 Subject: [PATCH 1/4] [SYCL] Add support for platform name and platform version in device whitelist PlatformName and PlatformVersion are now supported keys. Also all fields are optional now, so SYCL_DEVICE_WHITE_LIST="" matches any device Signed-off-by: Vlad Romanov --- sycl/source/detail/platform_impl.cpp | 43 +++++++++++++++++++-- sycl/test/config/white_list.cpp | 57 +++++++++++++++++++++------- 2 files changed, 82 insertions(+), 18 deletions(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index f3dfa35606ab4..57e26f4bee934 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -58,6 +58,12 @@ struct DevDescT { const char *devDriverVer = nullptr; int devDriverVerSize = 0; + + const char *pltName = nullptr; + int pltNameSize = 0; + + const char *pltVer = nullptr; + int pltVerSize = 0; }; static std::vector getWhiteListDesc() { @@ -65,9 +71,13 @@ static std::vector getWhiteListDesc() { if (!str) return {}; + + std::vector decDescs; const char devNameStr[] = "DeviceName"; const char driverVerStr[] = "DriverVersion"; + const char pltNameStr[] = "PlatformName"; + const char platformVerStr[] = "PlatformVersion"; decDescs.emplace_back(); while ('\0' != *str) { const char **valuePtr = nullptr; @@ -78,6 +88,14 @@ static std::vector getWhiteListDesc() { valuePtr = &decDescs.back().devName; size = &decDescs.back().devNameSize; str += sizeof(devNameStr) - 1; + } else if (0 == strncmp(pltNameStr, str, sizeof(pltNameStr) - 1)) { + valuePtr = &decDescs.back().pltName; + size = &decDescs.back().pltNameSize; + str += sizeof(pltNameStr) - 1; + } else if (0 == strncmp(platformVerStr, str, sizeof(platformVerStr) - 1)) { + valuePtr = &decDescs.back().pltVer; + size = &decDescs.back().pltVerSize; + str += sizeof(platformVerStr) - 1; } else if (0 == strncmp(driverVerStr, str, sizeof(driverVerStr) - 1)) { valuePtr = &decDescs.back().devDriverVer; size = &decDescs.back().devDriverVerSize; @@ -125,11 +143,19 @@ static std::vector getWhiteListDesc() { return decDescs; } -static void filterWhiteList(vector_class &pi_devices) { +static void filterWhiteList(vector_class &pi_devices, + RT::PiPlatform pi_platform) { const std::vector whiteList(getWhiteListDesc()); if (whiteList.empty()) return; + const string_class pltName = + sycl::detail::get_platform_info::get( + pi_platform); + + const string_class pltVer = sycl::detail::get_platform_info< + string_class, info::platform::version>::get(pi_platform); + int insertIDx = 0; for (RT::PiDevice dev : pi_devices) { const string_class devName = @@ -140,8 +166,17 @@ static void filterWhiteList(vector_class &pi_devices) { info::device::driver_version>::get(dev); for (const DevDescT &desc : whiteList) { - // At least device name is required field to consider the filter so far - if (nullptr == desc.devName || + if (nullptr != desc.pltName && + !std::regex_match( + pltName, std::regex(std::string(desc.pltName, desc.pltNameSize)))) + continue; + + if (nullptr != desc.pltVer && + !std::regex_match( + pltVer, std::regex(std::string(desc.pltVer, desc.pltVerSize)))) + continue; + + if (nullptr != desc.devName && !std::regex_match( devName, std::regex(std::string(desc.devName, desc.devNameSize)))) continue; @@ -179,7 +214,7 @@ platform_impl_pi::get_devices(info::device_type deviceType) const { // Filter out devices that are not present in the white list if (SYCLConfig::get()) - filterWhiteList(pi_devices); + filterWhiteList(pi_devices, m_platform); std::for_each(pi_devices.begin(), pi_devices.end(), [&res](const RT::PiDevice &a_pi_device) { diff --git a/sycl/test/config/white_list.cpp b/sycl/test/config/white_list.cpp index e3f9e11a5bc79..0219b64ced4db 100644 --- a/sycl/test/config/white_list.cpp +++ b/sycl/test/config/white_list.cpp @@ -1,8 +1,13 @@ // REQUIRES: cpu // RUN: %clangxx -fsycl %s -o %t.out -// RUN: env PRINT_DEVICE_INFO=1 %t.out > %t.conf -// RUN: env TEST_DEVICE_AVAILABLE=1 env SYCL_CONFIG_FILE_NAME=%t.conf %t.out -// RUN: env TEST_DEVICE_IS_NOT_AVAILABLE=1 env SYCL_DEVICE_WHITE_LIST="" %t.out +// +// RUN: env PRINT_DEVICE_INFO=1 %t.out > %t1.conf +// RUN: env TEST_DEVICE_AVAILABLE=1 env SYCL_CONFIG_FILE_NAME=%t1.conf %t.out +// +// RUN: env PRINT_PLATFORM_INFO=1 %t.out > %t2.conf +// RUN: env TEST_DEVICE_AVAILABLE=1 env SYCL_CONFIG_FILE_NAME=%t2.conf %t.out +// +// RUN: env TEST_DEVICE_IS_NOT_AVAILABLE=1 env SYCL_DEVICE_WHITE_LIST="PlatformName:{{SUCH NAME DOESN'T EXIST}}" %t.out #include #include @@ -12,25 +17,49 @@ using namespace cl; +static void replaceEscapeCharacters(std::string &Str) { + // As a stringwill be used as regexp pattern, we need to get rid of symbols + // that can be treated in a special way. Replace common special symbols with + // '.' which matches to any character + std::replace_if(Str.begin(), Str.end(), + [](const char Sym) { return '(' == Sym || ')' == Sym; }, '.'); +} + int main() { + // Expected that white list filter is not set + if (getenv("PRINT_PLATFORM_INFO")) { + for (const sycl::platform &Plt : sycl::platform::get_platforms()) + if (!Plt.is_host()) { + + std::string Name = Plt.get_info(); + const std::string Ver = + Plt.get_info(); + + replaceEscapeCharacters(Name); + + std::cout << "SYCL_DEVICE_WHITE_LIST=PlatformName:{{" << Name + << "}},PlatformVersion:{{" << Ver << "}}"; + + return 0; + } + throw std::runtime_error("Non host device is not found"); + } + // Expected that white list filter is not set if (getenv("PRINT_DEVICE_INFO")) { for (const sycl::platform &Plt : sycl::platform::get_platforms()) if (!Plt.is_host()) { const sycl::device Dev = Plt.get_devices().at(0); - std::string DevName = Dev.get_info(); - const std::string DevVer = + std::string Name = Dev.get_info(); + const std::string Ver = Dev.get_info(); - // As device name string will be used as regexp pattern, we need to - // get rid of symbols that can be treated in a special way. - // Replace common special symbols with '.' which matches to any sybmol - for (char &Sym : DevName) { - if (')' == Sym || '(' == Sym) - Sym = '.'; - } - std::cout << "SYCL_DEVICE_WHITE_LIST=DeviceName:{{" << DevName - << "}},DriverVersion:{{" << DevVer << "}}"; + + replaceEscapeCharacters(Name); + + std::cout << "SYCL_DEVICE_WHITE_LIST=DeviceName:{{" << Name + << "}},DriverVersion:{{" << Ver << "}}"; + return 0; } throw std::runtime_error("Non host device is not found"); From 64d13807f60424eabe21fd6638e9d0c9108c7cf3 Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Sat, 30 Nov 2019 18:44:04 +0300 Subject: [PATCH 2/4] apply comments and fix test. Signed-off-by: Vlad Romanov --- sycl/source/detail/platform_impl.cpp | 42 +++++++++++++++------------- sycl/test/config/white_list.cpp | 23 +++++++-------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 57e26f4bee934..912a4ce378adc 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -59,11 +59,11 @@ struct DevDescT { const char *devDriverVer = nullptr; int devDriverVerSize = 0; - const char *pltName = nullptr; - int pltNameSize = 0; + const char *platformName = nullptr; + int platformNameSize = 0; - const char *pltVer = nullptr; - int pltVerSize = 0; + const char *platformVer = nullptr; + int platformVerSize = 0; }; static std::vector getWhiteListDesc() { @@ -76,7 +76,7 @@ static std::vector getWhiteListDesc() { std::vector decDescs; const char devNameStr[] = "DeviceName"; const char driverVerStr[] = "DriverVersion"; - const char pltNameStr[] = "PlatformName"; + const char platformNameStr[] = "PlatformName"; const char platformVerStr[] = "PlatformVersion"; decDescs.emplace_back(); while ('\0' != *str) { @@ -88,13 +88,14 @@ static std::vector getWhiteListDesc() { valuePtr = &decDescs.back().devName; size = &decDescs.back().devNameSize; str += sizeof(devNameStr) - 1; - } else if (0 == strncmp(pltNameStr, str, sizeof(pltNameStr) - 1)) { - valuePtr = &decDescs.back().pltName; - size = &decDescs.back().pltNameSize; - str += sizeof(pltNameStr) - 1; + } else if (0 == + strncmp(platformNameStr, str, sizeof(platformNameStr) - 1)) { + valuePtr = &decDescs.back().platformName; + size = &decDescs.back().platformNameSize; + str += sizeof(platformNameStr) - 1; } else if (0 == strncmp(platformVerStr, str, sizeof(platformVerStr) - 1)) { - valuePtr = &decDescs.back().pltVer; - size = &decDescs.back().pltVerSize; + valuePtr = &decDescs.back().platformVer; + size = &decDescs.back().platformVerSize; str += sizeof(platformVerStr) - 1; } else if (0 == strncmp(driverVerStr, str, sizeof(driverVerStr) - 1)) { valuePtr = &decDescs.back().devDriverVer; @@ -149,31 +150,34 @@ static void filterWhiteList(vector_class &pi_devices, if (whiteList.empty()) return; - const string_class pltName = + const string_class platformName = sycl::detail::get_platform_info::get( pi_platform); - const string_class pltVer = sycl::detail::get_platform_info< + const string_class platformVer = sycl::detail::get_platform_info< string_class, info::platform::version>::get(pi_platform); int insertIDx = 0; for (RT::PiDevice dev : pi_devices) { const string_class devName = - sycl::detail::get_device_info::get(dev); + sycl::detail::get_device_info::get( + dev); const string_class devDriverVer = sycl::detail::get_device_info::get(dev); for (const DevDescT &desc : whiteList) { - if (nullptr != desc.pltName && - !std::regex_match( - pltName, std::regex(std::string(desc.pltName, desc.pltNameSize)))) + if (nullptr != desc.platformName && + !std::regex_match(platformName, + std::regex(std::string(desc.platformName, + desc.platformNameSize)))) continue; - if (nullptr != desc.pltVer && + if (nullptr != desc.platformVer && !std::regex_match( - pltVer, std::regex(std::string(desc.pltVer, desc.pltVerSize)))) + platformVer, + std::regex(std::string(desc.platformVer, desc.platformVerSize)))) continue; if (nullptr != desc.devName && diff --git a/sycl/test/config/white_list.cpp b/sycl/test/config/white_list.cpp index 0219b64ced4db..3cf639660066d 100644 --- a/sycl/test/config/white_list.cpp +++ b/sycl/test/config/white_list.cpp @@ -17,10 +17,8 @@ using namespace cl; -static void replaceEscapeCharacters(std::string &Str) { - // As a stringwill be used as regexp pattern, we need to get rid of symbols - // that can be treated in a special way. Replace common special symbols with - // '.' which matches to any character +static void replaceSpecialCharacters(std::string &Str) { + //Replace common special symbols with '.' which matches to any character std::replace_if(Str.begin(), Str.end(), [](const char Sym) { return '(' == Sym || ')' == Sym; }, '.'); } @@ -33,10 +31,11 @@ int main() { if (!Plt.is_host()) { std::string Name = Plt.get_info(); - const std::string Ver = - Plt.get_info(); - - replaceEscapeCharacters(Name); + std::string Ver = Plt.get_info(); + // As a string will be used as regexp pattern, we need to get rid of + // symbols that can be treated in a special way. + replaceSpecialCharacters(Name); + replaceSpecialCharacters(Ver); std::cout << "SYCL_DEVICE_WHITE_LIST=PlatformName:{{" << Name << "}},PlatformVersion:{{" << Ver << "}}"; @@ -52,10 +51,12 @@ int main() { if (!Plt.is_host()) { const sycl::device Dev = Plt.get_devices().at(0); std::string Name = Dev.get_info(); - const std::string Ver = - Dev.get_info(); + std::string Ver = Dev.get_info(); - replaceEscapeCharacters(Name); + // As a string will be used as regexp pattern, we need to get rid of + // symbols that can be treated in a special way. + replaceSpecialCharacters(Name); + replaceSpecialCharacters(Ver); std::cout << "SYCL_DEVICE_WHITE_LIST=DeviceName:{{" << Name << "}},DriverVersion:{{" << Ver << "}}"; From 6656a9125eba76bdcf8a937180ffa1af3d426cf3 Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Sat, 30 Nov 2019 18:54:00 +0300 Subject: [PATCH 3/4] apply comments Signed-off-by: Vlad Romanov --- sycl/source/detail/platform_impl.cpp | 2 -- sycl/test/config/white_list.cpp | 24 ++++++++++++------------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 912a4ce378adc..307390d2d3b23 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -71,8 +71,6 @@ static std::vector getWhiteListDesc() { if (!str) return {}; - - std::vector decDescs; const char devNameStr[] = "DeviceName"; const char driverVerStr[] = "DriverVersion"; diff --git a/sycl/test/config/white_list.cpp b/sycl/test/config/white_list.cpp index 3cf639660066d..4b3c756c7c646 100644 --- a/sycl/test/config/white_list.cpp +++ b/sycl/test/config/white_list.cpp @@ -27,11 +27,11 @@ int main() { // Expected that white list filter is not set if (getenv("PRINT_PLATFORM_INFO")) { - for (const sycl::platform &Plt : sycl::platform::get_platforms()) - if (!Plt.is_host()) { + for (const sycl::platform &Platform : sycl::platform::get_platforms()) + if (!Platform.is_host()) { - std::string Name = Plt.get_info(); - std::string Ver = Plt.get_info(); + std::string Name = Platform.get_info(); + std::string Ver = Platform.get_info(); // As a string will be used as regexp pattern, we need to get rid of // symbols that can be treated in a special way. replaceSpecialCharacters(Name); @@ -47,9 +47,9 @@ int main() { // Expected that white list filter is not set if (getenv("PRINT_DEVICE_INFO")) { - for (const sycl::platform &Plt : sycl::platform::get_platforms()) - if (!Plt.is_host()) { - const sycl::device Dev = Plt.get_devices().at(0); + for (const sycl::platform &Platform : sycl::platform::get_platforms()) + if (!Platform.is_host()) { + const sycl::device Dev = Platform.get_devices().at(0); std::string Name = Dev.get_info(); std::string Ver = Dev.get_info(); @@ -68,9 +68,9 @@ int main() { // Expected white list to be set with result from "PRINT_DEVICE_INFO" run if (getenv("TEST_DEVICE_AVAILABLE")) { - for (const sycl::platform &Plt : sycl::platform::get_platforms()) - if (!Plt.is_host()) { - if (Plt.get_devices().size() != 1) + for (const sycl::platform &Platform : sycl::platform::get_platforms()) + if (!Platform.is_host()) { + if (Platform.get_devices().size() != 1) throw std::runtime_error("Expected only one non host device."); return 0; @@ -80,8 +80,8 @@ int main() { // Expected white list to be set but empty if (getenv("TEST_DEVICE_IS_NOT_AVAILABLE")) { - for (const sycl::platform &Plt : sycl::platform::get_platforms()) - if (!Plt.is_host()) + for (const sycl::platform &Platform : sycl::platform::get_platforms()) + if (!Platform.is_host()) throw std::runtime_error("Expected no non host device is available"); return 0; } From 796505d493a1da20118ae328b4ecb7bfd0e446ec Mon Sep 17 00:00:00 2001 From: Vlad Romanov Date: Mon, 2 Dec 2019 13:29:28 +0300 Subject: [PATCH 4/4] fix space Signed-off-by: Vlad Romanov --- sycl/test/config/white_list.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/test/config/white_list.cpp b/sycl/test/config/white_list.cpp index 4b3c756c7c646..0b9db514828ea 100644 --- a/sycl/test/config/white_list.cpp +++ b/sycl/test/config/white_list.cpp @@ -18,7 +18,7 @@ using namespace cl; static void replaceSpecialCharacters(std::string &Str) { - //Replace common special symbols with '.' which matches to any character + // Replace common special symbols with '.' which matches to any character std::replace_if(Str.begin(), Str.end(), [](const char Sym) { return '(' == Sym || ')' == Sym; }, '.'); }