Caffe2 - C++ API
A deep learning, cross platform ML framework
flags.h
Go to the documentation of this file.
1 
36 #ifndef CAFFE2_CORE_FLAGS_H_
37 #define CAFFE2_CORE_FLAGS_H_
38 
39 #include "caffe2/core/registry.h"
40 
41 namespace caffe2 {
45 void SetUsageMessage(const string& str);
46 
50 const char* UsageMessage();
51 
60 bool ParseCaffeCommandLineFlags(int* pargc, char*** pargv);
65 
66 } // namespace caffe2
67 
68 
70 // Below are gflags and non-gflags specific implementations.
72 
73 #ifdef CAFFE2_USE_GFLAGS
74 
75 #include <gflags/gflags.h>
76 
77 // gflags before 2.0 uses namespace google and after 2.1 uses namespace gflags.
78 // Using GFLAGS_GFLAGS_H_ to capture this change.
79 #ifndef GFLAGS_GFLAGS_H_
80 namespace gflags = google;
81 #endif // GFLAGS_GFLAGS_H_
82 
83 #define CAFFE2_GFLAGS_DEF_WRAPPER(type, name, default_value, help_str) \
84  DEFINE_##type(name, default_value, help_str); \
85  namespace caffe2 { \
86  using ::FLAGS_##name; \
87  }
88 
89 #define CAFFE2_DEFINE_int(name, default_value, help_str) \
90  CAFFE2_GFLAGS_DEF_WRAPPER(int32, name, default_value, help_str)
91 #define CAFFE2_DEFINE_int64(name, default_value, help_str) \
92  CAFFE2_GFLAGS_DEF_WRAPPER(int64, name, default_value, help_str)
93 #define CAFFE2_DEFINE_double(name, default_value, help_str) \
94  CAFFE2_GFLAGS_DEF_WRAPPER(double, name, default_value, help_str)
95 #define CAFFE2_DEFINE_bool(name, default_value, help_str) \
96  CAFFE2_GFLAGS_DEF_WRAPPER(bool, name, default_value, help_str)
97 #define CAFFE2_DEFINE_string(name, default_value, help_str) \
98  CAFFE2_GFLAGS_DEF_WRAPPER(string, name, default_value, help_str)
99 
100 // DECLARE_typed_var should be used in header files and in the global namespace.
101 #define CAFFE2_GFLAGS_DECLARE_WRAPPER(type, name) \
102  DECLARE_##type(name); \
103  namespace caffe2 { \
104  using ::FLAGS_##name; \
105  } // namespace caffe2
106 
107 #define CAFFE2_DECLARE_int(name) CAFFE2_GFLAGS_DECLARE_WRAPPER(int32, name)
108 #define CAFFE2_DECLARE_int64(name) CAFFE2_GFLAGS_DECLARE_WRAPPER(int64, name)
109 #define CAFFE2_DECLARE_double(name) CAFFE2_GFLAGS_DECLARE_WRAPPER(double, name)
110 #define CAFFE2_DECLARE_bool(name) CAFFE2_GFLAGS_DECLARE_WRAPPER(bool, name)
111 #define CAFFE2_DECLARE_string(name) CAFFE2_GFLAGS_DECLARE_WRAPPER(string, name)
112 
113 #else // CAFFE2_USE_GFLAGS
114 
115 namespace caffe2 {
116 
118  public:
119  Caffe2FlagParser() {}
120  bool success() { return success_; }
121 
122  protected:
123  template <typename T>
124  bool Parse(const string& content, T* value);
125  bool success_;
126 };
127 
128 CAFFE_DECLARE_REGISTRY(Caffe2FlagsRegistry, Caffe2FlagParser, const string&);
129 
130 } // namespace caffe2
131 
132 // The macros are defined outside the caffe2 namespace. In your code, you should
133 // write the CAFFE2_DEFINE_* and CAFFE2_DECLARE_* macros outside any namespace
134 // as well.
135 
136 #define CAFFE2_DEFINE_typed_var(type, name, default_value, help_str) \
137  namespace caffe2 { \
138  CAFFE2_EXPORT type FLAGS_##name = default_value; \
139  namespace { \
140  class Caffe2FlagParser_##name : public Caffe2FlagParser { \
141  public: \
142  explicit Caffe2FlagParser_##name(const string& content) { \
143  success_ = Caffe2FlagParser::Parse<type>(content, &FLAGS_##name); \
144  } \
145  }; \
146  } \
147  RegistererCaffe2FlagsRegistry g_Caffe2FlagsRegistry_##name( \
148  #name, \
149  Caffe2FlagsRegistry(), \
150  RegistererCaffe2FlagsRegistry::DefaultCreator<Caffe2FlagParser_##name>, \
151  "(" #type ", default " #default_value ") " help_str); \
152  }
153 
154 #define CAFFE2_DEFINE_int(name, default_value, help_str) \
155  CAFFE2_DEFINE_typed_var(int, name, default_value, help_str)
156 #define CAFFE2_DEFINE_int64(name, default_value, help_str) \
157  CAFFE2_DEFINE_typed_var(int64_t, name, default_value, help_str)
158 #define CAFFE2_DEFINE_double(name, default_value, help_str) \
159  CAFFE2_DEFINE_typed_var(double, name, default_value, help_str)
160 #define CAFFE2_DEFINE_bool(name, default_value, help_str) \
161  CAFFE2_DEFINE_typed_var(bool, name, default_value, help_str)
162 #define CAFFE2_DEFINE_string(name, default_value, help_str) \
163  CAFFE2_DEFINE_typed_var(string, name, default_value, help_str)
164 
165 // DECLARE_typed_var should be used in header files and in the global namespace.
166 #define CAFFE2_DECLARE_typed_var(type, name) \
167  namespace caffe2 { \
168  CAFFE2_IMPORT extern type FLAGS_##name; \
169  } // namespace caffe2
170 
171 #define CAFFE2_DECLARE_int(name) CAFFE2_DECLARE_typed_var(int, name)
172 #define CAFFE2_DECLARE_int64(name) CAFFE2_DECLARE_typed_var(int64_t, name)
173 #define CAFFE2_DECLARE_double(name) CAFFE2_DECLARE_typed_var(double, name)
174 #define CAFFE2_DECLARE_bool(name) CAFFE2_DECLARE_typed_var(bool, name)
175 #define CAFFE2_DECLARE_string(name) CAFFE2_DECLARE_typed_var(string, name)
176 
177 #endif // CAFFE2_USE_GFLAGS
178 
179 #endif // CAFFE2_CORE_FLAGS_H_
bool ParseCaffeCommandLineFlags(int *pargc, char ***pargv)
Parses the commandline flags.
Definition: flags.cc:71
const char * UsageMessage()
Returns the usage message for the commandline tool set by SetUsageMessage.
Definition: flags.cc:69
void SetUsageMessage(const string &str)
Sets the usage message when a commandline tool is called with "--help".
Definition: flags.cc:68
Copyright (c) 2016-present, Facebook, Inc.
bool CommandLineFlagsHasBeenParsed()
Checks if the commandline flags has already been passed.
Definition: flags.cc:155