mlpack_main.hpp
Go to the documentation of this file.
1 
18 #ifndef MLPACK_CORE_UTIL_MLPACK_MAIN_HPP
19 #define MLPACK_CORE_UTIL_MLPACK_MAIN_HPP
20 
21 #define BINDING_TYPE_CLI 0
22 #define BINDING_TYPE_TEST 1
23 #define BINDING_TYPE_PYX 2
24 #define BINDING_TYPE_MARKDOWN 128
25 #define BINDING_TYPE_UNKNOWN -1
26 
27 #ifndef BINDING_TYPE
28 #define BINDING_TYPE BINDING_TYPE_UNKNOWN
29 #endif
30 
31 #if (BINDING_TYPE == BINDING_TYPE_CLI) // This is a command-line executable.
32 
33 // Matrices are transposed on load/save.
34 #define BINDING_MATRIX_TRANSPOSED true
35 
38 
43 #define PRINT_PARAM_STRING mlpack::bindings::cli::ParamString
44 
49 #define PRINT_PARAM_VALUE mlpack::bindings::cli::PrintValue
50 
58 #define PRINT_CALL mlpack::bindings::cli::ProgramCall
59 
64 #define PRINT_DATASET mlpack::bindings::cli::PrintDataset
65 
70 #define PRINT_MODEL mlpack::bindings::cli::PrintModel
71 
76 #define BINDING_IGNORE_CHECK mlpack::bindings::cli::IgnoreCheck
77 
78 namespace mlpack {
79 namespace util {
80 
81 template<typename T>
83 
84 }
85 }
86 
87 static const std::string testName = "";
91 
92 static void mlpackMain(); // This is typically defined after this include.
93 
94 int main(int argc, char** argv)
95 {
96  // Parse the command-line options; put them into CLI.
98  // Enable timing.
100 
101  // A "total_time" timer is run by default for each mlpack program.
102  mlpack::Timer::Start("total_time");
103 
104  mlpackMain();
105 
106  // Print output options, print verbose information, save model parameters,
107  // clean up, and so forth.
109 }
110 
111 #elif(BINDING_TYPE == BINDING_TYPE_TEST) // This is a unit test.
112 
113 // Matrices are not transposed on load/save.
114 #define BINDING_MATRIX_TRANSPOSED false
115 
119 
120 // These functions will do nothing.
121 #define PRINT_PARAM_STRING(A) std::string(" ")
122 #define PRINT_PARAM_VALUE(A, B) std::string(" ")
123 #define PRINT_DATASET(A) std::string(" ")
124 #define PRINT_MODEL(A) std::string(" ")
125 
133 #define PRINT_CALL(...) std::string(" ")
134 
139 #define BINDING_IGNORE_CHECK mlpack::bindings::tests::IgnoreCheck
140 
141 namespace mlpack {
142 namespace util {
143 
144 template<typename T>
146 
147 }
148 }
149 
150 // testName symbol should be defined in each binding test file
152 
153 #undef PROGRAM_INFO
154 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) \
155  static mlpack::util::ProgramDoc \
156  cli_programdoc_dummy_object = mlpack::util::ProgramDoc(NAME, SHORT_DESC, \
157  []() { return DESC; }, { __VA_ARGS__ })
158 
159 #elif(BINDING_TYPE == BINDING_TYPE_PYX) // This is a Python binding.
160 
161 // Matrices are transposed on load/save.
162 #define BINDING_MATRIX_TRANSPOSED true
163 
166 
171 #define PRINT_PARAM_STRING mlpack::bindings::python::ParamString
172 
177 #define PRINT_PARAM_VALUE mlpack::bindings::python::PrintValue
178 
183 #define PRINT_DATASET mlpack::bindings::python::PrintDataset
184 
189 #define PRINT_MODEL mlpack::bindings::python::PrintModel
190 
198 #define PRINT_CALL mlpack::bindings::python::ProgramCall
199 
204 #define BINDING_IGNORE_CHECK mlpack::bindings::python::IgnoreCheck
205 
206 namespace mlpack {
207 namespace util {
208 
209 template<typename T>
211 
212 }
213 }
214 
215 static const std::string testName = "";
217 
218 #undef PROGRAM_INFO
219 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) \
220  static mlpack::util::ProgramDoc \
221  cli_programdoc_dummy_object = mlpack::util::ProgramDoc(NAME, SHORT_DESC, \
222  []() { return DESC; }, { __VA_ARGS__ }); \
223  namespace mlpack { \
224  namespace bindings { \
225  namespace python { \
226  std::string programName = NAME; \
227  } \
228  } \
229  }
230 
231 PARAM_FLAG("verbose", "Display informational messages and the full list of "
232  "parameters and timers at the end of execution.", "v");
233 PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
234  " copied before the method is run. This is useful for debugging problems "
235  "where the input parameters are being modified by the algorithm, but can "
236  "slow down the code.", "");
237 
238 // Nothing else needs to be defined---the binding will use mlpackMain() as-is.
239 
240 #elif BINDING_TYPE == BINDING_TYPE_MARKDOWN
241 
242 // We use BINDING_NAME in PROGRAM_INFO() so it needs to be defined.
243 #ifndef BINDING_NAME
244  #error "BINDING_NAME must be defined when BINDING_TYPE is Markdown!"
245 #endif
246 
249 
254 #define PRINT_PARAM_STRING mlpack::bindings::markdown::ParamString
255 
260 #define PRINT_PARAM_VALUE mlpack::bindings::markdown::PrintValue
261 
266 #define PRINT_DATASET mlpack::bindings::markdown::PrintDataset
267 
272 #define PRINT_MODEL mlpack::bindings::markdown::PrintModel
273 
281 #define PRINT_CALL mlpack::bindings::markdown::ProgramCall
282 
287 #define BINDING_IGNORE_CHECK mlpack::bindings::markdown::IgnoreCheck
288 
289 // This doesn't actually matter for this binding type.
290 #define BINDING_MATRIX_TRANSPOSED true
291 
292 namespace mlpack {
293 namespace util {
294 
295 template<typename T>
297 
298 }
299 }
300 
303 
304 #undef PROGRAM_INFO
305 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) static \
306  mlpack::bindings::markdown::ProgramDocWrapper \
307  cli_programdoc_dummy_object = \
308  mlpack::bindings::markdown::ProgramDocWrapper(BINDING_NAME, NAME, \
309  SHORT_DESC, []() { return DESC; }, { __VA_ARGS__ }); \
310 
311 PARAM_FLAG("verbose", "Display informational messages and the full list of "
312  "parameters and timers at the end of execution.", "v");
313 
314 // CLI-specific parameters.
315 PARAM_FLAG("help", "Default help info.", "h");
316 PARAM_STRING_IN("info", "Print help on a specific option.", "", "");
317 PARAM_FLAG("version", "Display the version of mlpack.", "V");
318 
319 // Python-specific parameters.
320 PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
321  " copied before the method is run. This is useful for debugging problems "
322  "where the input parameters are being modified by the algorithm, but can "
323  "slow down the code.", "");
324 
325 #else
326 
327 #error "Unknown binding type! Be sure BINDING_TYPE is defined if you are " \
328  "including <mlpack/core/util/mlpack_main.hpp>.";
329 
330 #endif
331 
332 #include "param_checks.hpp"
333 
334 #endif
void EndProgram()
Handle command-line program termination.
Definition: end_program.hpp:26
.hpp
Definition: add_to_po.hpp:21
The Markdown option class.
Definition: md_option.hpp:33
void ParseCommandLine(int argc, char **argv)
Parse the command line, setting all of the options inside of the CLI object to their appropriate give...
The Python option class.
Definition: py_option.hpp:37
static void EnableTiming()
Enable timing of mlpack programs.
#define PARAM_STRING_IN(ID, DESC, ALIAS, DEF)
Define a string input parameter.
Definition: param.hpp:235
#define PARAM_FLAG(ID, DESC, ALIAS)
Define a flag parameter.
Definition: param.hpp:95
A static object whose constructor registers a parameter with the CLI class.
Definition: cli_option.hpp:47
A static object whose constructor registers a parameter with the CLI class.
Definition: test_option.hpp:40
static void Start(const std::string &name)
Start the given timer.