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_JL 3
25 #define BINDING_TYPE_GO 4
26 #define BINDING_TYPE_MARKDOWN 128
27 #define BINDING_TYPE_UNKNOWN -1
28 
29 #ifndef BINDING_TYPE
30 #define BINDING_TYPE BINDING_TYPE_UNKNOWN
31 #endif
32 
33 #if (BINDING_TYPE == BINDING_TYPE_CLI) // This is a command-line executable.
34 
35 // Matrices are transposed on load/save.
36 #define BINDING_MATRIX_TRANSPOSED true
37 
40 
45 #define PRINT_PARAM_STRING mlpack::bindings::cli::ParamString
46 
51 #define PRINT_PARAM_VALUE mlpack::bindings::cli::PrintValue
52 
60 #define PRINT_CALL mlpack::bindings::cli::ProgramCall
61 
66 #define PRINT_DATASET mlpack::bindings::cli::PrintDataset
67 
72 #define PRINT_MODEL mlpack::bindings::cli::PrintModel
73 
78 #define BINDING_IGNORE_CHECK mlpack::bindings::cli::IgnoreCheck
79 
80 namespace mlpack {
81 namespace util {
82 
83 template<typename T>
85 
86 }
87 }
88 
89 static const std::string testName = "";
93 
94 static void mlpackMain(); // This is typically defined after this include.
95 
96 int main(int argc, char** argv)
97 {
98  // Parse the command-line options; put them into CLI.
100  // Enable timing.
102 
103  // A "total_time" timer is run by default for each mlpack program.
104  mlpack::Timer::Start("total_time");
105 
106  mlpackMain();
107 
108  // Print output options, print verbose information, save model parameters,
109  // clean up, and so forth.
111 }
112 
113 #elif(BINDING_TYPE == BINDING_TYPE_TEST) // This is a unit test.
114 
115 // Matrices are not transposed on load/save.
116 #define BINDING_MATRIX_TRANSPOSED false
117 
121 
122 // These functions will do nothing.
123 #define PRINT_PARAM_STRING(A) std::string(" ")
124 #define PRINT_PARAM_VALUE(A, B) std::string(" ")
125 #define PRINT_DATASET(A) std::string(" ")
126 #define PRINT_MODEL(A) std::string(" ")
127 
135 #define PRINT_CALL(...) std::string(" ")
136 
141 #define BINDING_IGNORE_CHECK mlpack::bindings::tests::IgnoreCheck
142 
143 namespace mlpack {
144 namespace util {
145 
146 template<typename T>
148 
149 }
150 }
151 
152 // testName symbol should be defined in each binding test file
154 
155 #undef PROGRAM_INFO
156 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) \
157  static mlpack::util::ProgramDoc \
158  cli_programdoc_dummy_object = mlpack::util::ProgramDoc(NAME, SHORT_DESC, \
159  []() { return DESC; }, { __VA_ARGS__ })
160 
161 #elif(BINDING_TYPE == BINDING_TYPE_PYX) // This is a Python binding.
162 
163 // Matrices are transposed on load/save.
164 #define BINDING_MATRIX_TRANSPOSED true
165 
168 
173 #define PRINT_PARAM_STRING mlpack::bindings::python::ParamString
174 
179 #define PRINT_PARAM_VALUE mlpack::bindings::python::PrintValue
180 
185 #define PRINT_DATASET mlpack::bindings::python::PrintDataset
186 
191 #define PRINT_MODEL mlpack::bindings::python::PrintModel
192 
200 #define PRINT_CALL mlpack::bindings::python::ProgramCall
201 
206 #define BINDING_IGNORE_CHECK mlpack::bindings::python::IgnoreCheck
207 
208 namespace mlpack {
209 namespace util {
210 
211 template<typename T>
213 
214 }
215 }
216 
217 static const std::string testName = "";
219 
220 #undef PROGRAM_INFO
221 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) \
222  static mlpack::util::ProgramDoc \
223  cli_programdoc_dummy_object = mlpack::util::ProgramDoc(NAME, SHORT_DESC, \
224  []() { return DESC; }, { __VA_ARGS__ }); \
225  namespace mlpack { \
226  namespace bindings { \
227  namespace python { \
228  std::string programName = NAME; \
229  } \
230  } \
231  }
232 
233 PARAM_FLAG("verbose", "Display informational messages and the full list of "
234  "parameters and timers at the end of execution.", "v");
235 PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
236  " copied before the method is run. This is useful for debugging problems "
237  "where the input parameters are being modified by the algorithm, but can "
238  "slow down the code.", "");
239 
240 // Nothing else needs to be defined---the binding will use mlpackMain() as-is.
241 
242 #elif(BINDING_TYPE == BINDING_TYPE_JL) // This is a Julia binding.
243 
244 // Matrices are transposed on load/save.
245 #define BINDING_MATRIX_TRANSPOSED true
246 
249 
250 #define PRINT_PARAM_STRING mlpack::bindings::julia::ParamString
251 #define PRINT_PARAM_VALUE mlpack::bindings::julia::PrintValue
252 #define PRINT_DATASET mlpack::bindings::julia::PrintDataset
253 #define PRINT_MODEL mlpack::bindings::julia::PrintModel
254 #define PRINT_CALL mlpack::bindings::julia::ProgramCall
255 #define BINDING_IGNORE_CHECK mlpack::bindings::julia::IgnoreCheck
256 
257 namespace mlpack {
258 namespace util {
259 
260 template<typename T>
262 
263 }
264 }
265 
266 static const std::string testName = "";
268 
269 #undef PROGRAM_INFO
270 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) static \
271  mlpack::util::ProgramDoc \
272  cli_programdoc_dummy_object = mlpack::util::ProgramDoc(NAME, SHORT_DESC, \
273  []() { return DESC; }, { __VA_ARGS__ }); \
274  namespace mlpack { \
275  namespace bindings { \
276  namespace julia { \
277  std::string programName = NAME; \
278  } \
279  } \
280  }
281 
282 PARAM_FLAG("verbose", "Display informational messages and the full list of "
283  "parameters and timers at the end of execution.", "v");
284 
285 // Nothing else needs to be defined---the binding will use mlpackMain() as-is.
286 
287 #elif(BINDING_TYPE == BINDING_TYPE_GO) // This is a Go binding.
288 
289 // Matrices are transposed on load/save.
290 #define BINDING_MATRIX_TRANSPOSED true
291 
294 
295 #define PRINT_PARAM_STRING mlpack::bindings::go::ParamString
296 #define PRINT_PARAM_VALUE mlpack::bindings::go::PrintValue
297 #define PRINT_DATASET mlpack::bindings::go::PrintDataset
298 #define PRINT_MODEL mlpack::bindings::go::PrintModel
299 #define PRINT_CALL mlpack::bindings::go::ProgramCall
300 #define BINDING_IGNORE_CHECK mlpack::bindings::go::IgnoreCheck
301 
302 namespace mlpack {
303 namespace util {
304 
305 template<typename T>
306 using Option = mlpack::bindings::go::GoOption<T>;
307 
308 }
309 }
310 
311 static const std::string testName = "";
313 
314 #undef PROGRAM_INFO
315 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) \
316  static mlpack::util::ProgramDoc \
317  cli_programdoc_dummy_object = mlpack::util::ProgramDoc(NAME, SHORT_DESC, \
318  []() { return DESC; }, { __VA_ARGS__ }); \
319  namespace mlpack { \
320  namespace bindings { \
321  namespace go { \
322  std::string programName = NAME; \
323  } \
324  } \
325  }
326 
327 PARAM_FLAG("verbose", "Display informational messages and the full list of "
328  "parameters and timers at the end of execution.", "v");
329 
330 // Nothing else needs to be defined---the binding will use mlpackMain() as-is.
331 
332 #elif BINDING_TYPE == BINDING_TYPE_MARKDOWN
333 
334 // It doesn't really matter whether this is true or false...
335 #define BINDING_MATRIX_TRANSPOSED true
336 
337 // We use BINDING_NAME in PROGRAM_INFO() so it needs to be defined.
338 #ifndef BINDING_NAME
339  #error "BINDING_NAME must be defined when BINDING_TYPE is Markdown!"
340 #endif
341 
342 // This value doesn't actually matter, but it needs to be defined as something.
343 #define BINDING_MATRIX_TRANSPOSED true
344 
347 
352 #define PRINT_PARAM_STRING mlpack::bindings::markdown::ParamString
353 
358 #define PRINT_PARAM_VALUE mlpack::bindings::markdown::PrintValue
359 
364 #define PRINT_DATASET mlpack::bindings::markdown::PrintDataset
365 
370 #define PRINT_MODEL mlpack::bindings::markdown::PrintModel
371 
379 #define PRINT_CALL mlpack::bindings::markdown::ProgramCall
380 
385 #define BINDING_IGNORE_CHECK mlpack::bindings::markdown::IgnoreCheck
386 
387 // This doesn't actually matter for this binding type.
388 #define BINDING_MATRIX_TRANSPOSED true
389 
390 namespace mlpack {
391 namespace util {
392 
393 template<typename T>
395 
396 }
397 }
398 
401 
402 #undef PROGRAM_INFO
403 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) static \
404  mlpack::bindings::markdown::ProgramDocWrapper \
405  cli_programdoc_dummy_object = \
406  mlpack::bindings::markdown::ProgramDocWrapper(BINDING_NAME, NAME, \
407  SHORT_DESC, []() { return DESC; }, { __VA_ARGS__ }); \
408 
409 PARAM_FLAG("verbose", "Display informational messages and the full list of "
410  "parameters and timers at the end of execution.", "v");
411 
412 // CLI-specific parameters.
413 PARAM_FLAG("help", "Default help info.", "h");
414 PARAM_STRING_IN("info", "Print help on a specific option.", "", "");
415 PARAM_FLAG("version", "Display the version of mlpack.", "V");
416 
417 // Python-specific parameters.
418 PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
419  " copied before the method is run. This is useful for debugging problems "
420  "where the input parameters are being modified by the algorithm, but can "
421  "slow down the code.", "");
422 
423 #else
424 
425 #error "Unknown binding type! Be sure BINDING_TYPE is defined if you are " \
426  "including <mlpack/core/util/mlpack_main.hpp>.";
427 
428 #endif
429 
430 #include "param_checks.hpp"
431 
432 #endif
void EndProgram()
Handle command-line program termination.
Definition: end_program.hpp:26
strip_type.hpp
Definition: add_to_po.hpp:21
The Markdown option class.
Definition: md_option.hpp:33
The Go option class.
Definition: go_option.hpp:41
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.