1 #ifndef CAFFE2_CORE_INIT_H_ 2 #define CAFFE2_CORE_INIT_H_ 4 #include "caffe2/core/common.h" 5 #include "caffe2/core/flags.h" 6 #include "caffe2/core/logging.h" 13 typedef bool (*InitFunction)(
int*,
char***);
19 Register(InitFunction
function,
bool run_early,
const char* description) {
22 CAFFE_ENFORCE(!early_init_functions_run_yet_);
23 early_init_functions_.emplace_back(
function, description);
25 if (init_functions_run_yet_) {
28 LOG(WARNING) <<
"Running init function after GlobalInit: " 37 char** argv =
nullptr;
38 bool success = (
function)(&argc, &argv);
39 CAFFE_ENFORCE(success);
42 init_functions_.emplace_back(
function, description);
47 bool RunRegisteredEarlyInitFunctions(
int* pargc,
char*** pargv) {
48 CAFFE_ENFORCE(!early_init_functions_run_yet_);
49 early_init_functions_run_yet_ =
true;
50 return RunRegisteredInitFunctionsInternal(
51 early_init_functions_, pargc, pargv);
54 bool RunRegisteredInitFunctions(
int* pargc,
char*** pargv) {
55 CAFFE_ENFORCE(!init_functions_run_yet_);
56 init_functions_run_yet_ =
true;
57 return RunRegisteredInitFunctionsInternal(init_functions_, pargc, pargv);
64 bool RunRegisteredInitFunctionsInternal(
65 vector<std::pair<InitFunction, const char*>>& functions,
66 int* pargc,
char*** pargv) {
67 for (
const auto& init_pair : functions) {
68 VLOG(1) <<
"Running init function: " << init_pair.second;
69 if (!(*init_pair.first)(pargc, pargv)) {
70 LOG(ERROR) <<
"Initialization function failed.";
78 vector<std::pair<InitFunction, const char*> > early_init_functions_;
79 vector<std::pair<InitFunction, const char*> > init_functions_;
80 bool early_init_functions_run_yet_ =
false;
81 bool init_functions_run_yet_ =
false;
87 InitRegisterer(internal::Caffe2InitializeRegistry::InitFunction
function,
88 bool run_early,
const char* description) {
89 internal::Caffe2InitializeRegistry::Registry()
90 ->Register(
function, run_early, description);
94 #define REGISTER_CAFFE2_INIT_FUNCTION(name, function, description) \ 96 ::caffe2::InitRegisterer g_caffe2_initregisterer_##name( \ 97 function, false, description); \ 100 #define REGISTER_CAFFE2_EARLY_INIT_FUNCTION(name, function, description) \ 102 ::caffe2::InitRegisterer g_caffe2_initregisterer_##name( \ 103 function, true, description); \ 116 <<
"Caffe2 GlobalInit should be run before any other API calls.";
146 CAFFE2_API
bool GlobalInit(
int* pargc,
char*** argv);
157 #endif // CAFFE2_CORE_INIT_H_ bool GlobalInitAlreadyRun()
Determine whether GlobalInit has already been run.
A template class that allows one to register classes by keys.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
bool GlobalInit(int *pargc, char ***pargv)
Initialize the global environment of caffe2.