Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYCL] Add '--ignore-device-selectors' CLI option to sycl-ls and improve warning messages #12718

Merged
merged 7 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Comment on lines +201 to +204
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use any command line options parsing libraries anywhere in sycl?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do use LLVM's CLI support (https://llvm.org/docs/CommandLine.html) at a couple of places but I'm not sure if we should use that here because we just have two CLI options for sycl-ls and both of them are just boolean flags.

// 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
Loading