From 6e3aa21803a28780279a49ad5aa72632307d6dc6 Mon Sep 17 00:00:00 2001 From: Udit Agarwal <16324601+uditagarwal97@users.noreply.github.com> Date: Fri, 23 Feb 2024 00:05:11 -0800 Subject: [PATCH] [SYCL] Add '--ignore-device-selectors' CLI option to sycl-ls and improve warning messages (#12718) This PR adds a '--ignore-device-selectors' CLI option to sycl-ls that prints all platforms available in the user's system, irrespective of the DPCPP filter environment variables like ONEAPI_DEVICE_SELECTOR. --- sycl/test/tools/sycl-ls.test | 19 ++++- sycl/tools/sycl-ls/sycl-ls.cpp | 128 ++++++++++++++++++++++++++------- 2 files changed, 121 insertions(+), 26 deletions(-) diff --git a/sycl/test/tools/sycl-ls.test b/sycl/test/tools/sycl-ls.test index 422f2edbdda23..9e9fc9b079c37 100644 --- a/sycl/test/tools/sycl-ls.test +++ b/sycl/test/tools/sycl-ls.test @@ -1,3 +1,18 @@ --- Check that sycl-ls exits with 0 exit code. +-- Check sycl-ls exit code and output. -RUN: sycl-ls --verbose +RUN: sycl-ls --verbose > vanilla_verbose.out +RUN: sycl-ls > vanilla.out + +-- Check the functioning of '--ignore-device-selectors' CLI option. + +RUN: env ONEAPI_DEVICE_SELECTOR="opencl:*" sycl-ls --ignore-device-selectors > ods_ignore_device_selector.out +RUN: diff vanilla.out ods_ignore_device_selector.out + +RUN: env ONEAPI_DEVICE_SELECTOR="opencl:*" sycl-ls --ignore-device-selectors --verbose > ods_ignore_device_selector_v.out +RUN: diff vanilla_verbose.out ods_ignore_device_selector_v.out + +RUN: env SYCL_DEVICE_ALLOWLIST="BackendName:opencl" sycl-ls --ignore-device-selectors > sda_ignore_device_selector.out +RUN: diff vanilla.out sda_ignore_device_selector.out + +RUN: env SYCL_DEVICE_ALLOWLIST="BackendName:opencl" sycl-ls --ignore-device-selectors --verbose > sda_ignore_device_selector_v.out +RUN: diff vanilla_verbose.out sda_ignore_device_selector_v.out diff --git a/sycl/tools/sycl-ls/sycl-ls.cpp b/sycl/tools/sycl-ls/sycl-ls.cpp index ef2ea8fd2f121..1e7e69964cd6a 100644 --- a/sycl/tools/sycl-ls/sycl-ls.cpp +++ b/sycl/tools/sycl-ls/sycl-ls.cpp @@ -22,12 +22,20 @@ #include #include #include +#include using namespace sycl; +using namespace std::literals; // Controls verbose output vs. concise. bool verbose; +// Controls whether to discard filter environment variables or not. +bool DiscardFilters; + +// To store various filter environment variables. +std::vector FilterEnvVars; + // Trivial custom selector that selects a device of the given type. class custom_selector : public device_selector { info::device_type MType; @@ -105,44 +113,116 @@ static void printSelectorChoice(const device_selector &Selector, } } -int main(int argc, char **argv) { +static int printUsageAndExit() { + std::cout << "Usage: sycl-ls [--verbose] [--ignore-device-selectors]" + << std::endl; + std::cout << "This program lists all devices and backends discovered by SYCL." + << std::endl; + std::cout << "\n Options:" << std::endl; + std::cout + << "\t --verbose " << "\t Verbosely prints all the discovered platforms. " + << "It also lists the device chosen by various SYCL device selectors." + << std::endl; + std::cout + << "\t --ignore-device-selectors " + << "\t Lists all platforms available on the system irrespective " + << "of DPCPP filter environment variables (like ONEAPI_DEVICE_SELECTOR)." + << std::endl; - // See if verbose output is requested - if (argc == 1) - verbose = false; - else if (argc == 2 && std::string(argv[1]) == "--verbose") - verbose = true; - else { - std::cout << "Usage: sycl-ls [--verbose]" << std::endl; - return EXIT_FAILURE; - } + return EXIT_FAILURE; +} - bool SuppressNumberPrinting = false; +// Print warning and suppress printing device ids if any of +// the filter environment variable is set. +static void printWarningIfFiltersUsed(bool &SuppressNumberPrinting) { #ifndef __INTEL_PREVIEW_BREAKING_CHANGES const char *filter = std::getenv("SYCL_DEVICE_FILTER"); if (filter) { - std::cerr << "Warning: SYCL_DEVICE_FILTER environment variable is set to " - << filter << "." << std::endl; - std::cerr << "To see device ids, please unset SYCL_DEVICE_FILTER." - << std::endl - << std::endl; - SuppressNumberPrinting = true; + if (!DiscardFilters) { + std::cerr << "INFO: Output filtered by SYCL_DEVICE_FILTER " + << "environment variable, which is set to " << filter << "." + << std::endl; + std::cerr + << "To see device ids, use the --ignore-device-selectors CLI option." + << std::endl + << std::endl; + SuppressNumberPrinting = true; + } else + FilterEnvVars.push_back("SYCL_DEVICE_FILTER"); } #endif const char *ods_targets = std::getenv("ONEAPI_DEVICE_SELECTOR"); if (ods_targets) { - std::cerr - << "Warning: ONEAPI_DEVICE_SELECTOR environment variable is set to " - << ods_targets << "." << std::endl; - std::cerr << "To see device ids, please unset ONEAPI_DEVICE_SELECTOR." - << std::endl - << std::endl; - SuppressNumberPrinting = true; + if (!DiscardFilters) { + std::cerr << "INFO: Output filtered by ONEAPI_DEVICE_SELECTOR " + << "environment variable, which is set to " << ods_targets + << "." << std::endl; + std::cerr + << "To see device ids, use the --ignore-device-selectors CLI option." + << std::endl + << std::endl; + SuppressNumberPrinting = true; + } else + FilterEnvVars.push_back("ONEAPI_DEVICE_SELECTOR"); + } + + const char *sycl_dev_allow = std::getenv("SYCL_DEVICE_ALLOWLIST"); + if (sycl_dev_allow) { + if (!DiscardFilters) { + std::cerr << "INFO: Output filtered by SYCL_DEVICE_ALLOWLIST " + << "environment variable, which is set to " << sycl_dev_allow + << "." << std::endl; + std::cerr + << "To see device ids, use the --ignore-device-selectors CLI option." + << std::endl + << std::endl; + SuppressNumberPrinting = true; + } else + FilterEnvVars.push_back("SYCL_DEVICE_ALLOWLIST"); + } +} + +// Unset filter related environment variables namely, SYCL_DEVICE_FILTER, +// ONEAPI_DEVICE_SELECTOR, and SYCL_DEVICE_ALLOWLIST. +static void unsetFilterEnvVars() { + for (auto it : FilterEnvVars) { +#ifdef _WIN32 + _putenv_s(it.c_str(), ""); +#else + unsetenv(it.c_str()); +#endif } +} + +int main(int argc, char **argv) { + + if (argc == 1) { + verbose = false; + DiscardFilters = false; + } else { + // Parse CLI options. + for (int i = 1; i < argc; i++) { + if (argv[i] == "--verbose"sv) + verbose = true; + else if (argv[i] == "--ignore-device-selectors"sv) + DiscardFilters = true; + else + return printUsageAndExit(); + } + } + + bool SuppressNumberPrinting = false; + // Print warning and suppress printing device ids if any of + // the filter environment variable is set. + printWarningIfFiltersUsed(SuppressNumberPrinting); try { + // Unset all filter env. vars to get all available devices in the system. + if (DiscardFilters) + unsetFilterEnvVars(); + const auto &Platforms = platform::get_platforms(); // Keep track of the number of devices per backend