From 3481bdc865571d2cfac1576d0913ab3f827b5955 Mon Sep 17 00:00:00 2001 From: qijun Date: Fri, 29 Sep 2017 22:32:41 -0700 Subject: [PATCH] add global device context --- paddle/framework/executor.cc | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/paddle/framework/executor.cc b/paddle/framework/executor.cc index ccf67169496..8534e70f482 100644 --- a/paddle/framework/executor.cc +++ b/paddle/framework/executor.cc @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/framework/executor.h" - +#include #include "paddle/platform/device_context.h" namespace paddle { @@ -78,17 +78,28 @@ class ExecutorImpl : public Executor { ProgramDescView* view_; }; +template +std::unique_ptr make_unique(Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +static std::unique_ptr g_cpu_device_context = + make_unique(platform::CPUPlace()); + +#ifndef PADDLE_ONLY_CPU +static std::unique_ptr g_cuda_device_context = + make_unique(platform::GPUPlace(0)); +#endif + static 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 = - new platform::CPUDeviceContext(boost::get(place)); + device_context = g_cpu_device_context.get(); } #ifndef PADDLE_ONLY_CPU else if { - device_context = - new platform::CUDADeviceContext(boost::get(place)); + device_context = g_cuda_device_context.get(); } #endif return new ExecutorImpl(device_context, &pdesc, is_linear); -- GitLab