From 248ea14e6362e5d254920941a7004a0a6d784e6a Mon Sep 17 00:00:00 2001 From: German Andryeyev Date: Wed, 5 Jun 2024 16:02:04 -0400 Subject: [PATCH] SWDEV-459610 - Skip destruction for the child process Fork() duplicates all system memory resources, but runtime can't duplicate GPU resources. Thus, avoid tearDown() calls for the child process(s). Change-Id: Id6b12bacd5112b9ad3747c218e09cba98ea1b42c [ROCm/clr commit: ae2992ea434164007174c86299eb1b6543736952] --- projects/clr/rocclr/platform/runtime.cpp | 6 +++++- projects/clr/rocclr/platform/runtime.hpp | 8 +++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/projects/clr/rocclr/platform/runtime.cpp b/projects/clr/rocclr/platform/runtime.cpp index 571668cc70..444f736468 100644 --- a/projects/clr/rocclr/platform/runtime.cpp +++ b/projects/clr/rocclr/platform/runtime.cpp @@ -47,6 +47,7 @@ namespace amd { volatile bool Runtime::initialized_ = false; bool Runtime::LibraryDetached = false; +volatile int Runtime::pid_ = 0; bool Runtime::init() { if (initialized_) { @@ -80,6 +81,7 @@ bool Runtime::init() { } initialized_ = true; + pid_ = amd::Os::getProcessId(); ClTrace(LOG_DEBUG, LOG_INIT); return true; } @@ -104,7 +106,9 @@ std::vector RuntimeTearDown::external_; RuntimeTearDown::~RuntimeTearDown() { #ifndef _WIN32 - if (amd::IS_HIP) { + // Only perform destruction if process matches the initialization, + // to avoid a call with the child process after fork() + if (amd::IS_HIP && amd::Os::getProcessId() == Runtime::pid()) { for (auto it: external_) { it->release(); } diff --git a/projects/clr/rocclr/platform/runtime.hpp b/projects/clr/rocclr/platform/runtime.hpp index d1953aedaf..5e1a2ae66a 100644 --- a/projects/clr/rocclr/platform/runtime.hpp +++ b/projects/clr/rocclr/platform/runtime.hpp @@ -31,12 +31,16 @@ namespace amd { */ class Runtime : AllStatic { + static volatile int pid_; //!< Process ID for this runtime initialization static volatile bool initialized_; static bool LibraryDetached; public: //! Return true if the OpencCL runtime is already initialized - inline static bool initialized(); + inline static bool initialized() { return initialized_; } + + //! Return PID if the OCL/HIP runtime was initialized in the process + inline static int pid() { return pid_; } //! Initialize the OpenCL runtime. static bool init(); @@ -58,8 +62,6 @@ class Runtime : AllStatic { /*@}*/ -inline bool Runtime::initialized() { return initialized_; } - class RuntimeTearDown : public HeapObject { static std::vector external_;