diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 629f04618fa06..95f624c7d243c 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -12,8 +12,9 @@ subject to change. Do not rely on these variables in production code. | Environment variable | Values | Description | | -------------------- | ------ | ----------- | | SYCL_PI_TRACE | Described [below](#sycl_pi_trace-options) | Enable specified level of tracing for PI. | -| SYCL_BE | PI_OPENCL, PI_LEVEL_ZERO, PI_CUDA | Force SYCL RT to consider only devices of the specified backend during the device selection. | -| SYCL_DEVICE_TYPE | One of: CPU, GPU, ACC, HOST | Force SYCL to use the specified device type. If unset, default selection rules are applied. If set to any unlisted value, this control has no effect. If the requested device type is not found, a `cl::sycl::runtime_error` exception is thrown. If a non-default device selector is used, a device must satisfy both the selector and this control to be chosen. This control only has effect on devices created with a selector. | +| SYCL_BE | PI_OPENCL, PI_LEVEL_ZERO, PI_CUDA | Force SYCL RT to consider only devices of the specified backend during the device selection. We are planning to deprecate SYCL_BE environment variable in the future. The specific grace period is not decided yet. Please use the new env var SYCL_DEVICE_FILTER instead. | +| SYCL_DEVICE_TYPE | One of: CPU, GPU, ACC, HOST | Force SYCL to use the specified device type. If unset, default selection rules are applied. If set to any unlisted value, this control has no effect. If the requested device type is not found, a `cl::sycl::runtime_error` exception is thrown. If a non-default device selector is used, a device must satisfy both the selector and this control to be chosen. This control only has effect on devices created with a selector. We are planning to deprecate SYCL_DEVICE_TYPE environment variable in the future. The specific grace period is not decided yet. Please use the new env var SYCL_DEVICE_FILTER instead. | +| SYCL_DEVICE_FILTER (tentative name) | {backend:device_type:device_num} | Limits the SYCL RT to use only a subset of the system's devices. Setting this environment variable affects all of the device query functions and all of the device selectors. The value of this environment variable is a comma separated list of filters, where each filter is a triple of the form "backend:device_type:device_num" (without the quotes). Each element of the triple is optional, but each filter must have at least one value. Possible values of "backend" are "host", "level_zero", "opencl", "cuda", or "\*". Possible values of "device_type" are "host", "cpu", "gpu", "acc", or "\*". Device_num is an integer that indexes the enumeration of devices from the sycl::platform::get_device() call, where the first device in that enumeration has index zero. Assuming a filter has all three elements of the triple, it selects only those devices that come from the given backend, have the specified device type, AND have the given device index. If more than one filter is specified, the RT is restricted to the union of devices selected by all filters. The RT always includes the "host" backend and the host device regardless of the filter because the SYCL language requires this device to always be present. Therefore, including "host" in the list of filters is allowed but is unnecessary. Note that the standard selectors like gpu_selector or cpu_selector will throw an exception if the filtered list of devices does not include a device that satisfies the selector. In particular, limiting the devices to only those supported by the "level_zero" backend will cause the cpu_selector to throw an exception since that backend does not support any CPU devices. This environment variable can be used to limit loading only specified plugins into the SYCL RT. | | SYCL_PROGRAM_COMPILE_OPTIONS | String of valid OpenCL compile options | Override compile options for all programs. | | SYCL_PROGRAM_LINK_OPTIONS | String of valid OpenCL link options | Override link options for all programs. | | SYCL_USE_KERNEL_SPV | Path to the SPIR-V binary | Load device image from the specified file. If runtime is unable to read the file, `cl::sycl::runtime_error` exception is thrown.| diff --git a/sycl/include/CL/sycl/backend_types.hpp b/sycl/include/CL/sycl/backend_types.hpp index 5cf9ab922124f..80885a47c0b0c 100644 --- a/sycl/include/CL/sycl/backend_types.hpp +++ b/sycl/include/CL/sycl/backend_types.hpp @@ -18,23 +18,26 @@ __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { -enum class backend : char { host, opencl, level_zero, cuda }; +enum class backend : char { host, opencl, level_zero, cuda, all }; template struct interop; inline std::ostream &operator<<(std::ostream &Out, backend be) { switch (be) { case backend::host: - Out << std::string("host"); + Out << "host"; break; case backend::opencl: - Out << std::string("opencl"); + Out << "opencl"; break; case backend::level_zero: - Out << std::string("level_zero"); + Out << "level_zero"; break; case backend::cuda: - Out << std::string("cuda"); + Out << "cuda"; + break; + case backend::all: + Out << "all"; } return Out; } diff --git a/sycl/include/CL/sycl/detail/device_filter.hpp b/sycl/include/CL/sycl/detail/device_filter.hpp new file mode 100644 index 0000000000000..b65cf709d9dc0 --- /dev/null +++ b/sycl/include/CL/sycl/detail/device_filter.hpp @@ -0,0 +1,83 @@ +//==---------- device_filter.hpp - SYCL device filter descriptor -----------==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#pragma once + +#include +#include +#include + +#include +#include + +__SYCL_INLINE_NAMESPACE(cl) { +namespace sycl { +namespace detail { + +struct device_filter { + backend Backend = backend::all; + info::device_type DeviceType = info::device_type::all; + int DeviceNum = 0; + bool HasBackend = false; + bool HasDeviceType = false; + bool HasDeviceNum = false; + int MatchesSeen = 0; + + device_filter(){}; + device_filter(const std::string &FilterString); + friend std::ostream &operator<<(std::ostream &Out, + const device_filter &Filter); +}; + +class device_filter_list { + std::vector FilterList; + +public: + device_filter_list() {} + device_filter_list(const std::string &FilterString); + device_filter_list(device_filter &Filter); + void addFilter(device_filter &Filter); + std::vector &get() { return FilterList; } + friend std::ostream &operator<<(std::ostream &Out, + const device_filter_list &List); +}; + +inline std::ostream &operator<<(std::ostream &Out, + const device_filter &Filter) { + Out << Filter.Backend << ":"; + if (Filter.DeviceType == info::device_type::host) { + Out << "host"; + } else if (Filter.DeviceType == info::device_type::cpu) { + Out << "cpu"; + } else if (Filter.DeviceType == info::device_type::gpu) { + Out << "gpu"; + } else if (Filter.DeviceType == info::device_type::accelerator) { + Out << "accelerator"; + } else if (Filter.DeviceType == info::device_type::all) { + Out << "*"; + } else { + Out << "unknown"; + } + if (Filter.HasDeviceNum) { + Out << ":" << Filter.DeviceNum; + } + return Out; +} + +inline std::ostream &operator<<(std::ostream &Out, + const device_filter_list &List) { + for (const device_filter &Filter : List.FilterList) { + Out << Filter; + Out << ","; + } + return Out; +} + +} // namespace detail +} // namespace sycl +} // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/CMakeLists.txt b/sycl/source/CMakeLists.txt index 7c70f770d876a..fd2c69d59494d 100644 --- a/sycl/source/CMakeLists.txt +++ b/sycl/source/CMakeLists.txt @@ -108,6 +108,7 @@ set(SYCL_SOURCES "detail/config.cpp" "detail/context_impl.cpp" "detail/device_binary_image.cpp" + "detail/device_filter.cpp" "detail/device_impl.cpp" "detail/error_handling/enqueue_kernel.cpp" "detail/event_impl.cpp" diff --git a/sycl/source/detail/config.def b/sycl/source/detail/config.def index 846c7a3b1f13e..3f097c3796c25 100644 --- a/sycl/source/detail/config.def +++ b/sycl/source/detail/config.def @@ -16,3 +16,4 @@ CONFIG(SYCL_DEVICE_ALLOWLIST, 1024, __SYCL_DEVICE_ALLOWLIST) CONFIG(SYCL_BE, 16, __SYCL_BE) CONFIG(SYCL_PI_TRACE, 16, __SYCL_PI_TRACE) CONFIG(SYCL_DEVICELIB_NO_FALLBACK, 1, __SYCL_DEVICELIB_NO_FALLBACK) +CONFIG(SYCL_DEVICE_FILTER, 1024, __SYCL_DEVICE_FILTER) diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index ac6fe8fbcbd2b..4f1b54126ed72 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -10,7 +10,9 @@ #include #include +#include #include +#include #include #include @@ -163,6 +165,35 @@ template <> class SYCLConfig { } }; +template <> class SYCLConfig { + using BaseT = SYCLConfigBase; + +public: + static device_filter_list *get() { + static bool Initialized = false; + static device_filter_list *FilterList = nullptr; + + // Configuration parameters are processed only once, like reading a string + // from environment and converting it into a typed object. + if (Initialized) { + return FilterList; + } + + const char *ValStr = BaseT::getRawValue(); + if (ValStr) { + static device_filter_list DFL{ValStr}; + FilterList = &DFL; + } + // As mentioned above, configuration parameters are processed only once. + // If multiple threads are checking this env var at the same time, + // they will end up setting the configration to the same value. + // If other threads check after one thread already set configration, + // the threads will get the same value as the first thread. + Initialized = true; + return FilterList; + } +}; + } // namespace detail } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp new file mode 100644 index 0000000000000..4b2c2df525268 --- /dev/null +++ b/sycl/source/detail/device_filter.cpp @@ -0,0 +1,122 @@ +//==------------------- device_filter.cpp ----------------------------------==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include +#include +#include +#include + +#include + +__SYCL_INLINE_NAMESPACE(cl) { +namespace sycl { +namespace detail { + +device_filter::device_filter(const std::string &FilterString) { + const std::array, 5> + SyclDeviceTypeMap = {{{"host", info::device_type::host}, + {"cpu", info::device_type::cpu}, + {"gpu", info::device_type::gpu}, + {"acc", info::device_type::accelerator}, + {"*", info::device_type::all}}}; + const std::array, 5> SyclBeMap = { + {{"host", backend::host}, + {"opencl", backend::opencl}, + {"level_zero", backend::level_zero}, + {"cuda", backend::cuda}, + {"*", backend::all}}}; + + size_t Cursor = 0; + size_t ColonPos = 0; + auto findElement = [&](auto Element) { + size_t Found = FilterString.find(Element.first, Cursor); + if (Found == std::string::npos) + return false; + Cursor = Found; + return true; + }; + auto selectElement = [&](auto It, auto Map, auto EltIfNotFound) { + if (It == Map.end()) + return EltIfNotFound; + ColonPos = FilterString.find(":", Cursor); + if (ColonPos != std::string::npos) + Cursor = ColonPos + 1; + else + Cursor = Cursor + It->first.size(); + return It->second; + }; + + // Handle the optional 1st field of the filter, backend + // Check if the first entry matches with a known backend type + auto It = + std::find_if(std::begin(SyclBeMap), std::end(SyclBeMap), findElement); + // If no match is found, set the backend type backend::all + // which actually means 'any backend' will be a match. + Backend = selectElement(It, SyclBeMap, backend::all); + + // Handle the optional 2nd field of the filter - device type. + // Check if the 2nd entry matches with any known device type. + if (Cursor >= FilterString.size()) { + DeviceType = info::device_type::all; + } else { + auto Iter = std::find_if(std::begin(SyclDeviceTypeMap), + std::end(SyclDeviceTypeMap), findElement); + // If no match is found, set device_type 'all', + // which actually means 'any device_type' will be a match. + DeviceType = selectElement(Iter, SyclDeviceTypeMap, info::device_type::all); + } + + // Handle the optional 3rd field of the filter, device number + // Try to convert the remaining string to an integer. + // If succeessful, the converted integer is the desired device num. + if (Cursor < FilterString.size()) { + try { + DeviceNum = stoi(FilterString.substr(ColonPos + 1)); + HasDeviceNum = true; + } catch (...) { + std::string Message = + std::string("Invalid device filter: ") + FilterString + + "\nPossible backend values are {host,opencl,level_zero,cuda,*}.\n" + "Possible device types are {host,cpu,gpu,acc,*}.\n" + "Device number should be an non-negative integer.\n"; + throw cl::sycl::invalid_parameter_error(Message, PI_INVALID_VALUE); + } + } +} + +device_filter_list::device_filter_list(const std::string &FilterStr) { + // First, change the string in all lowercase. + // This means we allow the user to use both uppercase and lowercase strings. + std::string FilterString = FilterStr; + std::transform(FilterString.begin(), FilterString.end(), FilterString.begin(), + ::tolower); + // SYCL_DEVICE_FILTER can set multiple filters separated by commas. + // convert each filter triple string into an istance of device_filter class. + size_t Pos = 0; + while (Pos < FilterString.size()) { + size_t CommaPos = FilterString.find(",", Pos); + if (CommaPos == std::string::npos) { + CommaPos = FilterString.size(); + } + std::string SubString = FilterString.substr(Pos, CommaPos - Pos); + FilterList.push_back(device_filter(SubString)); + Pos = CommaPos + 1; + } +} + +device_filter_list::device_filter_list(device_filter &Filter) { + FilterList.push_back(Filter); +} + +void device_filter_list::addFilter(device_filter &Filter) { + FilterList.push_back(Filter); +} + +} // namespace detail +} // namespace sycl +} // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/detail/filter_selector_impl.cpp b/sycl/source/detail/filter_selector_impl.cpp index 58d85cf6e7388..b062c62cfe075 100644 --- a/sycl/source/detail/filter_selector_impl.cpp +++ b/sycl/source/detail/filter_selector_impl.cpp @@ -62,13 +62,13 @@ filter create_filter(const std::string &Input) { for (const std::string &Token : Tokens) { if (Token == "cpu" && !Result.HasDeviceType) { - Result.DeviceType = PI_DEVICE_TYPE_CPU; + Result.DeviceType = info::device_type::cpu; Result.HasDeviceType = true; } else if (Token == "gpu" && !Result.HasDeviceType) { - Result.DeviceType = PI_DEVICE_TYPE_GPU; + Result.DeviceType = info::device_type::gpu; Result.HasDeviceType = true; } else if (Token == "accelerator" && !Result.HasDeviceType) { - Result.DeviceType = PI_DEVICE_TYPE_ACC; + Result.DeviceType = info::device_type::accelerator; Result.HasDeviceType = true; } else if (Token == "opencl" && !Result.HasBackend) { Result.Backend = backend::opencl; @@ -134,8 +134,7 @@ int filter_selector_impl::operator()(const device &Dev) const { BackendOK = (BE == Filter.Backend); } if (Filter.HasDeviceType) { - RT::PiDeviceType DT = - sycl::detail::getSyclObjImpl(Dev)->get_device_type(); + info::device_type DT = Dev.get_info(); DeviceTypeOK = (DT == Filter.DeviceType); } if (Filter.HasDeviceNum) { diff --git a/sycl/source/detail/filter_selector_impl.hpp b/sycl/source/detail/filter_selector_impl.hpp index a4cde25f7c66c..99392fbfa2564 100644 --- a/sycl/source/detail/filter_selector_impl.hpp +++ b/sycl/source/detail/filter_selector_impl.hpp @@ -8,6 +8,7 @@ #pragma once +#include #include #include @@ -21,15 +22,7 @@ class device; namespace ONEAPI { namespace detail { -struct filter { - backend Backend = backend::host; - RT::PiDeviceType DeviceType = PI_DEVICE_TYPE_ALL; - int DeviceNum = 0; - bool HasBackend = false; - bool HasDeviceType = false; - bool HasDeviceNum = false; - int MatchesSeen = 0; -}; +typedef struct sycl::detail::device_filter filter; class filter_selector_impl { public: diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 578728e389ee8..598994783d897 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -14,6 +14,7 @@ #include "context_impl.hpp" #include #include +#include #include #include #include @@ -214,9 +215,33 @@ bool findPlugins(vector_class> &PluginNames) { // search is done for libpi_opencl.so/pi_opencl.dll file in LD_LIBRARY_PATH // env only. // - PluginNames.emplace_back(OPENCL_PLUGIN_NAME, backend::opencl); - PluginNames.emplace_back(LEVEL_ZERO_PLUGIN_NAME, backend::level_zero); - PluginNames.emplace_back(CUDA_PLUGIN_NAME, backend::cuda); + device_filter_list *FilterList = SYCLConfig::get(); + if (!FilterList) { + PluginNames.emplace_back(OPENCL_PLUGIN_NAME, backend::opencl); + PluginNames.emplace_back(LEVEL_ZERO_PLUGIN_NAME, backend::level_zero); + PluginNames.emplace_back(CUDA_PLUGIN_NAME, backend::cuda); + } else { + std::vector Filters = FilterList->get(); + bool OpenCLFound = false; + bool LevelZeroFound = false; + bool CudaFound = false; + for (const device_filter &Filter : Filters) { + backend Backend = Filter.Backend; + if (!OpenCLFound && + (Backend == backend::opencl || Backend == backend::all)) { + PluginNames.emplace_back(OPENCL_PLUGIN_NAME, backend::opencl); + OpenCLFound = true; + } else if (!LevelZeroFound && + (Backend == backend::level_zero || Backend == backend::all)) { + PluginNames.emplace_back(LEVEL_ZERO_PLUGIN_NAME, backend::level_zero); + LevelZeroFound = true; + } else if (!CudaFound && + (Backend == backend::cuda || Backend == backend::all)) { + PluginNames.emplace_back(CUDA_PLUGIN_NAME, backend::cuda); + CudaFound = true; + } + } + } return true; } diff --git a/sycl/source/device_selector.cpp b/sycl/source/device_selector.cpp index f0f9b23792599..8c664c3b98f7a 100644 --- a/sycl/source/device_selector.cpp +++ b/sycl/source/device_selector.cpp @@ -8,10 +8,12 @@ #include #include +#include #include #include #include #include +#include #include #include #include @@ -34,6 +36,31 @@ static bool isDeviceOfPreferredSyclBe(const device &Device) { backend::level_zero; } +// Return true if the given device 'Dev' matches with any filter +static bool isForcedDevice(const device &Dev, int Index = -1) { + detail::device_filter_list *FilterList = + detail::SYCLConfig::get(); + + if (!FilterList) + return false; + info::device_type Type = Dev.get_info(); + backend Backend; + if (Type == info::device_type::host) + Backend = backend::host; + else + Backend = detail::getSyclObjImpl(Dev)->getPlugin().getBackend(); + + for (const detail::device_filter &Filter : FilterList->get()) { + if ((Filter.Backend == Backend || Filter.Backend == backend::all) && + (Filter.DeviceType == Type || + Filter.DeviceType == info::device_type::all)) { + if (Index < 0 || (Filter.HasDeviceNum && Filter.DeviceNum == Index)) + return true; + } + } + return false; +} + device device_selector::select_device() const { vector_class devices = device::get_devices(); int score = REJECT_DEVICE_SCORE; @@ -59,6 +86,13 @@ device device_selector::select_device() const { if (dev_score < 0) continue; + // If SYCL_DEVICE_FILTER is set, give a bonus point for the device + // whose index matches with desired device number. + int index = &dev - &devices[0]; + if (isForcedDevice(dev, index)) { + dev_score += 1000; + } + // SYCL spec says: "If more than one device receives the high score then // one of those tied devices will be returned, but which of the devices // from the tied set is to be returned is not defined". Here we give a @@ -103,7 +137,12 @@ int default_selector::operator()(const device &dev) const { Score = 50; // override always wins - if (dev.get_info() == detail::get_forced_type()) + // filter device gets a high point. + if (isForcedDevice(dev)) + Score += 1000; + + else if (dev.get_info() == + detail::get_forced_type()) Score += 1000; if (dev.is_gpu()) @@ -122,7 +161,16 @@ int gpu_selector::operator()(const device &dev) const { int Score = REJECT_DEVICE_SCORE; if (dev.is_gpu()) { - Score = 1000; + detail::device_filter_list *FilterList = + detail::SYCLConfig::get(); + if (FilterList) { + if (isForcedDevice(dev)) + Score = 1000; + else + return Score; + } else { + Score = 1000; + } // Give preference to device of SYCL BE. if (isDeviceOfPreferredSyclBe(dev)) Score += 50; @@ -132,8 +180,18 @@ int gpu_selector::operator()(const device &dev) const { int cpu_selector::operator()(const device &dev) const { int Score = REJECT_DEVICE_SCORE; + if (dev.is_cpu()) { - Score = 1000; + detail::device_filter_list *FilterList = + detail::SYCLConfig::get(); + if (FilterList) { + if (isForcedDevice(dev)) + Score = 1000; + else + return Score; + } else { + Score = 1000; + } // Give preference to device of SYCL BE. if (isDeviceOfPreferredSyclBe(dev)) Score += 50; @@ -143,8 +201,18 @@ int cpu_selector::operator()(const device &dev) const { int accelerator_selector::operator()(const device &dev) const { int Score = REJECT_DEVICE_SCORE; + if (dev.is_accelerator()) { - Score = 1000; + detail::device_filter_list *FilterList = + detail::SYCLConfig::get(); + if (FilterList) { + if (isForcedDevice(dev)) + Score = 1000; + else + return Score; + } else { + Score = 1000; + } // Give preference to device of SYCL BE. if (isDeviceOfPreferredSyclBe(dev)) Score += 50; @@ -154,6 +222,7 @@ int accelerator_selector::operator()(const device &dev) const { int host_selector::operator()(const device &dev) const { int Score = REJECT_DEVICE_SCORE; + if (dev.is_host()) { Score = 1000; // Give preference to device of SYCL BE. diff --git a/sycl/test/filter_selector/select_device.cpp b/sycl/test/filter_selector/select_device.cpp new file mode 100644 index 0000000000000..1e9ccb8b03e4f --- /dev/null +++ b/sycl/test/filter_selector/select_device.cpp @@ -0,0 +1,80 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RU: env SYCL_DEVICE_FILTER="*" %t.out +// RU: env SYCL_DEVICE_FILTER=cpu %t.out +// RU: env SYCL_DEVICE_FILTER=level_zero:gpu %t.out +// RU: env SYCL_DEVICE_FILTER=opencl:gpu %t.out +// RUN: env SYCL_DEVICE_FILTER=cpu,level_zero:gpu %t.out +// RUN: env SYCL_DEVICE_FILTER=opencl:acc:0 %t.out +// +// Checks if only specified device types can be acquired from select_device +// when SYCL_DEVICE_FILTER is set +// Checks that no device is selected when no device of desired type is +// available. +// +// REQUIRES: opencl,level_zero,host,cpu,gpu,accelerator + +#include +#include + +using namespace cl::sycl; +using namespace std; + +int main() { + const char *envVal = std::getenv("SYCL_DEVICE_FILTER"); + std::string forcedPIs; + if (envVal) { + std::cout << "SYCL_DEVICE_FILTER=" << envVal << std::endl; + forcedPIs = envVal; + } + if (!envVal || forcedPIs == "*" || + forcedPIs.find("level_zero:gpu") != std::string::npos) { + default_selector ds; + device d = ds.select_device(); + string name = d.get_platform().get_info(); + assert(name.find("Level-Zero") != string::npos); + std::cout << "Level-zero GPU Device is found: " << std::boolalpha + << d.is_gpu() << std::endl; + } + if (envVal && forcedPIs != "*" && + forcedPIs.find("opencl:gpu") != std::string::npos) { + gpu_selector gs; + device d = gs.select_device(); + string name = d.get_platform().get_info(); + assert(name.find("OpenCL") != string::npos); + std::cout << "OpenCL GPU Device is found: " << std::boolalpha << d.is_gpu() + << std::endl; + } + if (!envVal || forcedPIs == "*" || + forcedPIs.find("cpu") != std::string::npos) { + cpu_selector cs; + device d = cs.select_device(); + std::cout << "CPU device is found: " << d.is_cpu() << std::endl; + } + // HOST device is always available regardless of SYCL_DEVICE_FILTER + { + host_selector hs; + device d = hs.select_device(); + std::cout << "HOST device is found: " << d.is_host() << std::endl; + } + if (!envVal || forcedPIs == "*" || + forcedPIs.find("acc") != std::string::npos) { + accelerator_selector as; + device d = as.select_device(); + std::cout << "ACC device is found: " << d.is_accelerator() << std::endl; + } + if (envVal && (forcedPIs.find("cpu") == std::string::npos && + forcedPIs.find("opencl") == std::string::npos && + forcedPIs.find("*") == std::string::npos)) { + try { + cpu_selector cs; + device d = cs.select_device(); + } catch (...) { + std::cout << "Expectedly, CPU device is not found." << std::endl; + return 0; // expected + } + std::cerr << "Error: CPU device is found" << std::endl; + return -1; + } + + return 0; +} diff --git a/sycl/test/filter_selector/select_device_acc.cpp b/sycl/test/filter_selector/select_device_acc.cpp new file mode 100644 index 0000000000000..c7d933e4b5e26 --- /dev/null +++ b/sycl/test/filter_selector/select_device_acc.cpp @@ -0,0 +1,69 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RU: env SYCL_DEVICE_FILTER=acc %t.out +// +// Checks if only specified device types can be acquired from select_device +// when SYCL_DEVICE_FILTER is set +// Checks that no device is selected when no device of desired type is +// available. +// +// REQUIRES: opencl,host,accelerator + +#include +#include + +using namespace cl::sycl; +using namespace std; + +int main() { + const char *envVal = std::getenv("SYCL_DEVICE_FILTER"); + std::string forcedPIs; + if (envVal) { + std::cout << "SYCL_DEVICE_FILTER=" << envVal << std::endl; + forcedPIs = envVal; + } + { + default_selector ds; + device d = ds.select_device(); + string name = d.get_platform().get_info(); + assert(name.find("OpenCL") != string::npos); + std::cout << "ACC Device is found: " << std::boolalpha << d.is_accelerator() + << std::endl; + } + { + gpu_selector gs; + try { + device d = gs.select_device(); + std::cerr << "GPU Device is found in error: " << std::boolalpha + << d.is_gpu() << std::endl; + return -1; + } catch (...) { + std::cout << "Expectedly, GPU device is not found." << std::endl; + } + } + { + cpu_selector cs; + try { + device d = cs.select_device(); + std::cerr << "CPU Device is found in error: " << std::boolalpha + << d.is_cpu() << std::endl; + return -1; + } catch (...) { + std::cout << "Expectedly, CPU device not is found." << std::endl; + } + } + // HOST device is always available regardless of SYCL_DEVICE_FILTER + { + host_selector hs; + device d = hs.select_device(); + std::cout << "HOST device is found: " << d.is_host() << std::endl; + } + { + accelerator_selector as; + device d = as.select_device(); + string name = d.get_platform().get_info(); + assert(name.find("OpenCL") != string::npos); + std::cout << "ACC device is found: " << d.is_accelerator() << std::endl; + } + + return 0; +} diff --git a/sycl/test/filter_selector/select_device_cpu.cpp b/sycl/test/filter_selector/select_device_cpu.cpp new file mode 100644 index 0000000000000..e16b850018e22 --- /dev/null +++ b/sycl/test/filter_selector/select_device_cpu.cpp @@ -0,0 +1,67 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RU: env SYCL_DEVICE_FILTER=cpu %t.out +// +// Checks if only specified device types can be acquired from select_device +// when SYCL_DEVICE_FILTER is set +// Checks that no device is selected when no device of desired type is +// available. +// +// REQUIRES: opencl,host,cpu + +#include +#include + +using namespace cl::sycl; +using namespace std; + +int main() { + const char *envVal = std::getenv("SYCL_DEVICE_FILTER"); + std::string forcedPIs; + if (envVal) { + std::cout << "SYCL_DEVICE_FILTER=" << envVal << std::endl; + forcedPIs = envVal; + } + { + default_selector ds; + device d = ds.select_device(); + string name = d.get_platform().get_info(); + assert(name.find("OpenCL") != string::npos); + std::cout << "CPU Device is found: " << std::boolalpha << d.is_cpu() + << std::endl; + } + { + gpu_selector gs; + try { + device d = gs.select_device(); + std::cerr << "GPU Device is found: " << std::boolalpha << d.is_gpu() + << std::endl; + return -1; + } catch (...) { + std::cout << "Expectedly, GPU device is not found." << std::endl; + } + } + { + cpu_selector cs; + device d = cs.select_device(); + std::cout << "CPU device is found: " << d.is_cpu() << std::endl; + } + // HOST device is always available regardless of SYCL_DEVICE_FILTER + { + host_selector hs; + device d = hs.select_device(); + std::cout << "HOST device is found: " << d.is_host() << std::endl; + } + { + accelerator_selector as; + try { + device d = as.select_device(); + std::cerr << "ACC device is found in error: " << d.is_accelerator() + << std::endl; + return -1; + } catch (...) { + std::cout << "Expectedly, ACC device is not found." << std::endl; + } + } + + return 0; +} diff --git a/sycl/test/filter_selector/select_device_cuda.cpp b/sycl/test/filter_selector/select_device_cuda.cpp new file mode 100644 index 0000000000000..a8b8ae15901e6 --- /dev/null +++ b/sycl/test/filter_selector/select_device_cuda.cpp @@ -0,0 +1,68 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RUN: env SYCL_DEVICE_FILTER=cuda:gpu %t.out +// +// Checks if only specified device types can be acquired from select_device +// when SYCL_DEVICE_FILTER is set. +// Checks that no device is selected when no device of desired type is +// available. +// +// REQUIRES: cuda,host,gpu + +#include +#include + +using namespace cl::sycl; +using namespace std; + +int main() { + const char *envVal = getenv("SYCL_DEVICE_FILTER"); + string forcedPIs; + if (envVal) { + cout << "SYCL_DEVICE_FILTER=" << envVal << std::endl; + forcedPIs = envVal; + } + + { + default_selector ds; + device d = ds.select_device(); + string name = d.get_platform().get_info(); + assert(name.find("CUDA") != string::npos); + cout << "CUDA GPU Device is found: " << boolalpha << d.is_gpu() + << std::endl; + } + { + gpu_selector gs; + device d = gs.select_device(); + string name = d.get_platform().get_info(); + assert(name.find("CUDA") != string::npos); + cout << name << " is found: " << boolalpha << d.is_gpu() << std::endl; + } + { + cpu_selector cs; + try { + device d = cs.select_device(); + cerr << "CPU device is found in error: " << d.is_cpu() << std::endl; + return -1; + } catch (...) { + cout << "Expectedly, cpu device is not found." << std::endl; + } + } + // HOST device is always available regardless of SYCL_DEVICE_FILTER + { + host_selector hs; + device d = hs.select_device(); + cout << "HOST device is found: " << d.is_host() << std::endl; + } + { + accelerator_selector as; + try { + device d = as.select_device(); + cerr << "ACC device is found in error: " << d.is_accelerator() + << std::endl; + } catch (...) { + cout << "Expectedly, ACC device is not found." << std::endl; + } + } + + return 0; +} diff --git a/sycl/test/filter_selector/select_device_level_zero.cpp b/sycl/test/filter_selector/select_device_level_zero.cpp new file mode 100644 index 0000000000000..c808945889949 --- /dev/null +++ b/sycl/test/filter_selector/select_device_level_zero.cpp @@ -0,0 +1,68 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RUN: env SYCL_DEVICE_FILTER=level_zero:gpu %t.out +// +// Checks if only specified device types can be acquired from select_device +// when SYCL_DEVICE_FILTER is set +// Checks that no device is selected when no device of desired type is +// available. +// +// REQUIRES: level_zero,host,gpu + +#include +#include + +using namespace cl::sycl; +using namespace std; + +int main() { + const char *envVal = getenv("SYCL_DEVICE_FILTER"); + string forcedPIs; + if (envVal) { + cout << "SYCL_DEVICE_FILTER=" << envVal << std::endl; + forcedPIs = envVal; + } + + { + default_selector ds; + device d = ds.select_device(); + string name = d.get_platform().get_info(); + assert(name.find("Level-Zero") != string::npos); + cout << "Level-Zero GPU Device is found: " << boolalpha << d.is_gpu() + << std::endl; + } + { + gpu_selector gs; + device d = gs.select_device(); + string name = d.get_platform().get_info(); + assert(name.find("Level-Zero") != string::npos); + cout << name << " is found: " << boolalpha << d.is_gpu() << std::endl; + } + { + cpu_selector cs; + try { + device d = cs.select_device(); + cerr << "CPU device is found in error: " << d.is_cpu() << std::endl; + return -1; + } catch (...) { + cout << "Expectedly, cpu device is not found." << std::endl; + } + } + // HOST device is always available regardless of SYCL_DEVICE_FILTER + { + host_selector hs; + device d = hs.select_device(); + cout << "HOST device is found: " << d.is_host() << std::endl; + } + { + accelerator_selector as; + try { + device d = as.select_device(); + cerr << "ACC device is found in error: " << d.is_accelerator() + << std::endl; + } catch (...) { + cout << "Expectedly, ACC device is not found." << std::endl; + } + } + + return 0; +} diff --git a/sycl/test/filter_selector/select_device_opencl.cpp b/sycl/test/filter_selector/select_device_opencl.cpp new file mode 100644 index 0000000000000..4629564ff1fb0 --- /dev/null +++ b/sycl/test/filter_selector/select_device_opencl.cpp @@ -0,0 +1,58 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RUN: env SYCL_DEVICE_FILTER=opencl %t.out +// +// Checks if only specified device types can be acquired from select_device +// when SYCL_DEVICE_FILTER is set +// Checks that no device is selected when no device of desired type is +// available. +// +// REQUIRES: opencl,host,gpu,cpu,accelerator + +#include +#include + +using namespace cl::sycl; +using namespace std; + +int main() { + const char *envVal = getenv("SYCL_DEVICE_FILTER"); + string forcedPIs; + if (envVal) { + cout << "SYCL_DEVICE_FILTER=" << envVal << std::endl; + forcedPIs = envVal; + } + + { + default_selector ds; + device d = ds.select_device(); + string name = d.get_platform().get_info(); + assert(name.find("OpenCL") != string::npos); + cout << "OpenCL GPU Device is found: " << boolalpha << d.is_gpu() + << std::endl; + } + { + gpu_selector gs; + device d = gs.select_device(); + string name = d.get_platform().get_info(); + assert(name.find("OpenCL") != string::npos); + cout << name << " is found: " << boolalpha << d.is_gpu() << std::endl; + } + { + cpu_selector cs; + device d = cs.select_device(); + cout << "CPU device is found : " << d.is_cpu() << std::endl; + } + // HOST device is always available regardless of SYCL_DEVICE_FILTER + { + host_selector hs; + device d = hs.select_device(); + cout << "HOST device is found: " << d.is_host() << std::endl; + } + { + accelerator_selector as; + device d = as.select_device(); + cout << "ACC device is found : " << d.is_accelerator() << std::endl; + } + + return 0; +}