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

add init_gflags interface #5193

Merged
merged 3 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
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 All @@ -40,6 +42,24 @@ static size_t UniqueIntegerGenerator() {
return generator.fetch_add(1);
}

std::once_flag gflags_init_flag;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I remember that we have a convention from Google Style Guide -- global variables must be of POD type.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@QiJune for the alternative, you can use pthread API for this global variable
http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_once.html

Copy link
Member Author

Choose a reason for hiding this comment

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

@reyoung pthread_once is used as follows:

pthread_once(pthread_once_t *once_control, void (*init_routine)(void));

We have to pass a vector of string. So it's not suitable.

@wangkuiyi Yes, but once_flag has no deconstructor. It behaves like a POD. And I have not found another better way to write this code yet.


// 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 @@ -468,6 +488,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)