diff --git a/projects/hip/api/hip/hip_context.cpp b/projects/hip/api/hip/hip_context.cpp index 46e4c864ff..dfeacdc924 100644 --- a/projects/hip/api/hip/hip_context.cpp +++ b/projects/hip/api/hip/hip_context.cpp @@ -28,12 +28,12 @@ THE SOFTWARE. #include std::vector g_devices; -std::once_flag g_ihipInitialized; namespace hip { thread_local amd::Context* g_context = nullptr; thread_local std::stack g_ctxtStack; +std::once_flag g_ihipInitialized; std::map g_nullStreams; @@ -53,7 +53,6 @@ void init() { context->release(); } else { g_devices.push_back(context); - g_context = context; } } } diff --git a/projects/hip/api/hip/hip_internal.hpp b/projects/hip/api/hip/hip_internal.hpp index 489b8b620f..099aa6ca25 100644 --- a/projects/hip/api/hip/hip_internal.hpp +++ b/projects/hip/api/hip/hip_internal.hpp @@ -28,26 +28,29 @@ THE SOFTWARE. #include #define HIP_INIT() \ - std::call_once(g_ihipInitialized, hip::init); - + std::call_once(hip::g_ihipInitialized, hip::init); \ + assert(g_devices.size() > 0); \ + if (hip::g_context == nullptr) { \ + hip::g_context = g_devices[0]; \ + } // This macro should be called at the beginning of every HIP API. #define HIP_INIT_API(...) \ - HIP_INIT(); \ - \ amd::Thread* thread = amd::Thread::current(); \ if (!CL_CHECK_THREAD(thread)) { \ return hipErrorOutOfMemory; \ - } + } \ + HIP_INIT(); namespace hc { class accelerator; class accelerator_view; }; -extern std::once_flag g_ihipInitialized; - namespace hip { + extern std::once_flag g_ihipInitialized; + extern thread_local amd::Context* g_context; + extern void init(); extern amd::Context* getCurrentContext();