Skip to content

Commit

Permalink
add init_gflags interface (#5193)
Browse files Browse the repository at this point in the history
* add init_gflags interface

* refine code

* follow comments
  • Loading branch information
QiJune authored Oct 30, 2017
1 parent 6c8dce9 commit a186b53
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions paddle/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ limitations under the License. */

#include "paddle/pybind/protobuf.h"

#include <mutex> // for call_once
#include "gflags/gflags.h"
#include "paddle/framework/backward.h"
#include "paddle/framework/executor.h"
#include "paddle/framework/feed_fetch_method.h"
Expand Down Expand Up @@ -45,6 +47,24 @@ static size_t UniqueIntegerGenerator() {
return generator.fetch_add(1);
}

std::once_flag gflags_init_flag;

// TODO(qijun) move init gflags to init.cc
void InitGflags(std::vector<std::string> &argv) {
std::call_once(gflags_init_flag, [&]() {
int argc = argv.size();
char **arr = new char *[argv.size()];
std::string line;
for (size_t i = 0; i < argv.size(); i++) {
arr[i] = &argv[i][0];
line += argv[i];
line += ' ';
}
google::ParseCommandLineFlags(&argc, &arr, true);
VLOG(1) << "Init commandline: " << line;
});
}

bool IsCompileGPU() {
#ifndef PADDLE_WITH_CUDA
return false;
Expand Down Expand Up @@ -483,6 +503,7 @@ All parameter, weight, gradient are variables in Paddle.
});

m.def("unique_integer", UniqueIntegerGenerator);
m.def("init_gflags", InitGflags);

m.def("is_compile_gpu", IsCompileGPU);
m.def("set_feed_variable", framework::SetFeedVariable);
Expand Down
10 changes: 10 additions & 0 deletions python/paddle/v2/framework/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
import sys
import core
__all__ = ['proto']
argv = []
if core.is_compile_gpu():
argv = list(sys.argv) + [
"--tryfromenv=fraction_of_gpu_memory_to_use,use_pinned_memory"
]
else:
argv = list(sys.argv) + ["--tryfromenv=use_pinned_memory"]
core.init_gflags(argv)

0 comments on commit a186b53

Please sign in to comment.