diff --git a/paddle/framework/device_data_transform_test.cu b/paddle/framework/device_data_transform_test.cu index 9fb26f09c7ed6aff3bfc98cf3f829e50adbf48bf..5d89f5546fa87241dec6364d86a100ca51bce687 100644 --- a/paddle/framework/device_data_transform_test.cu +++ b/paddle/framework/device_data_transform_test.cu @@ -105,8 +105,7 @@ static void BuildVar(const std::string& param_name, TEST(Operator, CPUtoGPU) { using namespace paddle::framework; using namespace paddle::platform; - - ASSERT_EQ(InitDevices({"CPU", "GPU:0"}), true); + InitDevices(); paddle::framework::Scope scope; paddle::platform::CPUPlace cpu_place; diff --git a/paddle/framework/init.cc b/paddle/framework/init.cc index e7087e063cbe8839716e3648d55cd25cc778f06f..e12bac1d78e3f6bbc46849c06b53e3b93e147cfc 100644 --- a/paddle/framework/init.cc +++ b/paddle/framework/init.cc @@ -40,40 +40,23 @@ void InitGflags(std::vector &argv) { }); } -bool InitDevices(const std::vector &devices) { - // device format - // CPU - // GPU:1 - // TODO(dzhwinter) : add device format annotation for users. +void InitDevices() { + /*Init all avaiable devices by default */ + std::vector places; - for (auto &device : devices) { - auto p = string::Piece(device); - if (string::HasPrefix(p, "CPU")) { - places.emplace_back(platform::CPUPlace()); - } else if (string::HasPrefix(p, "GPU")) { + places.emplace_back(platform::CPUPlace()); + #ifdef PADDLE_WITH_CUDA - auto pos = string::RFind(p, ':', string::Piece::npos); - auto number = device.substr(pos + 1); - places.emplace_back(platform::CUDAPlace(std::stoi(number))); + int count = platform::GetCUDADeviceCount(); + for (int i = 0; i < count; ++i) { + places.emplace_back(platform::CUDAPlace(i)); + } #else - LOG(WARNING) - << "'GPU' is not supported, Please re-compile with WITH_GPU option"; + LOG(WARNING) + << "'GPU' is not supported, Please re-compile with WITH_GPU option"; #endif - } else { - return false; - } - } - if (std::find_if(places.begin(), places.end(), - [&](const platform::Place &place) { - return platform::is_cpu_place(place); - }) == places.end()) { - places.emplace_back(platform::CPUPlace()); - LOG(WARNING) << "Not specified CPU device, create CPU by Default."; - } platform::DeviceContextPool::Init(places); - // framework::UseALL(); - return true; } void InitGLOG(const std::string &prog_name) { diff --git a/paddle/framework/init.h b/paddle/framework/init.h index 9c84a03ded52632047841f95badbcf44bc9f48d1..c8fd964d006baf729888414ded2aec85ba5a024e 100644 --- a/paddle/framework/init.h +++ b/paddle/framework/init.h @@ -24,7 +24,7 @@ void InitGflags(std::vector &argv); void InitGLOG(const std::string &prog_name); -bool InitDevices(const std::vector &devices); +void InitDevices(); } // namespace framework } // namespace paddle diff --git a/paddle/framework/init_test.cc b/paddle/framework/init_test.cc index f0788051d4855a175d2d7ea1f1a0805c776c462b..f837a965d3be7d40c20803ae4462b3bfd91bffd0 100644 --- a/paddle/framework/init_test.cc +++ b/paddle/framework/init_test.cc @@ -14,18 +14,13 @@ limitations under the License. */ #include "gtest/gtest.h" #include "paddle/framework/init.h" +#include "paddle/platform/device_context.h" -TEST(Init, InitDevices) { +TEST(InitDevices, CPU) { using paddle::framework::InitDevices; - std::vector ds1 = {"CPU"}; - ASSERT_EQ(InitDevices(ds1), true); + using paddle::platform::DeviceContextPool; -#ifdef PADDLE_WITH_CUDA - std::vector ds2 = {"CPU", "GPU:0", "GPU:1"}; - ASSERT_EQ(InitDevices(ds2), true); - - // test re-init - std::vector ds3 = {"GPU:0", "GPU:1"}; - ASSERT_EQ(InitDevices(ds3), true); -#endif + InitDevices(); + DeviceContextPool& pool = DeviceContextPool::Instance(); + ASSERT_GE(pool.size(), 1U); } diff --git a/paddle/framework/operator_test.cc b/paddle/framework/operator_test.cc index d002f3f238862a53ad7286570e2d0bbd2334c584..b69d7c7a7406eb3e18d385c568cb9c21b9b4107b 100644 --- a/paddle/framework/operator_test.cc +++ b/paddle/framework/operator_test.cc @@ -69,7 +69,7 @@ REGISTER_OP_WITHOUT_GRADIENT(test_operator, paddle::framework::OpWithoutKernelCheckerMaker); TEST(OperatorBase, all) { - paddle::framework::InitDevices({"CPU"}); + paddle::framework::InitDevices(); paddle::framework::proto::OpDesc op_desc; op_desc.set_type("test_operator"); BuildVar("input", {"IN1"}, op_desc.add_inputs()); @@ -195,7 +195,7 @@ REGISTER_OP_CPU_KERNEL(op_with_kernel, // test with single input TEST(OpKernel, all) { - paddle::framework::InitDevices({"CPU"}); + paddle::framework::InitDevices(); paddle::framework::proto::OpDesc op_desc; op_desc.set_type("op_with_kernel"); BuildVar("x", {"IN1"}, op_desc.add_inputs()); @@ -225,7 +225,7 @@ REGISTER_OP_CPU_KERNEL(op_multi_inputs_with_kernel, TEST(OpKernel, multi_inputs) { using namespace paddle::framework; - paddle::framework::InitDevices({"CPU"}); + paddle::framework::InitDevices(); proto::OpDesc op_desc; op_desc.set_type("op_multi_inputs_with_kernel"); @@ -264,7 +264,7 @@ class OperatorClone : public paddle::framework::OperatorBase { }; TEST(Operator, Clone) { - paddle::framework::InitDevices({"CPU"}); + paddle::framework::InitDevices(); OperatorClone a("ABC", paddle::framework::VariableNameMap{}, paddle::framework::VariableNameMap{}, paddle::framework::AttributeMap{}); diff --git a/paddle/inference/inference.cc b/paddle/inference/inference.cc index 49e39358e81bbee64a618be88ee0fca6aa438b93..37b8b20ddfcf2566b8410f950308309e5b2b2a7c 100644 --- a/paddle/inference/inference.cc +++ b/paddle/inference/inference.cc @@ -169,7 +169,7 @@ void InferenceEngine::Execute(const std::vector& feeds, } auto* place = new platform::CPUPlace(); - framework::InitDevices({"CPU"}); + framework::InitDevices(); framework::Executor* executor = new framework::Executor(*place); framework::Scope* scope = new framework::Scope(); diff --git a/paddle/platform/device_context.h b/paddle/platform/device_context.h index 7a0040c9c229af79ea8be1049dfd6c0d1b4d19cf..9826a642768a3346110a7872a726aba15eac1fb0 100644 --- a/paddle/platform/device_context.h +++ b/paddle/platform/device_context.h @@ -185,6 +185,8 @@ class DeviceContextPool { const typename DefaultDeviceContextType::TYPE*>(Get(place)); } + size_t size() const { return device_contexts_.size(); } + private: static DeviceContextPool* pool; constexpr static int LEFT_SHIFT = 8; diff --git a/paddle/testing/paddle_gtest_main.cc b/paddle/testing/paddle_gtest_main.cc index 108ff335bf6b920c648d4bfebbd6a40ffb6fd939..a7fb50ee4149a3c36077f83383f45f3106e7e0f1 100644 --- a/paddle/testing/paddle_gtest_main.cc +++ b/paddle/testing/paddle_gtest_main.cc @@ -34,11 +34,11 @@ int main(int argc, char** argv) { google::ParseCommandLineFlags(&new_argc, &new_argv_address, false); testing::InitGoogleTest(&argc, argv); paddle::memory::Used(paddle::platform::CPUPlace()); - std::vector devs = {"CPU"}; + #ifdef PADDLE_WITH_CUDA paddle::memory::Used(paddle::platform::CUDAPlace(0)); - devs.push_back("GPU:0"); #endif - paddle::framework::InitDevices(devs); + + paddle::framework::InitDevices(); return RUN_ALL_TESTS(); } diff --git a/python/paddle/v2/fluid/__init__.py b/python/paddle/v2/fluid/__init__.py index 3f178e252c32e7f2b97800a9b5ac7a7052e64992..ccd5998e3592a1f5dc795ee24875c1aed230587e 100644 --- a/python/paddle/v2/fluid/__init__.py +++ b/python/paddle/v2/fluid/__init__.py @@ -62,11 +62,7 @@ def __bootstrap__(): core.init_gflags([sys.argv[0]] + ["--tryfromenv=" + ",".join(read_env_flags)]) core.init_glog(sys.argv[0]) - - if core.is_compile_gpu(): - core.init_devices(["CPU", "GPU:0"]) - else: - core.init_devices(["CPU"]) + core.init_devices() __bootstrap__() diff --git a/python/paddle/v2/fluid/tests/test_batch_norm_op.py b/python/paddle/v2/fluid/tests/test_batch_norm_op.py index abbd48d2b843cedb77caffc13413d2f9695defa6..ac9418549f45f818257d74045cabb9c581816968 100644 --- a/python/paddle/v2/fluid/tests/test_batch_norm_op.py +++ b/python/paddle/v2/fluid/tests/test_batch_norm_op.py @@ -341,9 +341,6 @@ class TestBatchNormOp(OpTest): if core.is_compile_gpu() and core.op_support_gpu("batch_norm"): places.append(core.CUDAPlace(0)) - core.init_devices(["CPU", "GPU:0"]) - else: - core.init_devices(["CPU"]) for place in places: for data_format in ["NCHW", "NHWC"]: test_with_place(place, data_format, [2, 3, 4, 5])