Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"fix gpu init" #7528

Merged
merged 7 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions paddle/framework/init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include <string.h> // for strdup
#include <algorithm>
#include <stdexcept>
#include <string>

#include "paddle/framework/init.h"
Expand Down Expand Up @@ -46,17 +47,23 @@ void InitDevices() {

std::vector<platform::Place> places;
places.emplace_back(platform::CPUPlace());
int count = 0;

#ifdef PADDLE_WITH_CUDA
int count = platform::GetCUDADeviceCount();
for (int i = 0; i < count; ++i) {
places.emplace_back(platform::CUDAPlace(i));
try {
count = platform::GetCUDADeviceCount();
} catch (const std::exception &exp) {
LOG(WARNING) << "Compiled with WITH_GPU, but no GPU found in runtime.";
}
#else
LOG(WARNING)
<< "'GPU' is not supported, Please re-compile with WITH_GPU option";
<< "'CUDA' is not supported, Please re-compile with WITH_GPU option";
#endif

for (int i = 0; i < count; ++i) {
places.emplace_back(platform::CUDAPlace(i));
}

platform::DeviceContextPool::Init(places);
}

Expand Down
21 changes: 20 additions & 1 deletion paddle/framework/init_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,26 @@ TEST(InitDevices, CPU) {
using paddle::framework::InitDevices;
using paddle::platform::DeviceContextPool;

#ifndef PADDLE_WITH_CUDA
InitDevices();
DeviceContextPool& pool = DeviceContextPool::Instance();
ASSERT_GE(pool.size(), 1U);
ASSERT_EQ(pool.size(), 1U);
#endif
}

TEST(InitDevices, CUDA) {
using paddle::framework::InitDevices;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#ifdef PADDLE_WITH_CUDA should be placed in line31.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is compiled in nv_test.
So #ifdef PADDLE_WITH_CUDA is no needed.

using paddle::platform::DeviceContextPool;

int count = 0;
try {
count = paddle::platform::GetCUDADeviceCount();
} catch (const std::exception& exp) {
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If #ifdef PADDLE_WITH_CUDA is placed in line 31, I think try ... catch is unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


#ifdef PADDLE_WITH_CUDA
InitDevices();
DeviceContextPool& pool = DeviceContextPool::Instance();
ASSERT_EQ(pool.size(), 1U + static_cast<unsigned>(count));
#endif
}
14 changes: 14 additions & 0 deletions python/paddle/v2/fluid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,23 @@ def __bootstrap__():
]
if core.is_compile_gpu():
read_env_flags += ['fraction_of_gpu_memory_to_use', 'op_sync']

core.init_gflags([sys.argv[0]] +
["--tryfromenv=" + ",".join(read_env_flags)])
core.init_glog(sys.argv[0])

gpu_devices = os.getenv("CUDA_VISIBLE_DEVICES", '')
if core.is_compile_gpu():
if len(gpu_devices.split(",")) >= 1:
print(
'WARNING: CUDA_VISIBLE_DEVICES set to {0}, not empty . The computation '
'speed will not be optimized if you use multi-gpu. It will '
'fail if this PaddlePaddle binary is compiled without GPU option'
.format(gpu_devices),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. You want to remind users that currently fluid does not support multi-GPU training, right?
  2. If the user does not compile PaddlePaddle with GPU option, this warning will not show. So It will fail if this PaddlePaddle ... is unnecessary.

file=sys.stderr)
else:
gpu_devices = "0"
os.environ['CUDA_VISIBLE_DEVICES'] = gpu_devices
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic of line 99~110 maybe has some problem.

core.init_devices()


Expand Down