From 67fcc7cf02b07dd962e0773877c0631746393674 Mon Sep 17 00:00:00 2001 From: cyy Date: Tue, 28 Jan 2025 04:28:18 +0000 Subject: [PATCH] [3/N] Remove unnecessary once flag usage (#145672) Fixes #ISSUE_NUMBER Pull Request resolved: https://github.com/pytorch/pytorch/pull/145672 Approved by: https://github.com/albanD --- c10/core/impl/GPUTrace.cpp | 7 +++---- c10/cuda/CUDACachingAllocator.cpp | 7 +++---- c10/util/Logging.cpp | 7 +++---- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/c10/core/impl/GPUTrace.cpp b/c10/core/impl/GPUTrace.cpp index ac1f08d7e09..9a4bab6b64a 100644 --- a/c10/core/impl/GPUTrace.cpp +++ b/c10/core/impl/GPUTrace.cpp @@ -1,5 +1,4 @@ #include -#include namespace c10::impl { @@ -8,11 +7,11 @@ std::atomic GPUTrace::gpuTraceState{nullptr}; bool GPUTrace::haveState{false}; void GPUTrace::set_trace(const PyInterpreter* trace) { - static c10::once_flag flag; - c10::call_once(flag, [&]() { + static bool once_flag [[maybe_unused]] = [&]() { gpuTraceState.store(trace, std::memory_order_release); haveState = true; - }); + return true; + }(); } } // namespace c10::impl diff --git a/c10/cuda/CUDACachingAllocator.cpp b/c10/cuda/CUDACachingAllocator.cpp index 74ac1d0ff36..a84d79dd245 100644 --- a/c10/cuda/CUDACachingAllocator.cpp +++ b/c10/cuda/CUDACachingAllocator.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -978,10 +977,10 @@ static std::string reportProcessMemoryInfo(c10::DeviceIndex device) { if (!nvml_handle) { return ""; } - static c10::once_flag nvml_init; - c10::call_once(nvml_init, [] { + static bool nvml_init [[maybe_unused]] = []() { TORCH_INTERNAL_ASSERT(NVML_SUCCESS == DriverAPI::get()->nvmlInit_v2_()); - }); + return true; + }(); cudaDeviceProp prop{}; C10_CUDA_CHECK(cudaGetDeviceProperties(&prop, device)); diff --git a/c10/util/Logging.cpp b/c10/util/Logging.cpp index 15d00b94422..657fea76dcd 100644 --- a/c10/util/Logging.cpp +++ b/c10/util/Logging.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -161,8 +160,7 @@ void InitEventSampledHandlers( std::vector< std::pair>> handlers) { - static c10::once_flag flag; - c10::call_once(flag, [&]() { + static bool flag [[maybe_unused]] = [&]() { auto& registry = EventSampledHandlerRegistry(); for (auto& [event, handler] : handlers) { auto entry = registry.find(std::string{event}); @@ -171,7 +169,8 @@ void InitEventSampledHandlers( } entry->second = std::move(handler); } - }); + return true; + }(); } const std::unique_ptr& GetEventSampledHandler(