From d4be9730fced2a8effaf06412fa48e2aa0a8c325 Mon Sep 17 00:00:00 2001 From: qijun Date: Fri, 29 Sep 2017 23:44:52 -0700 Subject: [PATCH] fix gpu build error --- paddle/framework/executor.cc | 26 +++++++++++++++++--------- paddle/framework/executor_test.cc | 1 + 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/paddle/framework/executor.cc b/paddle/framework/executor.cc index 7fda2332b87..b38d6be16f2 100644 --- a/paddle/framework/executor.cc +++ b/paddle/framework/executor.cc @@ -80,26 +80,34 @@ class ExecutorImpl : public Executor { template std::unique_ptr make_unique(Args&&... args) { - return std::unique_ptr(new T(std::forward(args)...)); + return std::unique_ptr(new T(std::forward(args)...)); } -static std::unique_ptr g_cpu_device_context = - make_unique(platform::CPUPlace()); +platform::CPUDeviceContext* GetCPUDeviceContext() { + static std::unique_ptr g_cpu_device_context = + make_unique(platform::CPUPlace()); + return g_cpu_device_context.get(); +} #ifndef PADDLE_ONLY_CPU -static std::unique_ptr g_cuda_device_context = - make_unique(platform::GPUPlace(0)); +platform::CUDADeviceContext* GetCUDADeviceContext() { + static std::unique_ptr g_cuda_device_context = + make_unique(platform::GPUPlace(0)); + return g_cuda_device_context.get(); +} #endif Executor* NewLocalExecutor(const platform::Place& place, const ProgramDesc& pdesc, bool is_linear) { platform::DeviceContext* device_context = nullptr; if (platform::is_cpu_place(place)) { - device_context = g_cpu_device_context.get(); - } + device_context = GetCPUDeviceContext(); + } else if (platform::is_gpu_place(place)) { #ifndef PADDLE_ONLY_CPU - else if { - device_context = g_cuda_device_context.get(); + device_context = GetCUDADeviceContext(); + } +#else + PADDLE_THROW("'GPUPlace' is not supported in CPU only device."); } #endif return new ExecutorImpl(device_context, &pdesc, is_linear); diff --git a/paddle/framework/executor_test.cc b/paddle/framework/executor_test.cc index c046ae31582..6f8ca387684 100644 --- a/paddle/framework/executor_test.cc +++ b/paddle/framework/executor_test.cc @@ -23,4 +23,5 @@ TEST(Executor, Init) { CPUPlace cpu_place; Executor* executor = NewLocalExecutor(cpu_place, pdesc, true); executor->Run(); + delete executor; } \ No newline at end of file -- GitLab