From b4a5ffa60b151e7797422cbfbf0eda976fb457b7 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Sat, 1 Aug 2020 10:20:32 -0700 Subject: [PATCH 01/45] [SYCL] Implement new env var SYCL_DEVICE_TRIPLE This new env var takes a list of triples {device_type, backend, device_num} 1. This list means SYCL_RT will only use those specified devices. 2. This list also limits related plugins to be loaded by SYCL RT. This PR only implemented new env var and selective plugin loading (#2) Signed-off-by: Byoungro So --- sycl/include/CL/sycl/triple.hpp | 64 +++++++++++++ sycl/source/detail/config.def | 1 + sycl/source/detail/config.hpp | 114 ++++++++++++++++++++++++ sycl/source/detail/pi.cpp | 29 +++++- sycl/test/basic_tests/select_device.cpp | 70 +++++++++++++++ 5 files changed, 275 insertions(+), 3 deletions(-) create mode 100644 sycl/include/CL/sycl/triple.hpp create mode 100644 sycl/test/basic_tests/select_device.cpp diff --git a/sycl/include/CL/sycl/triple.hpp b/sycl/include/CL/sycl/triple.hpp new file mode 100644 index 0000000000000..360d0f466dff5 --- /dev/null +++ b/sycl/include/CL/sycl/triple.hpp @@ -0,0 +1,64 @@ +//==-------------- backend_types.hpp - SYCL backend types ------------------==// +// +// 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 +#include +#include + +__SYCL_INLINE_NAMESPACE(cl) { +namespace sycl { + +#define DEVICE_NUM_UNSPECIFIED -1 + +struct triple { + info::device_type DeviceType; + backend Backend; + int32_t DeviceNum; +}; + +inline std::ostream &operator<<(std::ostream &Out, triple Trp) { + if (Trp.DeviceType == info::device_type::host) { + Out << std::string("host"); + } else if (Trp.DeviceType == info::device_type::cpu) { + Out << std::string("cpu"); + } else if (Trp.DeviceType == info::device_type::gpu) { + Out << std::string("gpu"); + } else if (Trp.DeviceType == info::device_type::accelerator) { + Out << std::string("acceclerator"); + } else if (Trp.DeviceType == info::device_type::all) { + Out << std::string("*"); + } + Out << std::string(":"); + switch (Trp.Backend) { + case backend::host: + Out << std::string("host"); + break; + case backend::opencl: + Out << std::string("opencl"); + break; + case backend::level_zero: + Out << std::string("level-zero"); + break; + case backend::cuda: + Out << std::string("cuda"); + } + if (Trp.DeviceNum != DEVICE_NUM_UNSPECIFIED) { + Out << std::string(":") << Trp.DeviceNum; + } + return Out; +} + +} // namespace sycl +} // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/detail/config.def b/sycl/source/detail/config.def index 271eb51fa6530..a5ffaf46313ad 100644 --- a/sycl/source/detail/config.def +++ b/sycl/source/detail/config.def @@ -15,3 +15,4 @@ CONFIG(SYCL_DISABLE_EXECUTION_GRAPH_CLEANUP, 1, __SYCL_DISABLE_EXECUTION_GRAPH_C CONFIG(SYCL_DEVICE_ALLOWLIST, 1024, __SYCL_DEVICE_ALLOWLIST) CONFIG(SYCL_BE, 16, __SYCL_BE) CONFIG(SYCL_PI_TRACE, 16, __SYCL_PI_TRACE) +CONFIG(SYCL_DEVICE_TRIPLE, 1024, __SYCL_DEVICE_TRIPL) \ No newline at end of file diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index ac6fe8fbcbd2b..907d9307cc119 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include @@ -163,6 +165,118 @@ template <> class SYCLConfig { } }; +template <> class SYCLConfig { + using BaseT = SYCLConfigBase; + +public: + static std::vector *get() { + static bool Initialized = false; + static std::string String; + static std::vector TripleList; + + // Configuration parameters are processed only once, like reading a string + // from environment and converting it into a typed object. + if (Initialized) { + if (TripleList.size()) { + return &TripleList; + } else { + return nullptr; + } + } + + 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, 4> SyclBeMap = { + {{"opencl", backend::opencl}, + {"level0", backend::level_zero}, + {"level_zero", backend::level_zero}, + {"cuda", backend::cuda}}}; + + Initialized = true; + const char *ValStr = BaseT::getRawValue(); + if (ValStr) { + String = ValStr; + std::transform(String.begin(), String.end(), String.begin(), ::tolower); + bool MoreTriple = true; + size_t Pos = 0; + while (MoreTriple) { + MoreTriple = false; + triple Trp; + // device_type is required entry + auto It = std::find_if( + std::begin(SyclDeviceTypeMap), std::end(SyclDeviceTypeMap), + [=, + &Pos](const std::pair &element) { + size_t Found = String.find(element.first, Pos); + if (Found != std::string::npos) { + Pos = Found; + return true; + } + return false; + }); + if (It == SyclDeviceTypeMap.end()) + pi::die("Invalid device_type. " + "Valid values are host/cpu/gpu/acc/*"); + + // initialize optional entries with default values + if (It->second == info::device_type::gpu) { + Trp = {It->second, backend::level_zero, DEVICE_NUM_UNSPECIFIED}; + } else { + Trp = {It->second, backend::opencl, DEVICE_NUM_UNSPECIFIED}; + } + + // update optional entries, backend + size_t ColonPos = String.find(":", Pos); + size_t CommaPos = String.find(",", Pos); + + if (ColonPos != std::string::npos) { + Pos = ColonPos + 1; + if ((CommaPos != std::string::npos && ColonPos < CommaPos) || + (CommaPos == std::string::npos)) { + auto It = std::find_if( + std::begin(SyclBeMap), std::end(SyclBeMap), + [=, &Pos](const std::pair &element) { + size_t Found = String.find(element.first, Pos); + if (Found != std::string::npos) { + Pos = Found; + return true; + } + return false; + }); + if (It == SyclBeMap.end()) + pi::die("Invalid backend. " + "Valid values are opencl/level0/cuda"); + Trp.Backend = It->second; + } + + // update optional entry, device number + ColonPos = String.find(":", Pos); + if (ColonPos != std::string::npos) { + Pos = ColonPos + 1; + if ((CommaPos != std::string::npos && ColonPos < CommaPos) || + (CommaPos == std::string::npos)) { + Trp.DeviceNum = atoi(String.c_str() + Pos); + } + } + } + TripleList.push_back(Trp); + + if (CommaPos != std::string::npos) { + MoreTriple = true; + Pos = CommaPos + 1; + } + } // end of while + } else { + return nullptr; + } + return &TripleList; + } +}; + } // namespace detail } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 68b9f3cf59a27..e790d3a0d2fa8 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -214,9 +214,32 @@ 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); + std::vector *Triples = SYCLConfig::get(); + bool OpenclFound = false; + bool LevelZeroFound = false; + bool CudaFound = false; + if (!Triples || Triples->size() == 0) { + 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 { + for (triple Trp : *Triples) { + std::cout << "Triple=" << Trp << std::endl; + if (!OpenclFound && Trp.Backend == backend::opencl) { + std::cout << "loading opencl plugin" << std::endl; + PluginNames.emplace_back(OPENCL_PLUGIN_NAME, backend::opencl); + OpenclFound = true; + } else if (!LevelZeroFound && Trp.Backend == backend::level_zero) { + std::cout << "loading level_zero plugin" << std::endl; + PluginNames.emplace_back(LEVEL_ZERO_PLUGIN_NAME, backend::level_zero); + LevelZeroFound = true; + } else if (!CudaFound && Trp.Backend == backend::cuda) { + std::cout << "loading cuda plugin" << std::endl; + PluginNames.emplace_back(CUDA_PLUGIN_NAME, backend::cuda); + CudaFound = true; + } + } + } return true; } diff --git a/sycl/test/basic_tests/select_device.cpp b/sycl/test/basic_tests/select_device.cpp new file mode 100644 index 0000000000000..2418bb00bfbe8 --- /dev/null +++ b/sycl/test/basic_tests/select_device.cpp @@ -0,0 +1,70 @@ +// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out +// RUN: %t.out +// RUN: env SYCL_DEVICE_TRIPLE=cpu %t.out +// RUN: env SYCL_DEVICE_TRIPLE=gpu:level0 %t.out +// RUN: env SYCL_DEVICE_TRIPLE=gpu:opencl %t.out +// RUN: env SYCL_DEVICE_TRIPLE=cpu,gpu:level0 %t.out +// +// Checks if only specified device types can be acquired from select_device +// when SYCL_DEVICE_TRIPLE is set +// Checks that no device is selected when no device of desired type is +// available. + +#include +#include + +using namespace cl::sycl; + +int main() { + const char *envVal = std::getenv("SYCL_DEVICE_TRIPLE"); + std::string forcedPIs; + if (envVal) { + std::cout << "SYCL_DEVICE_TRIPLE=" << envVal << std::endl; + forcedPIs = envVal; + } + if (!envVal || forcedPIs.find("gpu:level0") != std::string::npos) { + default_selector ds; + device d = ds.select_device(); + std::cout << "Level-zero GPU Device is found: " << std::boolalpha + << d.is_gpu() << std::endl; + } + if (!envVal || forcedPIs.find("gpu:opencl") != std::string::npos) { + gpu_selector gs; + device d = gs.select_device(); + std::cout << "OpenCL GPU Device is found: " << std::boolalpha << d.is_gpu() + << std::endl; + } + if (!envVal || 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_TRIPLE + { + host_selector hs; + device d = hs.select_device(); + std::cout << "HOST device is found: " << d.is_host() << std::endl; + } + if (!envVal || forcedPIs.find("accelerator") != 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 && + // remove the following condition when SYCL_DEVICE_TRIPLE + // filter works in device selectors + 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::cout << "Error: CPU device is found" << std::endl; + return -1; + } + + return 0; +} From 0456825daa09116f7642e8674fb9ea1a6df5b78c Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Sat, 1 Aug 2020 10:45:03 -0700 Subject: [PATCH 02/45] added description of SYCL_DEVICE_TRIPLE in doc Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index b353135ecb9db..36d46e2ce6104 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -11,6 +11,7 @@ subject to change. Do not rely on these variables in production code. | Environment variable | Values | Description | | -------------------- | ------ | ----------- | +| SYCL_DEVICE_TRIPLE | device_type[:backend:device_num] | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple, a heuristic will choose the device that has the closest match. Possible values of device_type are *,host,cpu,gpu,acc,*. Possible values of backend are opencl, level0, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from get_device() call. Among triple entries, only the device_type is required. The other two entries, backend and device_num, are optional. For example, to use cpu and level0 gpu, one can set SYCL_DEVICE_TYPE=cpu,gpu:level0 | | 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. | From 72634d550b4330f4b9a2a48de2104bc030835366 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Sat, 1 Aug 2020 16:18:46 -0700 Subject: [PATCH 03/45] disable windows tentatively Signed-off-by: Byoungro So --- sycl/test/basic_tests/select_device.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/sycl/test/basic_tests/select_device.cpp b/sycl/test/basic_tests/select_device.cpp index 2418bb00bfbe8..bb89fa36a2b11 100644 --- a/sycl/test/basic_tests/select_device.cpp +++ b/sycl/test/basic_tests/select_device.cpp @@ -9,6 +9,7 @@ // when SYCL_DEVICE_TRIPLE is set // Checks that no device is selected when no device of desired type is // available. +// UNSUPPORTED: windows #include #include From 6ec2671d98675f59faf762816b517a6b6ae5bef6 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Sat, 1 Aug 2020 16:26:55 -0700 Subject: [PATCH 04/45] clean up format Signed-off-by: Byoungro So --- sycl/include/CL/sycl/triple.hpp | 2 +- sycl/source/detail/config.def | 2 +- sycl/source/detail/pi.cpp | 4 ---- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/sycl/include/CL/sycl/triple.hpp b/sycl/include/CL/sycl/triple.hpp index 360d0f466dff5..adba2a9e7c266 100644 --- a/sycl/include/CL/sycl/triple.hpp +++ b/sycl/include/CL/sycl/triple.hpp @@ -1,4 +1,4 @@ -//==-------------- backend_types.hpp - SYCL backend types ------------------==// +//==-------------- triple.hpp - SYCL device triple descripter --------------==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/sycl/source/detail/config.def b/sycl/source/detail/config.def index a5ffaf46313ad..4e45575f47751 100644 --- a/sycl/source/detail/config.def +++ b/sycl/source/detail/config.def @@ -15,4 +15,4 @@ CONFIG(SYCL_DISABLE_EXECUTION_GRAPH_CLEANUP, 1, __SYCL_DISABLE_EXECUTION_GRAPH_C CONFIG(SYCL_DEVICE_ALLOWLIST, 1024, __SYCL_DEVICE_ALLOWLIST) CONFIG(SYCL_BE, 16, __SYCL_BE) CONFIG(SYCL_PI_TRACE, 16, __SYCL_PI_TRACE) -CONFIG(SYCL_DEVICE_TRIPLE, 1024, __SYCL_DEVICE_TRIPL) \ No newline at end of file +CONFIG(SYCL_DEVICE_TRIPLE, 1024, __SYCL_DEVICE_TRIPL) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index e790d3a0d2fa8..dde01cfb31053 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -224,17 +224,13 @@ bool findPlugins(vector_class> &PluginNames) { PluginNames.emplace_back(CUDA_PLUGIN_NAME, backend::cuda); } else { for (triple Trp : *Triples) { - std::cout << "Triple=" << Trp << std::endl; if (!OpenclFound && Trp.Backend == backend::opencl) { - std::cout << "loading opencl plugin" << std::endl; PluginNames.emplace_back(OPENCL_PLUGIN_NAME, backend::opencl); OpenclFound = true; } else if (!LevelZeroFound && Trp.Backend == backend::level_zero) { - std::cout << "loading level_zero plugin" << std::endl; PluginNames.emplace_back(LEVEL_ZERO_PLUGIN_NAME, backend::level_zero); LevelZeroFound = true; } else if (!CudaFound && Trp.Backend == backend::cuda) { - std::cout << "loading cuda plugin" << std::endl; PluginNames.emplace_back(CUDA_PLUGIN_NAME, backend::cuda); CudaFound = true; } From 6b252176ecdf66b85c916e9c63ecd382faf390fa Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 14 Aug 2020 13:31:04 -0700 Subject: [PATCH 05/45] accmmodated feedback Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 2 +- sycl/include/CL/sycl/device_triple.hpp | 88 +++++++++++++++++++ sycl/source/CMakeLists.txt | 1 + sycl/source/detail/config.def | 2 +- sycl/source/detail/config.hpp | 108 +++--------------------- sycl/source/detail/pi.cpp | 22 ++--- sycl/source/device_triple.cpp | 100 ++++++++++++++++++++++ sycl/test/basic_tests/select_device.cpp | 19 +++-- 8 files changed, 224 insertions(+), 118 deletions(-) create mode 100644 sycl/include/CL/sycl/device_triple.hpp create mode 100644 sycl/source/device_triple.cpp diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index a93a7f06fbd6c..7f997dd6f3636 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -11,7 +11,7 @@ subject to change. Do not rely on these variables in production code. | Environment variable | Values | Description | | -------------------- | ------ | ----------- | -| SYCL_DEVICE_TRIPLE | device_type[:backend:device_num] | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple, a heuristic will choose the device that has the closest match. Possible values of device_type are *,host,cpu,gpu,acc,*. Possible values of backend are opencl, level0, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from get_device() call. Among triple entries, only the device_type is required. The other two entries, backend and device_num, are optional. For example, to use cpu and level0 gpu, one can set SYCL_DEVICE_TYPE=cpu,gpu:level0 | +| SYCL_DEVICE_TRIPLES | device_type[:backend:device_num] | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple, a heuristic will choose the device that has the closest match. Possible values of device_type are *,host,cpu,gpu,acc,*. Possible values of backend are opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. Among triple entries, only the device_type is required. The other two entries, backend and device_num, are optional. For example, to use cpu and level0 gpu device number 0, one can set SYCL_DEVICE_TRIPLES=cpu,gpu:level_zero:0 | | 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. | diff --git a/sycl/include/CL/sycl/device_triple.hpp b/sycl/include/CL/sycl/device_triple.hpp new file mode 100644 index 0000000000000..18fc6d6d05cb4 --- /dev/null +++ b/sycl/include/CL/sycl/device_triple.hpp @@ -0,0 +1,88 @@ +//==-------------- triple.hpp - SYCL device triple descripter --------------==// +// +// 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 { + +class device_triple { + info::device_type DeviceType; + backend Backend; + int32_t DeviceNum; + const int DEVICE_NUM_UNSPECIFIED = -1; + +public: + device_triple(std::string &TripleString); + info::device_type getDeviceType() const { return DeviceType; } + backend getBackend() const { return Backend; } + int32_t getDeviceNum() const { return DeviceNum; } + friend std::ostream &operator<<(std::ostream &Out, const device_triple &Trp); +}; + +class device_triple_list { + std::vector TripleList; + +public: + device_triple_list(std::string &TripleString); + device_triple_list(device_triple &Trp); + std::vector &get() { return TripleList; } + friend std::ostream &operator<<(std::ostream &Out, + const device_triple_list &List); +}; + +inline std::ostream &operator<<(std::ostream &Out, const device_triple &Trp) { + if (Trp.DeviceType == info::device_type::host) { + Out << std::string("host"); + } else if (Trp.DeviceType == info::device_type::cpu) { + Out << std::string("cpu"); + } else if (Trp.DeviceType == info::device_type::gpu) { + Out << std::string("gpu"); + } else if (Trp.DeviceType == info::device_type::accelerator) { + Out << std::string("acceclerator"); + } else if (Trp.DeviceType == info::device_type::all) { + Out << std::string("*"); + } + Out << std::string(":"); + switch (Trp.Backend) { + case backend::host: + Out << std::string("host"); + break; + case backend::opencl: + Out << std::string("opencl"); + break; + case backend::level_zero: + Out << std::string("level-zero"); + break; + case backend::cuda: + Out << std::string("cuda"); + } + if (Trp.DeviceNum != Trp.DEVICE_NUM_UNSPECIFIED) { + Out << std::string(":") << Trp.DeviceNum; + } + return Out; +} + +inline std::ostream &operator<<(std::ostream &Out, + const device_triple_list &List) { + for (const device_triple &Trp : List.TripleList) { + Out << Trp; + Out << ","; + } + return Out; +} + +} // namespace sycl +} // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/CMakeLists.txt b/sycl/source/CMakeLists.txt index 3460e11bae70b..3c8a63f95de72 100644 --- a/sycl/source/CMakeLists.txt +++ b/sycl/source/CMakeLists.txt @@ -140,6 +140,7 @@ set(SYCL_SOURCES "context.cpp" "device.cpp" "device_selector.cpp" + "device_triple.cpp" "event.cpp" "exception.cpp" "exception_list.cpp" diff --git a/sycl/source/detail/config.def b/sycl/source/detail/config.def index bae63944e9c19..13b1d92ce8605 100644 --- a/sycl/source/detail/config.def +++ b/sycl/source/detail/config.def @@ -16,4 +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_TRIPLE, 1024, __SYCL_DEVICE_TRIPLE) +CONFIG(SYCL_DEVICE_TRIPLES, 1024, __SYCL_DEVICE_TRIPLES) diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index 907d9307cc119..2bd0b01cedf13 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -11,8 +11,8 @@ #include #include #include +#include #include -#include #include #include @@ -165,115 +165,29 @@ template <> class SYCLConfig { } }; -template <> class SYCLConfig { - using BaseT = SYCLConfigBase; +template <> class SYCLConfig { + using BaseT = SYCLConfigBase; public: - static std::vector *get() { + static device_triple_list *get() { static bool Initialized = false; - static std::string String; - static std::vector TripleList; + static device_triple_list *TripleList = nullptr; // Configuration parameters are processed only once, like reading a string // from environment and converting it into a typed object. if (Initialized) { - if (TripleList.size()) { - return &TripleList; - } else { - return nullptr; - } + return TripleList; } - 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, 4> SyclBeMap = { - {{"opencl", backend::opencl}, - {"level0", backend::level_zero}, - {"level_zero", backend::level_zero}, - {"cuda", backend::cuda}}}; - Initialized = true; const char *ValStr = BaseT::getRawValue(); if (ValStr) { - String = ValStr; - std::transform(String.begin(), String.end(), String.begin(), ::tolower); - bool MoreTriple = true; - size_t Pos = 0; - while (MoreTriple) { - MoreTriple = false; - triple Trp; - // device_type is required entry - auto It = std::find_if( - std::begin(SyclDeviceTypeMap), std::end(SyclDeviceTypeMap), - [=, - &Pos](const std::pair &element) { - size_t Found = String.find(element.first, Pos); - if (Found != std::string::npos) { - Pos = Found; - return true; - } - return false; - }); - if (It == SyclDeviceTypeMap.end()) - pi::die("Invalid device_type. " - "Valid values are host/cpu/gpu/acc/*"); - - // initialize optional entries with default values - if (It->second == info::device_type::gpu) { - Trp = {It->second, backend::level_zero, DEVICE_NUM_UNSPECIFIED}; - } else { - Trp = {It->second, backend::opencl, DEVICE_NUM_UNSPECIFIED}; - } - - // update optional entries, backend - size_t ColonPos = String.find(":", Pos); - size_t CommaPos = String.find(",", Pos); - - if (ColonPos != std::string::npos) { - Pos = ColonPos + 1; - if ((CommaPos != std::string::npos && ColonPos < CommaPos) || - (CommaPos == std::string::npos)) { - auto It = std::find_if( - std::begin(SyclBeMap), std::end(SyclBeMap), - [=, &Pos](const std::pair &element) { - size_t Found = String.find(element.first, Pos); - if (Found != std::string::npos) { - Pos = Found; - return true; - } - return false; - }); - if (It == SyclBeMap.end()) - pi::die("Invalid backend. " - "Valid values are opencl/level0/cuda"); - Trp.Backend = It->second; - } - - // update optional entry, device number - ColonPos = String.find(":", Pos); - if (ColonPos != std::string::npos) { - Pos = ColonPos + 1; - if ((CommaPos != std::string::npos && ColonPos < CommaPos) || - (CommaPos == std::string::npos)) { - Trp.DeviceNum = atoi(String.c_str() + Pos); - } - } - } - TripleList.push_back(Trp); - - if (CommaPos != std::string::npos) { - MoreTriple = true; - Pos = CommaPos + 1; - } - } // end of while - } else { - return nullptr; + std::string TripleString = ValStr; + std::transform(TripleString.begin(), TripleString.end(), + TripleString.begin(), ::tolower); + TripleList = new device_triple_list(TripleString); } - return &TripleList; + return TripleList; } }; diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index dde01cfb31053..2abeba6ac2adc 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -214,23 +215,24 @@ bool findPlugins(vector_class> &PluginNames) { // search is done for libpi_opencl.so/pi_opencl.dll file in LD_LIBRARY_PATH // env only. // - std::vector *Triples = SYCLConfig::get(); - bool OpenclFound = false; - bool LevelZeroFound = false; - bool CudaFound = false; - if (!Triples || Triples->size() == 0) { + device_triple_list *TripleList = SYCLConfig::get(); + if (!TripleList) { 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 { - for (triple Trp : *Triples) { - if (!OpenclFound && Trp.Backend == backend::opencl) { + std::vector Triples = TripleList->get(); + bool OpenCLFound = false; + bool LevelZeroFound = false; + bool CudaFound = false; + for (const device_triple &Trp : Triples) { + if (!OpenCLFound && Trp.getBackend() == backend::opencl) { PluginNames.emplace_back(OPENCL_PLUGIN_NAME, backend::opencl); - OpenclFound = true; - } else if (!LevelZeroFound && Trp.Backend == backend::level_zero) { + OpenCLFound = true; + } else if (!LevelZeroFound && Trp.getBackend() == backend::level_zero) { PluginNames.emplace_back(LEVEL_ZERO_PLUGIN_NAME, backend::level_zero); LevelZeroFound = true; - } else if (!CudaFound && Trp.Backend == backend::cuda) { + } else if (!CudaFound && Trp.getBackend() == backend::cuda) { PluginNames.emplace_back(CUDA_PLUGIN_NAME, backend::cuda); CudaFound = true; } diff --git a/sycl/source/device_triple.cpp b/sycl/source/device_triple.cpp new file mode 100644 index 0000000000000..0f775bcf3f636 --- /dev/null +++ b/sycl/source/device_triple.cpp @@ -0,0 +1,100 @@ +//==------------------- device_triple.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 + +__SYCL_INLINE_NAMESPACE(cl) { +namespace sycl { + +device_triple::device_triple(std::string &TripleString) { + 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, 4> SyclBeMap = { + {{"opencl", backend::opencl}, + {"level0", backend::level_zero}, + {"level_zero", backend::level_zero}, + {"cuda", backend::cuda}}}; + + // device_type is a required entry + size_t Pos = 0; + auto It = std::find_if( + std::begin(SyclDeviceTypeMap), std::end(SyclDeviceTypeMap), + [=, &Pos](const std::pair &Element) { + size_t Found = TripleString.find(Element.first, Pos); + if (Found != std::string::npos) { + Pos = Found; + return true; + } + return false; + }); + if (It == SyclDeviceTypeMap.end()) + throw cl::sycl::invalid_parameter_error( + "Invalid device_type. Valid values are host/cpu/gpu/acc/*", + PI_INVALID_VALUE); + + DeviceType = It->second; + // initialize optional entries with default values + if (DeviceType == info::device_type::gpu) { + Backend = backend::level_zero; + } else { + Backend = backend::opencl; + } + DeviceNum = DEVICE_NUM_UNSPECIFIED; + + // update the optional 2nd entry, backend + size_t ColonPos = TripleString.find(":", Pos); + if (ColonPos != std::string::npos) { + Pos = ColonPos + 1; + auto It = + std::find_if(std::begin(SyclBeMap), std::end(SyclBeMap), + [=, &Pos](const std::pair &Element) { + size_t Found = TripleString.find(Element.first, Pos); + if (Found != std::string::npos) { + Pos = Found; + return true; + } + return false; + }); + if (It == SyclBeMap.end()) + throw cl::sycl::invalid_parameter_error( + "Invalid backend. Valid values are opencl/level0/cuda", + PI_INVALID_VALUE); + Backend = It->second; + } + + // update the optional 3rd entry, device number + ColonPos = TripleString.find(":", Pos); + if (ColonPos != std::string::npos && (ColonPos + 1) < TripleString.size()) { + DeviceNum = stoi(TripleString.substr(ColonPos + 1)); + } +} + +device_triple_list::device_triple_list(std::string &TripleString) { + std::transform(TripleString.begin(), TripleString.end(), TripleString.begin(), + ::tolower); + size_t Pos = 0; + while (Pos < TripleString.size()) { + size_t CommaPos = TripleString.find(",", Pos); + if (CommaPos == std::string::npos) { + CommaPos = TripleString.size(); + } + std::string SubString = TripleString.substr(Pos, CommaPos - Pos); + TripleList.push_back(device_triple(SubString)); + Pos = CommaPos + 1; + } +} + +} // namespace sycl +} // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/test/basic_tests/select_device.cpp b/sycl/test/basic_tests/select_device.cpp index bb89fa36a2b11..e62817ea2871d 100644 --- a/sycl/test/basic_tests/select_device.cpp +++ b/sycl/test/basic_tests/select_device.cpp @@ -1,12 +1,13 @@ // RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out // RUN: %t.out -// RUN: env SYCL_DEVICE_TRIPLE=cpu %t.out -// RUN: env SYCL_DEVICE_TRIPLE=gpu:level0 %t.out -// RUN: env SYCL_DEVICE_TRIPLE=gpu:opencl %t.out -// RUN: env SYCL_DEVICE_TRIPLE=cpu,gpu:level0 %t.out +// RUN: env SYCL_DEVICE_TRIPLES=cpu %t.out +// RUN: env SYCL_DEVICE_TRIPLES=gpu:level0 %t.out +// RUN: env SYCL_DEVICE_TRIPLES=gpu:opencl %t.out +// RUN: env SYCL_DEVICE_TRIPLES=cpu,gpu:level0 %t.out +// RUN: env SYCL_DEVICE_TRIPLES=acc:opencl:0 %t.out // // Checks if only specified device types can be acquired from select_device -// when SYCL_DEVICE_TRIPLE is set +// when SYCL_DEVICE_TRIPLES is set // Checks that no device is selected when no device of desired type is // available. // UNSUPPORTED: windows @@ -17,13 +18,13 @@ using namespace cl::sycl; int main() { - const char *envVal = std::getenv("SYCL_DEVICE_TRIPLE"); + const char *envVal = std::getenv("SYCL_DEVICE_TRIPLES"); std::string forcedPIs; if (envVal) { - std::cout << "SYCL_DEVICE_TRIPLE=" << envVal << std::endl; + std::cout << "SYCL_DEVICE_TRIPLES=" << envVal << std::endl; forcedPIs = envVal; } - if (!envVal || forcedPIs.find("gpu:level0") != std::string::npos) { + if (!envVal || forcedPIs.find("gpu:level_zero") != std::string::npos) { default_selector ds; device d = ds.select_device(); std::cout << "Level-zero GPU Device is found: " << std::boolalpha @@ -46,7 +47,7 @@ int main() { device d = hs.select_device(); std::cout << "HOST device is found: " << d.is_host() << std::endl; } - if (!envVal || forcedPIs.find("accelerator") != std::string::npos) { + if (!envVal || 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; From 0e9c8d4fb55b2e53f32d0bb6598876f4b5f331b1 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Sat, 15 Aug 2020 23:09:11 -0700 Subject: [PATCH 06/45] added * for a backend type Signed-off-by: Byoungro So --- sycl/include/CL/sycl/backend_types.hpp | 5 ++++- sycl/include/CL/sycl/device_triple.hpp | 3 +++ sycl/source/detail/pi.cpp | 10 +++++++--- sycl/source/device_triple.cpp | 17 ++++++++++++----- sycl/test/basic_tests/select_device.cpp | 17 +++++++++++------ 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/sycl/include/CL/sycl/backend_types.hpp b/sycl/include/CL/sycl/backend_types.hpp index 655bbf89d8d39..272ef72406fbd 100644 --- a/sycl/include/CL/sycl/backend_types.hpp +++ b/sycl/include/CL/sycl/backend_types.hpp @@ -18,7 +18,7 @@ __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; @@ -35,6 +35,9 @@ inline std::ostream &operator<<(std::ostream &Out, backend be) { break; case backend::cuda: Out << std::string("cuda"); + break; + case backend::all: + Out << std::string("all"); } return Out; } diff --git a/sycl/include/CL/sycl/device_triple.hpp b/sycl/include/CL/sycl/device_triple.hpp index 18fc6d6d05cb4..a32a99c269613 100644 --- a/sycl/include/CL/sycl/device_triple.hpp +++ b/sycl/include/CL/sycl/device_triple.hpp @@ -68,6 +68,9 @@ inline std::ostream &operator<<(std::ostream &Out, const device_triple &Trp) { break; case backend::cuda: Out << std::string("cuda"); + break; + case backend::all: + Out << std::string("*"); } if (Trp.DeviceNum != Trp.DEVICE_NUM_UNSPECIFIED) { Out << std::string(":") << Trp.DeviceNum; diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 2abeba6ac2adc..4f08f48ab62f9 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -226,13 +226,17 @@ bool findPlugins(vector_class> &PluginNames) { bool LevelZeroFound = false; bool CudaFound = false; for (const device_triple &Trp : Triples) { - if (!OpenCLFound && Trp.getBackend() == backend::opencl) { + backend Backend = Trp.getBackend(); + if (!OpenCLFound && + (Backend == backend::opencl || Backend == backend::all)) { PluginNames.emplace_back(OPENCL_PLUGIN_NAME, backend::opencl); OpenCLFound = true; - } else if (!LevelZeroFound && Trp.getBackend() == backend::level_zero) { + } 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 && Trp.getBackend() == backend::cuda) { + } else if (!CudaFound && + (Backend == backend::cuda || Backend == backend::all)) { PluginNames.emplace_back(CUDA_PLUGIN_NAME, backend::cuda); CudaFound = true; } diff --git a/sycl/source/device_triple.cpp b/sycl/source/device_triple.cpp index 0f775bcf3f636..189d58248bb07 100644 --- a/sycl/source/device_triple.cpp +++ b/sycl/source/device_triple.cpp @@ -23,9 +23,9 @@ device_triple::device_triple(std::string &TripleString) { {"*", info::device_type::all}}}; const std::array, 4> SyclBeMap = { {{"opencl", backend::opencl}, - {"level0", backend::level_zero}, {"level_zero", backend::level_zero}, - {"cuda", backend::cuda}}}; + {"cuda", backend::cuda}, + {"*", backend::all}}}; // device_type is a required entry size_t Pos = 0; @@ -46,7 +46,9 @@ device_triple::device_triple(std::string &TripleString) { DeviceType = It->second; // initialize optional entries with default values - if (DeviceType == info::device_type::gpu) { + if (DeviceType == info::device_type::all) { + Backend = backend::all; + } else if (DeviceType == info::device_type::gpu) { Backend = backend::level_zero; } else { Backend = backend::opencl; @@ -69,7 +71,7 @@ device_triple::device_triple(std::string &TripleString) { }); if (It == SyclBeMap.end()) throw cl::sycl::invalid_parameter_error( - "Invalid backend. Valid values are opencl/level0/cuda", + "Invalid backend. Valid values are opencl/level_zero/cuda/*", PI_INVALID_VALUE); Backend = It->second; } @@ -77,7 +79,12 @@ device_triple::device_triple(std::string &TripleString) { // update the optional 3rd entry, device number ColonPos = TripleString.find(":", Pos); if (ColonPos != std::string::npos && (ColonPos + 1) < TripleString.size()) { - DeviceNum = stoi(TripleString.substr(ColonPos + 1)); + try { + DeviceNum = stoi(TripleString.substr(ColonPos + 1)); + } catch (...) { + throw cl::sycl::invalid_parameter_error( + "Invalid device number. An integer is needed.", PI_INVALID_VALUE); + } } } diff --git a/sycl/test/basic_tests/select_device.cpp b/sycl/test/basic_tests/select_device.cpp index e62817ea2871d..9c13b7cf85647 100644 --- a/sycl/test/basic_tests/select_device.cpp +++ b/sycl/test/basic_tests/select_device.cpp @@ -1,9 +1,10 @@ // RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out // RUN: %t.out +// RUN: env SYCL_DEVICE_TRIPLES="*" %t.out // RUN: env SYCL_DEVICE_TRIPLES=cpu %t.out -// RUN: env SYCL_DEVICE_TRIPLES=gpu:level0 %t.out +// RUN: env SYCL_DEVICE_TRIPLES=gpu:level_zero %t.out // RUN: env SYCL_DEVICE_TRIPLES=gpu:opencl %t.out -// RUN: env SYCL_DEVICE_TRIPLES=cpu,gpu:level0 %t.out +// RUN: env SYCL_DEVICE_TRIPLES=cpu,gpu:level_zero %t.out // RUN: env SYCL_DEVICE_TRIPLES=acc:opencl:0 %t.out // // Checks if only specified device types can be acquired from select_device @@ -24,19 +25,22 @@ int main() { std::cout << "SYCL_DEVICE_TRIPLES=" << envVal << std::endl; forcedPIs = envVal; } - if (!envVal || forcedPIs.find("gpu:level_zero") != std::string::npos) { + if (!envVal || forcedPIs == "*" || + forcedPIs.find("gpu:level_zero") != std::string::npos) { default_selector ds; device d = ds.select_device(); std::cout << "Level-zero GPU Device is found: " << std::boolalpha << d.is_gpu() << std::endl; } - if (!envVal || forcedPIs.find("gpu:opencl") != std::string::npos) { + if (!envVal || forcedPIs == "*" || + forcedPIs.find("gpu:opencl") != std::string::npos) { gpu_selector gs; device d = gs.select_device(); std::cout << "OpenCL GPU Device is found: " << std::boolalpha << d.is_gpu() << std::endl; } - if (!envVal || forcedPIs.find("cpu") != std::string::npos) { + 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; @@ -47,7 +51,8 @@ int main() { device d = hs.select_device(); std::cout << "HOST device is found: " << d.is_host() << std::endl; } - if (!envVal || forcedPIs.find("acc") != std::string::npos) { + 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; From 35937b565e6c643975c2271133adc4ba35bd78bd Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 24 Aug 2020 19:21:42 -0700 Subject: [PATCH 07/45] changed the order of device triple Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 1 - sycl/include/CL/sycl/device_triple.hpp | 28 ++++---- sycl/source/device_triple.cpp | 96 +++++++++++++------------ sycl/test/basic_tests/select_device.cpp | 12 ++-- 4 files changed, 70 insertions(+), 67 deletions(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 7f997dd6f3636..629f04618fa06 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -11,7 +11,6 @@ subject to change. Do not rely on these variables in production code. | Environment variable | Values | Description | | -------------------- | ------ | ----------- | -| SYCL_DEVICE_TRIPLES | device_type[:backend:device_num] | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple, a heuristic will choose the device that has the closest match. Possible values of device_type are *,host,cpu,gpu,acc,*. Possible values of backend are opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. Among triple entries, only the device_type is required. The other two entries, backend and device_num, are optional. For example, to use cpu and level0 gpu device number 0, one can set SYCL_DEVICE_TRIPLES=cpu,gpu:level_zero:0 | | 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. | diff --git a/sycl/include/CL/sycl/device_triple.hpp b/sycl/include/CL/sycl/device_triple.hpp index a32a99c269613..18ee51fc63a9b 100644 --- a/sycl/include/CL/sycl/device_triple.hpp +++ b/sycl/include/CL/sycl/device_triple.hpp @@ -19,15 +19,15 @@ __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { class device_triple { - info::device_type DeviceType; backend Backend; + info::device_type DeviceType; int32_t DeviceNum; const int DEVICE_NUM_UNSPECIFIED = -1; public: device_triple(std::string &TripleString); - info::device_type getDeviceType() const { return DeviceType; } backend getBackend() const { return Backend; } + info::device_type getDeviceType() const { return DeviceType; } int32_t getDeviceNum() const { return DeviceNum; } friend std::ostream &operator<<(std::ostream &Out, const device_triple &Trp); }; @@ -44,18 +44,6 @@ class device_triple_list { }; inline std::ostream &operator<<(std::ostream &Out, const device_triple &Trp) { - if (Trp.DeviceType == info::device_type::host) { - Out << std::string("host"); - } else if (Trp.DeviceType == info::device_type::cpu) { - Out << std::string("cpu"); - } else if (Trp.DeviceType == info::device_type::gpu) { - Out << std::string("gpu"); - } else if (Trp.DeviceType == info::device_type::accelerator) { - Out << std::string("acceclerator"); - } else if (Trp.DeviceType == info::device_type::all) { - Out << std::string("*"); - } - Out << std::string(":"); switch (Trp.Backend) { case backend::host: Out << std::string("host"); @@ -72,6 +60,18 @@ inline std::ostream &operator<<(std::ostream &Out, const device_triple &Trp) { case backend::all: Out << std::string("*"); } + Out << std::string(":"); + if (Trp.DeviceType == info::device_type::host) { + Out << std::string("host"); + } else if (Trp.DeviceType == info::device_type::cpu) { + Out << std::string("cpu"); + } else if (Trp.DeviceType == info::device_type::gpu) { + Out << std::string("gpu"); + } else if (Trp.DeviceType == info::device_type::accelerator) { + Out << std::string("acceclerator"); + } else if (Trp.DeviceType == info::device_type::all) { + Out << std::string("*"); + } if (Trp.DeviceNum != Trp.DEVICE_NUM_UNSPECIFIED) { Out << std::string(":") << Trp.DeviceNum; } diff --git a/sycl/source/device_triple.cpp b/sycl/source/device_triple.cpp index 189d58248bb07..b97686c8e4558 100644 --- a/sycl/source/device_triple.cpp +++ b/sycl/source/device_triple.cpp @@ -10,6 +10,7 @@ #include #include #include +#include __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { @@ -27,64 +28,67 @@ device_triple::device_triple(std::string &TripleString) { {"cuda", backend::cuda}, {"*", backend::all}}}; - // device_type is a required entry - size_t Pos = 0; - auto It = std::find_if( + // handle the optional 1st entry, backend + size_t Cursor = 0; + size_t ColonPos = TripleString.find(":", Cursor); + auto It = std::find_if(std::begin(SyclBeMap), std::end(SyclBeMap), + [=, &Cursor](const std::pair &Element) { + size_t Found = TripleString.find(Element.first, Cursor); + if (Found != std::string::npos) { + Cursor = Found; + return true; + } + return false; + }); + if (It == SyclBeMap.end()) { + Backend = backend::all; + } else { + Backend = It->second; + if (ColonPos != std::string::npos) { + Cursor = ColonPos + 1; + } else { + Cursor = Cursor + It->first.size(); + } + } + + // handle the optional 2nd entry, device type + auto Iter = std::find_if( std::begin(SyclDeviceTypeMap), std::end(SyclDeviceTypeMap), - [=, &Pos](const std::pair &Element) { - size_t Found = TripleString.find(Element.first, Pos); + [=, &Cursor](const std::pair &Element) { + size_t Found = TripleString.find(Element.first, Cursor); if (Found != std::string::npos) { - Pos = Found; + Cursor = Found; return true; } return false; }); - if (It == SyclDeviceTypeMap.end()) - throw cl::sycl::invalid_parameter_error( - "Invalid device_type. Valid values are host/cpu/gpu/acc/*", - PI_INVALID_VALUE); - - DeviceType = It->second; - // initialize optional entries with default values - if (DeviceType == info::device_type::all) { - Backend = backend::all; - } else if (DeviceType == info::device_type::gpu) { - Backend = backend::level_zero; + if (Iter == SyclDeviceTypeMap.end()) { + DeviceType = info::device_type::all; } else { - Backend = backend::opencl; - } - DeviceNum = DEVICE_NUM_UNSPECIFIED; - - // update the optional 2nd entry, backend - size_t ColonPos = TripleString.find(":", Pos); - if (ColonPos != std::string::npos) { - Pos = ColonPos + 1; - auto It = - std::find_if(std::begin(SyclBeMap), std::end(SyclBeMap), - [=, &Pos](const std::pair &Element) { - size_t Found = TripleString.find(Element.first, Pos); - if (Found != std::string::npos) { - Pos = Found; - return true; - } - return false; - }); - if (It == SyclBeMap.end()) - throw cl::sycl::invalid_parameter_error( - "Invalid backend. Valid values are opencl/level_zero/cuda/*", - PI_INVALID_VALUE); - Backend = It->second; + DeviceType = Iter->second; + ColonPos = TripleString.find(":", Cursor); + if (ColonPos != std::string::npos) { + Cursor = ColonPos + 1; + } else { + Cursor = Cursor + Iter->first.size(); + } } - - // update the optional 3rd entry, device number - ColonPos = TripleString.find(":", Pos); - if (ColonPos != std::string::npos && (ColonPos + 1) < TripleString.size()) { + + // handle the optional 3rd entry, device number + if (Cursor < TripleString.size()) { try { DeviceNum = stoi(TripleString.substr(ColonPos + 1)); } catch (...) { - throw cl::sycl::invalid_parameter_error( - "Invalid device number. An integer is needed.", PI_INVALID_VALUE); + char message[100]; + strcpy(message, "Invalid device triple: "); + std::strcat(message, TripleString.c_str()); + std::strcat(message, "\nPossible backend values are {opencl,level_zero,cuda,*}."); + std::strcat(message, "\nPossible device types are {host,cpu,gpu,acc,*}."); + std::strcat(message, "\nDevice number should be an non-negative integer.\n"); + throw cl::sycl::invalid_parameter_error(message, PI_INVALID_VALUE); } + } else { + DeviceNum = DEVICE_NUM_UNSPECIFIED; } } diff --git a/sycl/test/basic_tests/select_device.cpp b/sycl/test/basic_tests/select_device.cpp index 9c13b7cf85647..e33d9f918c8fc 100644 --- a/sycl/test/basic_tests/select_device.cpp +++ b/sycl/test/basic_tests/select_device.cpp @@ -2,10 +2,10 @@ // RUN: %t.out // RUN: env SYCL_DEVICE_TRIPLES="*" %t.out // RUN: env SYCL_DEVICE_TRIPLES=cpu %t.out -// RUN: env SYCL_DEVICE_TRIPLES=gpu:level_zero %t.out -// RUN: env SYCL_DEVICE_TRIPLES=gpu:opencl %t.out -// RUN: env SYCL_DEVICE_TRIPLES=cpu,gpu:level_zero %t.out -// RUN: env SYCL_DEVICE_TRIPLES=acc:opencl:0 %t.out +// RUN: env SYCL_DEVICE_TRIPLES=level_zero:gpu %t.out +// RUN: env SYCL_DEVICE_TRIPLES=opencl:gpu %t.out +// RUN: env SYCL_DEVICE_TRIPLES=cpu,level_zero:gpu %t.out +// RUN: env SYCL_DEVICE_TRIPLES=opencl:acc:0 %t.out // // Checks if only specified device types can be acquired from select_device // when SYCL_DEVICE_TRIPLES is set @@ -45,7 +45,7 @@ int main() { 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_TRIPLE + // HOST device is always available regardless of SYCL_DEVICE_TRIPLES { host_selector hs; device d = hs.select_device(); @@ -58,7 +58,7 @@ int main() { std::cout << "ACC device is found: " << d.is_accelerator() << std::endl; } if (envVal && (forcedPIs.find("cpu") == std::string::npos && - // remove the following condition when SYCL_DEVICE_TRIPLE + // remove the following condition when SYCL_DEVICE_TRIPLES // filter works in device selectors forcedPIs.find("opencl") == std::string::npos && forcedPIs.find("*") == std::string::npos)) { From 8e3829214291d0f7cfae955afc4c65a40ef5ea85 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 24 Aug 2020 20:39:19 -0700 Subject: [PATCH 08/45] clang-format Signed-off-by: Byoungro So --- sycl/include/CL/sycl/triple.hpp | 64 ------------------------- sycl/source/device_triple.cpp | 31 ++++++------ sycl/test/basic_tests/select_device.cpp | 1 - 3 files changed, 17 insertions(+), 79 deletions(-) delete mode 100644 sycl/include/CL/sycl/triple.hpp diff --git a/sycl/include/CL/sycl/triple.hpp b/sycl/include/CL/sycl/triple.hpp deleted file mode 100644 index adba2a9e7c266..0000000000000 --- a/sycl/include/CL/sycl/triple.hpp +++ /dev/null @@ -1,64 +0,0 @@ -//==-------------- triple.hpp - SYCL device triple descripter --------------==// -// -// 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 -#include -#include - -__SYCL_INLINE_NAMESPACE(cl) { -namespace sycl { - -#define DEVICE_NUM_UNSPECIFIED -1 - -struct triple { - info::device_type DeviceType; - backend Backend; - int32_t DeviceNum; -}; - -inline std::ostream &operator<<(std::ostream &Out, triple Trp) { - if (Trp.DeviceType == info::device_type::host) { - Out << std::string("host"); - } else if (Trp.DeviceType == info::device_type::cpu) { - Out << std::string("cpu"); - } else if (Trp.DeviceType == info::device_type::gpu) { - Out << std::string("gpu"); - } else if (Trp.DeviceType == info::device_type::accelerator) { - Out << std::string("acceclerator"); - } else if (Trp.DeviceType == info::device_type::all) { - Out << std::string("*"); - } - Out << std::string(":"); - switch (Trp.Backend) { - case backend::host: - Out << std::string("host"); - break; - case backend::opencl: - Out << std::string("opencl"); - break; - case backend::level_zero: - Out << std::string("level-zero"); - break; - case backend::cuda: - Out << std::string("cuda"); - } - if (Trp.DeviceNum != DEVICE_NUM_UNSPECIFIED) { - Out << std::string(":") << Trp.DeviceNum; - } - return Out; -} - -} // namespace sycl -} // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/device_triple.cpp b/sycl/source/device_triple.cpp index b97686c8e4558..32a403941de0f 100644 --- a/sycl/source/device_triple.cpp +++ b/sycl/source/device_triple.cpp @@ -8,9 +8,9 @@ #include #include +#include #include #include -#include __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { @@ -31,15 +31,16 @@ device_triple::device_triple(std::string &TripleString) { // handle the optional 1st entry, backend size_t Cursor = 0; size_t ColonPos = TripleString.find(":", Cursor); - auto It = std::find_if(std::begin(SyclBeMap), std::end(SyclBeMap), - [=, &Cursor](const std::pair &Element) { - size_t Found = TripleString.find(Element.first, Cursor); - if (Found != std::string::npos) { - Cursor = Found; - return true; - } - return false; - }); + auto It = std::find_if( + std::begin(SyclBeMap), std::end(SyclBeMap), + [=, &Cursor](const std::pair &Element) { + size_t Found = TripleString.find(Element.first, Cursor); + if (Found != std::string::npos) { + Cursor = Found; + return true; + } + return false; + }); if (It == SyclBeMap.end()) { Backend = backend::all; } else { @@ -50,7 +51,7 @@ device_triple::device_triple(std::string &TripleString) { Cursor = Cursor + It->first.size(); } } - + // handle the optional 2nd entry, device type auto Iter = std::find_if( std::begin(SyclDeviceTypeMap), std::end(SyclDeviceTypeMap), @@ -73,7 +74,7 @@ device_triple::device_triple(std::string &TripleString) { Cursor = Cursor + Iter->first.size(); } } - + // handle the optional 3rd entry, device number if (Cursor < TripleString.size()) { try { @@ -82,9 +83,11 @@ device_triple::device_triple(std::string &TripleString) { char message[100]; strcpy(message, "Invalid device triple: "); std::strcat(message, TripleString.c_str()); - std::strcat(message, "\nPossible backend values are {opencl,level_zero,cuda,*}."); + std::strcat(message, + "\nPossible backend values are {opencl,level_zero,cuda,*}."); std::strcat(message, "\nPossible device types are {host,cpu,gpu,acc,*}."); - std::strcat(message, "\nDevice number should be an non-negative integer.\n"); + std::strcat(message, + "\nDevice number should be an non-negative integer.\n"); throw cl::sycl::invalid_parameter_error(message, PI_INVALID_VALUE); } } else { diff --git a/sycl/test/basic_tests/select_device.cpp b/sycl/test/basic_tests/select_device.cpp index e33d9f918c8fc..d80b6dcee87cd 100644 --- a/sycl/test/basic_tests/select_device.cpp +++ b/sycl/test/basic_tests/select_device.cpp @@ -11,7 +11,6 @@ // when SYCL_DEVICE_TRIPLES is set // Checks that no device is selected when no device of desired type is // available. -// UNSUPPORTED: windows #include #include From 779d304b429e72e9814932d3e31b5e82255462fa Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 25 Aug 2020 10:56:37 -0700 Subject: [PATCH 09/45] Update sycl/include/CL/sycl/device_triple.hpp Co-authored-by: Romanov Vlad <17316488+romanovvlad@users.noreply.github.com> --- sycl/include/CL/sycl/device_triple.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/include/CL/sycl/device_triple.hpp b/sycl/include/CL/sycl/device_triple.hpp index 18ee51fc63a9b..dd26bcdda3c75 100644 --- a/sycl/include/CL/sycl/device_triple.hpp +++ b/sycl/include/CL/sycl/device_triple.hpp @@ -1,4 +1,4 @@ -//==-------------- triple.hpp - SYCL device triple descripter --------------==// +//==-------------- device_triple.hpp - SYCL device triple descriptor --------------==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. From f8034c3cfb314b68924a7441f8f7244d4715bf47 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 25 Aug 2020 10:57:51 -0700 Subject: [PATCH 10/45] Update sycl/source/device_triple.cpp Co-authored-by: Romanov Vlad <17316488+romanovvlad@users.noreply.github.com> --- sycl/source/device_triple.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sycl/source/device_triple.cpp b/sycl/source/device_triple.cpp index 32a403941de0f..ca0dbdf8630f1 100644 --- a/sycl/source/device_triple.cpp +++ b/sycl/source/device_triple.cpp @@ -8,10 +8,11 @@ #include #include -#include #include #include +#include + __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { From da4eab22e72b90ed6e545804ab41e614bd93a1c9 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 25 Aug 2020 11:09:27 -0700 Subject: [PATCH 11/45] feedback accmmodated Signed-off-by: Byoungro So --- sycl/include/CL/sycl/device_triple.hpp | 2 +- sycl/source/detail/config.hpp | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/sycl/include/CL/sycl/device_triple.hpp b/sycl/include/CL/sycl/device_triple.hpp index 18ee51fc63a9b..46ebac5ce88a7 100644 --- a/sycl/include/CL/sycl/device_triple.hpp +++ b/sycl/include/CL/sycl/device_triple.hpp @@ -22,7 +22,7 @@ class device_triple { backend Backend; info::device_type DeviceType; int32_t DeviceNum; - const int DEVICE_NUM_UNSPECIFIED = -1; + static constexpr int DEVICE_NUM_UNSPECIFIED = -1; public: device_triple(std::string &TripleString); diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index 2bd0b01cedf13..0aec59e670253 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -183,9 +183,8 @@ template <> class SYCLConfig { const char *ValStr = BaseT::getRawValue(); if (ValStr) { std::string TripleString = ValStr; - std::transform(TripleString.begin(), TripleString.end(), - TripleString.begin(), ::tolower); - TripleList = new device_triple_list(TripleString); + static device_triple_list DTL = TripleString; + TripleList = &DTL; } return TripleList; } From fa1fd6e8b39faa2d62241305dc8e4cdd5a9e3481 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 25 Aug 2020 11:18:45 -0700 Subject: [PATCH 12/45] clang-format Signed-off-by: Byoungro So --- sycl/include/CL/sycl/device_triple.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/include/CL/sycl/device_triple.hpp b/sycl/include/CL/sycl/device_triple.hpp index 76da0fbeb4252..f539bd627ce53 100644 --- a/sycl/include/CL/sycl/device_triple.hpp +++ b/sycl/include/CL/sycl/device_triple.hpp @@ -1,4 +1,4 @@ -//==-------------- device_triple.hpp - SYCL device triple descriptor --------------==// +//==---------- device_triple.hpp - SYCL device triple descriptor -----------==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. From 230bbd4de8d742058630ca73357d0ec99e900fc0 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 26 Aug 2020 11:53:51 -0700 Subject: [PATCH 13/45] moved device_triple.hpp/cpp into 'detail' namespace Signed-off-by: Byoungro So --- sycl/include/CL/sycl/{ => detail}/device_triple.hpp | 2 ++ sycl/source/CMakeLists.txt | 2 +- sycl/source/detail/config.hpp | 2 +- sycl/source/{ => detail}/device_triple.cpp | 4 +++- sycl/source/detail/pi.cpp | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) rename sycl/include/CL/sycl/{ => detail}/device_triple.hpp (98%) rename sycl/source/{ => detail}/device_triple.cpp (97%) diff --git a/sycl/include/CL/sycl/device_triple.hpp b/sycl/include/CL/sycl/detail/device_triple.hpp similarity index 98% rename from sycl/include/CL/sycl/device_triple.hpp rename to sycl/include/CL/sycl/detail/device_triple.hpp index f539bd627ce53..bf518a0b83f94 100644 --- a/sycl/include/CL/sycl/device_triple.hpp +++ b/sycl/include/CL/sycl/detail/device_triple.hpp @@ -17,6 +17,7 @@ __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { +namespace detail { class device_triple { backend Backend; @@ -87,5 +88,6 @@ inline std::ostream &operator<<(std::ostream &Out, return Out; } +} // namespace detail } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/CMakeLists.txt b/sycl/source/CMakeLists.txt index 3c8a63f95de72..814def0cd621d 100644 --- a/sycl/source/CMakeLists.txt +++ b/sycl/source/CMakeLists.txt @@ -109,6 +109,7 @@ set(SYCL_SOURCES "detail/context_impl.cpp" "detail/device_binary_image.cpp" "detail/device_impl.cpp" + "detail/device_triple.cpp" "detail/error_handling/enqueue_kernel.cpp" "detail/event_impl.cpp" "detail/force_device.cpp" @@ -140,7 +141,6 @@ set(SYCL_SOURCES "context.cpp" "device.cpp" "device_selector.cpp" - "device_triple.cpp" "event.cpp" "exception.cpp" "exception_list.cpp" diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index 0aec59e670253..f613f4ff73448 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -10,8 +10,8 @@ #include #include +#include #include -#include #include #include diff --git a/sycl/source/device_triple.cpp b/sycl/source/detail/device_triple.cpp similarity index 97% rename from sycl/source/device_triple.cpp rename to sycl/source/detail/device_triple.cpp index ca0dbdf8630f1..f3ae48d331259 100644 --- a/sycl/source/device_triple.cpp +++ b/sycl/source/detail/device_triple.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include +#include #include #include #include @@ -15,6 +15,7 @@ __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { +namespace detail { device_triple::device_triple(std::string &TripleString) { const std::array, 5> @@ -111,5 +112,6 @@ device_triple_list::device_triple_list(std::string &TripleString) { } } +} // namespace detail } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index bed4e5a4ef317..8a1f28547d5a2 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -14,8 +14,8 @@ #include "context_impl.hpp" #include #include +#include #include -#include #include #include From 18bb025d038367349b53084cf2ba90f0196e4bd4 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 3 Sep 2020 22:43:02 -0700 Subject: [PATCH 14/45] refactored with device_filter data structure Signed-off-by: Byoungro So --- sycl/include/CL/sycl/detail/device_filter.hpp | 94 +++++++++++++++++++ sycl/include/CL/sycl/detail/device_triple.hpp | 93 ------------------ sycl/source/CMakeLists.txt | 2 +- sycl/source/detail/config.def | 2 +- sycl/source/detail/config.hpp | 20 ++-- .../{device_triple.cpp => device_filter.cpp} | 39 ++++---- sycl/source/detail/filter_selector_impl.cpp | 11 +-- sycl/source/detail/filter_selector_impl.hpp | 13 +-- sycl/source/detail/pi.cpp | 12 +-- sycl/test/basic_tests/select_device.cpp | 22 ++--- 10 files changed, 150 insertions(+), 158 deletions(-) create mode 100644 sycl/include/CL/sycl/detail/device_filter.hpp delete mode 100644 sycl/include/CL/sycl/detail/device_triple.hpp rename sycl/source/detail/{device_triple.cpp => device_filter.cpp} (74%) 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..9db044a46277d --- /dev/null +++ b/sycl/include/CL/sycl/detail/device_filter.hpp @@ -0,0 +1,94 @@ +//==---------- 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::host; + 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(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(std::string& FilterString); + device_filter_list(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) { + switch (Filter.Backend) { + case backend::host: + Out << std::string("host"); + break; + case backend::opencl: + Out << std::string("opencl"); + break; + case backend::level_zero: + Out << std::string("level-zero"); + break; + case backend::cuda: + Out << std::string("cuda"); + break; + case backend::all: + Out << std::string("*"); + } + Out << std::string(":"); + if (Filter.DeviceType == info::device_type::host) { + Out << std::string("host"); + } else if (Filter.DeviceType == info::device_type::cpu) { + Out << std::string("cpu"); + } else if (Filter.DeviceType == info::device_type::gpu) { + Out << std::string("gpu"); + } else if (Filter.DeviceType == info::device_type::accelerator) { + Out << std::string("acceclerator"); + } else if (Filter.DeviceType == info::device_type::all) { + Out << std::string("*"); + } + if (!Filter.HasDeviceNum) { + Out << std::string(":") << 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/include/CL/sycl/detail/device_triple.hpp b/sycl/include/CL/sycl/detail/device_triple.hpp deleted file mode 100644 index bf518a0b83f94..0000000000000 --- a/sycl/include/CL/sycl/detail/device_triple.hpp +++ /dev/null @@ -1,93 +0,0 @@ -//==---------- device_triple.hpp - SYCL device triple 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 { - -class device_triple { - backend Backend; - info::device_type DeviceType; - int32_t DeviceNum; - static constexpr int DEVICE_NUM_UNSPECIFIED = -1; - -public: - device_triple(std::string &TripleString); - backend getBackend() const { return Backend; } - info::device_type getDeviceType() const { return DeviceType; } - int32_t getDeviceNum() const { return DeviceNum; } - friend std::ostream &operator<<(std::ostream &Out, const device_triple &Trp); -}; - -class device_triple_list { - std::vector TripleList; - -public: - device_triple_list(std::string &TripleString); - device_triple_list(device_triple &Trp); - std::vector &get() { return TripleList; } - friend std::ostream &operator<<(std::ostream &Out, - const device_triple_list &List); -}; - -inline std::ostream &operator<<(std::ostream &Out, const device_triple &Trp) { - switch (Trp.Backend) { - case backend::host: - Out << std::string("host"); - break; - case backend::opencl: - Out << std::string("opencl"); - break; - case backend::level_zero: - Out << std::string("level-zero"); - break; - case backend::cuda: - Out << std::string("cuda"); - break; - case backend::all: - Out << std::string("*"); - } - Out << std::string(":"); - if (Trp.DeviceType == info::device_type::host) { - Out << std::string("host"); - } else if (Trp.DeviceType == info::device_type::cpu) { - Out << std::string("cpu"); - } else if (Trp.DeviceType == info::device_type::gpu) { - Out << std::string("gpu"); - } else if (Trp.DeviceType == info::device_type::accelerator) { - Out << std::string("acceclerator"); - } else if (Trp.DeviceType == info::device_type::all) { - Out << std::string("*"); - } - if (Trp.DeviceNum != Trp.DEVICE_NUM_UNSPECIFIED) { - Out << std::string(":") << Trp.DeviceNum; - } - return Out; -} - -inline std::ostream &operator<<(std::ostream &Out, - const device_triple_list &List) { - for (const device_triple &Trp : List.TripleList) { - Out << Trp; - Out << ","; - } - return Out; -} - -} // namespace detail -} // namespace sycl -} // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/CMakeLists.txt b/sycl/source/CMakeLists.txt index eac5892897e35..fd2c69d59494d 100644 --- a/sycl/source/CMakeLists.txt +++ b/sycl/source/CMakeLists.txt @@ -108,8 +108,8 @@ set(SYCL_SOURCES "detail/config.cpp" "detail/context_impl.cpp" "detail/device_binary_image.cpp" + "detail/device_filter.cpp" "detail/device_impl.cpp" - "detail/device_triple.cpp" "detail/error_handling/enqueue_kernel.cpp" "detail/event_impl.cpp" "detail/filter_selector_impl.cpp" diff --git a/sycl/source/detail/config.def b/sycl/source/detail/config.def index 13b1d92ce8605..3f097c3796c25 100644 --- a/sycl/source/detail/config.def +++ b/sycl/source/detail/config.def @@ -16,4 +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_TRIPLES, 1024, __SYCL_DEVICE_TRIPLES) +CONFIG(SYCL_DEVICE_FILTER, 1024, __SYCL_DEVICE_FILTER) diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index f613f4ff73448..d819edece80ae 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include @@ -165,28 +165,28 @@ template <> class SYCLConfig { } }; -template <> class SYCLConfig { - using BaseT = SYCLConfigBase; +template <> class SYCLConfig { + using BaseT = SYCLConfigBase; public: - static device_triple_list *get() { + static device_filter_list* get() { static bool Initialized = false; - static device_triple_list *TripleList = nullptr; + 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 TripleList; + return FilterList; } Initialized = true; const char *ValStr = BaseT::getRawValue(); if (ValStr) { - std::string TripleString = ValStr; - static device_triple_list DTL = TripleString; - TripleList = &DTL; + std::string FilterString = ValStr; + static device_filter_list DFL = FilterString; + FilterList = &DFL; } - return TripleList; + return FilterList; } }; diff --git a/sycl/source/detail/device_triple.cpp b/sycl/source/detail/device_filter.cpp similarity index 74% rename from sycl/source/detail/device_triple.cpp rename to sycl/source/detail/device_filter.cpp index f3ae48d331259..7ed5aab421133 100644 --- a/sycl/source/detail/device_triple.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -1,4 +1,4 @@ -//==------------------- device_triple.cpp ----------------------------------==// +//==------------------- 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. @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include +#include #include #include #include @@ -17,7 +17,7 @@ __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { namespace detail { -device_triple::device_triple(std::string &TripleString) { +device_filter::device_filter(std::string& FilterString) { const std::array, 5> SyclDeviceTypeMap = {{{"host", info::device_type::host}, {"cpu", info::device_type::cpu}, @@ -32,11 +32,11 @@ device_triple::device_triple(std::string &TripleString) { // handle the optional 1st entry, backend size_t Cursor = 0; - size_t ColonPos = TripleString.find(":", Cursor); + size_t ColonPos = FilterString.find(":", Cursor); auto It = std::find_if( std::begin(SyclBeMap), std::end(SyclBeMap), [=, &Cursor](const std::pair &Element) { - size_t Found = TripleString.find(Element.first, Cursor); + size_t Found = FilterString.find(Element.first, Cursor); if (Found != std::string::npos) { Cursor = Found; return true; @@ -58,7 +58,7 @@ device_triple::device_triple(std::string &TripleString) { auto Iter = std::find_if( std::begin(SyclDeviceTypeMap), std::end(SyclDeviceTypeMap), [=, &Cursor](const std::pair &Element) { - size_t Found = TripleString.find(Element.first, Cursor); + size_t Found = FilterString.find(Element.first, Cursor); if (Found != std::string::npos) { Cursor = Found; return true; @@ -69,7 +69,7 @@ device_triple::device_triple(std::string &TripleString) { DeviceType = info::device_type::all; } else { DeviceType = Iter->second; - ColonPos = TripleString.find(":", Cursor); + ColonPos = FilterString.find(":", Cursor); if (ColonPos != std::string::npos) { Cursor = ColonPos + 1; } else { @@ -78,13 +78,14 @@ device_triple::device_triple(std::string &TripleString) { } // handle the optional 3rd entry, device number - if (Cursor < TripleString.size()) { + if (Cursor < FilterString.size()) { try { - DeviceNum = stoi(TripleString.substr(ColonPos + 1)); + DeviceNum = stoi(FilterString.substr(ColonPos + 1)); + HasDeviceNum = true; } catch (...) { char message[100]; - strcpy(message, "Invalid device triple: "); - std::strcat(message, TripleString.c_str()); + strcpy(message, "Invalid device filter: "); + std::strcat(message, FilterString.c_str()); std::strcat(message, "\nPossible backend values are {opencl,level_zero,cuda,*}."); std::strcat(message, "\nPossible device types are {host,cpu,gpu,acc,*}."); @@ -92,22 +93,20 @@ device_triple::device_triple(std::string &TripleString) { "\nDevice number should be an non-negative integer.\n"); throw cl::sycl::invalid_parameter_error(message, PI_INVALID_VALUE); } - } else { - DeviceNum = DEVICE_NUM_UNSPECIFIED; } } -device_triple_list::device_triple_list(std::string &TripleString) { - std::transform(TripleString.begin(), TripleString.end(), TripleString.begin(), +device_filter_list::device_filter_list(std::string& FilterString) { + std::transform(FilterString.begin(), FilterString.end(), FilterString.begin(), ::tolower); size_t Pos = 0; - while (Pos < TripleString.size()) { - size_t CommaPos = TripleString.find(",", Pos); + while (Pos < FilterString.size()) { + size_t CommaPos = FilterString.find(",", Pos); if (CommaPos == std::string::npos) { - CommaPos = TripleString.size(); + CommaPos = FilterString.size(); } - std::string SubString = TripleString.substr(Pos, CommaPos - Pos); - TripleList.push_back(device_triple(SubString)); + std::string SubString = FilterString.substr(Pos, CommaPos - Pos); + FilterList.push_back(device_filter(SubString)); Pos = CommaPos + 1; } } diff --git a/sycl/source/detail/filter_selector_impl.cpp b/sycl/source/detail/filter_selector_impl.cpp index 58d85cf6e7388..8d5ef2b433a01 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,9 +134,8 @@ 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(); - DeviceTypeOK = (DT == Filter.DeviceType); + info::device_type DT = Dev.get_info(); + DeviceTypeOK = (DT == Filter.DeviceType); } if (Filter.HasDeviceNum) { // Only check device number if we're good on the previous matches diff --git a/sycl/source/detail/filter_selector_impl.hpp b/sycl/source/detail/filter_selector_impl.hpp index a4cde25f7c66c..e1406b51787bc 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,16 +22,8 @@ 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: filter_selector_impl(const std::string &filter); diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 8a1f28547d5a2..598994783d897 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -14,7 +14,7 @@ #include "context_impl.hpp" #include #include -#include +#include #include #include #include @@ -215,18 +215,18 @@ bool findPlugins(vector_class> &PluginNames) { // search is done for libpi_opencl.so/pi_opencl.dll file in LD_LIBRARY_PATH // env only. // - device_triple_list *TripleList = SYCLConfig::get(); - if (!TripleList) { + 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 Triples = TripleList->get(); + std::vector Filters = FilterList->get(); bool OpenCLFound = false; bool LevelZeroFound = false; bool CudaFound = false; - for (const device_triple &Trp : Triples) { - backend Backend = Trp.getBackend(); + 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); diff --git a/sycl/test/basic_tests/select_device.cpp b/sycl/test/basic_tests/select_device.cpp index d80b6dcee87cd..82d76c7d9f721 100644 --- a/sycl/test/basic_tests/select_device.cpp +++ b/sycl/test/basic_tests/select_device.cpp @@ -1,14 +1,14 @@ // RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out // RUN: %t.out -// RUN: env SYCL_DEVICE_TRIPLES="*" %t.out -// RUN: env SYCL_DEVICE_TRIPLES=cpu %t.out -// RUN: env SYCL_DEVICE_TRIPLES=level_zero:gpu %t.out -// RUN: env SYCL_DEVICE_TRIPLES=opencl:gpu %t.out -// RUN: env SYCL_DEVICE_TRIPLES=cpu,level_zero:gpu %t.out -// RUN: env SYCL_DEVICE_TRIPLES=opencl:acc:0 %t.out +// RUN: env SYCL_DEVICE_FILTER="*" %t.out +// RUN: env SYCL_DEVICE_FILTER=cpu %t.out +// RUN: env SYCL_DEVICE_FILTER=level_zero:gpu %t.out +// RUN: 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_TRIPLES is set +// when SYCL_DEVICE_FILTER is set // Checks that no device is selected when no device of desired type is // available. @@ -18,10 +18,10 @@ using namespace cl::sycl; int main() { - const char *envVal = std::getenv("SYCL_DEVICE_TRIPLES"); + const char *envVal = std::getenv("SYCL_DEVICE_FILTER"); std::string forcedPIs; if (envVal) { - std::cout << "SYCL_DEVICE_TRIPLES=" << envVal << std::endl; + std::cout << "SYCL_DEVICE_FILTER=" << envVal << std::endl; forcedPIs = envVal; } if (!envVal || forcedPIs == "*" || @@ -44,7 +44,7 @@ int main() { 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_TRIPLES + // HOST device is always available regardless of SYCL_DEVICE_FILTER { host_selector hs; device d = hs.select_device(); @@ -57,7 +57,7 @@ int main() { std::cout << "ACC device is found: " << d.is_accelerator() << std::endl; } if (envVal && (forcedPIs.find("cpu") == std::string::npos && - // remove the following condition when SYCL_DEVICE_TRIPLES + // remove the following condition when SYCL_DEVICE_FILTER // filter works in device selectors forcedPIs.find("opencl") == std::string::npos && forcedPIs.find("*") == std::string::npos)) { From 0eb0697218a914904aa621c099026c9792eece2f Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 3 Sep 2020 22:48:10 -0700 Subject: [PATCH 15/45] clang-format Signed-off-by: Byoungro So --- sycl/include/CL/sycl/detail/device_filter.hpp | 26 ++++++++++--------- sycl/source/detail/config.hpp | 2 +- sycl/source/detail/device_filter.cpp | 4 +-- sycl/source/detail/filter_selector_impl.cpp | 2 +- sycl/source/detail/filter_selector_impl.hpp | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/sycl/include/CL/sycl/detail/device_filter.hpp b/sycl/include/CL/sycl/detail/device_filter.hpp index 9db044a46277d..90c317216b446 100644 --- a/sycl/include/CL/sycl/detail/device_filter.hpp +++ b/sycl/include/CL/sycl/detail/device_filter.hpp @@ -28,9 +28,10 @@ struct device_filter { bool HasDeviceNum = false; int MatchesSeen = 0; - device_filter() {}; - device_filter(std::string& FilterString); - friend std::ostream &operator<<(std::ostream &Out, const device_filter &Filter); + device_filter(){}; + device_filter(std::string &FilterString); + friend std::ostream &operator<<(std::ostream &Out, + const device_filter &Filter); }; class device_filter_list { @@ -38,14 +39,15 @@ class device_filter_list { public: device_filter_list() {} - device_filter_list(std::string& FilterString); - device_filter_list(device_filter& Filter); - std::vector& get() { return FilterList; } - friend std::ostream& operator<<(std::ostream& Out, - const device_filter_list& List); + device_filter_list(std::string &FilterString); + device_filter_list(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) { +inline std::ostream &operator<<(std::ostream &Out, + const device_filter &Filter) { switch (Filter.Backend) { case backend::host: Out << std::string("host"); @@ -80,9 +82,9 @@ inline std::ostream &operator<<(std::ostream& Out, const device_filter& Filter) return Out; } -inline std::ostream &operator<<(std::ostream& Out, - const device_filter_list& List) { - for (const device_filter& Filter : List.FilterList) { +inline std::ostream &operator<<(std::ostream &Out, + const device_filter_list &List) { + for (const device_filter &Filter : List.FilterList) { Out << Filter; Out << ","; } diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index d819edece80ae..e56eeacdde98b 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -169,7 +169,7 @@ template <> class SYCLConfig { using BaseT = SYCLConfigBase; public: - static device_filter_list* get() { + static device_filter_list *get() { static bool Initialized = false; static device_filter_list *FilterList = nullptr; diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index 7ed5aab421133..31835b17f85e4 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -17,7 +17,7 @@ __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { namespace detail { -device_filter::device_filter(std::string& FilterString) { +device_filter::device_filter(std::string &FilterString) { const std::array, 5> SyclDeviceTypeMap = {{{"host", info::device_type::host}, {"cpu", info::device_type::cpu}, @@ -96,7 +96,7 @@ device_filter::device_filter(std::string& FilterString) { } } -device_filter_list::device_filter_list(std::string& FilterString) { +device_filter_list::device_filter_list(std::string &FilterString) { std::transform(FilterString.begin(), FilterString.end(), FilterString.begin(), ::tolower); size_t Pos = 0; diff --git a/sycl/source/detail/filter_selector_impl.cpp b/sycl/source/detail/filter_selector_impl.cpp index 8d5ef2b433a01..b062c62cfe075 100644 --- a/sycl/source/detail/filter_selector_impl.cpp +++ b/sycl/source/detail/filter_selector_impl.cpp @@ -135,7 +135,7 @@ int filter_selector_impl::operator()(const device &Dev) const { } if (Filter.HasDeviceType) { info::device_type DT = Dev.get_info(); - DeviceTypeOK = (DT == Filter.DeviceType); + DeviceTypeOK = (DT == Filter.DeviceType); } if (Filter.HasDeviceNum) { // Only check device number if we're good on the previous matches diff --git a/sycl/source/detail/filter_selector_impl.hpp b/sycl/source/detail/filter_selector_impl.hpp index e1406b51787bc..99392fbfa2564 100644 --- a/sycl/source/detail/filter_selector_impl.hpp +++ b/sycl/source/detail/filter_selector_impl.hpp @@ -23,7 +23,7 @@ namespace ONEAPI { namespace detail { typedef struct sycl::detail::device_filter filter; - + class filter_selector_impl { public: filter_selector_impl(const std::string &filter); From 1b12fb25c2d81ee413b292aac16621d31e90996b Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 8 Sep 2020 08:58:35 -0700 Subject: [PATCH 16/45] Update sycl/include/CL/sycl/detail/device_filter.hpp Co-authored-by: kbobrovs --- sycl/include/CL/sycl/detail/device_filter.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/include/CL/sycl/detail/device_filter.hpp b/sycl/include/CL/sycl/detail/device_filter.hpp index 90c317216b446..3abf854bf04c3 100644 --- a/sycl/include/CL/sycl/detail/device_filter.hpp +++ b/sycl/include/CL/sycl/detail/device_filter.hpp @@ -76,7 +76,7 @@ inline std::ostream &operator<<(std::ostream &Out, } else if (Filter.DeviceType == info::device_type::all) { Out << std::string("*"); } - if (!Filter.HasDeviceNum) { + if (Filter.HasDeviceNum) { Out << std::string(":") << Filter.DeviceNum; } return Out; From c1475c7a0f40053707e9063d1d8d038db34a6bf6 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 8 Sep 2020 11:25:05 -0700 Subject: [PATCH 17/45] added comments about Initialized Signed-off-by: Byoungro So --- sycl/source/detail/config.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index e56eeacdde98b..d03c13c974b35 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -179,13 +179,18 @@ template <> class SYCLConfig { return FilterList; } - Initialized = true; const char *ValStr = BaseT::getRawValue(); if (ValStr) { std::string FilterString = ValStr; static device_filter_list DFL = FilterString; FilterList = &DFL; } + // as mentioned above, configuration parameters are process 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; } }; From 1c0226bfb9c7553653222f36f24883215159594f Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 8 Sep 2020 14:35:45 -0700 Subject: [PATCH 18/45] added back documentation of new env var Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 629f04618fa06..7887f65e4a63e 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -14,6 +14,7 @@ subject to change. Do not rely on these variables in production code. | 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_DEVICE_FILTER (TBD) | {backend:device_type:backend:device_num} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | | 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.| From a1f075ed90ae0c0be509aa12e9970d7ee5b6c823 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 8 Sep 2020 14:40:42 -0700 Subject: [PATCH 19/45] fixed a typo Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 7887f65e4a63e..f5fab8c674f3f 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -14,7 +14,7 @@ subject to change. Do not rely on these variables in production code. | 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_DEVICE_FILTER (TBD) | {backend:device_type:backend:device_num} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | +| SYCL_DEVICE_FILTER (name TBD) | {backend:device_type:device_num} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | | 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.| From e0d037f6f50c168d5e0b4849022b9c035b50dad5 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 10 Sep 2020 10:20:57 -0700 Subject: [PATCH 20/45] clarifiied HOST availability for default_selector Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index f5fab8c674f3f..d3269c8e1b81f 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -14,7 +14,7 @@ subject to change. Do not rely on these variables in production code. | 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_DEVICE_FILTER (name TBD) | {backend:device_type:device_num} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | +| SYCL_DEVICE_FILTER (name TBD) | {backend:device_type:device_num} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. HOST device is always available for the default_selecotr and the filter won't affect HOST device availability in any case. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | | 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.| From 7721ca50a8f4621cfb06a6ffd6398769a439264a Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 10 Sep 2020 10:33:08 -0700 Subject: [PATCH 21/45] typo Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index d3269c8e1b81f..4882ea1c75dd7 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -14,7 +14,7 @@ subject to change. Do not rely on these variables in production code. | 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_DEVICE_FILTER (name TBD) | {backend:device_type:device_num} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. HOST device is always available for the default_selecotr and the filter won't affect HOST device availability in any case. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | +| SYCL_DEVICE_FILTER (name TBD) | {backend:device_type:device_num} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. HOST device is always available for the default_selector and the filter won't affect HOST device availability in any case. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | | 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.| From 432eb20a936fb91bdc51e0485cac53d9f56a86ce Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 10 Sep 2020 11:11:51 -0700 Subject: [PATCH 22/45] Update sycl/doc/EnvironmentVariables.md Co-authored-by: Pavel Chupin <45979248+pvchupin@users.noreply.github.com> --- sycl/doc/EnvironmentVariables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 4882ea1c75dd7..fdd05412f3a80 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -14,7 +14,7 @@ subject to change. Do not rely on these variables in production code. | 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_DEVICE_FILTER (name TBD) | {backend:device_type:device_num} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. HOST device is always available for the default_selector and the filter won't affect HOST device availability in any case. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | +| SYCL_DEVICE_FILTER (tentative name) | {backend:device_type:device_num} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. HOST device is always available for the default_selector and the filter won't affect HOST device availability in any case. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | | 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.| From ff720c4eb2d202bc60b0b5ae319c2ce6a2cd85d1 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 10 Sep 2020 11:53:58 -0700 Subject: [PATCH 23/45] added deprecation notice Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 4882ea1c75dd7..2137e7a0650c8 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -12,8 +12,8 @@ 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 variables 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 variables in the future. The specific grace period is not decided yet. Please use the new env var SYCL_DEVICE_FILTER instead. | | SYCL_DEVICE_FILTER (name TBD) | {backend:device_type:device_num} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. HOST device is always available for the default_selector and the filter won't affect HOST device availability in any case. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | | 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. | From 7a375f412573befa39084e408e5f4df87c7190a2 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 10 Sep 2020 11:59:21 -0700 Subject: [PATCH 24/45] typo Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 0612d7b8f6a76..da97240e8c662 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -12,8 +12,8 @@ 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. We are planning to deprecate SYCL_BE environment variables 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 variables in the future. The specific grace period is not decided yet. Please use the new env var SYCL_DEVICE_FILTER instead. | +| 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} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. HOST device is always available for the default_selector and the filter won't affect HOST device availability in any case. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | | 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. | From 52c1c88775b24cab72e72af5703334705ce278f5 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 10 Sep 2020 23:58:01 -0700 Subject: [PATCH 25/45] added DeviceNum bonus point Signed-off-by: Byoungro So --- sycl/source/device_selector.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/sycl/source/device_selector.cpp b/sycl/source/device_selector.cpp index f0f9b23792599..e4e1495984608 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,11 +36,23 @@ static bool isDeviceOfPreferredSyclBe(const device &Device) { backend::level_zero; } +static bool isDeviceOfPreferredNumber(detail::device_filter_list *FilterList, + int index) { + for (const detail::device_filter &Filter : FilterList->get()) { + if (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; const device *res = nullptr; + detail::device_filter_list *FilterList = + detail::SYCLConfig::get(); + for (const auto &dev : devices) { int dev_score = (*this)(dev); @@ -59,6 +73,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 (FilterList && isDeviceOfPreferredNumber(FilterList, index)) { + dev_score += 30; + } + // 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 From c46a497e9114c9c6b86e722abf9759fb56a222f6 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 11 Sep 2020 14:03:22 -0700 Subject: [PATCH 26/45] description change Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index da97240e8c662..754e06b236330 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -14,7 +14,9 @@ subject to change. Do not rely on these variables in production code. | 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. 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} | Force SYCL RT to use the specified device with optional backend and device_num information. It can list any number of triples separated by commas. This env var affects all different device selectors and device discovery. When no device that can satisfy the triple by a default_selector, a heuristic will choose the device that has the closest match. HOST device is always available for the default_selector and the filter won't affect HOST device availability in any case. Possible values of device_type are \*,host,cpu,gpu,acc. Possible values of backend are \*,opencl, level_zero, cuda. Device_num is an unsigned integer that indexes the enumeration of devices from sycl::platform::get_device() call. All triple entries are optional, but one of them should be present. For example, to use cpu and level_zero gpu device number 0, one can set SYCL_DEVICE_FILTER=cpu,level_zero:gpu::0. This environment variable will limit loading only specified plugins into the SYCL RT. For example, SYCL_DEVICE_FILTER=level_zero and cpu_selector will throw an error because the CPU device is not supported by the level_zero backend. | +| 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. +This environment variable will limit loading only specified plugins into the SYCL RT. 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. + | | 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.| From facf40243302a9874c2aea622e617091fdb13211 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 11 Sep 2020 14:04:46 -0700 Subject: [PATCH 27/45] format \* Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 754e06b236330..038c3071fd0b4 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -15,8 +15,7 @@ subject to change. Do not rely on these variables in production code. | 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. -This environment variable will limit loading only specified plugins into the SYCL RT. 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 will limit loading only specified plugins into the SYCL RT. 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. | | 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.| From be447995020171ef796dc71962fb7da78fc2fd64 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 11 Sep 2020 14:12:34 -0700 Subject: [PATCH 28/45] fix overflow Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 038c3071fd0b4..428dd14abb432 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -14,8 +14,7 @@ subject to change. Do not rely on these variables in production code. | 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. 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. -This environment variable will limit loading only specified plugins into the SYCL RT. 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. | +| 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. This environment variable will limit loading only specified plugins into the SYCL RT. 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. | | 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.| From 800afe45d845f48ff13617bac515ff4c172a4143 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 11 Sep 2020 15:47:38 -0700 Subject: [PATCH 29/45] moved loading plugin stmt Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 428dd14abb432..95f624c7d243c 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -14,7 +14,7 @@ subject to change. Do not rely on these variables in production code. | 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. 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. This environment variable will limit loading only specified plugins into the SYCL RT. 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. | +| 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.| From dd0621726c5a4792b989966d5d0829fe46e3d8ad Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Sat, 12 Sep 2020 11:48:08 -0700 Subject: [PATCH 30/45] give bonus points only when backend, device_type, device_num matched. Signed-off-by: Byoungro So --- sycl/source/device_selector.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/sycl/source/device_selector.cpp b/sycl/source/device_selector.cpp index e4e1495984608..db86650a75893 100644 --- a/sycl/source/device_selector.cpp +++ b/sycl/source/device_selector.cpp @@ -36,10 +36,20 @@ static bool isDeviceOfPreferredSyclBe(const device &Device) { backend::level_zero; } +// Return true if the given device 'Dev' matches with any filter static bool isDeviceOfPreferredNumber(detail::device_filter_list *FilterList, - int index) { + const device& Dev, int Index) { + 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.HasDeviceNum && Filter.DeviceNum == index) + if ((Filter.Backend == Backend || Filter.Backend == backend::all) && + (Filter.DeviceType == Type || Filter.DeviceType == info::device_type::all) && + (Filter.HasDeviceNum && Filter.DeviceNum == Index)) return true; } return false; @@ -76,7 +86,7 @@ device device_selector::select_device() const { // 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 (FilterList && isDeviceOfPreferredNumber(FilterList, index)) { + if (FilterList && isDeviceOfPreferredNumber(FilterList, dev, index)) { dev_score += 30; } From 686401783305afdd109fa068637665082ffba27e Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Sat, 12 Sep 2020 11:52:46 -0700 Subject: [PATCH 31/45] clang-format Signed-off-by: Byoungro So --- sycl/source/device_selector.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sycl/source/device_selector.cpp b/sycl/source/device_selector.cpp index db86650a75893..ebb47652a43ab 100644 --- a/sycl/source/device_selector.cpp +++ b/sycl/source/device_selector.cpp @@ -38,18 +38,19 @@ static bool isDeviceOfPreferredSyclBe(const device &Device) { // Return true if the given device 'Dev' matches with any filter static bool isDeviceOfPreferredNumber(detail::device_filter_list *FilterList, - const device& Dev, int Index) { + const device &Dev, int Index) { 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) && - (Filter.HasDeviceNum && Filter.DeviceNum == Index)) + (Filter.DeviceType == Type || + Filter.DeviceType == info::device_type::all) && + (Filter.HasDeviceNum && Filter.DeviceNum == Index)) return true; } return false; From 84942030f61369a6052019fd7e821a0397963358 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Sun, 13 Sep 2020 10:32:55 -0700 Subject: [PATCH 32/45] change as requested by feedback Signed-off-by: Byoungro So --- sycl/include/CL/sycl/detail/device_filter.hpp | 4 ++-- sycl/source/detail/config.hpp | 2 +- sycl/source/detail/device_filter.cpp | 19 +++++++++---------- sycl/source/device_selector.cpp | 4 +++- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/sycl/include/CL/sycl/detail/device_filter.hpp b/sycl/include/CL/sycl/detail/device_filter.hpp index 3abf854bf04c3..500d50cf4545d 100644 --- a/sycl/include/CL/sycl/detail/device_filter.hpp +++ b/sycl/include/CL/sycl/detail/device_filter.hpp @@ -29,7 +29,7 @@ struct device_filter { int MatchesSeen = 0; device_filter(){}; - device_filter(std::string &FilterString); + device_filter(const std::string &FilterString); friend std::ostream &operator<<(std::ostream &Out, const device_filter &Filter); }; @@ -72,7 +72,7 @@ inline std::ostream &operator<<(std::ostream &Out, } else if (Filter.DeviceType == info::device_type::gpu) { Out << std::string("gpu"); } else if (Filter.DeviceType == info::device_type::accelerator) { - Out << std::string("acceclerator"); + Out << std::string("accelerator"); } else if (Filter.DeviceType == info::device_type::all) { Out << std::string("*"); } diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index d03c13c974b35..05c8dc6e9966a 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -185,7 +185,7 @@ template <> class SYCLConfig { static device_filter_list DFL = FilterString; FilterList = &DFL; } - // as mentioned above, configuration parameters are process only once. + // 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, diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index 31835b17f85e4..ae895fafff66d 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -17,7 +17,7 @@ __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { namespace detail { -device_filter::device_filter(std::string &FilterString) { +device_filter::device_filter(const std::string &FilterString) { const std::array, 5> SyclDeviceTypeMap = {{{"host", info::device_type::host}, {"cpu", info::device_type::cpu}, @@ -83,15 +83,14 @@ device_filter::device_filter(std::string &FilterString) { DeviceNum = stoi(FilterString.substr(ColonPos + 1)); HasDeviceNum = true; } catch (...) { - char message[100]; - strcpy(message, "Invalid device filter: "); - std::strcat(message, FilterString.c_str()); - std::strcat(message, - "\nPossible backend values are {opencl,level_zero,cuda,*}."); - std::strcat(message, "\nPossible device types are {host,cpu,gpu,acc,*}."); - std::strcat(message, - "\nDevice number should be an non-negative integer.\n"); - throw cl::sycl::invalid_parameter_error(message, PI_INVALID_VALUE); + std::string Message = + std::string("Invalid device filter: ") + FilterString + + std::string( + "\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.c_str(), + PI_INVALID_VALUE); } } } diff --git a/sycl/source/device_selector.cpp b/sycl/source/device_selector.cpp index ebb47652a43ab..421705ffa420d 100644 --- a/sycl/source/device_selector.cpp +++ b/sycl/source/device_selector.cpp @@ -39,6 +39,8 @@ static bool isDeviceOfPreferredSyclBe(const device &Device) { // Return true if the given device 'Dev' matches with any filter static bool isDeviceOfPreferredNumber(detail::device_filter_list *FilterList, const device &Dev, int Index) { + if (!FilterList) + return false; info::device_type Type = Dev.get_info(); backend Backend; if (Type == info::device_type::host) @@ -87,7 +89,7 @@ device device_selector::select_device() const { // 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 (FilterList && isDeviceOfPreferredNumber(FilterList, dev, index)) { + if (isDeviceOfPreferredNumber(FilterList, dev, index)) { dev_score += 30; } From 156045a94960ec68959b729162cc80d4c3928933 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 15 Sep 2020 19:40:17 -0700 Subject: [PATCH 33/45] respond to feedback Signed-off-by: Byoungro So --- sycl/include/CL/sycl/backend_types.hpp | 10 +-- sycl/include/CL/sycl/detail/device_filter.hpp | 29 ++++---- sycl/source/detail/config.hpp | 4 +- sycl/source/detail/device_filter.cpp | 35 +++++++--- sycl/source/device_selector.cpp | 62 +++++++++++++---- .../select_device.cpp | 26 ++++--- .../filter_selector/select_device_acc.cpp | 69 +++++++++++++++++++ .../filter_selector/select_device_cpu.cpp | 66 ++++++++++++++++++ .../filter_selector/select_device_cuda.cpp | 67 ++++++++++++++++++ .../select_device_level_zero.cpp | 67 ++++++++++++++++++ .../filter_selector/select_device_opencl.cpp | 58 ++++++++++++++++ 11 files changed, 439 insertions(+), 54 deletions(-) rename sycl/test/{basic_tests => filter_selector}/select_device.cpp (74%) create mode 100644 sycl/test/filter_selector/select_device_acc.cpp create mode 100644 sycl/test/filter_selector/select_device_cpu.cpp create mode 100644 sycl/test/filter_selector/select_device_cuda.cpp create mode 100644 sycl/test/filter_selector/select_device_level_zero.cpp create mode 100644 sycl/test/filter_selector/select_device_opencl.cpp diff --git a/sycl/include/CL/sycl/backend_types.hpp b/sycl/include/CL/sycl/backend_types.hpp index bfce267d8e107..80885a47c0b0c 100644 --- a/sycl/include/CL/sycl/backend_types.hpp +++ b/sycl/include/CL/sycl/backend_types.hpp @@ -25,19 +25,19 @@ 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 << std::string("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 index 500d50cf4545d..452c9985852a8 100644 --- a/sycl/include/CL/sycl/detail/device_filter.hpp +++ b/sycl/include/CL/sycl/detail/device_filter.hpp @@ -20,7 +20,7 @@ namespace sycl { namespace detail { struct device_filter { - backend Backend = backend::host; + backend Backend = backend::all; info::device_type DeviceType = info::device_type::all; int DeviceNum = 0; bool HasBackend = false; @@ -39,8 +39,9 @@ class device_filter_list { public: device_filter_list() {} - device_filter_list(std::string &FilterString); + 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); @@ -50,34 +51,34 @@ inline std::ostream &operator<<(std::ostream &Out, const device_filter &Filter) { switch (Filter.Backend) { 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 << std::string("*"); + Out << "*"; } - Out << std::string(":"); + Out << ":"; if (Filter.DeviceType == info::device_type::host) { - Out << std::string("host"); + Out << "host"; } else if (Filter.DeviceType == info::device_type::cpu) { - Out << std::string("cpu"); + Out << "cpu"; } else if (Filter.DeviceType == info::device_type::gpu) { - Out << std::string("gpu"); + Out << "gpu"; } else if (Filter.DeviceType == info::device_type::accelerator) { - Out << std::string("accelerator"); + Out << "accelerator"; } else if (Filter.DeviceType == info::device_type::all) { - Out << std::string("*"); + Out << "*"; } if (Filter.HasDeviceNum) { - Out << std::string(":") << Filter.DeviceNum; + Out << ":" << Filter.DeviceNum; } return Out; } diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index 05c8dc6e9966a..111764d04415e 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -181,8 +181,8 @@ template <> class SYCLConfig { const char *ValStr = BaseT::getRawValue(); if (ValStr) { - std::string FilterString = ValStr; - static device_filter_list DFL = FilterString; + //std::string FilterString = ValStr; + static device_filter_list DFL{ValStr}; FilterList = &DFL; } // As mentioned above, configuration parameters are processed only once. diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index ae895fafff66d..89c46d2d79c24 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -30,9 +30,10 @@ device_filter::device_filter(const std::string &FilterString) { {"cuda", backend::cuda}, {"*", backend::all}}}; - // handle the optional 1st entry, backend + // handle the optional 1st field of the filter, backend size_t Cursor = 0; size_t ColonPos = FilterString.find(":", Cursor); + // check if the first entry matches with a known backend type auto It = std::find_if( std::begin(SyclBeMap), std::end(SyclBeMap), [=, &Cursor](const std::pair &Element) { @@ -43,6 +44,8 @@ device_filter::device_filter(const std::string &FilterString) { } return false; }); + // if no match is found, set the backend type backend::all + // which actually means 'any backend' will be a match. if (It == SyclBeMap.end()) { Backend = backend::all; } else { @@ -54,7 +57,8 @@ device_filter::device_filter(const std::string &FilterString) { } } - // handle the optional 2nd entry, device type + // handle the optional 2nd field of the filter, device type + // check if the 2nd entry matches with any known device type. auto Iter = std::find_if( std::begin(SyclDeviceTypeMap), std::end(SyclDeviceTypeMap), [=, &Cursor](const std::pair &Element) { @@ -65,6 +69,8 @@ device_filter::device_filter(const std::string &FilterString) { } return false; }); + // if no match is found, set device_type 'all' + // which actually means 'any device_type' will be a match. if (Iter == SyclDeviceTypeMap.end()) { DeviceType = info::device_type::all; } else { @@ -77,7 +83,9 @@ device_filter::device_filter(const std::string &FilterString) { } } - // handle the optional 3rd entry, device number + // 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)); @@ -85,19 +93,22 @@ device_filter::device_filter(const std::string &FilterString) { } catch (...) { std::string Message = std::string("Invalid device filter: ") + FilterString + - std::string( "\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.c_str(), - PI_INVALID_VALUE); + "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(std::string &FilterString) { +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); @@ -110,6 +121,14 @@ device_filter_list::device_filter_list(std::string &FilterString) { } } +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/device_selector.cpp b/sycl/source/device_selector.cpp index 421705ffa420d..81b4babf0e9be 100644 --- a/sycl/source/device_selector.cpp +++ b/sycl/source/device_selector.cpp @@ -37,8 +37,10 @@ static bool isDeviceOfPreferredSyclBe(const device &Device) { } // Return true if the given device 'Dev' matches with any filter -static bool isDeviceOfPreferredNumber(detail::device_filter_list *FilterList, - const device &Dev, int Index) { +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(); @@ -51,9 +53,10 @@ static bool isDeviceOfPreferredNumber(detail::device_filter_list *FilterList, 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) && - (Filter.HasDeviceNum && Filter.DeviceNum == Index)) - return true; + Filter.DeviceType == info::device_type::all)) { + if (Index < 0 || (Filter.HasDeviceNum && Filter.DeviceNum == Index)) + return true; + } } return false; } @@ -63,9 +66,6 @@ device device_selector::select_device() const { int score = REJECT_DEVICE_SCORE; const device *res = nullptr; - detail::device_filter_list *FilterList = - detail::SYCLConfig::get(); - for (const auto &dev : devices) { int dev_score = (*this)(dev); @@ -89,8 +89,8 @@ device device_selector::select_device() const { // 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 (isDeviceOfPreferredNumber(FilterList, dev, index)) { - dev_score += 30; + if (isForcedDevice(dev, index)) { + dev_score += 1000; } // SYCL spec says: "If more than one device receives the high score then @@ -137,7 +137,11 @@ 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()) @@ -156,7 +160,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; @@ -166,8 +179,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; @@ -177,8 +200,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; @@ -188,6 +221,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/basic_tests/select_device.cpp b/sycl/test/filter_selector/select_device.cpp similarity index 74% rename from sycl/test/basic_tests/select_device.cpp rename to sycl/test/filter_selector/select_device.cpp index 82d76c7d9f721..1e9ccb8b03e4f 100644 --- a/sycl/test/basic_tests/select_device.cpp +++ b/sycl/test/filter_selector/select_device.cpp @@ -1,9 +1,8 @@ // RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out -// RUN: %t.out -// RUN: env SYCL_DEVICE_FILTER="*" %t.out -// RUN: env SYCL_DEVICE_FILTER=cpu %t.out -// RUN: env SYCL_DEVICE_FILTER=level_zero:gpu %t.out -// RUN: env SYCL_DEVICE_FILTER=opencl:gpu %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 // @@ -11,11 +10,14 @@ // 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"); @@ -25,16 +27,20 @@ int main() { forcedPIs = envVal; } if (!envVal || forcedPIs == "*" || - forcedPIs.find("gpu:level_zero") != std::string::npos) { + 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("gpu:opencl") != std::string::npos) { + 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; } @@ -57,8 +63,6 @@ int main() { std::cout << "ACC device is found: " << d.is_accelerator() << std::endl; } if (envVal && (forcedPIs.find("cpu") == std::string::npos && - // remove the following condition when SYCL_DEVICE_FILTER - // filter works in device selectors forcedPIs.find("opencl") == std::string::npos && forcedPIs.find("*") == std::string::npos)) { try { @@ -68,7 +72,7 @@ int main() { std::cout << "Expectedly, CPU device is not found." << std::endl; return 0; // expected } - std::cout << "Error: CPU device is found" << std::endl; + std::cerr << "Error: CPU device is found" << std::endl; return -1; } 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..eb0f6fda25312 --- /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=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,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("OpneCL") != 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("OpneCL") != 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..471e38c743d80 --- /dev/null +++ b/sycl/test/filter_selector/select_device_cpu.cpp @@ -0,0 +1,66 @@ +// 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("OpneCL") != 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..371a465766716 --- /dev/null +++ b/sycl/test/filter_selector/select_device_cuda.cpp @@ -0,0 +1,67 @@ +// 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..87fbf229659fe --- /dev/null +++ b/sycl/test/filter_selector/select_device_level_zero.cpp @@ -0,0 +1,67 @@ +// 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..2ebe590988e85 --- /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; +} From 8de75007af1af32fce3633084ed33f2184ebf474 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 15 Sep 2020 19:48:04 -0700 Subject: [PATCH 34/45] clang-format Signed-off-by: Byoungro So --- sycl/source/detail/config.hpp | 2 +- sycl/source/detail/device_filter.cpp | 10 +++---- sycl/source/device_selector.cpp | 29 ++++++++++--------- .../filter_selector/select_device_acc.cpp | 12 ++++---- .../filter_selector/select_device_cpu.cpp | 11 +++---- .../filter_selector/select_device_cuda.cpp | 9 +++--- .../select_device_level_zero.cpp | 9 +++--- .../filter_selector/select_device_opencl.cpp | 6 ++-- 8 files changed, 46 insertions(+), 42 deletions(-) diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index 111764d04415e..3a3ff71d3e873 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -181,7 +181,7 @@ template <> class SYCLConfig { const char *ValStr = BaseT::getRawValue(); if (ValStr) { - //std::string FilterString = ValStr; + // std::string FilterString = ValStr; static device_filter_list DFL{ValStr}; FilterList = &DFL; } diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index 89c46d2d79c24..e24859ee909f6 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -93,9 +93,9 @@ device_filter::device_filter(const std::string &FilterString) { } 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"; + "\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); } } @@ -121,11 +121,11 @@ device_filter_list::device_filter_list(const std::string &FilterStr) { } } -device_filter_list::device_filter_list(device_filter& Filter) { +device_filter_list::device_filter_list(device_filter &Filter) { FilterList.push_back(Filter); } -void device_filter_list::addFilter(device_filter& Filter) { +void device_filter_list::addFilter(device_filter &Filter) { FilterList.push_back(Filter); } diff --git a/sycl/source/device_selector.cpp b/sycl/source/device_selector.cpp index 81b4babf0e9be..8c664c3b98f7a 100644 --- a/sycl/source/device_selector.cpp +++ b/sycl/source/device_selector.cpp @@ -37,9 +37,9 @@ static bool isDeviceOfPreferredSyclBe(const device &Device) { } // Return true if the given device 'Dev' matches with any filter -static bool isForcedDevice(const device &Dev, int Index=-1) { +static bool isForcedDevice(const device &Dev, int Index = -1) { detail::device_filter_list *FilterList = - detail::SYCLConfig::get(); + detail::SYCLConfig::get(); if (!FilterList) return false; @@ -55,7 +55,7 @@ static bool isForcedDevice(const device &Dev, int Index=-1) { (Filter.DeviceType == Type || Filter.DeviceType == info::device_type::all)) { if (Index < 0 || (Filter.HasDeviceNum && Filter.DeviceNum == Index)) - return true; + return true; } } return false; @@ -140,8 +140,9 @@ int default_selector::operator()(const device &dev) const { // filter device gets a high point. if (isForcedDevice(dev)) Score += 1000; - - else if (dev.get_info() == detail::get_forced_type()) + + else if (dev.get_info() == + detail::get_forced_type()) Score += 1000; if (dev.is_gpu()) @@ -161,12 +162,12 @@ int gpu_selector::operator()(const device &dev) const { if (dev.is_gpu()) { detail::device_filter_list *FilterList = - detail::SYCLConfig::get(); + detail::SYCLConfig::get(); if (FilterList) { if (isForcedDevice(dev)) - Score = 1000; + Score = 1000; else - return Score; + return Score; } else { Score = 1000; } @@ -182,12 +183,12 @@ int cpu_selector::operator()(const device &dev) const { if (dev.is_cpu()) { detail::device_filter_list *FilterList = - detail::SYCLConfig::get(); + detail::SYCLConfig::get(); if (FilterList) { if (isForcedDevice(dev)) - Score = 1000; + Score = 1000; else - return Score; + return Score; } else { Score = 1000; } @@ -203,12 +204,12 @@ int accelerator_selector::operator()(const device &dev) const { if (dev.is_accelerator()) { detail::device_filter_list *FilterList = - detail::SYCLConfig::get(); + detail::SYCLConfig::get(); if (FilterList) { if (isForcedDevice(dev)) - Score = 1000; + Score = 1000; else - return Score; + return Score; } else { Score = 1000; } diff --git a/sycl/test/filter_selector/select_device_acc.cpp b/sycl/test/filter_selector/select_device_acc.cpp index eb0f6fda25312..c5b7511a0d48b 100644 --- a/sycl/test/filter_selector/select_device_acc.cpp +++ b/sycl/test/filter_selector/select_device_acc.cpp @@ -26,15 +26,15 @@ int main() { device d = ds.select_device(); string name = d.get_platform().get_info(); assert(name.find("OpneCL") != string::npos); - std::cout << "ACC Device is found: " << std::boolalpha - << d.is_accelerator() << std::endl; + 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; + 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; @@ -44,8 +44,8 @@ int main() { cpu_selector cs; try { device d = cs.select_device(); - std::cerr << "CPU Device is found in error: " << std::boolalpha << d.is_cpu() - << std::endl; + 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; diff --git a/sycl/test/filter_selector/select_device_cpu.cpp b/sycl/test/filter_selector/select_device_cpu.cpp index 471e38c743d80..8eed712417eec 100644 --- a/sycl/test/filter_selector/select_device_cpu.cpp +++ b/sycl/test/filter_selector/select_device_cpu.cpp @@ -26,15 +26,15 @@ int main() { device d = ds.select_device(); string name = d.get_platform().get_info(); assert(name.find("OpneCL") != string::npos); - std::cout << "CPU Device is found: " << std::boolalpha - << d.is_cpu() << std::endl; + 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; + << std::endl; return -1; } catch (...) { std::cout << "Expectedly, GPU device is not found." << std::endl; @@ -55,9 +55,10 @@ int main() { accelerator_selector as; try { device d = as.select_device(); - std::cerr << "ACC device is found in error: " << d.is_accelerator() << std::endl; + std::cerr << "ACC device is found in error: " << d.is_accelerator() + << std::endl; return -1; - } catch(...) { + } catch (...) { std::cout << "Expectedly, ACC device is not found." << std::endl; } } diff --git a/sycl/test/filter_selector/select_device_cuda.cpp b/sycl/test/filter_selector/select_device_cuda.cpp index 371a465766716..da1f6f46a7530 100644 --- a/sycl/test/filter_selector/select_device_cuda.cpp +++ b/sycl/test/filter_selector/select_device_cuda.cpp @@ -21,14 +21,14 @@ int main() { 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; + cout << "Cuda GPU Device is found: " << boolalpha << d.is_gpu() + << std::endl; } { gpu_selector gs; @@ -57,7 +57,8 @@ int main() { accelerator_selector as; try { device d = as.select_device(); - cerr << "ACC device is found in error: " << d.is_accelerator() << std::endl; + cerr << "ACC device is found in error: " << d.is_accelerator() + << std::endl; } catch (...) { cout << "Expectedly, ACC device is not found." << std::endl; } diff --git a/sycl/test/filter_selector/select_device_level_zero.cpp b/sycl/test/filter_selector/select_device_level_zero.cpp index 87fbf229659fe..c808945889949 100644 --- a/sycl/test/filter_selector/select_device_level_zero.cpp +++ b/sycl/test/filter_selector/select_device_level_zero.cpp @@ -21,14 +21,14 @@ int main() { 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; + cout << "Level-Zero GPU Device is found: " << boolalpha << d.is_gpu() + << std::endl; } { gpu_selector gs; @@ -57,7 +57,8 @@ int main() { accelerator_selector as; try { device d = as.select_device(); - cerr << "ACC device is found in error: " << d.is_accelerator() << std::endl; + cerr << "ACC device is found in error: " << d.is_accelerator() + << std::endl; } catch (...) { cout << "Expectedly, ACC device is not found." << std::endl; } diff --git a/sycl/test/filter_selector/select_device_opencl.cpp b/sycl/test/filter_selector/select_device_opencl.cpp index 2ebe590988e85..4629564ff1fb0 100644 --- a/sycl/test/filter_selector/select_device_opencl.cpp +++ b/sycl/test/filter_selector/select_device_opencl.cpp @@ -21,14 +21,14 @@ int main() { 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; + cout << "OpenCL GPU Device is found: " << boolalpha << d.is_gpu() + << std::endl; } { gpu_selector gs; From 39c0725786a3b8242355505c886a5e47cc666212 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 15 Sep 2020 20:58:19 -0700 Subject: [PATCH 35/45] typo Signed-off-by: Byoungro So --- sycl/test/filter_selector/select_device_cuda.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sycl/test/filter_selector/select_device_cuda.cpp b/sycl/test/filter_selector/select_device_cuda.cpp index da1f6f46a7530..3e850175cbfe5 100644 --- a/sycl/test/filter_selector/select_device_cuda.cpp +++ b/sycl/test/filter_selector/select_device_cuda.cpp @@ -26,8 +26,9 @@ int main() { 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() + + assert(name.find("CUDA") != string::npos); + cout << "CUDA GPU Device is found: " << boolalpha << d.is_gpu() << std::endl; } { From f7f371892e68753d6f729d3b65e2c08347b9ffb3 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 15 Sep 2020 21:18:02 -0700 Subject: [PATCH 36/45] clang-format Signed-off-by: Byoungro So --- sycl/test/filter_selector/select_device_cuda.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/test/filter_selector/select_device_cuda.cpp b/sycl/test/filter_selector/select_device_cuda.cpp index 3e850175cbfe5..6223e38adb122 100644 --- a/sycl/test/filter_selector/select_device_cuda.cpp +++ b/sycl/test/filter_selector/select_device_cuda.cpp @@ -26,7 +26,7 @@ int main() { 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; From 4399a96931b9a16b0782b7f25a01c0d4c71f22d6 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 15 Sep 2020 22:57:15 -0700 Subject: [PATCH 37/45] respond to more feedback Signed-off-by: Byoungro So --- sycl/include/CL/sycl/detail/device_filter.hpp | 18 +----------------- .../filter_selector/select_device_cuda.cpp | 3 +-- 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/sycl/include/CL/sycl/detail/device_filter.hpp b/sycl/include/CL/sycl/detail/device_filter.hpp index 452c9985852a8..e0f9beee7e7f9 100644 --- a/sycl/include/CL/sycl/detail/device_filter.hpp +++ b/sycl/include/CL/sycl/detail/device_filter.hpp @@ -49,23 +49,7 @@ class device_filter_list { inline std::ostream &operator<<(std::ostream &Out, const device_filter &Filter) { - switch (Filter.Backend) { - case backend::host: - Out << "host"; - break; - case backend::opencl: - Out << "opencl"; - break; - case backend::level_zero: - Out << "level-zero"; - break; - case backend::cuda: - Out << "cuda"; - break; - case backend::all: - Out << "*"; - } - Out << ":"; + Out << Filter.Backend << ":"; if (Filter.DeviceType == info::device_type::host) { Out << "host"; } else if (Filter.DeviceType == info::device_type::cpu) { diff --git a/sycl/test/filter_selector/select_device_cuda.cpp b/sycl/test/filter_selector/select_device_cuda.cpp index 6223e38adb122..a8b8ae15901e6 100644 --- a/sycl/test/filter_selector/select_device_cuda.cpp +++ b/sycl/test/filter_selector/select_device_cuda.cpp @@ -26,7 +26,6 @@ int main() { 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; @@ -35,7 +34,7 @@ int main() { gpu_selector gs; device d = gs.select_device(); string name = d.get_platform().get_info(); - assert(name.find("Cuda") != string::npos); + assert(name.find("CUDA") != string::npos); cout << name << " is found: " << boolalpha << d.is_gpu() << std::endl; } { From 9b83eee5fc891586e2eeb8d8bdb913926c316442 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 16 Sep 2020 07:35:31 -0700 Subject: [PATCH 38/45] Update sycl/test/filter_selector/select_device_cpu.cpp Co-authored-by: vladimirlaz --- sycl/test/filter_selector/select_device_cpu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/test/filter_selector/select_device_cpu.cpp b/sycl/test/filter_selector/select_device_cpu.cpp index 8eed712417eec..e16b850018e22 100644 --- a/sycl/test/filter_selector/select_device_cpu.cpp +++ b/sycl/test/filter_selector/select_device_cpu.cpp @@ -25,7 +25,7 @@ int main() { default_selector ds; device d = ds.select_device(); string name = d.get_platform().get_info(); - assert(name.find("OpneCL") != string::npos); + assert(name.find("OpenCL") != string::npos); std::cout << "CPU Device is found: " << std::boolalpha << d.is_cpu() << std::endl; } From 092673f74071f17f34376d2e748302bf0f430f23 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 16 Sep 2020 07:36:11 -0700 Subject: [PATCH 39/45] Update sycl/test/filter_selector/select_device_acc.cpp Co-authored-by: vladimirlaz --- sycl/test/filter_selector/select_device_acc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/test/filter_selector/select_device_acc.cpp b/sycl/test/filter_selector/select_device_acc.cpp index c5b7511a0d48b..0408048ef7278 100644 --- a/sycl/test/filter_selector/select_device_acc.cpp +++ b/sycl/test/filter_selector/select_device_acc.cpp @@ -1,5 +1,5 @@ // RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out -// RU: env SYCL_DEVICE_FILTER=cpu %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 From 84a80ef9bc29fd117c0e92b553d409f3d7a7eb0a Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 16 Sep 2020 07:36:32 -0700 Subject: [PATCH 40/45] Update sycl/test/filter_selector/select_device_acc.cpp Co-authored-by: vladimirlaz --- sycl/test/filter_selector/select_device_acc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/test/filter_selector/select_device_acc.cpp b/sycl/test/filter_selector/select_device_acc.cpp index 0408048ef7278..64ab90e600e7a 100644 --- a/sycl/test/filter_selector/select_device_acc.cpp +++ b/sycl/test/filter_selector/select_device_acc.cpp @@ -25,7 +25,7 @@ int main() { default_selector ds; device d = ds.select_device(); string name = d.get_platform().get_info(); - assert(name.find("OpneCL") != string::npos); + assert(name.find("OpenCL") != string::npos); std::cout << "ACC Device is found: " << std::boolalpha << d.is_accelerator() << std::endl; } From 0a0cf635065caa715171da5ae28ecfac978553ec Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 16 Sep 2020 07:36:46 -0700 Subject: [PATCH 41/45] Update sycl/source/detail/config.hpp Co-authored-by: vladimirlaz --- sycl/source/detail/config.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index 3a3ff71d3e873..4f1b54126ed72 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -181,7 +181,6 @@ template <> class SYCLConfig { const char *ValStr = BaseT::getRawValue(); if (ValStr) { - // std::string FilterString = ValStr; static device_filter_list DFL{ValStr}; FilterList = &DFL; } From dd12cbacc68e75404003d967503c5e57dd3b9ce8 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 16 Sep 2020 08:18:25 -0700 Subject: [PATCH 42/45] Update sycl/test/filter_selector/select_device_acc.cpp Co-authored-by: vladimirlaz --- sycl/test/filter_selector/select_device_acc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/test/filter_selector/select_device_acc.cpp b/sycl/test/filter_selector/select_device_acc.cpp index 64ab90e600e7a..c7d933e4b5e26 100644 --- a/sycl/test/filter_selector/select_device_acc.cpp +++ b/sycl/test/filter_selector/select_device_acc.cpp @@ -61,7 +61,7 @@ int main() { accelerator_selector as; device d = as.select_device(); string name = d.get_platform().get_info(); - assert(name.find("OpneCL") != string::npos); + assert(name.find("OpenCL") != string::npos); std::cout << "ACC device is found: " << d.is_accelerator() << std::endl; } From f3c63871e621dd2c865c0fb8689dd4e8c4a41bbf Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 16 Sep 2020 10:46:52 -0700 Subject: [PATCH 43/45] Update sycl/source/detail/device_filter.cpp Co-authored-by: Vyacheslav Klochkov <34946562+v-klochkov@users.noreply.github.com> --- sycl/source/detail/device_filter.cpp | 72 +++++++++++----------------- 1 file changed, 27 insertions(+), 45 deletions(-) diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index e24859ee909f6..f9ec00365ce8d 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -32,56 +32,38 @@ device_filter::device_filter(const std::string &FilterString) { // handle the optional 1st field of the filter, backend size_t Cursor = 0; - size_t ColonPos = FilterString.find(":", Cursor); - // check if the first entry matches with a known backend type - auto It = std::find_if( - std::begin(SyclBeMap), std::end(SyclBeMap), - [=, &Cursor](const std::pair &Element) { - size_t Found = FilterString.find(Element.first, Cursor); - if (Found != std::string::npos) { - Cursor = Found; - return true; - } - return false; - }); - // if no match is found, set the backend type backend::all - // which actually means 'any backend' will be a match. - if (It == SyclBeMap.end()) { - Backend = backend::all; - } else { - Backend = It->second; - if (ColonPos != std::string::npos) { + 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 { + else Cursor = Cursor + It->first.size(); - } - } + return It->second; + }; + // 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. + // Handle the optional 2nd field of the filter - device type. + // Check if the 2nd entry matches with any known device type. auto Iter = std::find_if( - std::begin(SyclDeviceTypeMap), std::end(SyclDeviceTypeMap), - [=, &Cursor](const std::pair &Element) { - size_t Found = FilterString.find(Element.first, Cursor); - if (Found != std::string::npos) { - Cursor = Found; - return true; - } - return false; - }); - // if no match is found, set device_type 'all' + 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. - if (Iter == SyclDeviceTypeMap.end()) { - DeviceType = info::device_type::all; - } else { - DeviceType = Iter->second; - ColonPos = FilterString.find(":", Cursor); - if (ColonPos != std::string::npos) { - Cursor = ColonPos + 1; - } else { - Cursor = Cursor + Iter->first.size(); - } - } + 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. From 4708688567433f6cfcf61b9307789df8801ce53e Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 16 Sep 2020 10:54:39 -0700 Subject: [PATCH 44/45] clang-format Signed-off-by: Byoungro So --- sycl/source/detail/device_filter.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index f9ec00365ce8d..add0b5c4e78fa 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -51,16 +51,16 @@ device_filter::device_filter(const std::string &FilterString) { return It->second; }; // Check if the first entry matches with a known backend type - auto It = std::find_if( - std::begin(SyclBeMap), std::end(SyclBeMap), findElement); + 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. - auto Iter = std::find_if( - std::begin(SyclDeviceTypeMap), std::end(SyclDeviceTypeMap), findElement); + 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); From ba2c293007d83c7104ad75b984c2c1d537372a62 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 16 Sep 2020 17:11:15 -0700 Subject: [PATCH 45/45] fixed error caused in lambda Signed-off-by: Byoungro So --- sycl/include/CL/sycl/detail/device_filter.hpp | 2 ++ sycl/source/detail/device_filter.cpp | 26 ++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/sycl/include/CL/sycl/detail/device_filter.hpp b/sycl/include/CL/sycl/detail/device_filter.hpp index e0f9beee7e7f9..b65cf709d9dc0 100644 --- a/sycl/include/CL/sycl/detail/device_filter.hpp +++ b/sycl/include/CL/sycl/detail/device_filter.hpp @@ -60,6 +60,8 @@ inline std::ostream &operator<<(std::ostream &Out, Out << "accelerator"; } else if (Filter.DeviceType == info::device_type::all) { Out << "*"; + } else { + Out << "unknown"; } if (Filter.HasDeviceNum) { Out << ":" << Filter.DeviceNum; diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index add0b5c4e78fa..4b2c2df525268 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -24,16 +24,16 @@ device_filter::device_filter(const std::string &FilterString) { {"gpu", info::device_type::gpu}, {"acc", info::device_type::accelerator}, {"*", info::device_type::all}}}; - const std::array, 4> SyclBeMap = { - {{"opencl", backend::opencl}, + const std::array, 5> SyclBeMap = { + {{"host", backend::host}, + {"opencl", backend::opencl}, {"level_zero", backend::level_zero}, {"cuda", backend::cuda}, {"*", backend::all}}}; - // handle the optional 1st field of the filter, backend size_t Cursor = 0; size_t ColonPos = 0; - auto findElement = [&](auto &Element) { + auto findElement = [&](auto Element) { size_t Found = FilterString.find(Element.first, Cursor); if (Found == std::string::npos) return false; @@ -50,6 +50,8 @@ device_filter::device_filter(const std::string &FilterString) { 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); @@ -59,13 +61,17 @@ device_filter::device_filter(const std::string &FilterString) { // Handle the optional 2nd field of the filter - device type. // Check if the 2nd entry matches with any known device type. - 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); + 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 + // 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()) {