Caffe2 - C++ API
A deep learning, cross platform ML framework
init.cc
1 #include "caffe2/core/init.h"
2 #include "caffe2/core/operator.h" // for StaticLinkingProtector
3 #include "caffe2/core/scope_guard.h"
4 
5 #include <iomanip>
6 #include <mutex>
7 
8 C10_DEFINE_bool(
9  caffe2_version,
10  false,
11  "Print Caffe2 version and build options on startup");
12 
13 namespace caffe2 {
14 
15 namespace internal {
16 // Keep track of stages of initialization to differentiate between
17 // (a) Re-entrant calls to GlobalInit (e.g. caller registers a Caffe2 init
18 // function which might in turn indirectly invoke GlobalInit).
19 // (b) Successive calls to GlobalInit, which are handled as documented in
20 // init.h.
21 // Note that this is NOT attempting to address thread-safety, see comments
22 // in init.h.
23 enum class State {
24  Uninitialized,
25  Initializing,
26  Initialized,
27 };
28 
29 Caffe2InitializeRegistry* Caffe2InitializeRegistry::Registry() {
30  static Caffe2InitializeRegistry gRegistry;
31  return &gRegistry;
32 }
33 
34 State& GlobalInitState() {
35  static State state = State::Uninitialized;
36  return state;
37 }
38 } // namespace internal
39 
41  return internal::GlobalInitState() == internal::State::Initialized;
42 }
43 
44 bool GlobalInit(int* pargc, char*** pargv) {
45  static std::recursive_mutex init_mutex;
46  std::lock_guard<std::recursive_mutex> guard(init_mutex);
47  internal::State& init_state = internal::GlobalInitState();
48  static StaticLinkingProtector g_protector;
49  bool success = true;
50 
51  // NOTE: if init_state == internal::State::Initializing at this point, do
52  // nothing because that indicates a re-entrant call
53  if (init_state == internal::State::Initialized) {
54  VLOG(1) << "GlobalInit has already been called: re-parsing gflags only.";
55  // Reparse command line flags
56  success &= c10::ParseCommandLineFlags(pargc, pargv);
57  UpdateLoggingLevelsFromFlags();
58  } else if (init_state == internal::State::Uninitialized) {
59  init_state = internal::State::Initializing;
60  auto init_state_guard = MakeGuard([&] {
61  // If an exception is thrown, go back to Uninitialized state
62  if (init_state == internal::State::Initializing) {
63  init_state = internal::State::Uninitialized;
64  }
65  });
66 
67  success &= internal::Caffe2InitializeRegistry::Registry()
68  ->RunRegisteredEarlyInitFunctions(pargc, pargv);
69  CAFFE_ENFORCE(
70  success, "Failed to run some early init functions for caffe2.");
71  success &= c10::ParseCommandLineFlags(pargc, pargv);
72  success &= InitCaffeLogging(pargc, *pargv);
73  // Print out the current build version. Using cerr as LOG(INFO) might be off
74  if (FLAGS_caffe2_version) {
75  std::cerr << "Caffe2 build configuration: " << std::endl;
76  for (const auto& it : GetBuildOptions()) {
77  std::cerr << " " << std::setw(25) << std::left << it.first << " : "
78  << it.second << std::endl;
79  }
80  }
81  // All other initialization functions.
82  success &= internal::Caffe2InitializeRegistry::Registry()
83  ->RunRegisteredInitFunctions(pargc, pargv);
84 
85  init_state =
86  success ? internal::State::Initialized : internal::State::Uninitialized;
87  }
88  CAFFE_ENFORCE(success, "Failed to run some init functions for caffe2.");
89  // TODO: if we fail GlobalInit(), should we continue?
90  return success;
91 }
92 
93 bool GlobalInit() {
94  // This is a version of the GlobalInit where no argument is passed in.
95  // On mobile devices, use this global init, since we cannot pass the
96  // command line options to caffe2, no arguments are passed.
97  int mobile_argc = 1;
98  static char caffe2_name[] = "caffe2";
99  char* mobile_name = &caffe2_name[0];
100  char** mobile_argv = &mobile_name;
101  return ::caffe2::GlobalInit(&mobile_argc, &mobile_argv);
102 }
103 } // namespace caffe2
bool GlobalInitAlreadyRun()
Determine whether GlobalInit has already been run.
Definition: init.cc:40
detail::ScopeGuardImplDecay< F > MakeGuard(F &&f) noexcept(noexcept(detail::ScopeGuardImplDecay< F >(static_cast< F && >(f))))
ScopeGuard is a general implementation of the "Initialization is Resource Acquisition" idiom...
Definition: scope_guard.h:153
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
C10_API bool ParseCommandLineFlags(int *pargc, char ***pargv)
Parses the commandline flags.