diff --git a/paddle/framework/executor.cc b/paddle/framework/executor.cc index ccf67169496940387781190803d674fd96326bcb..8534e70f48247e04a3eb2e5b059d2fac7a44dd55 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);