[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
This commit is contained in:
cyy 2025-01-28 04:28:18 +00:00 committed by PyTorch MergeBot
parent 01a4d86b31
commit 67fcc7cf02
3 changed files with 9 additions and 12 deletions

View file

@ -1,5 +1,4 @@
#include <c10/core/impl/GPUTrace.h> #include <c10/core/impl/GPUTrace.h>
#include <c10/util/CallOnce.h>
namespace c10::impl { namespace c10::impl {
@ -8,11 +7,11 @@ std::atomic<const PyInterpreter*> GPUTrace::gpuTraceState{nullptr};
bool GPUTrace::haveState{false}; bool GPUTrace::haveState{false};
void GPUTrace::set_trace(const PyInterpreter* trace) { void GPUTrace::set_trace(const PyInterpreter* trace) {
static c10::once_flag flag; static bool once_flag [[maybe_unused]] = [&]() {
c10::call_once(flag, [&]() {
gpuTraceState.store(trace, std::memory_order_release); gpuTraceState.store(trace, std::memory_order_release);
haveState = true; haveState = true;
}); return true;
}();
} }
} // namespace c10::impl } // namespace c10::impl

View file

@ -5,7 +5,6 @@
#include <c10/cuda/CUDAException.h> #include <c10/cuda/CUDAException.h>
#include <c10/cuda/CUDAFunctions.h> #include <c10/cuda/CUDAFunctions.h>
#include <c10/cuda/CUDAGuard.h> #include <c10/cuda/CUDAGuard.h>
#include <c10/util/CallOnce.h>
#include <c10/util/Gauge.h> #include <c10/util/Gauge.h>
#include <c10/util/ScopeExit.h> #include <c10/util/ScopeExit.h>
#include <c10/util/UniqueVoidPtr.h> #include <c10/util/UniqueVoidPtr.h>
@ -978,10 +977,10 @@ static std::string reportProcessMemoryInfo(c10::DeviceIndex device) {
if (!nvml_handle) { if (!nvml_handle) {
return ""; return "";
} }
static c10::once_flag nvml_init; static bool nvml_init [[maybe_unused]] = []() {
c10::call_once(nvml_init, [] {
TORCH_INTERNAL_ASSERT(NVML_SUCCESS == DriverAPI::get()->nvmlInit_v2_()); TORCH_INTERNAL_ASSERT(NVML_SUCCESS == DriverAPI::get()->nvmlInit_v2_());
}); return true;
}();
cudaDeviceProp prop{}; cudaDeviceProp prop{};
C10_CUDA_CHECK(cudaGetDeviceProperties(&prop, device)); C10_CUDA_CHECK(cudaGetDeviceProperties(&prop, device));

View file

@ -1,5 +1,4 @@
#include <c10/util/Backtrace.h> #include <c10/util/Backtrace.h>
#include <c10/util/CallOnce.h>
#include <c10/util/Flags.h> #include <c10/util/Flags.h>
#include <c10/util/Lazy.h> #include <c10/util/Lazy.h>
#include <c10/util/Logging.h> #include <c10/util/Logging.h>
@ -161,8 +160,7 @@ void InitEventSampledHandlers(
std::vector< std::vector<
std::pair<std::string_view, std::unique_ptr<EventSampledHandler>>> std::pair<std::string_view, std::unique_ptr<EventSampledHandler>>>
handlers) { handlers) {
static c10::once_flag flag; static bool flag [[maybe_unused]] = [&]() {
c10::call_once(flag, [&]() {
auto& registry = EventSampledHandlerRegistry(); auto& registry = EventSampledHandlerRegistry();
for (auto& [event, handler] : handlers) { for (auto& [event, handler] : handlers) {
auto entry = registry.find(std::string{event}); auto entry = registry.find(std::string{event});
@ -171,7 +169,8 @@ void InitEventSampledHandlers(
} }
entry->second = std::move(handler); entry->second = std::move(handler);
} }
}); return true;
}();
} }
const std::unique_ptr<EventSampledHandler>& GetEventSampledHandler( const std::unique_ptr<EventSampledHandler>& GetEventSampledHandler(