From 89b0badd481039cda1e28282ed311bd00256a077 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 13 Sep 2021 16:40:28 -0700 Subject: [PATCH 1/5] [SYCL][L0] Fix mismatched ZE call count We should create ZeCallCount map only once. Signed-off-by: Byoungro So --- sycl/plugins/level_zero/pi_level_zero.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index 054e7306aec9e..3ffb2f4c42602 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -1529,7 +1529,7 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms, PrintPiTrace = true; } - if (ZeDebug & ZE_DEBUG_CALL_COUNT) { + if ((ZeDebug & ZE_DEBUG_CALL_COUNT) && !ZeCallCount) { ZeCallCount = new std::map; } From 46d6f62838692a85fb835d57e376c2013d7a1a9e Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 13 Sep 2021 18:46:48 -0700 Subject: [PATCH 2/5] initialize ZeCallCount with lambda Signed-off-by: Byoungro So --- sycl/plugins/level_zero/pi_level_zero.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index 3ffb2f4c42602..b386797e14935 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -137,9 +137,6 @@ static pi_result mapError(ze_result_t ZeResult) { return It->second; } -// This will count the calls to Level-Zero -static std::map *ZeCallCount = nullptr; - // Trace a call to Level-Zero RT #define ZE_CALL(ZeName, ZeArgs) \ { \ @@ -184,6 +181,14 @@ static void zePrint(const char *Format, ...) { } } +// This will count the calls to Level-Zero +static std::map *ZeCallCount = [] { + if (ZeDebug & ZE_DEBUG_CALL_COUNT) { + return new std::map; + } + return (std::map *)nullptr; +}(); + // Helper function to implement zeHostSynchronize. // The behavior is to avoid infinite wait during host sync under ZE_DEBUG. // This allows for a much more responsive debugging of hangs. @@ -1529,10 +1534,6 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms, PrintPiTrace = true; } - if ((ZeDebug & ZE_DEBUG_CALL_COUNT) && !ZeCallCount) { - ZeCallCount = new std::map; - } - if (NumEntries == 0 && Platforms != nullptr) { return PI_INVALID_VALUE; } From 50b1afa21a9b8c00ccc3e0d2f5b7700c6df28505 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Mon, 13 Sep 2021 19:50:06 -0700 Subject: [PATCH 3/5] use call_once Signed-off-by: Byoungro So --- sycl/plugins/level_zero/pi_level_zero.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index b386797e14935..6f603d23e49fe 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -137,6 +137,9 @@ static pi_result mapError(ze_result_t ZeResult) { return It->second; } +// This will count the calls to Level-Zero +static std::map *ZeCallCount = nullptr; + // Trace a call to Level-Zero RT #define ZE_CALL(ZeName, ZeArgs) \ { \ @@ -181,14 +184,6 @@ static void zePrint(const char *Format, ...) { } } -// This will count the calls to Level-Zero -static std::map *ZeCallCount = [] { - if (ZeDebug & ZE_DEBUG_CALL_COUNT) { - return new std::map; - } - return (std::map *)nullptr; -}(); - // Helper function to implement zeHostSynchronize. // The behavior is to avoid infinite wait during host sync under ZE_DEBUG. // This allows for a much more responsive debugging of hangs. @@ -1534,6 +1529,12 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms, PrintPiTrace = true; } + static std::once_flag ZeCallCountInitialized; + std::call_once(ZeCallCountInitialized, []() { + if (ZeDebug & ZE_DEBUG_CALL_COUNT) + ZeCallCount = new std::map; + }); + if (NumEntries == 0 && Platforms != nullptr) { return PI_INVALID_VALUE; } From f0d52bfd0ac331f35a756af7532abce490eb9596 Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 14 Sep 2021 11:10:03 -0700 Subject: [PATCH 4/5] catch exceptions Signed-off-by: Byoungro So --- sycl/plugins/level_zero/pi_level_zero.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index 6f603d23e49fe..2bd059bc2c843 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -1530,10 +1530,15 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms, } static std::once_flag ZeCallCountInitialized; - std::call_once(ZeCallCountInitialized, []() { - if (ZeDebug & ZE_DEBUG_CALL_COUNT) - ZeCallCount = new std::map; - }); + try { + std::call_once(ZeCallCountInitialized, []() { + if (ZeDebug & ZE_DEBUG_CALL_COUNT) { + ZeCallCount = new std::map; + } + }); + } catch (...) { + return PI_OUT_OF_HOST_MEMORY; + } if (NumEntries == 0 && Platforms != nullptr) { return PI_INVALID_VALUE; From c1b82fe911e89cac52675f15835fdc30fd01745e Mon Sep 17 00:00:00 2001 From: Byoungro So Date: Tue, 14 Sep 2021 12:36:38 -0700 Subject: [PATCH 5/5] additional catch Signed-off-by: Byoungro So --- sycl/plugins/level_zero/pi_level_zero.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index 2bd059bc2c843..6f26e75ec60eb 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -1536,8 +1536,10 @@ pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms, ZeCallCount = new std::map; } }); - } catch (...) { + } catch (const std::bad_alloc &) { return PI_OUT_OF_HOST_MEMORY; + } catch (...) { + return PI_ERROR_UNKNOWN; } if (NumEntries == 0 && Platforms != nullptr) {