Caffe2 - C++ API
A deep learning, cross platform ML framework
flags.h
Go to the documentation of this file.
1 
20 #ifndef CAFFE2_CORE_FLAGS_H_
21 #define CAFFE2_CORE_FLAGS_H_
22 
23 #include "caffe2/core/registry.h"
24 
25 namespace caffe2 {
29 void SetUsageMessage(const string& str);
30 
34 const char* UsageMessage();
35 
44 bool ParseCaffeCommandLineFlags(int* pargc, char*** pargv);
49 
50 } // namespace caffe2
51 
52 
54 // Below are gflags and non-gflags specific implementations.
56 
57 #ifdef CAFFE2_USE_GFLAGS
58 
59 #include <gflags/gflags.h>
60 
61 // gflags before 2.0 uses namespace google and after 2.1 uses namespace gflags.
62 // Using GFLAGS_GFLAGS_H_ to capture this change.
63 #ifndef GFLAGS_GFLAGS_H_
64 namespace gflags = google;
65 #endif // GFLAGS_GFLAGS_H_
66 
67 #define CAFFE2_GFLAGS_DEF_WRAPPER(type, name, default_value, help_str) \
68  DEFINE_##type(name, default_value, help_str); \
69  namespace caffe2 { \
70  using ::FLAGS_##name; \
71  }
72 
73 #define CAFFE2_DEFINE_int(...) CAFFE2_GFLAGS_DEF_WRAPPER(int32, __VA_ARGS__)
74 #define CAFFE2_DEFINE_int64(...) CAFFE2_GFLAGS_DEF_WRAPPER(int64, __VA_ARGS__)
75 #define CAFFE2_DEFINE_double(...) CAFFE2_GFLAGS_DEF_WRAPPER(double, __VA_ARGS__)
76 #define CAFFE2_DEFINE_bool(...) CAFFE2_GFLAGS_DEF_WRAPPER(bool, __VA_ARGS__)
77 #define CAFFE2_DEFINE_string(name, default_value, help_str) \
78  CAFFE2_GFLAGS_DEF_WRAPPER(string, name, default_value, help_str)
79 
80 // DECLARE_typed_var should be used in header files and in the global namespace.
81 #define CAFFE2_GFLAGS_DECLARE_WRAPPER(type, name) \
82  DECLARE_##type(name); \
83  namespace caffe2 { \
84  using ::FLAGS_##name; \
85  } // namespace caffe2
86 
87 #define CAFFE2_DECLARE_int(name) CAFFE2_GFLAGS_DECLARE_WRAPPER(int32, name)
88 #define CAFFE2_DECLARE_int64(name) CAFFE2_GFLAGS_DECLARE_WRAPPER(int64, name)
89 #define CAFFE2_DECLARE_double(name) CAFFE2_GFLAGS_DECLARE_WRAPPER(double, name)
90 #define CAFFE2_DECLARE_bool(name) CAFFE2_GFLAGS_DECLARE_WRAPPER(bool, name)
91 #define CAFFE2_DECLARE_string(name) CAFFE2_GFLAGS_DECLARE_WRAPPER(string, name)
92 
93 #else // CAFFE2_USE_GFLAGS
94 
95 namespace caffe2 {
96 
98  public:
99  Caffe2FlagParser() {}
100  bool success() { return success_; }
101 
102  protected:
103  template <typename T>
104  bool Parse(const string& content, T* value);
105  bool success_;
106 };
107 
108 CAFFE_DECLARE_REGISTRY(Caffe2FlagsRegistry, Caffe2FlagParser, const string&);
109 
110 } // namespace caffe2
111 
112 // The macros are defined outside the caffe2 namespace. In your code, you should
113 // write the CAFFE2_DEFINE_* and CAFFE2_DECLARE_* macros outside any namespace
114 // as well.
115 
116 #define CAFFE2_DEFINE_typed_var(type, name, default_value, help_str) \
117  namespace caffe2 { \
118  type FLAGS_##name = default_value; \
119  namespace { \
120  class Caffe2FlagParser_##name : public Caffe2FlagParser { \
121  public: \
122  explicit Caffe2FlagParser_##name(const string& content) { \
123  success_ = Caffe2FlagParser::Parse<type>(content, &FLAGS_##name); \
124  } \
125  }; \
126  } \
127  RegistererCaffe2FlagsRegistry g_Caffe2FlagsRegistry_##name( \
128  #name, Caffe2FlagsRegistry(), \
129  RegistererCaffe2FlagsRegistry::DefaultCreator<Caffe2FlagParser_##name>, \
130  "(" #type ", default " #default_value ") " help_str); \
131  }
132 
133 #define CAFFE2_DEFINE_int(name, default_value, help_str) \
134  CAFFE2_DEFINE_typed_var(int, name, default_value, help_str)
135 #define CAFFE2_DEFINE_int64(name, default_value, help_str) \
136  CAFFE2_DEFINE_typed_var(int64_t, name, default_value, help_str)
137 #define CAFFE2_DEFINE_double(name, default_value, help_str) \
138  CAFFE2_DEFINE_typed_var(double, name, default_value, help_str)
139 #define CAFFE2_DEFINE_bool(name, default_value, help_str) \
140  CAFFE2_DEFINE_typed_var(bool, name, default_value, help_str)
141 #define CAFFE2_DEFINE_string(name, default_value, help_str) \
142  CAFFE2_DEFINE_typed_var(string, name, default_value, help_str)
143 
144 // DECLARE_typed_var should be used in header files and in the global namespace.
145 #define CAFFE2_DECLARE_typed_var(type, name) \
146  namespace caffe2 { \
147  extern type FLAGS_##name; \
148  } // namespace caffe2
149 
150 #define CAFFE2_DECLARE_int(name) CAFFE2_DECLARE_typed_var(int, name)
151 #define CAFFE2_DECLARE_int64(name) CAFFE2_DECLARE_typed_var(int64_t, name)
152 #define CAFFE2_DECLARE_double(name) CAFFE2_DECLARE_typed_var(double, name)
153 #define CAFFE2_DECLARE_bool(name) CAFFE2_DECLARE_typed_var(bool, name)
154 #define CAFFE2_DECLARE_string(name) CAFFE2_DECLARE_typed_var(string, name)
155 
156 #endif // CAFFE2_USE_GFLAGS
157 
158 #endif // CAFFE2_CORE_FLAGS_H_
bool ParseCaffeCommandLineFlags(int *pargc, char ***pargv)
Parses the commandline flags.
Definition: flags.cc:55
const char * UsageMessage()
Returns the usage message for the commandline tool set by SetUsageMessage.
Definition: flags.cc:53
void SetUsageMessage(const string &str)
Sets the usage message when a commandline tool is called with "--help".
Definition: flags.cc:52
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
bool CommandLineFlagsHasBeenParsed()
Checks if the commandline flags has already been passed.
Definition: flags.cc:139