Skip to content

Commit

Permalink
[SYCL] Add '--ignore-device-selectors' CLI option to sycl-ls and impr…
Browse files Browse the repository at this point in the history
…ove 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.
  • Loading branch information
uditagarwal97 authored Feb 23, 2024
1 parent 652d3ea commit 6e3aa21
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 26 deletions.
19 changes: 17 additions & 2 deletions sycl/test/tools/sycl-ls.test
Original file line number Diff line number Diff line change
@@ -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
128 changes: 104 additions & 24 deletions sycl/tools/sycl-ls/sycl-ls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@
#include <iostream>
#include <map>
#include <stdlib.h>
#include <vector>

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<std::string> FilterEnvVars;

// Trivial custom selector that selects a device of the given type.
class custom_selector : public device_selector {
info::device_type MType;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6e3aa21

Please sign in to comment.