From e8983905af66262ef477070944913daf34ce6f42 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 23 Apr 2021 15:26:49 -0700 Subject: [PATCH 01/47] [SYCL] Add device_type into sycl-ls prefix By adding the device_type into each device prefix listing in sycl-ls, the user can easily set SYCL_DEVICE_FILTER correctly. Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 2 +- sycl/tools/sycl-ls/sycl-ls.cpp | 31 ++++++++++++++----------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 258d137080bda..8e5adf7bc840f 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -65,7 +65,7 @@ Possible values of "device_type" are: - acc - \* -Device_num is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each backend. For example, SYCL_DEVICE_FILTER=2 will return all devices with index '2' from all different backends. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. +Device_num is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each platform. For example, SYCL_DEVICE_FILTER=2 will return all devices with index '2' from all different platforms. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. 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 does not include the "host" backend and the host device automatically unless one of the filters explicitly specifies the "host" device type. Therefore, SYCL_DEVICE_FILTER=host should be set to enforce SYCL to use the host device only. diff --git a/sycl/tools/sycl-ls/sycl-ls.cpp b/sycl/tools/sycl-ls/sycl-ls.cpp index a50371ed8c934..95bb54762569d 100644 --- a/sycl/tools/sycl-ls/sycl-ls.cpp +++ b/sycl/tools/sycl-ls/sycl-ls.cpp @@ -38,27 +38,24 @@ class custom_selector : public device_selector { } }; -static void printDeviceInfo(const device &Device, const std::string &Prepend) { +std::string getDeviceTypeName(const device &Device) { auto DeviceType = Device.get_info(); - std::string DeviceTypeName; switch (DeviceType) { case info::device_type::cpu: - DeviceTypeName = "CPU "; - break; + return "cpu"; case info::device_type::gpu: - DeviceTypeName = "GPU "; - break; + return "gpu"; case info::device_type::host: - DeviceTypeName = "HOST"; - break; + return "host"; case info::device_type::accelerator: - DeviceTypeName = "ACC "; - break; + return "acc"; default: - DeviceTypeName = "UNKNOWN"; - break; + return "unknown"; } +} +static void printDeviceInfo(const device &Device, const std::string &Prepend) { + std::string DeviceTypeName = getDeviceTypeName(Device); auto DeviceVersion = Device.get_info(); auto DeviceName = Device.get_info(); auto DeviceVendor = Device.get_info(); @@ -73,9 +70,8 @@ static void printDeviceInfo(const device &Device, const std::string &Prepend) { } else { auto DevicePlatform = Device.get_info(); auto DevicePlatformName = DevicePlatform.get_info(); - std::cout << Prepend << DeviceTypeName << ": " << DevicePlatformName << " " - << DeviceVersion << " [" << DeviceDriverVersion << "]" - << std::endl; + std::cout << Prepend << " : " << DevicePlatformName << " " << DeviceVersion + << " [" << DeviceDriverVersion << "]" << std::endl; } } @@ -83,7 +79,7 @@ static void printSelectorChoice(const device_selector &Selector, const std::string &Prepend) { try { const auto &Dev = device(Selector); - printDeviceInfo(Dev, Prepend); + printDeviceInfo(Dev, Prepend + getDeviceTypeName(Dev)); } catch (const cl::sycl::runtime_error &Exception) { // Truncate long string so it can fit in one-line @@ -132,7 +128,8 @@ int main(int argc, char **argv) { std::cout << " Device [#" << DeviceNum << "]:" << std::endl; else { backend Backend = Platform.get_backend(); - std::cout << "[" << Backend << ":" << DeviceNum << "] "; + std::cout << "[" << Backend << ":" << getDeviceTypeName(Device) << ":" + << DeviceNum << "]"; } ++DeviceNum; printDeviceInfo(Device, verbose ? " " : ""); From 0bebee462c1bed269340bad78a14bd9ced25c8cc Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 27 Apr 2021 10:26:29 -0700 Subject: [PATCH 02/47] changed device-id unique per backend Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 2 +- sycl/tools/sycl-ls/sycl-ls.cpp | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 8e5adf7bc840f..258d137080bda 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -65,7 +65,7 @@ Possible values of "device_type" are: - acc - \* -Device_num is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each platform. For example, SYCL_DEVICE_FILTER=2 will return all devices with index '2' from all different platforms. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. +Device_num is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each backend. For example, SYCL_DEVICE_FILTER=2 will return all devices with index '2' from all different backends. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. 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 does not include the "host" backend and the host device automatically unless one of the filters explicitly specifies the "host" device type. Therefore, SYCL_DEVICE_FILTER=host should be set to enforce SYCL to use the host device only. diff --git a/sycl/tools/sycl-ls/sycl-ls.cpp b/sycl/tools/sycl-ls/sycl-ls.cpp index 95bb54762569d..7e60b5135eb8b 100644 --- a/sycl/tools/sycl-ls/sycl-ls.cpp +++ b/sycl/tools/sycl-ls/sycl-ls.cpp @@ -24,6 +24,8 @@ using namespace cl::sycl; +#define NumOfBackends 5 + // Controls verbose output vs. concise. bool verbose; @@ -107,9 +109,14 @@ int main(int argc, char **argv) { std::cout << "Platforms: " << Platforms.size() << std::endl; uint32_t PlatformNum = 0; + std::vector DeviceNums; + // For each backend, device num starts at zero. + for (int I = 0; I < NumOfBackends; I++) { + DeviceNums.push_back(0); + } for (const auto &Platform : Platforms) { - uint32_t DeviceNum = 0; + backend Backend = Platform.get_backend(); ++PlatformNum; if (verbose) { auto PlatformVersion = Platform.get_info(); @@ -124,10 +131,10 @@ int main(int argc, char **argv) { if (verbose) std::cout << " Devices : " << Devices.size() << std::endl; for (const auto &Device : Devices) { + uint32_t DeviceNum = DeviceNums[(int)Backend]++; if (verbose) std::cout << " Device [#" << DeviceNum << "]:" << std::endl; else { - backend Backend = Platform.get_backend(); std::cout << "[" << Backend << ":" << getDeviceTypeName(Device) << ":" << DeviceNum << "]"; } From 67adc59fea5ce3caeb2be94cc90a4efae700eb1b Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 27 Apr 2021 14:17:31 -0700 Subject: [PATCH 03/47] filter device with backend-unique id Signed-off-by: Byoungro So --- sycl/source/detail/platform_impl.cpp | 20 +++++++++++++++++--- sycl/tools/sycl-ls/sycl-ls.cpp | 12 +++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 88fdece5def65..79600895cf577 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -20,6 +20,8 @@ #include #include +#define NumOfBackends (int)backend::all + __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { namespace detail { @@ -116,8 +118,9 @@ vector_class platform_impl::get_platforms() { platform Platform = detail::createSyclObjFromImpl( getOrMakePlatformImpl(PiPlatform, Plugins[i])); // Skip platforms which do not contain requested device types - if (!Platform.get_devices(ForcedType).empty() && - !IsBannedPlatform(Platform)) + if (!IsBannedPlatform(Platform) && + (ForcedType == info::device_type::all || + !Platform.get_devices(ForcedType).empty())) Platforms.push_back(Platform); } } @@ -303,9 +306,20 @@ static void filterDeviceFilter(vector_class &PiDevices, if (!FilterList) return; + // remember the last backend that has gone through tis filter function + // to assign a unique device id number across platforms that belong to + // the same backend. For example, opencl:cpu:0, opencl:acc:1, opencl:gpu:2 + static backend lastBackend = backend::all; backend Backend = Plugin.getBackend(); int InsertIDx = 0; - int DeviceNum = 0; + // DeviceNums should be given consecutive numbers across platforms. + // So, we keep the device num for the successive calls to this function. + static int DeviceNum = 0; + if (lastBackend != Backend) { + DeviceNum = 0; + lastBackend = Backend; + } + for (RT::PiDevice Device : PiDevices) { RT::PiDeviceType PiDevType; Plugin.call(Device, PI_DEVICE_INFO_TYPE, diff --git a/sycl/tools/sycl-ls/sycl-ls.cpp b/sycl/tools/sycl-ls/sycl-ls.cpp index 7e60b5135eb8b..7d7bfe499ec10 100644 --- a/sycl/tools/sycl-ls/sycl-ls.cpp +++ b/sycl/tools/sycl-ls/sycl-ls.cpp @@ -24,7 +24,7 @@ using namespace cl::sycl; -#define NumOfBackends 5 +#define NumOfBackends (int)backend::all // Controls verbose output vs. concise. bool verbose; @@ -104,6 +104,16 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } + const char *filter = std::getenv("SYCL_DEVICE_FILTER"); + if (filter) { + std::cout << "Warning: SYCL_DEVICE_FILTER environment variable is set to " + << filter << "." << std::endl; + std::cout + << "To see the correct device id, please unset SYCL_DEVICE_FILTER." + << std::endl + << std::endl; + } + const auto &Platforms = platform::get_platforms(); if (verbose) std::cout << "Platforms: " << Platforms.size() << std::endl; From 018e5ad72468a11b9cd00b27e799db99c6891a39 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 27 Apr 2021 14:49:00 -0700 Subject: [PATCH 04/47] changed to static_cast Signed-off-by: Byoungro So --- sycl/source/detail/platform_impl.cpp | 2 -- sycl/tools/sycl-ls/sycl-ls.cpp | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 79600895cf577..eb7612d251007 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -20,8 +20,6 @@ #include #include -#define NumOfBackends (int)backend::all - __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { namespace detail { diff --git a/sycl/tools/sycl-ls/sycl-ls.cpp b/sycl/tools/sycl-ls/sycl-ls.cpp index 7d7bfe499ec10..020d52dabb36c 100644 --- a/sycl/tools/sycl-ls/sycl-ls.cpp +++ b/sycl/tools/sycl-ls/sycl-ls.cpp @@ -24,7 +24,7 @@ using namespace cl::sycl; -#define NumOfBackends (int)backend::all +#define NumOfBackends static_cast(backend::all) // Controls verbose output vs. concise. bool verbose; From 2cd0eae1fb02a9d0c0e5ea1ad1c37216862c26da Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 27 Apr 2021 17:46:08 -0700 Subject: [PATCH 05/47] uppercase device type Signed-off-by: Byoungro So --- sycl/tools/sycl-ls/sycl-ls.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sycl/tools/sycl-ls/sycl-ls.cpp b/sycl/tools/sycl-ls/sycl-ls.cpp index 020d52dabb36c..de664b4629578 100644 --- a/sycl/tools/sycl-ls/sycl-ls.cpp +++ b/sycl/tools/sycl-ls/sycl-ls.cpp @@ -57,14 +57,14 @@ std::string getDeviceTypeName(const device &Device) { } static void printDeviceInfo(const device &Device, const std::string &Prepend) { - std::string DeviceTypeName = getDeviceTypeName(Device); auto DeviceVersion = Device.get_info(); auto DeviceName = Device.get_info(); auto DeviceVendor = Device.get_info(); auto DeviceDriverVersion = Device.get_info(); if (verbose) { - std::cout << Prepend << "Type : " << DeviceTypeName << std::endl; + std::cout << Prepend << "Type : " << getDeviceTypeName(Device) + << std::endl; std::cout << Prepend << "Version : " << DeviceVersion << std::endl; std::cout << Prepend << "Name : " << DeviceName << std::endl; std::cout << Prepend << "Vendor : " << DeviceVendor << std::endl; @@ -81,8 +81,10 @@ static void printSelectorChoice(const device_selector &Selector, const std::string &Prepend) { try { const auto &Dev = device(Selector); - printDeviceInfo(Dev, Prepend + getDeviceTypeName(Dev)); - + std::string DeviceTypeName = getDeviceTypeName(Dev); + std::transform(DeviceTypeName.begin(), DeviceTypeName.end(), + DeviceTypeName.begin(), ::toupper); + printDeviceInfo(Dev, Prepend + DeviceTypeName); } catch (const cl::sycl::runtime_error &Exception) { // Truncate long string so it can fit in one-line std::string What = Exception.what(); From 4f6d9c4a33c218395431199c4b0f81a0853eb363 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 27 Apr 2021 23:41:27 -0700 Subject: [PATCH 06/47] revert uppercase Signed-off-by: Byoungro So --- sycl/tools/sycl-ls/sycl-ls.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/sycl/tools/sycl-ls/sycl-ls.cpp b/sycl/tools/sycl-ls/sycl-ls.cpp index de664b4629578..995c2b07950e0 100644 --- a/sycl/tools/sycl-ls/sycl-ls.cpp +++ b/sycl/tools/sycl-ls/sycl-ls.cpp @@ -82,8 +82,6 @@ static void printSelectorChoice(const device_selector &Selector, try { const auto &Dev = device(Selector); std::string DeviceTypeName = getDeviceTypeName(Dev); - std::transform(DeviceTypeName.begin(), DeviceTypeName.end(), - DeviceTypeName.begin(), ::toupper); printDeviceInfo(Dev, Prepend + DeviceTypeName); } catch (const cl::sycl::runtime_error &Exception) { // Truncate long string so it can fit in one-line From dc6d7e0f86a529fdc2fded9cf9e967ab97be44ae Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 28 Apr 2021 09:31:30 -0700 Subject: [PATCH 07/47] Update sycl/source/detail/platform_impl.cpp Co-authored-by: vladimirlaz --- sycl/source/detail/platform_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index eb7612d251007..740b3883cb656 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -304,7 +304,7 @@ static void filterDeviceFilter(vector_class &PiDevices, if (!FilterList) return; - // remember the last backend that has gone through tis filter function + // remember the last backend that has gone through this filter function // to assign a unique device id number across platforms that belong to // the same backend. For example, opencl:cpu:0, opencl:acc:1, opencl:gpu:2 static backend lastBackend = backend::all; From 006bc0e304b0aa9ee1ca84c2b3684a993a9bfc6f Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 28 Apr 2021 10:32:40 -0700 Subject: [PATCH 08/47] Update sycl/tools/sycl-ls/sycl-ls.cpp Co-authored-by: smaslov-intel <48694368+smaslov-intel@users.noreply.github.com> --- sycl/tools/sycl-ls/sycl-ls.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sycl/tools/sycl-ls/sycl-ls.cpp b/sycl/tools/sycl-ls/sycl-ls.cpp index 995c2b07950e0..052ec8d71d030 100644 --- a/sycl/tools/sycl-ls/sycl-ls.cpp +++ b/sycl/tools/sycl-ls/sycl-ls.cpp @@ -119,11 +119,8 @@ int main(int argc, char **argv) { std::cout << "Platforms: " << Platforms.size() << std::endl; uint32_t PlatformNum = 0; - std::vector DeviceNums; // For each backend, device num starts at zero. - for (int I = 0; I < NumOfBackends; I++) { - DeviceNums.push_back(0); - } + std::vector DeviceNums(backend::all, 0); for (const auto &Platform : Platforms) { backend Backend = Platform.get_backend(); From a8756f11b1731a7bbf4d1ee90b5995c60fd431c1 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 28 Apr 2021 10:45:30 -0700 Subject: [PATCH 09/47] feedback accommodated Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 2 +- sycl/tools/sycl-ls/sycl-ls.cpp | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 258d137080bda..8ba78b2bbdcfd 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -65,7 +65,7 @@ Possible values of "device_type" are: - acc - \* -Device_num is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each backend. For example, SYCL_DEVICE_FILTER=2 will return all devices with index '2' from all different backends. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. +Device_num is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each backend. For example, SYCL_DEVICE_FILTER=2 will return all devices with index '2' from all different backends. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. Please note that 'device_num' is affected if SYCL_DEVICE_ALLOWLIST is set because it also limits the allowed devices. 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 does not include the "host" backend and the host device automatically unless one of the filters explicitly specifies the "host" device type. Therefore, SYCL_DEVICE_FILTER=host should be set to enforce SYCL to use the host device only. diff --git a/sycl/tools/sycl-ls/sycl-ls.cpp b/sycl/tools/sycl-ls/sycl-ls.cpp index 052ec8d71d030..a980f7ed688dd 100644 --- a/sycl/tools/sycl-ls/sycl-ls.cpp +++ b/sycl/tools/sycl-ls/sycl-ls.cpp @@ -24,8 +24,6 @@ using namespace cl::sycl; -#define NumOfBackends static_cast(backend::all) - // Controls verbose output vs. concise. bool verbose; @@ -120,7 +118,7 @@ int main(int argc, char **argv) { uint32_t PlatformNum = 0; // For each backend, device num starts at zero. - std::vector DeviceNums(backend::all, 0); + std::vector DeviceNums(static_cast(backend::all), 0); for (const auto &Platform : Platforms) { backend Backend = Platform.get_backend(); From 806d7f57a1cbf7fd113f8b9e0e8b7822b2359d9f Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 13 May 2021 16:05:36 -0700 Subject: [PATCH 10/47] keep last device id in plugin class Signed-off-by: Byoungro So --- sycl/source/detail/pi.cpp | 10 +++++--- sycl/source/detail/platform_impl.cpp | 35 ++++++++++++---------------- sycl/source/detail/plugin.hpp | 7 ++++-- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index f5a393d2c0d82..acc49877a8d85 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -267,8 +267,9 @@ int unloadPlugin(void *Library) { return unloadOsLibrary(Library); } // Currently, we bind to a singe plugin. bool bindPlugin(void *Library, PiPlugin *PluginInformation) { - decltype(::piPluginInit) *PluginInitializeFunction = (decltype( - &::piPluginInit))(getOsLibraryFuncAddress(Library, "piPluginInit")); + decltype(::piPluginInit) *PluginInitializeFunction = + (decltype(&::piPluginInit))(getOsLibraryFuncAddress(Library, + "piPluginInit")); if (PluginInitializeFunction == nullptr) return false; @@ -295,7 +296,10 @@ const vector_class &initialize() { std::call_once(PluginsInitDone, []() { initializePlugins(&GlobalHandler::instance().getPlugins()); }); - + vector_class Plugins = GlobalHandler::instance().getPlugins(); + for (plugin Plugin : Plugins) { + Plugin.setLastDeviceId(0); + } return GlobalHandler::instance().getPlugins(); } diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 740b3883cb656..f74d410edd154 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -116,9 +116,8 @@ vector_class platform_impl::get_platforms() { platform Platform = detail::createSyclObjFromImpl( getOrMakePlatformImpl(PiPlatform, Plugins[i])); // Skip platforms which do not contain requested device types - if (!IsBannedPlatform(Platform) && - (ForcedType == info::device_type::all || - !Platform.get_devices(ForcedType).empty())) + if (!Platform.get_devices(ForcedType).empty() && + !IsBannedPlatform(Platform)) Platforms.push_back(Platform); } } @@ -299,30 +298,22 @@ static void filterAllowList(vector_class &PiDevices, // This function matches devices in the order of backend, device_type, and // device_num. static void filterDeviceFilter(vector_class &PiDevices, - const plugin &Plugin) { + std::shared_ptr Plugin) { device_filter_list *FilterList = SYCLConfig::get(); if (!FilterList) return; - // remember the last backend that has gone through this filter function - // to assign a unique device id number across platforms that belong to - // the same backend. For example, opencl:cpu:0, opencl:acc:1, opencl:gpu:2 - static backend lastBackend = backend::all; - backend Backend = Plugin.getBackend(); + backend Backend = Plugin->getBackend(); int InsertIDx = 0; - // DeviceNums should be given consecutive numbers across platforms. - // So, we keep the device num for the successive calls to this function. - static int DeviceNum = 0; - if (lastBackend != Backend) { - DeviceNum = 0; - lastBackend = Backend; - } + // DeviceIds should be given consecutive numbers across platforms in the same + // backend + int DeviceNum = Plugin->getLastDeviceId(); for (RT::PiDevice Device : PiDevices) { RT::PiDeviceType PiDevType; - Plugin.call(Device, PI_DEVICE_INFO_TYPE, - sizeof(RT::PiDeviceType), - &PiDevType, nullptr); + Plugin->call(Device, PI_DEVICE_INFO_TYPE, + sizeof(RT::PiDeviceType), + &PiDevType, nullptr); // Assumption here is that there is 1-to-1 mapping between PiDevType and // Sycl device type for GPU, CPU, and ACC. info::device_type DeviceType = pi::cast(PiDevType); @@ -350,6 +341,10 @@ static void filterDeviceFilter(vector_class &PiDevices, DeviceNum++; } PiDevices.resize(InsertIDx); + // remember the last backend that has gone through this filter function + // to assign a unique device id number across platforms that belong to + // the same backend. For example, opencl:cpu:0, opencl:acc:1, opencl:gpu:2 + Plugin->setLastDeviceId(DeviceNum); } std::shared_ptr platform_impl::getOrMakeDeviceImpl( @@ -409,7 +404,7 @@ platform_impl::get_devices(info::device_type DeviceType) const { filterAllowList(PiDevices, MPlatform, this->getPlugin()); // Filter out devices that are not compatible with SYCL_DEVICE_FILTER - filterDeviceFilter(PiDevices, Plugin); + filterDeviceFilter(PiDevices, MPlugin); PlatformImplPtr PlatformImpl = getOrMakePlatformImpl(MPlatform, *MPlugin); std::transform( diff --git a/sycl/source/detail/plugin.hpp b/sycl/source/detail/plugin.hpp index 3480ac81f643e..917982e04769e 100644 --- a/sycl/source/detail/plugin.hpp +++ b/sycl/source/detail/plugin.hpp @@ -36,7 +36,7 @@ class plugin { plugin(RT::PiPlugin Plugin, backend UseBackend, void *LibraryHandle) : MPlugin(Plugin), MBackend(UseBackend), MLibraryHandle(LibraryHandle), - TracingMutex(std::make_shared()) {} + TracingMutex(std::make_shared()), LastDeviceId(0) {} plugin &operator=(const plugin &) = default; plugin(const plugin &) = default; @@ -110,13 +110,16 @@ class plugin { void *getLibraryHandle() const { return MLibraryHandle; } void *getLibraryHandle() { return MLibraryHandle; } int unload() { return RT::unloadPlugin(MLibraryHandle); } + int getLastDeviceId() { return LastDeviceId; } + void setLastDeviceId(int id) { LastDeviceId = id; } private: RT::PiPlugin MPlugin; backend MBackend; void *MLibraryHandle; // the handle returned from dlopen std::shared_ptr TracingMutex; -}; // class plugin + int LastDeviceId; // represents the unique id of the last device +}; // class plugin } // namespace detail } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) From 0a6229789a0b8a30f84f13f6021596cde14f1a74 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 13 May 2021 19:42:58 -0700 Subject: [PATCH 11/47] fix clang-format Signed-off-by: Byoungro So --- sycl/source/detail/pi.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index acc49877a8d85..55a9a99b51a81 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -267,9 +267,8 @@ int unloadPlugin(void *Library) { return unloadOsLibrary(Library); } // Currently, we bind to a singe plugin. bool bindPlugin(void *Library, PiPlugin *PluginInformation) { - decltype(::piPluginInit) *PluginInitializeFunction = - (decltype(&::piPluginInit))(getOsLibraryFuncAddress(Library, - "piPluginInit")); + decltype(::piPluginInit) *PluginInitializeFunction = (decltype( + &::piPluginInit))(getOsLibraryFuncAddress(Library, "piPluginInit")); if (PluginInitializeFunction == nullptr) return false; From 24cd0674646ef0f0d60c0e1b6110ce6d2c4c2cec Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 14 May 2021 10:22:11 -0700 Subject: [PATCH 12/47] Update sycl/doc/EnvironmentVariables.md Co-authored-by: vladimirlaz --- 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 8ba78b2bbdcfd..2603d49df3a15 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -65,7 +65,7 @@ Possible values of "device_type" are: - acc - \* -Device_num is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each backend. For example, SYCL_DEVICE_FILTER=2 will return all devices with index '2' from all different backends. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. Please note that 'device_num' is affected if SYCL_DEVICE_ALLOWLIST is set because it also limits the allowed devices. +Device_num is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each backend. For example, SYCL_DEVICE_FILTER=2 will return all devices with index '2' from all different backends. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. When SYCL_DEVICE_ALOWLIST is set it is applied before enumerating devices and affects `device_num` values.``` 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 does not include the "host" backend and the host device automatically unless one of the filters explicitly specifies the "host" device type. Therefore, SYCL_DEVICE_FILTER=host should be set to enforce SYCL to use the host device only. From 4d836dd3b27e6f1b55387ca9d98f7de42f3a75fe Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 14 May 2021 21:43:06 -0700 Subject: [PATCH 13/47] keep LastDeviceId per platform Signed-off-by: Byoungro So --- sycl/doc/EnvironmentVariables.md | 32 ++++++++++---------- sycl/source/detail/pi.cpp | 8 +++-- sycl/source/detail/platform_impl.cpp | 7 +++-- sycl/source/detail/plugin.hpp | 44 ++++++++++++++++++++++++---- 4 files changed, 64 insertions(+), 27 deletions(-) diff --git a/sycl/doc/EnvironmentVariables.md b/sycl/doc/EnvironmentVariables.md index 2603d49df3a15..ce53e7fc17ad6 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -49,27 +49,27 @@ subject to change. Do not rely on these variables in production code. ### SYCL_DEVICE_FILTER -This environment variable 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 (platform::get_devices() and platform::get_platforms()) and all of the device selectors. +This environment variable 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 (`platform::get_devices()` and `platform::get_platforms()`) 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 -- \* +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` +- `*` -Possible values of "device_type" are: -- host -- cpu -- gpu -- acc -- \* +Possible values of `device_type` are: +- `host` +- `cpu` +- `gpu` +- `acc` +- `*` -Device_num is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each backend. For example, SYCL_DEVICE_FILTER=2 will return all devices with index '2' from all different backends. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. When SYCL_DEVICE_ALOWLIST is set it is applied before enumerating devices and affects `device_num` values.``` +`device_num` is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each backend. For example, `SYCL_DEVICE_FILTER=2` will return all devices with index '2' from all different backends. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. When `SYCL_DEVICE_ALOWLIST` is set, it is applied before enumerating devices and affects `device_num` values. -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 does not include the "host" backend and the host device automatically unless one of the filters explicitly specifies the "host" device type. Therefore, SYCL_DEVICE_FILTER=host should be set to enforce SYCL to use the host device only. +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 does not include the `host` backend and the `host` device automatically unless one of the filters explicitly specifies the `host` device type. Therefore, `SYCL_DEVICE_FILTER=host` should be set to enforce SYCL to use the `host` device only. -Note that all device selectors will throw an exception if the filtered list of devices does not include a device that satisfies the selector. For instance, SYCL_DEVICE_FILTER=cpu,level_zero will cause host_selector() to throw an exception. SYCL_DEVICE_FILTER also limits loading only specified plugins into the SYCL RT. In particular, SYCL_DEVICE_FILTER=level_zero will cause the cpu_selector to throw an exception since SYCL RT will only load the level_zero backend which does not support any CPU devices at this time. When multiple devices satisfy the filter (e..g, SYCL_DEVICE_FILTER=gpu), only one of them will be selected. +Note that all device selectors will throw an exception if the filtered list of devices does not include a device that satisfies the selector. For instance, `SYCL_DEVICE_FILTER=cpu,level_zero` will cause `host_selector()` to throw an exception. `SYCL_DEVICE_FILTER` also limits loading only specified plugins into the SYCL RT. In particular, `SYCL_DEVICE_FILTER=level_zero` will cause the `cpu_selector` to throw an exception since SYCL RT will only load the `level_zero` backend which does not support any CPU devices at this time. When multiple devices satisfy the filter (e..g, `SYCL_DEVICE_FILTER=gpu`), only one of them will be selected. ### SYCL_PRINT_EXECUTION_GRAPH Options diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 55a9a99b51a81..4874dc5560b14 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -267,8 +267,9 @@ int unloadPlugin(void *Library) { return unloadOsLibrary(Library); } // Currently, we bind to a singe plugin. bool bindPlugin(void *Library, PiPlugin *PluginInformation) { - decltype(::piPluginInit) *PluginInitializeFunction = (decltype( - &::piPluginInit))(getOsLibraryFuncAddress(Library, "piPluginInit")); + decltype(::piPluginInit) *PluginInitializeFunction = + (decltype(&::piPluginInit))(getOsLibraryFuncAddress(Library, + "piPluginInit")); if (PluginInitializeFunction == nullptr) return false; @@ -295,9 +296,10 @@ const vector_class &initialize() { std::call_once(PluginsInitDone, []() { initializePlugins(&GlobalHandler::instance().getPlugins()); }); + // reset LastDeviceIds to zeros vector_class Plugins = GlobalHandler::instance().getPlugins(); for (plugin Plugin : Plugins) { - Plugin.setLastDeviceId(0); + Plugin.resetLastDeviceIds(); } return GlobalHandler::instance().getPlugins(); } diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index a3ade0f363822..9ecfc2731b072 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -298,6 +298,7 @@ static void filterAllowList(vector_class &PiDevices, // This function matches devices in the order of backend, device_type, and // device_num. static void filterDeviceFilter(vector_class &PiDevices, + RT::PiPlatform Platform, std::shared_ptr Plugin) { device_filter_list *FilterList = SYCLConfig::get(); if (!FilterList) @@ -307,7 +308,7 @@ static void filterDeviceFilter(vector_class &PiDevices, int InsertIDx = 0; // DeviceIds should be given consecutive numbers across platforms in the same // backend - int DeviceNum = Plugin->getLastDeviceId(); + int DeviceNum = Plugin->getStartingDeviceId(Platform); for (RT::PiDevice Device : PiDevices) { RT::PiDeviceType PiDevType; @@ -344,7 +345,7 @@ static void filterDeviceFilter(vector_class &PiDevices, // remember the last backend that has gone through this filter function // to assign a unique device id number across platforms that belong to // the same backend. For example, opencl:cpu:0, opencl:acc:1, opencl:gpu:2 - Plugin->setLastDeviceId(DeviceNum); + Plugin->setLastDeviceId(Platform, DeviceNum); } std::shared_ptr platform_impl::getOrMakeDeviceImpl( @@ -404,7 +405,7 @@ platform_impl::get_devices(info::device_type DeviceType) const { filterAllowList(PiDevices, MPlatform, this->getPlugin()); // Filter out devices that are not compatible with SYCL_DEVICE_FILTER - filterDeviceFilter(PiDevices, MPlugin); + filterDeviceFilter(PiDevices, MPlatform, MPlugin); PlatformImplPtr PlatformImpl = getOrMakePlatformImpl(MPlatform, *MPlugin); std::transform( diff --git a/sycl/source/detail/plugin.hpp b/sycl/source/detail/plugin.hpp index 917982e04769e..cea36e0048262 100644 --- a/sycl/source/detail/plugin.hpp +++ b/sycl/source/detail/plugin.hpp @@ -36,7 +36,7 @@ class plugin { plugin(RT::PiPlugin Plugin, backend UseBackend, void *LibraryHandle) : MPlugin(Plugin), MBackend(UseBackend), MLibraryHandle(LibraryHandle), - TracingMutex(std::make_shared()), LastDeviceId(0) {} + TracingMutex(std::make_shared()) {} plugin &operator=(const plugin &) = default; plugin(const plugin &) = default; @@ -110,16 +110,50 @@ class plugin { void *getLibraryHandle() const { return MLibraryHandle; } void *getLibraryHandle() { return MLibraryHandle; } int unload() { return RT::unloadPlugin(MLibraryHandle); } - int getLastDeviceId() { return LastDeviceId; } - void setLastDeviceId(int id) { LastDeviceId = id; } + // return the index os PiPlatforms. + // If not found, add it and return its index. + int getPlatformId(RT::PiPlatform Platform) { + auto It = std::find(PiPlatforms.begin(), PiPlatforms.end(), Platform); + if (It != PiPlatforms.end()) { + return It - PiPlatforms.begin(); + } else { + PiPlatforms.push_back(Platform); + LastDeviceIds.push_back(0); + return PiPlatforms.size() - 1; + } + } + // Device ids are consecutive across platforms within a plugin. + // We need to return the same starting index for the given platform. + // So, instead of returing the last device id of the given platform, + // return the last device id of the predecessor platform. + int getStartingDeviceId(RT::PiPlatform Platform) { + int PlatformId = getPlatformId(Platform); + if (PlatformId == 0) + return 0; + else + return LastDeviceIds[PlatformId - 1]; + } + // set the id of the last device for the given platform + void setLastDeviceId(RT::PiPlatform Platform, int Id) { + int PlatformId = getPlatformId(Platform); + LastDeviceIds[PlatformId] = Id; + } + // reset all last device ids to zeros + void resetLastDeviceIds() { + std::fill(LastDeviceIds.begin(), LastDeviceIds.end(), 0); + } private: RT::PiPlugin MPlugin; backend MBackend; void *MLibraryHandle; // the handle returned from dlopen std::shared_ptr TracingMutex; - int LastDeviceId; // represents the unique id of the last device -}; // class plugin + // vector of PiPlatforms that belong to this plugin + std::vector PiPlatforms; + // represents the unique ids of the last device of each platform + // index of this vector corresponds to the index in PiPlatforms vector. + std::vector LastDeviceIds; +}; // class plugin } // namespace detail } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) From 5a7b3c5f4b4fb09dd40963dfb29fda6443ba813a Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 14 May 2021 23:04:44 -0700 Subject: [PATCH 14/47] clang-format Signed-off-by: Byoungro So --- sycl/source/detail/pi.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 4874dc5560b14..6ee000acd48fb 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -267,9 +267,8 @@ int unloadPlugin(void *Library) { return unloadOsLibrary(Library); } // Currently, we bind to a singe plugin. bool bindPlugin(void *Library, PiPlugin *PluginInformation) { - decltype(::piPluginInit) *PluginInitializeFunction = - (decltype(&::piPluginInit))(getOsLibraryFuncAddress(Library, - "piPluginInit")); + decltype(::piPluginInit) *PluginInitializeFunction = (decltype( + &::piPluginInit))(getOsLibraryFuncAddress(Library, "piPluginInit")); if (PluginInitializeFunction == nullptr) return false; From e0b6c40ff4707c63fad2f89d7994fdc0dd5d1160 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 18 May 2021 12:03:03 -0700 Subject: [PATCH 15/47] Update sycl/doc/EnvironmentVariables.md Co-authored-by: Pavel Chupin --- 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 ce53e7fc17ad6..33936d1b3a983 100644 --- a/sycl/doc/EnvironmentVariables.md +++ b/sycl/doc/EnvironmentVariables.md @@ -65,7 +65,7 @@ Possible values of `device_type` are: - `acc` - `*` -`device_num` is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each backend. For example, `SYCL_DEVICE_FILTER=2` will return all devices with index '2' from all different backends. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. When `SYCL_DEVICE_ALOWLIST` is set, it is applied before enumerating devices and affects `device_num` values. +`device_num` is an integer that indexes the enumeration of devices from the sycl-ls utility tool, where the first device in that enumeration has index zero in each backend. For example, `SYCL_DEVICE_FILTER=2` will return all devices with index '2' from all different backends. If multiple devices satisfy this device number (e.g., GPU and CPU devices can be assigned device number '2'), then default_selector will choose the device with the highest heuristic point. When `SYCL_DEVICE_ALLOWLIST` is set, it is applied before enumerating devices and affects `device_num` values. 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 does not include the `host` backend and the `host` device automatically unless one of the filters explicitly specifies the `host` device type. Therefore, `SYCL_DEVICE_FILTER=host` should be set to enforce SYCL to use the `host` device only. From cfdefd0d4fdf4d57cb3815f4bc1a509dbbbb5c4f Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 21 Jun 2021 15:37:49 -0700 Subject: [PATCH 16/47] refactored device cache Signed-off-by: Byoungro So --- sycl/source/detail/device_filter.cpp | 13 + sycl/source/detail/device_impl.cpp | 1 + sycl/source/detail/pi.cpp | 322 +++++++++++++++++- sycl/source/detail/platform_impl.cpp | 179 ++-------- sycl/source/detail/platform_impl.hpp | 5 +- sycl/source/detail/plugin.hpp | 37 -- sycl/source/device.cpp | 7 +- .../get_native_interop/test_get_native.cpp | 4 +- 8 files changed, 364 insertions(+), 204 deletions(-) diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index 8558d0d9e4f94..f1400ab96f4de 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -18,6 +18,19 @@ namespace sycl { namespace detail { device_filter::device_filter(const std::string &FilterString) { + const std::array, 5> + SyclDeviceTypeMap = {{{"host", info::device_type::host}, + {"cpu", info::device_type::cpu}, + {"gpu", info::device_type::gpu}, + {"acc", info::device_type::accelerator}, + {"*", info::device_type::all}}}; + const std::array, 5> SyclBeMap = { + {{"opencl", backend::opencl}, + {"level_zero", backend::level_zero}, + {"cuda", backend::cuda}, + {"host", backend::host}, + {"*", backend::all}}}; + size_t Cursor = 0; size_t ColonPos = 0; auto findElement = [&](auto Element) { diff --git a/sycl/source/detail/device_impl.cpp b/sycl/source/detail/device_impl.cpp index 85305d397987d..6e48e38d6f0e3 100644 --- a/sycl/source/detail/device_impl.cpp +++ b/sycl/source/detail/device_impl.cpp @@ -44,6 +44,7 @@ device_impl::device_impl(pi_native_handle InteropDeviceHandle, // With SYCL-2020 BE generalization "make" functions are used instead. Plugin.call( InteropDeviceHandle, nullptr, &MDevice); + std::cout << "interop device constructor " << std::endl; InteroperabilityConstructor = true; } diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index e73d971661824..a6d981cba9f07 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -26,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -303,6 +305,306 @@ bool trace(TraceLevel Level) { return (TraceLevelMask & Level) == Level; } +static bool IsBannedPlatform(platform Platform) { + // The NVIDIA OpenCL platform is currently not compatible with DPC++ + // since it is only 1.2 but gets selected by default in many systems + // There is also no support on the PTX backend for OpenCL consumption, + // and there have been some internal reports. + // To avoid problems on default users and deployment of DPC++ on platforms + // where CUDA is available, the OpenCL support is disabled. + // + auto IsNVIDIAOpenCL = [](platform Platform) { + if (Platform.is_host()) + return false; + + const bool HasCUDA = Platform.get_info().find( + "NVIDIA CUDA") != std::string::npos; + const auto Backend = + detail::getSyclObjImpl(Platform)->getPlugin().getBackend(); + const bool IsCUDAOCL = (HasCUDA && Backend == backend::opencl); + if (detail::pi::trace(detail::pi::TraceLevel::PI_TRACE_ALL) && IsCUDAOCL) { + std::cout << "SYCL_PI_TRACE[all]: " + << "NVIDIA CUDA OpenCL platform found but is not compatible." + << std::endl; + } + return IsCUDAOCL; + }; + return IsNVIDIAOpenCL(Platform); +} + +std::string getValue(const std::string &AllowList, size_t &Pos, + unsigned long int Size) { + size_t Prev = Pos; + if ((Pos = AllowList.find("{{", Pos)) == std::string::npos) { + throw sycl::runtime_error("Malformed syntax in SYCL_DEVICE_ALLOWLIST", + PI_INVALID_VALUE); + } + if (Pos > Prev + Size) { + throw sycl::runtime_error("Malformed syntax in SYCL_DEVICE_ALLOWLIST", + PI_INVALID_VALUE); + } + + Pos = Pos + 2; + size_t Start = Pos; + if ((Pos = AllowList.find("}}", Pos)) == std::string::npos) { + throw sycl::runtime_error("Malformed syntax in SYCL_DEVICE_ALLOWLIST", + PI_INVALID_VALUE); + } + std::string Value = AllowList.substr(Start, Pos - Start); + Pos = Pos + 2; + return Value; +} + +struct DevDescT { + std::string DevName; + std::string DevDriverVer; + std::string PlatName; + std::string PlatVer; +}; + +static std::vector getAllowListDesc() { + std::string AllowList(SYCLConfig::get()); + if (AllowList.empty()) + return {}; + + std::string DeviceName("DeviceName:"); + std::string DriverVersion("DriverVersion:"); + std::string PlatformName("PlatformName:"); + std::string PlatformVersion("PlatformVersion:"); + std::vector DecDescs; + DecDescs.emplace_back(); + + size_t Pos = 0; + while (Pos < AllowList.size()) { + if ((AllowList.compare(Pos, DeviceName.size(), DeviceName)) == 0) { + DecDescs.back().DevName = getValue(AllowList, Pos, DeviceName.size()); + if (AllowList[Pos] == ',') { + Pos++; + } + } + + else if ((AllowList.compare(Pos, DriverVersion.size(), DriverVersion)) == + 0) { + DecDescs.back().DevDriverVer = + getValue(AllowList, Pos, DriverVersion.size()); + if (AllowList[Pos] == ',') { + Pos++; + } + } + + else if ((AllowList.compare(Pos, PlatformName.size(), PlatformName)) == 0) { + DecDescs.back().PlatName = getValue(AllowList, Pos, PlatformName.size()); + if (AllowList[Pos] == ',') { + Pos++; + } + } + + else if ((AllowList.compare(Pos, PlatformVersion.size(), + PlatformVersion)) == 0) { + DecDescs.back().PlatVer = + getValue(AllowList, Pos, PlatformVersion.size()); + } else if (AllowList.find('|', Pos) != std::string::npos) { + Pos = AllowList.find('|') + 1; + while (AllowList[Pos] == ' ') { + Pos++; + } + DecDescs.emplace_back(); + } + + else { + throw sycl::runtime_error("Unrecognized key in device allowlist", + PI_INVALID_VALUE); + } + } // while (Pos <= AllowList.size()) + return DecDescs; +} + +enum class FilterState { DENIED, ALLOWED }; + +static void filterAllowList(vector_class &PiDevices, + RT::PiPlatform PiPlatform, const plugin &Plugin) { + const std::vector AllowList(getAllowListDesc()); + if (AllowList.empty()) + return; + + FilterState DevNameState = FilterState::ALLOWED; + FilterState DevVerState = FilterState::ALLOWED; + FilterState PlatNameState = FilterState::ALLOWED; + FilterState PlatVerState = FilterState::ALLOWED; + + const string_class PlatformName = + sycl::detail::get_platform_info::get( + PiPlatform, Plugin); + + const string_class PlatformVer = + sycl::detail::get_platform_info::get(PiPlatform, + Plugin); + + int InsertIDx = 0; + for (RT::PiDevice Device : PiDevices) { + const string_class DeviceName = + sycl::detail::get_device_info::get( + Device, Plugin); + + const string_class DeviceDriverVer = sycl::detail::get_device_info< + string_class, info::device::driver_version>::get(Device, Plugin); + + for (const DevDescT &Desc : AllowList) { + if (!Desc.PlatName.empty()) { + if (!std::regex_match(PlatformName, std::regex(Desc.PlatName))) { + PlatNameState = FilterState::DENIED; + continue; + } + } + + if (!Desc.PlatVer.empty()) { + if (!std::regex_match(PlatformVer, std::regex(Desc.PlatVer))) { + PlatVerState = FilterState::DENIED; + continue; + } + } + + if (!Desc.DevName.empty()) { + if (!std::regex_match(DeviceName, std::regex(Desc.DevName))) { + DevNameState = FilterState::DENIED; + continue; + } + } + + if (!Desc.DevDriverVer.empty()) { + if (!std::regex_match(DeviceDriverVer, std::regex(Desc.DevDriverVer))) { + DevVerState = FilterState::DENIED; + continue; + } + } + + if (DevNameState == FilterState::ALLOWED && + DevVerState == FilterState::ALLOWED && + PlatNameState == FilterState::ALLOWED && + PlatVerState == FilterState::ALLOWED) + PiDevices[InsertIDx++] = Device; + break; + } + } + PiDevices.resize(InsertIDx); +} + +// Filter out the devices that are not compatible with SYCL_DEVICE_FILTER. +// All three entries (backend:device_type:device_num) are optional. +// The missing entries are constructed using '*', which means 'any' | 'all' +// by the device_filter constructor. +// This function matches devices in the order of backend, device_type, and +// device_num. +static void filterDeviceFilter(vector_class &PiDevices, + RT::PiPlatform Platform, const plugin &Plugin, + int DeviceNum) { + device_filter_list *FilterList = SYCLConfig::get(); + if (!FilterList) + return; + + backend Backend = Plugin.getBackend(); + int InsertIDx = 0; + + for (RT::PiDevice Device : PiDevices) { + RT::PiDeviceType PiDevType; + Plugin.call(Device, PI_DEVICE_INFO_TYPE, + sizeof(RT::PiDeviceType), + &PiDevType, nullptr); + // Assumption here is that there is 1-to-1 mapping between PiDevType and + // Sycl device type for GPU, CPU, and ACC. + info::device_type DeviceType = pi::cast(PiDevType); + + for (const device_filter &Filter : FilterList->get()) { + backend FilterBackend = Filter.Backend; + // First, match the backend entry + if (FilterBackend == Backend || FilterBackend == backend::all) { + info::device_type FilterDevType = Filter.DeviceType; + // Next, match the device_type entry + if (FilterDevType == info::device_type::all) { + // Last, match the device_num entry + if (!Filter.HasDeviceNum || DeviceNum == Filter.DeviceNum) { + PiDevices[InsertIDx++] = Device; + break; + } + } else if (FilterDevType == DeviceType) { + if (!Filter.HasDeviceNum || DeviceNum == Filter.DeviceNum) { + PiDevices[InsertIDx++] = Device; + break; + } + } + } + } + DeviceNum++; + } + PiDevices.resize(InsertIDx); +} + +// Fill up the platform cache and device cache for the given Plugin. +// Thi( + 0, nullptr, &NumPlatforms) != PI_SUCCESS) + return; + + if (NumPlatforms) { + vector_class PiPlatforms(NumPlatforms); + if (Plugin.call_nocheck( + NumPlatforms, PiPlatforms.data(), nullptr) != PI_SUCCESS) + return; + + std::vector &PlatformCache = + GlobalHandler::instance().getPlatformCache(); + + int DeviceNum = 0; + for (const auto &PiPlatform : PiPlatforms) { + PlatformImplPtr PlatformImpl = + std::make_shared(PiPlatform, Plugin); + { + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); + + PlatformCache.emplace_back(PlatformImpl); + } + platform Platform = detail::createSyclObjFromImpl(PlatformImpl); + + if (IsBannedPlatform(Platform)) + continue; + // get devices + info::device_type DeviceType = info::device_type::all; + pi_uint32 NumDevices = 0; + Plugin.call( + PiPlatform, pi::cast(DeviceType), 0, + pi::cast(nullptr), &NumDevices); + + if (NumDevices == 0) + continue; + + vector_class PiDevices(NumDevices); + Plugin.call( + PiPlatform, pi::cast(DeviceType), NumDevices, + PiDevices.data(), nullptr); + + // Filter out devices that are not present in the allowlist + if (SYCLConfig::get()) + filterAllowList(PiDevices, PiPlatform, Plugin); + + int UnfilteredDeviceCount = PiDevices.size(); + // Filter out devices that are not compatible with SYCL_DEVICE_FILTER + filterDeviceFilter(PiDevices, PiPlatform, Plugin, DeviceNum); + + for (const RT::PiDevice& PiDevice : PiDevices) { + std::shared_ptr Device = + PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl); + } + DeviceNum += UnfilteredDeviceCount; + } // end of for + } // end of if +} + // Initializes all available Plugins. const vector_class &initialize() { static std::once_flag PluginsInitDone; @@ -310,11 +612,6 @@ const vector_class &initialize() { std::call_once(PluginsInitDone, []() { initializePlugins(&GlobalHandler::instance().getPlugins()); }); - // reset LastDeviceIds to zeros - vector_class Plugins = GlobalHandler::instance().getPlugins(); - for (plugin Plugin : Plugins) { - Plugin.resetLastDeviceIds(); - } return GlobalHandler::instance().getPlugins(); } @@ -379,8 +676,21 @@ static void initializePlugins(vector_class *Plugins) { std::cerr << "SYCL_PI_TRACE[basic]: " << "Plugin found and successfully loaded: " << PluginNames[I].first << std::endl; - } + fillPlatformAndDeviceCache(Plugins->back()); + } // end of for + + // The host platform should always be available unless not allowed by the + // SYCL_DEVICE_FILTER + detail::device_filter_list *FilterList = + detail::SYCLConfig::get(); + if (!FilterList || FilterList->containsHost()) { + std::vector &PlatformCache = + GlobalHandler::instance().getPlatformCache(); + PlatformImplPtr PlatformImpl = platform_impl::getHostPlatformImpl(); + PlatformCache.emplace_back(PlatformImpl); + platform_impl::makeHostDevice(); + } #ifdef XPTI_ENABLE_INSTRUMENTATION if (!(xptiTraceEnabled() && !XPTIInitDone)) return; diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 40ee03a27f5fd..201e96f1b3ac7 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -28,10 +28,15 @@ using PlatformImplPtr = std::shared_ptr; PlatformImplPtr platform_impl::getHostPlatformImpl() { static PlatformImplPtr HostImpl = std::make_shared(); - return HostImpl; } +void platform_impl::makeHostDevice() { + PlatformImplPtr HostImpl = getHostPlatformImpl(); + if (HostImpl->MDeviceCache.size() == 0) + HostImpl->MDeviceCache.emplace_back(std::make_shared()); +} + PlatformImplPtr platform_impl::getOrMakePlatformImpl(RT::PiPlatform PiPlatform, const plugin &Plugin) { PlatformImplPtr Result; @@ -65,138 +70,25 @@ PlatformImplPtr platform_impl::getPlatformFromPiDevice(RT::PiDevice PiDevice, return getOrMakePlatformImpl(Plt, Plugin); } -static bool IsBannedPlatform(platform Platform) { - // The NVIDIA OpenCL platform is currently not compatible with DPC++ - // since it is only 1.2 but gets selected by default in many systems - // There is also no support on the PTX backend for OpenCL consumption, - // and there have been some internal reports. - // To avoid problems on default users and deployment of DPC++ on platforms - // where CUDA is available, the OpenCL support is disabled. - // - auto IsNVIDIAOpenCL = [](platform Platform) { - if (Platform.is_host()) - return false; - - const bool HasCUDA = Platform.get_info().find( - "NVIDIA CUDA") != std::string::npos; - const auto Backend = - detail::getSyclObjImpl(Platform)->getPlugin().getBackend(); - const bool IsCUDAOCL = (HasCUDA && Backend == backend::opencl); - if (detail::pi::trace(detail::pi::TraceLevel::PI_TRACE_ALL) && IsCUDAOCL) { - std::cout << "SYCL_PI_TRACE[all]: " - << "NVIDIA CUDA OpenCL platform found but is not compatible." - << std::endl; - } - return IsCUDAOCL; - }; - return IsNVIDIAOpenCL(Platform); -} - vector_class platform_impl::get_platforms() { vector_class Platforms; - const vector_class &Plugins = RT::initialize(); - - info::device_type ForcedType = detail::get_forced_type(); - for (unsigned int i = 0; i < Plugins.size(); i++) { - - pi_uint32 NumPlatforms = 0; - // Move to the next plugin if the plugin fails to initialize. - // This way platforms from other plugins get a chance to be discovered. - if (Plugins[i].call_nocheck( - 0, nullptr, &NumPlatforms) != PI_SUCCESS) - continue; - - if (NumPlatforms) { - vector_class PiPlatforms(NumPlatforms); - if (Plugins[i].call_nocheck( - NumPlatforms, PiPlatforms.data(), nullptr) != PI_SUCCESS) - return Platforms; - - for (const auto &PiPlatform : PiPlatforms) { - platform Platform = detail::createSyclObjFromImpl( - getOrMakePlatformImpl(PiPlatform, Plugins[i])); - // Skip platforms which do not contain requested device types - if (!Platform.get_devices(ForcedType).empty() && - !IsBannedPlatform(Platform)) - Platforms.push_back(Platform); - } - } + RT::initialize(); + std::vector &PlatformCache = + GlobalHandler::instance().getPlatformCache(); + for (const PlatformImplPtr& PlatformImpl : PlatformCache) { + platform Platform = detail::createSyclObjFromImpl(PlatformImpl); + Platforms.push_back(Platform); } - - // The host platform should always be available unless not allowed by the - // SYCL_DEVICE_FILTER - detail::device_filter_list *FilterList = - detail::SYCLConfig::get(); - if (!FilterList || FilterList->backendCompatible(backend::host)) - Platforms.emplace_back(platform()); - return Platforms; } -// Filter out the devices that are not compatible with SYCL_DEVICE_FILTER. -// All three entries (backend:device_type:device_num) are optional. -// The missing entries are constructed using '*', which means 'any' | 'all' -// by the device_filter constructor. -// This function matches devices in the order of backend, device_type, and -// device_num. -static void filterDeviceFilter(vector_class &PiDevices, - RT::PiPlatform Platform, - std::shared_ptr Plugin) { - device_filter_list *FilterList = SYCLConfig::get(); - if (!FilterList) - return; - - backend Backend = Plugin->getBackend(); - int InsertIDx = 0; - // DeviceIds should be given consecutive numbers across platforms in the same - // backend - int DeviceNum = Plugin->getStartingDeviceId(Platform); - - for (RT::PiDevice Device : PiDevices) { - RT::PiDeviceType PiDevType; - Plugin->call(Device, PI_DEVICE_INFO_TYPE, - sizeof(RT::PiDeviceType), - &PiDevType, nullptr); - // Assumption here is that there is 1-to-1 mapping between PiDevType and - // Sycl device type for GPU, CPU, and ACC. - info::device_type DeviceType = pi::cast(PiDevType); - - for (const device_filter &Filter : FilterList->get()) { - backend FilterBackend = Filter.Backend; - // First, match the backend entry - if (FilterBackend == Backend || FilterBackend == backend::all) { - info::device_type FilterDevType = Filter.DeviceType; - // Next, match the device_type entry - if (FilterDevType == info::device_type::all) { - // Last, match the device_num entry - if (!Filter.HasDeviceNum || DeviceNum == Filter.DeviceNum) { - PiDevices[InsertIDx++] = Device; - break; - } - } else if (FilterDevType == DeviceType) { - if (!Filter.HasDeviceNum || DeviceNum == Filter.DeviceNum) { - PiDevices[InsertIDx++] = Device; - break; - } - } - } - } - DeviceNum++; - } - PiDevices.resize(InsertIDx); - // remember the last backend that has gone through this filter function - // to assign a unique device id number across platforms that belong to - // the same backend. For example, opencl:cpu:0, opencl:acc:1, opencl:gpu:2 - Plugin->setLastDeviceId(Platform, DeviceNum); -} - std::shared_ptr platform_impl::getOrMakeDeviceImpl( RT::PiDevice PiDevice, const std::shared_ptr &PlatformImpl) { const std::lock_guard Guard(MDeviceMapMutex); // If we've already seen this device, return the impl - for (const std::weak_ptr &DeviceWP : MDeviceCache) { - if (std::shared_ptr Device = DeviceWP.lock()) { + for (const std::shared_ptr &DeviceWP : MDeviceCache) { + if (std::shared_ptr Device = DeviceWP) { if (Device->getHandleRef() == PiDevice) return Device; } @@ -206,14 +98,13 @@ std::shared_ptr platform_impl::getOrMakeDeviceImpl( std::shared_ptr Result = std::make_shared(PiDevice, PlatformImpl); MDeviceCache.emplace_back(Result); - return Result; } vector_class platform_impl::get_devices(info::device_type DeviceType) const { vector_class Res; - if (is_host() && (DeviceType == info::device_type::host || + /*if (is_host() && (DeviceType == info::device_type::host || DeviceType == info::device_type::all)) { // If SYCL_DEVICE_FILTER is set, check if filter contains host. device_filter_list *FilterList = SYCLConfig::get(); @@ -226,37 +117,15 @@ platform_impl::get_devices(info::device_type DeviceType) const { // an empty vector will be returned. if (is_host() || DeviceType == info::device_type::host) return Res; - - pi_uint32 NumDevices = 0; - const detail::plugin &Plugin = getPlugin(); - Plugin.call( - MPlatform, pi::cast(DeviceType), 0, - pi::cast(nullptr), &NumDevices); - - if (NumDevices == 0) - return Res; - - vector_class PiDevices(NumDevices); - // TODO catch an exception and put it to list of asynchronous exceptions - Plugin.call(MPlatform, - pi::cast(DeviceType), - NumDevices, PiDevices.data(), nullptr); - - // Filter out devices that are not present in the SYCL_DEVICE_ALLOWLIST - if (SYCLConfig::get()) - applyAllowList(PiDevices, MPlatform, this->getPlugin()); - - // Filter out devices that are not compatible with SYCL_DEVICE_FILTER - filterDeviceFilter(PiDevices, MPlatform, MPlugin); - - PlatformImplPtr PlatformImpl = getOrMakePlatformImpl(MPlatform, *MPlugin); - std::transform( - PiDevices.begin(), PiDevices.end(), std::back_inserter(Res), - [PlatformImpl](const RT::PiDevice &PiDevice) -> device { - return detail::createSyclObjFromImpl( - PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl)); - }); - + */ + for (const std::shared_ptr &Device : MDeviceCache) { + // Assumption here is that there is 1-to-1 mapping between PiDevType and + // Sycl device type for GPU, CPU, and ACC. + info::device_type PiDeviceType = pi::cast(Device->get_device_type()); + if (DeviceType == info::device_type::all || DeviceType == PiDeviceType) + Res.push_back(detail::createSyclObjFromImpl(Device)); + } + return Res; } diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index 8af8874413a94..e8fc183630aaf 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -158,6 +158,9 @@ class platform_impl { /// \return the host platform impl static std::shared_ptr getHostPlatformImpl(); + /// Add a Host device into the Host platform impl + static void makeHostDevice(); + /// Queries the cache to see if the specified PiPlatform has been seen /// before. If so, return the cached platform_impl, otherwise create a new /// one and cache it. @@ -184,7 +187,7 @@ class platform_impl { bool MHostPlatform = false; RT::PiPlatform MPlatform = 0; std::shared_ptr MPlugin; - std::vector> MDeviceCache; + std::vector> MDeviceCache; std::mutex MDeviceMapMutex; }; diff --git a/sycl/source/detail/plugin.hpp b/sycl/source/detail/plugin.hpp index cea36e0048262..3480ac81f643e 100644 --- a/sycl/source/detail/plugin.hpp +++ b/sycl/source/detail/plugin.hpp @@ -110,49 +110,12 @@ class plugin { void *getLibraryHandle() const { return MLibraryHandle; } void *getLibraryHandle() { return MLibraryHandle; } int unload() { return RT::unloadPlugin(MLibraryHandle); } - // return the index os PiPlatforms. - // If not found, add it and return its index. - int getPlatformId(RT::PiPlatform Platform) { - auto It = std::find(PiPlatforms.begin(), PiPlatforms.end(), Platform); - if (It != PiPlatforms.end()) { - return It - PiPlatforms.begin(); - } else { - PiPlatforms.push_back(Platform); - LastDeviceIds.push_back(0); - return PiPlatforms.size() - 1; - } - } - // Device ids are consecutive across platforms within a plugin. - // We need to return the same starting index for the given platform. - // So, instead of returing the last device id of the given platform, - // return the last device id of the predecessor platform. - int getStartingDeviceId(RT::PiPlatform Platform) { - int PlatformId = getPlatformId(Platform); - if (PlatformId == 0) - return 0; - else - return LastDeviceIds[PlatformId - 1]; - } - // set the id of the last device for the given platform - void setLastDeviceId(RT::PiPlatform Platform, int Id) { - int PlatformId = getPlatformId(Platform); - LastDeviceIds[PlatformId] = Id; - } - // reset all last device ids to zeros - void resetLastDeviceIds() { - std::fill(LastDeviceIds.begin(), LastDeviceIds.end(), 0); - } private: RT::PiPlugin MPlugin; backend MBackend; void *MLibraryHandle; // the handle returned from dlopen std::shared_ptr TracingMutex; - // vector of PiPlatforms that belong to this plugin - std::vector PiPlatforms; - // represents the unique ids of the last device of each platform - // index of this vector corresponds to the index in PiPlatforms vector. - std::vector LastDeviceIds; }; // class plugin } // namespace detail } // namespace sycl diff --git a/sycl/source/device.cpp b/sycl/source/device.cpp index e824b5faba039..efc4ad0f55ee2 100644 --- a/sycl/source/device.cpp +++ b/sycl/source/device.cpp @@ -50,6 +50,7 @@ device::device(const device_selector &deviceSelector) { vector_class device::get_devices(info::device_type deviceType) { vector_class devices; + /* detail::device_filter_list *FilterList = detail::SYCLConfig::get(); // Host device availability should depend on the forced type @@ -65,6 +66,7 @@ vector_class device::get_devices(info::device_type deviceType) { } else { includeHost = detail::match_types(deviceType, info::device_type::host); } + */ info::device_type forced_type = detail::get_forced_type(); // Exclude devices which do not match requested device type if (detail::match_types(deviceType, forced_type)) { @@ -76,6 +78,7 @@ vector_class device::get_devices(info::device_type deviceType) { if (ForcedBackend) if (!plt.is_host() && plt.get_backend() != *ForcedBackend) continue; + /* // If SYCL_DEVICE_FILTER is set, skip platforms that is incompatible // with the filter specification. if (FilterList && !FilterList->backendCompatible(plt.get_backend())) @@ -86,12 +89,12 @@ vector_class device::get_devices(info::device_type deviceType) { plt.get_devices(info::device_type::host)); if (!host_device.empty()) devices.insert(devices.end(), host_device.begin(), host_device.end()); - } else { + } else {*/ vector_class found_devices(plt.get_devices(deviceType)); if (!found_devices.empty()) devices.insert(devices.end(), found_devices.begin(), found_devices.end()); - } + //} } } return devices; diff --git a/sycl/unittests/get_native_interop/test_get_native.cpp b/sycl/unittests/get_native_interop/test_get_native.cpp index 5945c3130a305..5d2a305ec8f7c 100644 --- a/sycl/unittests/get_native_interop/test_get_native.cpp +++ b/sycl/unittests/get_native_interop/test_get_native.cpp @@ -129,7 +129,5 @@ TEST(GetNativeTest, GetNativeHandle) { get_native(Device); get_native(Event); - // When creating a context, the piDeviceRetain is called so here is the 6 - // retain calls - ASSERT_EQ(TestCounter, 6) << "Not all the retain methods was called"; + ASSERT_EQ(TestCounter, 5) << "Not all the retain methods was called " << TestCounter; } From 5c85960d6bfd2383c3baff3254281731e0924d98 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 21 Jun 2021 15:57:44 -0700 Subject: [PATCH 17/47] fixed clang-format Signed-off-by: Byoungro So --- sycl/source/detail/pi.cpp | 2 +- sycl/source/detail/platform_impl.cpp | 21 ++-------- sycl/source/device.cpp | 38 ++----------------- sycl/tools/sycl-ls/sycl-ls.cpp | 6 +-- .../get_native_interop/test_get_native.cpp | 3 +- 5 files changed, 13 insertions(+), 57 deletions(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index a6d981cba9f07..0d95d9579d020 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -596,7 +596,7 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { // Filter out devices that are not compatible with SYCL_DEVICE_FILTER filterDeviceFilter(PiDevices, PiPlatform, Plugin, DeviceNum); - for (const RT::PiDevice& PiDevice : PiDevices) { + for (const RT::PiDevice &PiDevice : PiDevices) { std::shared_ptr Device = PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl); } diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 201e96f1b3ac7..050e0370da5dc 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -75,7 +75,7 @@ vector_class platform_impl::get_platforms() { RT::initialize(); std::vector &PlatformCache = GlobalHandler::instance().getPlatformCache(); - for (const PlatformImplPtr& PlatformImpl : PlatformCache) { + for (const PlatformImplPtr &PlatformImpl : PlatformCache) { platform Platform = detail::createSyclObjFromImpl(PlatformImpl); Platforms.push_back(Platform); } @@ -104,28 +104,15 @@ std::shared_ptr platform_impl::getOrMakeDeviceImpl( vector_class platform_impl::get_devices(info::device_type DeviceType) const { vector_class Res; - /*if (is_host() && (DeviceType == info::device_type::host || - DeviceType == info::device_type::all)) { - // If SYCL_DEVICE_FILTER is set, check if filter contains host. - device_filter_list *FilterList = SYCLConfig::get(); - if (!FilterList || FilterList->containsHost()) { - Res.push_back(device()); - } - } - - // If any DeviceType other than host was requested for host platform, - // an empty vector will be returned. - if (is_host() || DeviceType == info::device_type::host) - return Res; - */ for (const std::shared_ptr &Device : MDeviceCache) { // Assumption here is that there is 1-to-1 mapping between PiDevType and // Sycl device type for GPU, CPU, and ACC. - info::device_type PiDeviceType = pi::cast(Device->get_device_type()); + info::device_type PiDeviceType = + pi::cast(Device->get_device_type()); if (DeviceType == info::device_type::all || DeviceType == PiDeviceType) Res.push_back(detail::createSyclObjFromImpl(Device)); } - + return Res; } diff --git a/sycl/source/device.cpp b/sycl/source/device.cpp index efc4ad0f55ee2..57239d8de74b1 100644 --- a/sycl/source/device.cpp +++ b/sycl/source/device.cpp @@ -50,23 +50,6 @@ device::device(const device_selector &deviceSelector) { vector_class device::get_devices(info::device_type deviceType) { vector_class devices; - /* - detail::device_filter_list *FilterList = - detail::SYCLConfig::get(); - // Host device availability should depend on the forced type - bool includeHost = false; - // If SYCL_DEVICE_FILTER is set, we don't automatically include it. - // We will check if host devices are specified in the filter below. - if (FilterList) { - if (deviceType != info::device_type::host && - deviceType != info::device_type::all) - includeHost = false; - else - includeHost = FilterList->containsHost(); - } else { - includeHost = detail::match_types(deviceType, info::device_type::host); - } - */ info::device_type forced_type = detail::get_forced_type(); // Exclude devices which do not match requested device type if (detail::match_types(deviceType, forced_type)) { @@ -78,23 +61,10 @@ vector_class device::get_devices(info::device_type deviceType) { if (ForcedBackend) if (!plt.is_host() && plt.get_backend() != *ForcedBackend) continue; - /* - // If SYCL_DEVICE_FILTER is set, skip platforms that is incompatible - // with the filter specification. - if (FilterList && !FilterList->backendCompatible(plt.get_backend())) - continue; - - if (includeHost && plt.is_host()) { - vector_class host_device( - plt.get_devices(info::device_type::host)); - if (!host_device.empty()) - devices.insert(devices.end(), host_device.begin(), host_device.end()); - } else {*/ - vector_class found_devices(plt.get_devices(deviceType)); - if (!found_devices.empty()) - devices.insert(devices.end(), found_devices.begin(), - found_devices.end()); - //} + vector_class found_devices(plt.get_devices(deviceType)); + if (!found_devices.empty()) + devices.insert(devices.end(), found_devices.begin(), + found_devices.end()); } } return devices; diff --git a/sycl/tools/sycl-ls/sycl-ls.cpp b/sycl/tools/sycl-ls/sycl-ls.cpp index a980f7ed688dd..b9b38ca73e91b 100644 --- a/sycl/tools/sycl-ls/sycl-ls.cpp +++ b/sycl/tools/sycl-ls/sycl-ls.cpp @@ -68,10 +68,8 @@ static void printDeviceInfo(const device &Device, const std::string &Prepend) { std::cout << Prepend << "Vendor : " << DeviceVendor << std::endl; std::cout << Prepend << "Driver : " << DeviceDriverVersion << std::endl; } else { - auto DevicePlatform = Device.get_info(); - auto DevicePlatformName = DevicePlatform.get_info(); - std::cout << Prepend << " : " << DevicePlatformName << " " << DeviceVersion - << " [" << DeviceDriverVersion << "]" << std::endl; + std::cout << Prepend << " : " << DeviceName << " " << DeviceVersion << " [" + << DeviceDriverVersion << "]" << std::endl; } } diff --git a/sycl/unittests/get_native_interop/test_get_native.cpp b/sycl/unittests/get_native_interop/test_get_native.cpp index 5d2a305ec8f7c..2945d79fccdc9 100644 --- a/sycl/unittests/get_native_interop/test_get_native.cpp +++ b/sycl/unittests/get_native_interop/test_get_native.cpp @@ -129,5 +129,6 @@ TEST(GetNativeTest, GetNativeHandle) { get_native(Device); get_native(Event); - ASSERT_EQ(TestCounter, 5) << "Not all the retain methods was called " << TestCounter; + ASSERT_EQ(TestCounter, 5) + << "Not all the retain methods was called " << TestCounter; } From 8f6a09b15f5d9134892e208a7572a2e31394bbae Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 28 Jun 2021 11:54:05 -0700 Subject: [PATCH 18/47] shared_ptr in global device cache Signed-off-by: Byoungro So --- sycl/source/detail/global_handler.cpp | 22 ++++++++++++++++++++++ sycl/source/detail/global_handler.hpp | 6 ++++++ sycl/source/detail/pi.cpp | 9 ++++++++- sycl/source/detail/platform_impl.cpp | 17 +++++++++++------ sycl/source/detail/platform_impl.hpp | 7 ++++--- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index 06cf90b3e11f9..0aeaeecc17c97 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -125,11 +125,33 @@ std::mutex &GlobalHandler::getHandlerExtendedMembersMutex() { return *MHandlerExtendedMembersMutex; } +std::vector &GlobalHandler::getDeviceCache() { + if (MDeviceCache) + return *MDeviceCache; + + const std::lock_guard Lock{MFieldsLock}; + if (!MDeviceCache) + MDeviceCache = std::make_unique>(); + + return *MDeviceCache; +} +std::mutex &GlobalHandler::getDeviceCacheMutex() { + if (MDeviceCacheMutex) + return *MDeviceCacheMutex; + + const std::lock_guard Lock{MFieldsLock}; + if (!MDeviceCacheMutex) + MDeviceCacheMutex = std::make_unique(); + + return *MDeviceCacheMutex; +} + void shutdown() { // First, release resources, that may access plugins. GlobalHandler::instance().MScheduler.reset(nullptr); GlobalHandler::instance().MProgramManager.reset(nullptr); GlobalHandler::instance().MPlatformCache.reset(nullptr); + GlobalHandler::instance().MDeviceCache.reset(nullptr); // Call to GlobalHandler::instance().getPlugins() initializes plugins. If // user application has loaded SYCL runtime, and never called any APIs, diff --git a/sycl/source/detail/global_handler.hpp b/sycl/source/detail/global_handler.hpp index 9b2c582b164ae..b25d5c0c2303f 100644 --- a/sycl/source/detail/global_handler.hpp +++ b/sycl/source/detail/global_handler.hpp @@ -22,8 +22,10 @@ class ProgramManager; class Sync; class plugin; class device_filter_list; +class device_impl; using PlatformImplPtr = std::shared_ptr; +using DeviceImplPtr = std::shared_ptr; /// Wrapper class for global data structures with non-trivial destructors. /// @@ -57,6 +59,8 @@ class GlobalHandler { std::vector &getPlugins(); device_filter_list &getDeviceFilterList(const std::string &InitValue); std::mutex &getHandlerExtendedMembersMutex(); + std::vector &getDeviceCache(); + std::mutex &getDeviceCacheMutex(); private: friend void shutdown(); @@ -79,6 +83,8 @@ class GlobalHandler { std::unique_ptr MDeviceFilterList; // The mutex for synchronizing accesses to handlers extended members std::unique_ptr MHandlerExtendedMembersMutex; + std::unique_ptr> MDeviceCache; + std::unique_ptr MDeviceCacheMutex; }; } // namespace detail } // namespace sycl diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 0d95d9579d020..1622f160ae080 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -558,6 +558,8 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { std::vector &PlatformCache = GlobalHandler::instance().getPlatformCache(); + std::vector &DeviceCache = + GlobalHandler::instance().getDeviceCache(); int DeviceNum = 0; for (const auto &PiPlatform : PiPlatforms) { @@ -599,6 +601,7 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { for (const RT::PiDevice &PiDevice : PiDevices) { std::shared_ptr Device = PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl); + DeviceCache.emplace_back(Device); } DeviceNum += UnfilteredDeviceCount; } // end of for @@ -687,9 +690,13 @@ static void initializePlugins(vector_class *Plugins) { if (!FilterList || FilterList->containsHost()) { std::vector &PlatformCache = GlobalHandler::instance().getPlatformCache(); + std::vector &DeviceCache = + GlobalHandler::instance().getDeviceCache(); PlatformImplPtr PlatformImpl = platform_impl::getHostPlatformImpl(); PlatformCache.emplace_back(PlatformImpl); - platform_impl::makeHostDevice(); + std::shared_ptr Device = + platform_impl::getOrMakeHostDeviceImpl(); + DeviceCache.emplace_back(Device); } #ifdef XPTI_ENABLE_INSTRUMENTATION if (!(xptiTraceEnabled() && !XPTIInitDone)) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 050e0370da5dc..4ee09dfa8a601 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -31,10 +31,14 @@ PlatformImplPtr platform_impl::getHostPlatformImpl() { return HostImpl; } -void platform_impl::makeHostDevice() { +std::shared_ptr platform_impl::getOrMakeHostDeviceImpl() { PlatformImplPtr HostImpl = getHostPlatformImpl(); - if (HostImpl->MDeviceCache.size() == 0) - HostImpl->MDeviceCache.emplace_back(std::make_shared()); + if (HostImpl->MDeviceCache.size() == 0) { + std::shared_ptr DeviceSP = std::make_shared(); + HostImpl->MDeviceCache.emplace_back(DeviceSP); + return DeviceSP; + } + return HostImpl->MDeviceCache[0].lock(); } PlatformImplPtr platform_impl::getOrMakePlatformImpl(RT::PiPlatform PiPlatform, @@ -87,8 +91,8 @@ std::shared_ptr platform_impl::getOrMakeDeviceImpl( const std::lock_guard Guard(MDeviceMapMutex); // If we've already seen this device, return the impl - for (const std::shared_ptr &DeviceWP : MDeviceCache) { - if (std::shared_ptr Device = DeviceWP) { + for (const std::weak_ptr &DeviceWP : MDeviceCache) { + if (std::shared_ptr Device = DeviceWP.lock()) { if (Device->getHandleRef() == PiDevice) return Device; } @@ -104,9 +108,10 @@ std::shared_ptr platform_impl::getOrMakeDeviceImpl( vector_class platform_impl::get_devices(info::device_type DeviceType) const { vector_class Res; - for (const std::shared_ptr &Device : MDeviceCache) { + for (const std::weak_ptr &DeviceWP : MDeviceCache) { // Assumption here is that there is 1-to-1 mapping between PiDevType and // Sycl device type for GPU, CPU, and ACC. + std::shared_ptr Device = DeviceWP.lock(); info::device_type PiDeviceType = pi::cast(Device->get_device_type()); if (DeviceType == info::device_type::all || DeviceType == PiDeviceType) diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index e8fc183630aaf..9f8dc9e5d655a 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -158,8 +158,9 @@ class platform_impl { /// \return the host platform impl static std::shared_ptr getHostPlatformImpl(); - /// Add a Host device into the Host platform impl - static void makeHostDevice(); + /// Get a Host device into from the Host platform impl + /// If not created, create one and add it to MDeviceCache + static std::shared_ptr getOrMakeHostDeviceImpl(); /// Queries the cache to see if the specified PiPlatform has been seen /// before. If so, return the cached platform_impl, otherwise create a new @@ -187,7 +188,7 @@ class platform_impl { bool MHostPlatform = false; RT::PiPlatform MPlatform = 0; std::shared_ptr MPlugin; - std::vector> MDeviceCache; + std::vector> MDeviceCache; std::mutex MDeviceMapMutex; }; From faab5a78019400074f5f7173f12a255b969e5e54 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 29 Jun 2021 12:40:29 -0700 Subject: [PATCH 19/47] removed debugging print Signed-off-by: Byoungro So --- sycl/source/detail/device_impl.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/sycl/source/detail/device_impl.cpp b/sycl/source/detail/device_impl.cpp index 1754ca70528c6..a7af1c50bad70 100644 --- a/sycl/source/detail/device_impl.cpp +++ b/sycl/source/detail/device_impl.cpp @@ -44,7 +44,6 @@ device_impl::device_impl(pi_native_handle InteropDeviceHandle, // With SYCL-2020 BE generalization "make" functions are used instead. Plugin.call( InteropDeviceHandle, nullptr, &MDevice); - std::cout << "interop device constructor " << std::endl; InteroperabilityConstructor = true; } From c096a7b52e1d575054bb3a72fe6ae11317edfdb1 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 29 Jun 2021 12:46:53 -0700 Subject: [PATCH 20/47] fix clang-format Signed-off-by: Byoungro So --- sycl/source/device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/device.cpp b/sycl/source/device.cpp index 7adeec3c6f380..bf8c5b7d4f1f6 100644 --- a/sycl/source/device.cpp +++ b/sycl/source/device.cpp @@ -61,7 +61,7 @@ std::vector device::get_devices(info::device_type deviceType) { if (ForcedBackend) if (!plt.is_host() && plt.get_backend() != *ForcedBackend) continue; - std::vector found_devices(plt.get_devices(deviceType)); + std::vector found_devices(plt.get_devices(deviceType)); if (!found_devices.empty()) devices.insert(devices.end(), found_devices.begin(), found_devices.end()); From 6e906ebefa220782747d4a18d320a1e7e56966b2 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 30 Jun 2021 12:54:35 -0700 Subject: [PATCH 21/47] Update sycl/source/detail/pi.cpp Co-authored-by: Romanov Vlad <17316488+romanovvlad@users.noreply.github.com> --- sycl/source/detail/pi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 32dfefe5f11c6..24a06f03d13a6 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -317,7 +317,7 @@ static bool IsBannedPlatform(platform Platform) { // There is also no support on the PTX backend for OpenCL consumption, // and there have been some internal reports. // To avoid problems on default users and deployment of DPC++ on platforms - // where CUDA is available, the OpenCL support is disabled. + // where CUDA is available, the NVidiaOpenCL support is disabled. // auto IsNVIDIAOpenCL = [](platform Platform) { if (Platform.is_host()) From 54876fc2e99b5fee400d4ead87d67aed22fe5c71 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 30 Jun 2021 12:54:47 -0700 Subject: [PATCH 22/47] Update sycl/source/detail/pi.cpp Co-authored-by: Romanov Vlad <17316488+romanovvlad@users.noreply.github.com> --- sycl/source/detail/pi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 24a06f03d13a6..b37dfd14aa0e6 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -323,7 +323,7 @@ static bool IsBannedPlatform(platform Platform) { if (Platform.is_host()) return false; - const bool HasCUDA = Platform.get_info().find( + const bool IsCUDA = Platform.get_info().find( "NVIDIA CUDA") != std::string::npos; const auto Backend = detail::getSyclObjImpl(Platform)->getPlugin().getBackend(); From 70c415bd29b03f57e87d2a9b50e5fd4f54364f39 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 30 Jun 2021 12:55:04 -0700 Subject: [PATCH 23/47] Update sycl/source/detail/pi.cpp Co-authored-by: Romanov Vlad <17316488+romanovvlad@users.noreply.github.com> --- sycl/source/detail/pi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index b37dfd14aa0e6..5f41191d2308c 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -319,7 +319,7 @@ static bool IsBannedPlatform(platform Platform) { // To avoid problems on default users and deployment of DPC++ on platforms // where CUDA is available, the NVidiaOpenCL support is disabled. // - auto IsNVIDIAOpenCL = [](platform Platform) { + auto IsNVIDIAOpenCL = [](const platform &Platform) { if (Platform.is_host()) return false; From 5d9f90085de4f0c4366b09660c7d5175b2a9d9f9 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 30 Jun 2021 12:55:41 -0700 Subject: [PATCH 24/47] Update sycl/source/detail/pi.cpp Co-authored-by: Romanov Vlad <17316488+romanovvlad@users.noreply.github.com> --- sycl/source/detail/pi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 5f41191d2308c..1c3f3752fad56 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -311,7 +311,7 @@ bool trace(TraceLevel Level) { return (TraceLevelMask & Level) == Level; } -static bool IsBannedPlatform(platform Platform) { +static bool IsBannedPlatform(const platform &Platform) { // The NVIDIA OpenCL platform is currently not compatible with DPC++ // since it is only 1.2 but gets selected by default in many systems // There is also no support on the PTX backend for OpenCL consumption, From f650faec02a2a0c2e3e9f310a58787a4e7f87955 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 30 Jun 2021 14:13:43 -0700 Subject: [PATCH 25/47] respond to feedback Signed-off-by: Byoungro So --- sycl/source/detail/config.hpp | 4 +- sycl/source/detail/device_filter.cpp | 13 ------- sycl/source/detail/pi.cpp | 42 +++++++++++++-------- sycl/unittests/allowlist/ParseAllowList.cpp | 4 +- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index dd91d24eba642..851cb88621e4f 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -186,10 +186,10 @@ static const std::array, 5> // Array is used by SYCL_DEVICE_FILTER and SYCL_DEVICE_ALLOWLIST static const std::array, 5> SyclBeMap = { - {{"host", backend::host}, - {"opencl", backend::opencl}, + {{"opencl", backend::opencl}, {"level_zero", backend::level_zero}, {"cuda", backend::cuda}, + {"host", backend::host}, {"*", backend::all}}}; template <> class SYCLConfig { diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index f1400ab96f4de..8558d0d9e4f94 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -18,19 +18,6 @@ namespace sycl { namespace detail { device_filter::device_filter(const std::string &FilterString) { - const std::array, 5> - SyclDeviceTypeMap = {{{"host", info::device_type::host}, - {"cpu", info::device_type::cpu}, - {"gpu", info::device_type::gpu}, - {"acc", info::device_type::accelerator}, - {"*", info::device_type::all}}}; - const std::array, 5> SyclBeMap = { - {{"opencl", backend::opencl}, - {"level_zero", backend::level_zero}, - {"cuda", backend::cuda}, - {"host", backend::host}, - {"*", backend::all}}}; - size_t Cursor = 0; size_t ColonPos = 0; auto findElement = [&](auto Element) { diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 32dfefe5f11c6..1f95b82cce192 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -338,8 +338,8 @@ static bool IsBannedPlatform(platform Platform) { return IsNVIDIAOpenCL(Platform); } -std::string getValue(const std::string &AllowList, size_t &Pos, - unsigned long int Size) { +std::string getAllowListValue(const std::string &AllowList, size_t &Pos, + unsigned long int Size) { size_t Prev = Pos; if ((Pos = AllowList.find("{{", Pos)) == std::string::npos) { throw sycl::runtime_error("Malformed syntax in SYCL_DEVICE_ALLOWLIST", @@ -383,7 +383,8 @@ static std::vector getAllowListDesc() { size_t Pos = 0; while (Pos < AllowList.size()) { if ((AllowList.compare(Pos, DeviceName.size(), DeviceName)) == 0) { - DecDescs.back().DevName = getValue(AllowList, Pos, DeviceName.size()); + DecDescs.back().DevName = + getAllowListValue(AllowList, Pos, DeviceName.size()); if (AllowList[Pos] == ',') { Pos++; } @@ -392,14 +393,15 @@ static std::vector getAllowListDesc() { else if ((AllowList.compare(Pos, DriverVersion.size(), DriverVersion)) == 0) { DecDescs.back().DevDriverVer = - getValue(AllowList, Pos, DriverVersion.size()); + getAllowListValue(AllowList, Pos, DriverVersion.size()); if (AllowList[Pos] == ',') { Pos++; } } else if ((AllowList.compare(Pos, PlatformName.size(), PlatformName)) == 0) { - DecDescs.back().PlatName = getValue(AllowList, Pos, PlatformName.size()); + DecDescs.back().PlatName = + getAllowListValue(AllowList, Pos, PlatformName.size()); if (AllowList[Pos] == ',') { Pos++; } @@ -408,7 +410,7 @@ static std::vector getAllowListDesc() { else if ((AllowList.compare(Pos, PlatformVersion.size(), PlatformVersion)) == 0) { DecDescs.back().PlatVer = - getValue(AllowList, Pos, PlatformVersion.size()); + getAllowListValue(AllowList, Pos, PlatformVersion.size()); } else if (AllowList.find('|', Pos) != std::string::npos) { Pos = AllowList.find('|') + 1; while (AllowList[Pos] == ' ') { @@ -571,16 +573,11 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { for (const auto &PiPlatform : PiPlatforms) { PlatformImplPtr PlatformImpl = std::make_shared(PiPlatform, Plugin); - { - const std::lock_guard Guard( - GlobalHandler::instance().getPlatformMapMutex()); - - PlatformCache.emplace_back(PlatformImpl); - } platform Platform = detail::createSyclObjFromImpl(PlatformImpl); if (IsBannedPlatform(Platform)) continue; + // get devices info::device_type DeviceType = info::device_type::all; pi_uint32 NumDevices = 0; @@ -604,10 +601,18 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { // Filter out devices that are not compatible with SYCL_DEVICE_FILTER filterDeviceFilter(PiDevices, PiPlatform, Plugin, DeviceNum); - for (const RT::PiDevice &PiDevice : PiDevices) { - std::shared_ptr Device = - PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl); - DeviceCache.emplace_back(Device); + if (PiDevices.size() != 0) { + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); + + PlatformCache.emplace_back(PlatformImpl); + const std::lock_guard DeviceCacheLock( + GlobalHandler::instance().getDeviceCacheMutex()); + for (const RT::PiDevice &PiDevice : PiDevices) { + std::shared_ptr Device = + PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl); + DeviceCache.emplace_back(Device); + } } DeviceNum += UnfilteredDeviceCount; } // end of for @@ -699,6 +704,11 @@ static void initializePlugins(std::vector *Plugins) { detail::device_filter_list *FilterList = detail::SYCLConfig::get(); if (!FilterList || FilterList->containsHost()) { + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); + const std::lock_guard DeviceCacheLock( + GlobalHandler::instance().getDeviceCacheMutex()); + std::vector &PlatformCache = GlobalHandler::instance().getPlatformCache(); std::vector &DeviceCache = diff --git a/sycl/unittests/allowlist/ParseAllowList.cpp b/sycl/unittests/allowlist/ParseAllowList.cpp index 69c816caa3c1c..b61458acd2c4f 100644 --- a/sycl/unittests/allowlist/ParseAllowList.cpp +++ b/sycl/unittests/allowlist/ParseAllowList.cpp @@ -164,10 +164,10 @@ TEST(ParseAllowListTests, CheckAllValidBackendNameValuesAreProcessed) { } sycl::detail::AllowListParsedT ActualValue = sycl::detail::parseAllowList(AllowList); - sycl::detail::AllowListParsedT ExpectedValue{{{"BackendName", "host"}}, - {{"BackendName", "opencl"}}, + sycl::detail::AllowListParsedT ExpectedValue{{{"BackendName", "opencl"}}, {{"BackendName", "level_zero"}}, {{"BackendName", "cuda"}}, + {{"BackendName", "host"}}, {{"BackendName", "*"}}}; EXPECT_EQ(ExpectedValue, ActualValue); } From 6ea7d394d995101cbbc97ecf168cd9c8b5d02a1f Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 30 Jun 2021 14:19:26 -0700 Subject: [PATCH 26/47] additional typo Signed-off-by: Byoungro So --- sycl/source/detail/pi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 7bf5f81380ee5..a2f81f2208e8e 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -327,7 +327,7 @@ static bool IsBannedPlatform(const platform &Platform) { "NVIDIA CUDA") != std::string::npos; const auto Backend = detail::getSyclObjImpl(Platform)->getPlugin().getBackend(); - const bool IsCUDAOCL = (HasCUDA && Backend == backend::opencl); + const bool IsCUDAOCL = (IsCUDA && Backend == backend::opencl); if (detail::pi::trace(detail::pi::TraceLevel::PI_TRACE_ALL) && IsCUDAOCL) { std::cout << "SYCL_PI_TRACE[all]: " << "NVIDIA CUDA OpenCL platform found but is not compatible." From 4b7b0d1a9e78327782afe1eb4392e58807c793b0 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 30 Jun 2021 15:17:16 -0700 Subject: [PATCH 27/47] fix format Signed-off-by: Byoungro So --- sycl/source/detail/pi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index a2f81f2208e8e..1949b68e3097e 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -324,7 +324,7 @@ static bool IsBannedPlatform(const platform &Platform) { return false; const bool IsCUDA = Platform.get_info().find( - "NVIDIA CUDA") != std::string::npos; + "NVIDIA CUDA") != std::string::npos; const auto Backend = detail::getSyclObjImpl(Platform)->getPlugin().getBackend(); const bool IsCUDAOCL = (IsCUDA && Backend == backend::opencl); From 10f78935cd0cc77a3bb9eb58e6aa3bc76c5cdbb1 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 1 Jul 2021 12:33:59 -0700 Subject: [PATCH 28/47] fix windows teardown Signed-off-by: Byoungro So --- sycl/source/detail/global_handler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index 0aeaeecc17c97..ba8bc73d5d017 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -173,7 +173,7 @@ void shutdown() { } #ifdef _WIN32 -BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { +extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { // Perform actions based on the reason for calling. switch (fdwReason) { case DLL_PROCESS_DETACH: From 72161681d7636e70d8399e4e43f2b44071d84581 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 1 Jul 2021 12:37:12 -0700 Subject: [PATCH 29/47] fix clang-format Signed-off-by: Byoungro So --- sycl/source/detail/global_handler.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index ba8bc73d5d017..b812de2b67cc6 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -173,7 +173,8 @@ void shutdown() { } #ifdef _WIN32 -extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { +extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, + LPVOID lpReserved) { // Perform actions based on the reason for calling. switch (fdwReason) { case DLL_PROCESS_DETACH: From d1f572d5567135975aafaeebd5fe9a380634071b Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 1 Jul 2021 16:03:35 -0700 Subject: [PATCH 30/47] fix declspec Signed-off-by: Byoungro So --- sycl/source/detail/global_handler.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index b812de2b67cc6..df6cd8aa5af6b 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -173,8 +173,8 @@ void shutdown() { } #ifdef _WIN32 -extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, - LPVOID lpReserved) { +extern "C" __declspec(dllexport) BOOL WINAPI + DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { // Perform actions based on the reason for calling. switch (fdwReason) { case DLL_PROCESS_DETACH: From b19f9695bc7330c27dca14e6baa9f0608e6f1dce Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 2 Jul 2021 10:23:47 -0700 Subject: [PATCH 31/47] use sycl export Signed-off-by: Byoungro So --- sycl/source/detail/global_handler.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index df6cd8aa5af6b..8f70457e67750 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -173,8 +173,9 @@ void shutdown() { } #ifdef _WIN32 -extern "C" __declspec(dllexport) BOOL WINAPI - DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { +extern "C" __SYCL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL, + DWORD fdwReason, + LPVOID lpReserved) { // Perform actions based on the reason for calling. switch (fdwReason) { case DLL_PROCESS_DETACH: From 028cebc63b20d6fc046a4382615f3dc74876eb62 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 6 Jul 2021 16:09:16 -0700 Subject: [PATCH 32/47] Update sycl/source/detail/pi.cpp Co-authored-by: Romanov Vlad <17316488+romanovvlad@users.noreply.github.com> --- sycl/source/detail/pi.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 1949b68e3097e..b956b8d65e699 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -602,10 +602,12 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { filterDeviceFilter(PiDevices, PiPlatform, Plugin, DeviceNum); if (PiDevices.size() != 0) { + { const std::lock_guard Guard( GlobalHandler::instance().getPlatformMapMutex()); PlatformCache.emplace_back(PlatformImpl); + } const std::lock_guard DeviceCacheLock( GlobalHandler::instance().getDeviceCacheMutex()); for (const RT::PiDevice &PiDevice : PiDevices) { From ad3518844bb9ba0a5635990a9f8cf0b58bf08bc9 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 6 Jul 2021 16:56:41 -0700 Subject: [PATCH 33/47] respond to feedback Signed-off-by: Byoungro So --- sycl/source/detail/pi.cpp | 28 +++++++++++++++------------- sycl/source/detail/platform_impl.cpp | 2 ++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index b956b8d65e699..a6e31262703ea 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -602,12 +602,12 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { filterDeviceFilter(PiDevices, PiPlatform, Plugin, DeviceNum); if (PiDevices.size() != 0) { - { - const std::lock_guard Guard( - GlobalHandler::instance().getPlatformMapMutex()); + { + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); - PlatformCache.emplace_back(PlatformImpl); - } + PlatformCache.emplace_back(PlatformImpl); + } const std::lock_guard DeviceCacheLock( GlobalHandler::instance().getDeviceCacheMutex()); for (const RT::PiDevice &PiDevice : PiDevices) { @@ -706,17 +706,19 @@ static void initializePlugins(std::vector *Plugins) { detail::device_filter_list *FilterList = detail::SYCLConfig::get(); if (!FilterList || FilterList->containsHost()) { - const std::lock_guard Guard( - GlobalHandler::instance().getPlatformMapMutex()); - const std::lock_guard DeviceCacheLock( - GlobalHandler::instance().getDeviceCacheMutex()); + { + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); + std::vector &PlatformCache = + GlobalHandler::instance().getPlatformCache(); + PlatformImplPtr PlatformImpl = platform_impl::getHostPlatformImpl(); + PlatformCache.emplace_back(PlatformImpl); + } - std::vector &PlatformCache = - GlobalHandler::instance().getPlatformCache(); std::vector &DeviceCache = GlobalHandler::instance().getDeviceCache(); - PlatformImplPtr PlatformImpl = platform_impl::getHostPlatformImpl(); - PlatformCache.emplace_back(PlatformImpl); + const std::lock_guard DeviceCacheLock( + GlobalHandler::instance().getDeviceCacheMutex()); std::shared_ptr Device = platform_impl::getOrMakeHostDeviceImpl(); DeviceCache.emplace_back(Device); diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 508fb0082cb6e..f4929f09373a8 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -77,6 +77,8 @@ PlatformImplPtr platform_impl::getPlatformFromPiDevice(RT::PiDevice PiDevice, std::vector platform_impl::get_platforms() { std::vector Platforms; RT::initialize(); + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); std::vector &PlatformCache = GlobalHandler::instance().getPlatformCache(); for (const PlatformImplPtr &PlatformImpl : PlatformCache) { From ef1946733254c04f8c20ce1202a29cabeddf3067 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Thu, 15 Jul 2021 23:21:59 -0700 Subject: [PATCH 34/47] fix clang-format Signed-off-by: Byoungro So --- sycl/unittests/allowlist/ParseAllowList.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sycl/unittests/allowlist/ParseAllowList.cpp b/sycl/unittests/allowlist/ParseAllowList.cpp index b215a843e215c..5f400cd1c872a 100644 --- a/sycl/unittests/allowlist/ParseAllowList.cpp +++ b/sycl/unittests/allowlist/ParseAllowList.cpp @@ -165,9 +165,9 @@ TEST(ParseAllowListTests, CheckAllValidBackendNameValuesAreProcessed) { sycl::detail::AllowListParsedT ActualValue = sycl::detail::parseAllowList(AllowList); sycl::detail::AllowListParsedT ExpectedValue{ - {{"BackendName", "opencl"}}, {{"BackendName", "level_zero"}}, - {{"BackendName", "cuda"}}, {{"BackendName", "rocm"}}, - {{"BackendName", "host"}}, {{"BackendName", "*"}}}; + {{"BackendName", "opencl"}}, {{"BackendName", "level_zero"}}, + {{"BackendName", "cuda"}}, {{"BackendName", "rocm"}}, + {{"BackendName", "host"}}, {{"BackendName", "*"}}}; EXPECT_EQ(ExpectedValue, ActualValue); } From eadb8489f3e311141ff49620c9da9c10dee9c4b0 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 16 Jul 2021 20:54:41 -0700 Subject: [PATCH 35/47] merge two caches for device/platform Signed-off-by: Byoungro So --- sycl/source/detail/global_handler.cpp | 14 ++---- sycl/source/detail/global_handler.hpp | 9 ++-- sycl/source/detail/pi.cpp | 42 +++++++---------- sycl/source/detail/platform_impl.cpp | 65 ++++++++++++++------------- sycl/source/detail/platform_impl.hpp | 6 --- 5 files changed, 56 insertions(+), 80 deletions(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index e32c6dab8cc36..48f2794801976 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -52,8 +52,8 @@ ProgramManager &GlobalHandler::getProgramManager() { Sync &GlobalHandler::getSync() { return getOrCreate(MSync); } -std::vector &GlobalHandler::getPlatformCache() { - return getOrCreate(MPlatformCache); +std::map> &GlobalHandler::getPlatformDeviceCache() { + return getOrCreate(MPlatformDeviceCache); } std::mutex &GlobalHandler::getPlatformMapMutex() { @@ -75,19 +75,11 @@ std::mutex &GlobalHandler::getHandlerExtendedMembersMutex() { return getOrCreate(MHandlerExtendedMembersMutex); } -std::vector &GlobalHandler::getDeviceCache() { - return getOrCreate(MDeviceCache); -} -std::mutex &GlobalHandler::getDeviceCacheMutex() { - return getOrCreate(MDeviceCacheMutex); -} - void shutdown() { // First, release resources, that may access plugins. GlobalHandler::instance().MScheduler.Inst.reset(nullptr); GlobalHandler::instance().MProgramManager.Inst.reset(nullptr); - GlobalHandler::instance().MPlatformCache.Inst.reset(nullptr); - GlobalHandler::instance().MDeviceCache.Inst.reset(nullptr); + GlobalHandler::instance().MPlatformDeviceCache.Inst.reset(nullptr); // Call to GlobalHandler::instance().getPlugins() initializes plugins. If // user application has loaded SYCL runtime, and never called any APIs, diff --git a/sycl/source/detail/global_handler.hpp b/sycl/source/detail/global_handler.hpp index c014499acbf61..47437a889c063 100644 --- a/sycl/source/detail/global_handler.hpp +++ b/sycl/source/detail/global_handler.hpp @@ -12,6 +12,7 @@ #include #include +#include __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { @@ -53,14 +54,12 @@ class GlobalHandler { Scheduler &getScheduler(); ProgramManager &getProgramManager(); Sync &getSync(); - std::vector &getPlatformCache(); + std::map> &getPlatformDeviceCache(); std::mutex &getPlatformMapMutex(); std::mutex &getFilterMutex(); std::vector &getPlugins(); device_filter_list &getDeviceFilterList(const std::string &InitValue); std::mutex &getHandlerExtendedMembersMutex(); - std::vector &getDeviceCache(); - std::mutex &getDeviceCacheMutex(); private: friend void shutdown(); @@ -81,15 +80,13 @@ class GlobalHandler { InstWithLock MScheduler; InstWithLock MProgramManager; InstWithLock MSync; - InstWithLock> MPlatformCache; + InstWithLock>> MPlatformDeviceCache; InstWithLock MPlatformMapMutex; InstWithLock MFilterMutex; InstWithLock> MPlugins; InstWithLock MDeviceFilterList; // The mutex for synchronizing accesses to handlers extended members InstWithLock MHandlerExtendedMembersMutex; - InstWithLock> MDeviceCache; - InstWithLock MDeviceCacheMutex; }; } // namespace detail } // namespace sycl diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 9be4decb872dd..6395a32e42d45 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -564,10 +564,8 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { NumPlatforms, PiPlatforms.data(), nullptr) != PI_SUCCESS) return; - std::vector &PlatformCache = - GlobalHandler::instance().getPlatformCache(); - std::vector &DeviceCache = - GlobalHandler::instance().getDeviceCache(); + std::map> &PlatformDeviceCache = + GlobalHandler::instance().getPlatformDeviceCache(); int DeviceNum = 0; for (const auto &PiPlatform : PiPlatforms) { @@ -602,19 +600,16 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { filterDeviceFilter(PiDevices, PiPlatform, Plugin, DeviceNum); if (PiDevices.size() != 0) { - { - const std::lock_guard Guard( - GlobalHandler::instance().getPlatformMapMutex()); - - PlatformCache.emplace_back(PlatformImpl); - } - const std::lock_guard DeviceCacheLock( - GlobalHandler::instance().getDeviceCacheMutex()); + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); + std::vector DeviceCache; for (const RT::PiDevice &PiDevice : PiDevices) { std::shared_ptr Device = PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl); DeviceCache.emplace_back(Device); } + + PlatformDeviceCache[PlatformImpl] = DeviceCache; } DeviceNum += UnfilteredDeviceCount; } // end of for @@ -706,22 +701,15 @@ static void initializePlugins(std::vector *Plugins) { detail::device_filter_list *FilterList = detail::SYCLConfig::get(); if (!FilterList || FilterList->containsHost()) { - { - const std::lock_guard Guard( - GlobalHandler::instance().getPlatformMapMutex()); - std::vector &PlatformCache = - GlobalHandler::instance().getPlatformCache(); - PlatformImplPtr PlatformImpl = platform_impl::getHostPlatformImpl(); - PlatformCache.emplace_back(PlatformImpl); - } - std::vector &DeviceCache = - GlobalHandler::instance().getDeviceCache(); - const std::lock_guard DeviceCacheLock( - GlobalHandler::instance().getDeviceCacheMutex()); - std::shared_ptr Device = - platform_impl::getOrMakeHostDeviceImpl(); - DeviceCache.emplace_back(Device); + std::map> &PlatformDeviceCache = + GlobalHandler::instance().getPlatformDeviceCache(); + PlatformImplPtr PlatformImpl = platform_impl::getHostPlatformImpl(); + DeviceImplPtr Device = std::make_shared(); + std::vector DeviceCache{Device}; + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); + PlatformDeviceCache[PlatformImpl] = DeviceCache; } #ifdef XPTI_ENABLE_INSTRUMENTATION if (!(xptiTraceEnabled() && !XPTIInitDone)) diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index f4929f09373a8..0245f800c0c40 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -31,16 +31,6 @@ PlatformImplPtr platform_impl::getHostPlatformImpl() { return HostImpl; } -std::shared_ptr platform_impl::getOrMakeHostDeviceImpl() { - PlatformImplPtr HostImpl = getHostPlatformImpl(); - if (HostImpl->MDeviceCache.size() == 0) { - std::shared_ptr DeviceSP = std::make_shared(); - HostImpl->MDeviceCache.emplace_back(DeviceSP); - return DeviceSP; - } - return HostImpl->MDeviceCache[0].lock(); -} - PlatformImplPtr platform_impl::getOrMakePlatformImpl(RT::PiPlatform PiPlatform, const plugin &Plugin) { PlatformImplPtr Result; @@ -48,18 +38,18 @@ PlatformImplPtr platform_impl::getOrMakePlatformImpl(RT::PiPlatform PiPlatform, const std::lock_guard Guard( GlobalHandler::instance().getPlatformMapMutex()); - std::vector &PlatformCache = - GlobalHandler::instance().getPlatformCache(); + std::map> &PlatformDeviceCache = + GlobalHandler::instance().getPlatformDeviceCache(); // If we've already seen this platform, return the impl - for (const auto &PlatImpl : PlatformCache) { - if (PlatImpl->getHandleRef() == PiPlatform) - return PlatImpl; + for (const auto &Pair : PlatformDeviceCache) { + if (Pair.first->getHandleRef() == PiPlatform) + return Pair.first; } // Otherwise make the impl Result = std::make_shared(PiPlatform, Plugin); - PlatformCache.emplace_back(Result); + PlatformDeviceCache[Result] = std::vector(); } return Result; @@ -79,41 +69,56 @@ std::vector platform_impl::get_platforms() { RT::initialize(); const std::lock_guard Guard( GlobalHandler::instance().getPlatformMapMutex()); - std::vector &PlatformCache = - GlobalHandler::instance().getPlatformCache(); - for (const PlatformImplPtr &PlatformImpl : PlatformCache) { - platform Platform = detail::createSyclObjFromImpl(PlatformImpl); + std::map> &PlatformDeviceCache = + GlobalHandler::instance().getPlatformDeviceCache(); + for (const auto &Pair : PlatformDeviceCache) { + platform Platform = detail::createSyclObjFromImpl(Pair.first); Platforms.push_back(Platform); } return Platforms; } -std::shared_ptr platform_impl::getOrMakeDeviceImpl( +DeviceImplPtr platform_impl::getOrMakeDeviceImpl( RT::PiDevice PiDevice, const std::shared_ptr &PlatformImpl) { - const std::lock_guard Guard(MDeviceMapMutex); + + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); + std::map> &PlatformDeviceCache = + GlobalHandler::instance().getPlatformDeviceCache(); // If we've already seen this device, return the impl - for (const std::weak_ptr &DeviceWP : MDeviceCache) { - if (std::shared_ptr Device = DeviceWP.lock()) { - if (Device->getHandleRef() == PiDevice) - return Device; - } + std::vector& DeviceCache = PlatformDeviceCache[PlatformImpl]; + for (const DeviceImplPtr &Device : DeviceCache) { + if (Device->getHandleRef() == PiDevice) + return Device; } // Otherwise make the impl std::shared_ptr Result = std::make_shared(PiDevice, PlatformImpl); - MDeviceCache.emplace_back(Result); + DeviceCache.emplace_back(Result); return Result; } std::vector platform_impl::get_devices(info::device_type DeviceType) const { std::vector Res; - for (const std::weak_ptr &DeviceWP : MDeviceCache) { + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); + std::map> &PlatformDeviceCache = + GlobalHandler::instance().getPlatformDeviceCache(); + + // If we've already seen this device, return the impl + PlatformImplPtr Platform = nullptr; + for (const auto &Pair : PlatformDeviceCache) { + if (Pair.first.get() == this) { + Platform = Pair.first; + } + } + std::vector& DeviceCache = PlatformDeviceCache[Platform]; + for (const DeviceImplPtr &Device : DeviceCache) { // Assumption here is that there is 1-to-1 mapping between PiDevType and // Sycl device type for GPU, CPU, and ACC. - std::shared_ptr Device = DeviceWP.lock(); info::device_type PiDeviceType = pi::cast(Device->get_device_type()); if (DeviceType == info::device_type::all || DeviceType == PiDeviceType) diff --git a/sycl/source/detail/platform_impl.hpp b/sycl/source/detail/platform_impl.hpp index c8ae03c5250a6..c04eb290b04af 100644 --- a/sycl/source/detail/platform_impl.hpp +++ b/sycl/source/detail/platform_impl.hpp @@ -158,10 +158,6 @@ class platform_impl { /// \return the host platform impl static std::shared_ptr getHostPlatformImpl(); - /// Get a Host device into from the Host platform impl - /// If not created, create one and add it to MDeviceCache - static std::shared_ptr getOrMakeHostDeviceImpl(); - /// Queries the cache to see if the specified PiPlatform has been seen /// before. If so, return the cached platform_impl, otherwise create a new /// one and cache it. @@ -188,8 +184,6 @@ class platform_impl { bool MHostPlatform = false; RT::PiPlatform MPlatform = 0; std::shared_ptr MPlugin; - std::vector> MDeviceCache; - std::mutex MDeviceMapMutex; }; } // namespace detail From 317387f8eafe5de7739efa1ef70de9a499a3651d Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 16 Jul 2021 20:55:21 -0700 Subject: [PATCH 36/47] clang-format Signed-off-by: Byoungro So --- sycl/source/detail/global_handler.cpp | 3 ++- sycl/source/detail/global_handler.hpp | 8 +++++--- sycl/source/detail/pi.cpp | 2 +- sycl/source/detail/platform_impl.cpp | 4 ++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index 48f2794801976..c60086735ebc4 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -52,7 +52,8 @@ ProgramManager &GlobalHandler::getProgramManager() { Sync &GlobalHandler::getSync() { return getOrCreate(MSync); } -std::map> &GlobalHandler::getPlatformDeviceCache() { +std::map> & +GlobalHandler::getPlatformDeviceCache() { return getOrCreate(MPlatformDeviceCache); } diff --git a/sycl/source/detail/global_handler.hpp b/sycl/source/detail/global_handler.hpp index 47437a889c063..3d43367078ea3 100644 --- a/sycl/source/detail/global_handler.hpp +++ b/sycl/source/detail/global_handler.hpp @@ -11,8 +11,8 @@ #include #include -#include #include +#include __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { @@ -54,7 +54,8 @@ class GlobalHandler { Scheduler &getScheduler(); ProgramManager &getProgramManager(); Sync &getSync(); - std::map> &getPlatformDeviceCache(); + std::map> & + getPlatformDeviceCache(); std::mutex &getPlatformMapMutex(); std::mutex &getFilterMutex(); std::vector &getPlugins(); @@ -80,7 +81,8 @@ class GlobalHandler { InstWithLock MScheduler; InstWithLock MProgramManager; InstWithLock MSync; - InstWithLock>> MPlatformDeviceCache; + InstWithLock>> + MPlatformDeviceCache; InstWithLock MPlatformMapMutex; InstWithLock MFilterMutex; InstWithLock> MPlugins; diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 6395a32e42d45..cbaeaea6f2f51 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -608,7 +608,7 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl); DeviceCache.emplace_back(Device); } - + PlatformDeviceCache[PlatformImpl] = DeviceCache; } DeviceNum += UnfilteredDeviceCount; diff --git a/sycl/source/detail/platform_impl.cpp b/sycl/source/detail/platform_impl.cpp index 0245f800c0c40..2ab263cd6ef85 100644 --- a/sycl/source/detail/platform_impl.cpp +++ b/sycl/source/detail/platform_impl.cpp @@ -87,7 +87,7 @@ DeviceImplPtr platform_impl::getOrMakeDeviceImpl( GlobalHandler::instance().getPlatformDeviceCache(); // If we've already seen this device, return the impl - std::vector& DeviceCache = PlatformDeviceCache[PlatformImpl]; + std::vector &DeviceCache = PlatformDeviceCache[PlatformImpl]; for (const DeviceImplPtr &Device : DeviceCache) { if (Device->getHandleRef() == PiDevice) return Device; @@ -115,7 +115,7 @@ platform_impl::get_devices(info::device_type DeviceType) const { Platform = Pair.first; } } - std::vector& DeviceCache = PlatformDeviceCache[Platform]; + std::vector &DeviceCache = PlatformDeviceCache[Platform]; for (const DeviceImplPtr &Device : DeviceCache) { // Assumption here is that there is 1-to-1 mapping between PiDevType and // Sycl device type for GPU, CPU, and ACC. From 8751ad8b04e805a35af9fdd01cbe7f5ec27719fb Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 16 Jul 2021 21:41:23 -0700 Subject: [PATCH 37/47] fix deadlock Signed-off-by: Byoungro So --- sycl/source/detail/pi.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index 1aa99d10a9155..dfb716dbd197d 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -652,12 +652,12 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { filterDeviceFilter(PiDevices, PiPlatform, Plugin, DeviceNum); if (PiDevices.size() != 0) { - const std::lock_guard Guard( - GlobalHandler::instance().getPlatformMapMutex()); std::vector DeviceCache; for (const RT::PiDevice &PiDevice : PiDevices) { std::shared_ptr Device = PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl); + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); DeviceCache.emplace_back(Device); } From e8be632342f01a1f141f0d0917ffc63a8501446f Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Sat, 17 Jul 2021 18:23:57 -0700 Subject: [PATCH 38/47] optimize device_impl Signed-off-by: Byoungro So --- sycl/source/detail/pi.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sycl/source/detail/pi.cpp b/sycl/source/detail/pi.cpp index dfb716dbd197d..ff74f168a4414 100644 --- a/sycl/source/detail/pi.cpp +++ b/sycl/source/detail/pi.cpp @@ -653,14 +653,13 @@ void fillPlatformAndDeviceCache(plugin &Plugin) { if (PiDevices.size() != 0) { std::vector DeviceCache; + const std::lock_guard Guard( + GlobalHandler::instance().getPlatformMapMutex()); for (const RT::PiDevice &PiDevice : PiDevices) { std::shared_ptr Device = - PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl); - const std::lock_guard Guard( - GlobalHandler::instance().getPlatformMapMutex()); + std::make_shared(PiDevice, PlatformImpl); DeviceCache.emplace_back(Device); } - PlatformDeviceCache[PlatformImpl] = DeviceCache; } DeviceNum += UnfilteredDeviceCount; From e8a26da2107faf9f8136368bf9d92f08a29e170b Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 19 Jul 2021 23:05:44 -0700 Subject: [PATCH 39/47] trial revert of windows fix Signed-off-by: Byoungro So --- sycl/source/detail/global_handler.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index c60086735ebc4..8e8fcfc775e73 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -102,9 +102,7 @@ void shutdown() { } #ifdef _WIN32 -extern "C" __SYCL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL, - DWORD fdwReason, - LPVOID lpReserved) { +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { // Perform actions based on the reason for calling. switch (fdwReason) { case DLL_PROCESS_DETACH: From d3b9cdacead591ea63c0e9a1f7fc120968111786 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 23 Jul 2021 17:57:02 -0700 Subject: [PATCH 40/47] tokenize filter Signed-off-by: Byoungro So --- sycl/include/CL/sycl/detail/device_filter.hpp | 3 + sycl/source/detail/device_filter.cpp | 56 +++++++++++++------ sycl/source/detail/filter_selector_impl.cpp | 27 +-------- 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/sycl/include/CL/sycl/detail/device_filter.hpp b/sycl/include/CL/sycl/detail/device_filter.hpp index 746f7714e21a0..813c22282fb14 100644 --- a/sycl/include/CL/sycl/detail/device_filter.hpp +++ b/sycl/include/CL/sycl/detail/device_filter.hpp @@ -82,6 +82,9 @@ inline std::ostream &operator<<(std::ostream &Out, return Out; } +std::vector tokenize(const std::string &Filter, + const std::string &Delim); + } // namespace detail } // namespace sycl } // __SYCL_INLINE_NAMESPACE(cl) diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index 3b0847d105571..f35b452d7eb8e 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -17,19 +17,44 @@ __SYCL_INLINE_NAMESPACE(cl) { namespace sycl { namespace detail { +std::vector tokenize(const std::string &Filter, + const std::string &Delim) { + std::vector Tokens; + size_t Pos = 0; + std::string Input = Filter; + std::string Tok; + + while ((Pos = Input.find(Delim)) != std::string::npos) { + Tok = Input.substr(0, Pos); + Input.erase(0, Pos + Delim.length()); + + if (!Tok.empty()) { + Tokens.push_back(std::move(Tok)); + } + } + + // Add remainder + if (!Input.empty()) + Tokens.push_back(std::move(Input)); + + return Tokens; +} + device_filter::device_filter(const std::string &FilterString) { - size_t Cursor = 0; - size_t ColonPos = 0; + std::string SubString; + auto findElement = [&](auto Element) { - size_t Found = FilterString.find(Element.first, Cursor); + size_t Found = SubString.find(Element.first); if (Found == std::string::npos) return false; - Cursor = Found; return true; }; // Handle the optional 1st field of the filter, backend // Check if the first entry matches with a known backend type + std::vector Tokens = tokenize(FilterString, ":"); + size_t i = 0; + SubString = Tokens[i]; auto It = std::find_if(std::begin(SyclBeMap), std::end(SyclBeMap), findElement); // If no match is found, set the backend type backend::all @@ -38,15 +63,14 @@ device_filter::device_filter(const std::string &FilterString) { Backend = backend::all; else { Backend = It->second; - ColonPos = FilterString.find(":", Cursor); - if (ColonPos != std::string::npos) - Cursor = ColonPos + 1; - else - Cursor = Cursor + It->first.size(); + i++; + if (i < Tokens.size()) + SubString = Tokens[i]; } + // Handle the optional 2nd field of the filter - device type. // Check if the 2nd entry matches with any known device type. - if (Cursor >= FilterString.size()) { + if (i >= Tokens.size()) { DeviceType = info::device_type::all; } else { auto Iter = std::find_if(std::begin(SyclDeviceTypeMap), @@ -57,20 +81,18 @@ device_filter::device_filter(const std::string &FilterString) { 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(); + i++; + if (i < Tokens.size()) + SubString = Tokens[i]; } } // 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()) { + if (i < Tokens.size()) { try { - DeviceNum = stoi(FilterString.substr(Cursor)); + DeviceNum = stoi(SubString); HasDeviceNum = true; } catch (...) { std::string Message = diff --git a/sycl/source/detail/filter_selector_impl.cpp b/sycl/source/detail/filter_selector_impl.cpp index a1e35d838b276..fa78bcf55d12e 100644 --- a/sycl/source/detail/filter_selector_impl.cpp +++ b/sycl/source/detail/filter_selector_impl.cpp @@ -24,35 +24,12 @@ namespace sycl { namespace ONEAPI { namespace detail { -std::vector tokenize(const std::string &Filter, - const std::string &Delim) { - std::vector Tokens; - size_t Pos = 0; - std::string Input = Filter; - std::string Tok; - - while ((Pos = Input.find(Delim)) != std::string::npos) { - Tok = Input.substr(0, Pos); - Input.erase(0, Pos + Delim.length()); - - if (!Tok.empty()) { - Tokens.push_back(std::move(Tok)); - } - } - - // Add remainder - if (!Input.empty()) - Tokens.push_back(std::move(Input)); - - return Tokens; -} - filter create_filter(const std::string &Input) { filter Result; constexpr auto Error = "Invalid filter string! Valid strings conform to " "BE:DeviceType:DeviceNum, where any are optional"; - std::vector Tokens = tokenize(Input, ":"); + std::vector Tokens = sycl::detail::tokenize(Input, ":"); std::regex IntegerExpr("[[:digit:]]+"); // There should only be up to 3 tokens. @@ -106,7 +83,7 @@ filter create_filter(const std::string &Input) { filter_selector_impl::filter_selector_impl(const std::string &Input) : mFilters(), mRanker(), mNumDevicesSeen(0), mMatchFound(false) { - std::vector Filters = detail::tokenize(Input, ","); + std::vector Filters = sycl::detail::tokenize(Input, ","); mNumTotalDevices = device::get_devices().size(); for (const std::string &Filter : Filters) { From 1dc38dffe7d5a2b54f6c3926639143318c17b0c8 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Sun, 25 Jul 2021 14:26:38 -0700 Subject: [PATCH 41/47] try to disable cleaning up device cache Signed-off-by: Byoungro So --- sycl/source/detail/global_handler.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index 8e8fcfc775e73..9231342172f11 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -80,7 +80,11 @@ void shutdown() { // First, release resources, that may access plugins. GlobalHandler::instance().MScheduler.Inst.reset(nullptr); GlobalHandler::instance().MProgramManager.Inst.reset(nullptr); +#ifndef _WIN32 + // HACK: there is a problem of L0 driver getting unloaded prematurely on + // Windows. This causes crash while destroying devices in the cache. GlobalHandler::instance().MPlatformDeviceCache.Inst.reset(nullptr); +#endif // Call to GlobalHandler::instance().getPlugins() initializes plugins. If // user application has loaded SYCL runtime, and never called any APIs, @@ -102,7 +106,9 @@ void shutdown() { } #ifdef _WIN32 -BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { +extern "C" __SYCL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL, + DWORD fdwReason, + LPVOID lpReserved) { // Perform actions based on the reason for calling. switch (fdwReason) { case DLL_PROCESS_DETACH: From d2e967a4d35f6f2c7bb43ae377f4e5a4b9a58fab Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Sun, 25 Jul 2021 16:44:58 -0700 Subject: [PATCH 42/47] revert Signed-off-by: Byoungro So --- sycl/source/detail/global_handler.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index 9231342172f11..8e8fcfc775e73 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -80,11 +80,7 @@ void shutdown() { // First, release resources, that may access plugins. GlobalHandler::instance().MScheduler.Inst.reset(nullptr); GlobalHandler::instance().MProgramManager.Inst.reset(nullptr); -#ifndef _WIN32 - // HACK: there is a problem of L0 driver getting unloaded prematurely on - // Windows. This causes crash while destroying devices in the cache. GlobalHandler::instance().MPlatformDeviceCache.Inst.reset(nullptr); -#endif // Call to GlobalHandler::instance().getPlugins() initializes plugins. If // user application has loaded SYCL runtime, and never called any APIs, @@ -106,9 +102,7 @@ void shutdown() { } #ifdef _WIN32 -extern "C" __SYCL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL, - DWORD fdwReason, - LPVOID lpReserved) { +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { // Perform actions based on the reason for calling. switch (fdwReason) { case DLL_PROCESS_DETACH: From cc428ff06cbd0c6329fbda2c61b289283adf85dc Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 26 Jul 2021 12:12:49 -0700 Subject: [PATCH 43/47] Update sycl/source/detail/device_filter.cpp Co-authored-by: Romanov Vlad --- sycl/source/detail/device_filter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index f35b452d7eb8e..07298f1007c96 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -53,7 +53,7 @@ device_filter::device_filter(const std::string &FilterString) { // Handle the optional 1st field of the filter, backend // Check if the first entry matches with a known backend type std::vector Tokens = tokenize(FilterString, ":"); - size_t i = 0; + size_t I = 0; SubString = Tokens[i]; auto It = std::find_if(std::begin(SyclBeMap), std::end(SyclBeMap), findElement); From 0a4c4fc93fa4c7c367446fbc0439eccc589d960a Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 26 Jul 2021 12:12:56 -0700 Subject: [PATCH 44/47] Update sycl/source/detail/device_filter.cpp Co-authored-by: Romanov Vlad --- sycl/source/detail/device_filter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index 07298f1007c96..551cd3161906d 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -43,7 +43,7 @@ std::vector tokenize(const std::string &Filter, device_filter::device_filter(const std::string &FilterString) { std::string SubString; - auto findElement = [&](auto Element) { + auto FindElement = [&](auto Element) { size_t Found = SubString.find(Element.first); if (Found == std::string::npos) return false; From ffa38139292449b690e59f28596fc766b9cbc792 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 26 Jul 2021 12:34:09 -0700 Subject: [PATCH 45/47] feedback Signed-off-by: Byoungro So --- sycl/source/detail/device_filter.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/sycl/source/detail/device_filter.cpp b/sycl/source/detail/device_filter.cpp index 551cd3161906d..49fa95c6921a9 100644 --- a/sycl/source/detail/device_filter.cpp +++ b/sycl/source/detail/device_filter.cpp @@ -41,10 +41,11 @@ std::vector tokenize(const std::string &Filter, } device_filter::device_filter(const std::string &FilterString) { - std::string SubString; + std::vector Tokens = tokenize(FilterString, ":"); + size_t I = 0; auto FindElement = [&](auto Element) { - size_t Found = SubString.find(Element.first); + size_t Found = Tokens[I].find(Element.first); if (Found == std::string::npos) return false; return true; @@ -52,47 +53,40 @@ device_filter::device_filter(const std::string &FilterString) { // Handle the optional 1st field of the filter, backend // Check if the first entry matches with a known backend type - std::vector Tokens = tokenize(FilterString, ":"); - size_t I = 0; - SubString = Tokens[i]; auto It = - std::find_if(std::begin(SyclBeMap), std::end(SyclBeMap), findElement); + 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. if (It == SyclBeMap.end()) Backend = backend::all; else { Backend = It->second; - i++; - if (i < Tokens.size()) - SubString = Tokens[i]; + I++; } // Handle the optional 2nd field of the filter - device type. // Check if the 2nd entry matches with any known device type. - if (i >= Tokens.size()) { + if (I >= Tokens.size()) { DeviceType = info::device_type::all; } else { auto Iter = std::find_if(std::begin(SyclDeviceTypeMap), - std::end(SyclDeviceTypeMap), findElement); + 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; - i++; - if (i < Tokens.size()) - SubString = Tokens[i]; + I++; } } // 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 (i < Tokens.size()) { + if (I < Tokens.size()) { try { - DeviceNum = stoi(SubString); + DeviceNum = stoi(Tokens[I]); HasDeviceNum = true; } catch (...) { std::string Message = From 19de5f41840274c5e5634a9ebf1ba8bccb5ab1e1 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Wed, 28 Jul 2021 18:22:38 -0700 Subject: [PATCH 46/47] trial tests Signed-off-by: Byoungro So --- .../sycl-ls-unique-device-id-level-zero.cpp | 14 ++++++++++++++ .../regression/sycl-ls-unique-device-id-opencl.cpp | 14 ++++++++++++++ sycl/tools/sycl-ls/CMakeLists.txt | 9 ++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 sycl/test/regression/sycl-ls-unique-device-id-level-zero.cpp create mode 100644 sycl/test/regression/sycl-ls-unique-device-id-opencl.cpp diff --git a/sycl/test/regression/sycl-ls-unique-device-id-level-zero.cpp b/sycl/test/regression/sycl-ls-unique-device-id-level-zero.cpp new file mode 100644 index 0000000000000..ce72f58daa655 --- /dev/null +++ b/sycl/test/regression/sycl-ls-unique-device-id-level-zero.cpp @@ -0,0 +1,14 @@ +// REQUIRES: level_zero + +// RUN: env SYCL_DEVICE_FILTER=level_zero sycl-ls | FileCheck %s --check-prefixes=CHECK-OPENCL + +// CHECK-OPENCL-COUNT-1: [level_zero:{{.*}}:0] + +//==-- sycl-ls-unique-device-id-level-zero.cpp - SYCL test for unique device id +//--===// +// +// 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 +// +//===----------------------------------------------------------------------===// \ No newline at end of file diff --git a/sycl/test/regression/sycl-ls-unique-device-id-opencl.cpp b/sycl/test/regression/sycl-ls-unique-device-id-opencl.cpp new file mode 100644 index 0000000000000..c5b904b810cb6 --- /dev/null +++ b/sycl/test/regression/sycl-ls-unique-device-id-opencl.cpp @@ -0,0 +1,14 @@ +// REQUIRES: opencl + +// RUN: env SYCL_DEVICE_FILTER=opencl sycl-ls | FileCheck %s --check-prefixes=CHECK-OPENCL + +// CHECK-OPENCL-COUNT-1: [opencl:{{.*}}:0] + +//==-- sycl-ls-unique-device-id-opencl.cpp - SYCL test for unique device id +//--===// +// +// 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 +// +//===----------------------------------------------------------------------===// \ No newline at end of file diff --git a/sycl/tools/sycl-ls/CMakeLists.txt b/sycl/tools/sycl-ls/CMakeLists.txt index a409db2d79d5f..f5909cf49d1c5 100644 --- a/sycl/tools/sycl-ls/CMakeLists.txt +++ b/sycl/tools/sycl-ls/CMakeLists.txt @@ -1,9 +1,16 @@ add_executable(sycl-ls sycl-ls.cpp) add_dependencies(sycl-ls sycl) target_include_directories(sycl-ls PRIVATE "${sycl_inc_dir}") + +set(sycl_lib sycl) +string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type_lower) +if (WIN32 AND "${build_type_lower}" MATCHES "debug") + set(sycl_lib sycld) +endif() + target_link_libraries(sycl-ls PRIVATE - sycl + ${sycl_lib} OpenCL-Headers ) install(TARGETS sycl-ls From 7f02fc3c899426eb38b608f228188663fa8275ce Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Fri, 30 Jul 2021 19:23:49 -0700 Subject: [PATCH 47/47] try Windows shutdown Signed-off-by: Byoungro So --- sycl/source/detail/global_handler.cpp | 4 +++- sycl/test/regression/sycl-ls-unique-device-id-level-zero.cpp | 5 +++-- sycl/test/regression/sycl-ls-unique-device-id-opencl.cpp | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/sycl/source/detail/global_handler.cpp b/sycl/source/detail/global_handler.cpp index 8e8fcfc775e73..c60086735ebc4 100644 --- a/sycl/source/detail/global_handler.cpp +++ b/sycl/source/detail/global_handler.cpp @@ -102,7 +102,9 @@ void shutdown() { } #ifdef _WIN32 -BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { +extern "C" __SYCL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL, + DWORD fdwReason, + LPVOID lpReserved) { // Perform actions based on the reason for calling. switch (fdwReason) { case DLL_PROCESS_DETACH: diff --git a/sycl/test/regression/sycl-ls-unique-device-id-level-zero.cpp b/sycl/test/regression/sycl-ls-unique-device-id-level-zero.cpp index ce72f58daa655..0b609156a919e 100644 --- a/sycl/test/regression/sycl-ls-unique-device-id-level-zero.cpp +++ b/sycl/test/regression/sycl-ls-unique-device-id-level-zero.cpp @@ -1,8 +1,9 @@ // REQUIRES: level_zero -// RUN: env SYCL_DEVICE_FILTER=level_zero sycl-ls | FileCheck %s --check-prefixes=CHECK-OPENCL +// RUN: env SYCL_DEVICE_FILTER=level_zero sycl-ls | FileCheck %s --check-prefixes=CHECK-LEVELZERO -// CHECK-OPENCL-COUNT-1: [level_zero:{{.*}}:0] +// CHECK-LEVELZERO-COUNT-1: [level_zero:{{.*}}:0] +// CHECK-LEVELZERO-NOT: [level_zero:{{.*}}:0] //==-- sycl-ls-unique-device-id-level-zero.cpp - SYCL test for unique device id //--===// diff --git a/sycl/test/regression/sycl-ls-unique-device-id-opencl.cpp b/sycl/test/regression/sycl-ls-unique-device-id-opencl.cpp index c5b904b810cb6..599187b3e3365 100644 --- a/sycl/test/regression/sycl-ls-unique-device-id-opencl.cpp +++ b/sycl/test/regression/sycl-ls-unique-device-id-opencl.cpp @@ -3,6 +3,7 @@ // RUN: env SYCL_DEVICE_FILTER=opencl sycl-ls | FileCheck %s --check-prefixes=CHECK-OPENCL // CHECK-OPENCL-COUNT-1: [opencl:{{.*}}:0] +// CHECK-OPENCL-NOT: [opencl:{{.*}}:0] //==-- sycl-ls-unique-device-id-opencl.cpp - SYCL test for unique device id //--===//