Caffe2 - C++ API
A deep learning, cross platform ML framework
init.h
1 #ifndef CAFFE2_CORE_INIT_H_
2 #define CAFFE2_CORE_INIT_H_
3 
4 #include "caffe2/core/common.h"
5 #include "caffe2/core/flags.h"
6 #include "caffe2/core/logging.h"
7 
8 namespace caffe2 {
9 
10 namespace internal {
11 class CAFFE2_API Caffe2InitializeRegistry {
12  public:
13  typedef bool (*InitFunction)(int*, char***);
14  // Registry() is defined in .cpp file to make registration work across
15  // multiple shared libraries loaded with RTLD_LOCAL
17 
18  void
19  Register(InitFunction function, bool run_early, const char* description) {
20  if (run_early) {
21  // Disallow registration after GlobalInit of early init functions
22  CAFFE_ENFORCE(!early_init_functions_run_yet_);
23  early_init_functions_.emplace_back(function, description);
24  } else {
25  if (init_functions_run_yet_) {
26  // Run immediately, since GlobalInit already ran. This should be
27  // rare but we want to allow it in some cases.
28  LOG(WARNING) << "Running init function after GlobalInit: "
29  << description;
30  // TODO(orionr): Consider removing argc and argv for non-early
31  // registration. Unfortunately that would require a new InitFunction
32  // typedef, so not making the change right now.
33  //
34  // Note that init doesn't receive argc and argv, so the function
35  // might fail and we want to raise an error in that case.
36  int argc = 0;
37  char** argv = nullptr;
38  bool success = (function)(&argc, &argv);
39  CAFFE_ENFORCE(success);
40  } else {
41  // Wait until GlobalInit to run
42  init_functions_.emplace_back(function, description);
43  }
44  }
45  }
46 
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);
52  }
53 
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);
58  }
59 
60  private:
61  // Run all registered initialization functions. This has to be called AFTER
62  // all static initialization are finished and main() has started, since we are
63  // using logging.
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.";
71  return false;
72  }
73  }
74  return true;
75  }
76 
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;
82 };
83 } // namespace internal
84 
85 class CAFFE2_API InitRegisterer {
86  public:
87  InitRegisterer(internal::Caffe2InitializeRegistry::InitFunction function,
88  bool run_early, const char* description) {
89  internal::Caffe2InitializeRegistry::Registry()
90  ->Register(function, run_early, description);
91  }
92 };
93 
94 #define REGISTER_CAFFE2_INIT_FUNCTION(name, function, description) \
95  namespace { \
96  ::caffe2::InitRegisterer g_caffe2_initregisterer_##name( \
97  function, false, description); \
98  } // namespace
99 
100 #define REGISTER_CAFFE2_EARLY_INIT_FUNCTION(name, function, description) \
101  namespace { \
102  ::caffe2::InitRegisterer g_caffe2_initregisterer_##name( \
103  function, true, description); \
104  } // namespace
105 
109 CAFFE2_API bool GlobalInitAlreadyRun();
110 
111 class CAFFE2_API GlobalInitIsCalledGuard {
112  public:
114  if (!GlobalInitAlreadyRun()) {
115  LOG(WARNING)
116  << "Caffe2 GlobalInit should be run before any other API calls.";
117  }
118  }
119 };
120 
146 CAFFE2_API bool GlobalInit(int* pargc, char*** argv);
147 
155 CAFFE2_API bool GlobalInit();
156 } // namespace caffe2
157 #endif // CAFFE2_CORE_INIT_H_
bool GlobalInitAlreadyRun()
Determine whether GlobalInit has already been run.
Definition: init.cc:40
A template class that allows one to register classes by keys.
Definition: Registry.h:54
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
bool GlobalInit(int *pargc, char ***pargv)
Initialize the global environment of caffe2.
Definition: init.cc:44