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_R 5
27 #define BINDING_TYPE_MARKDOWN 128
28 #define BINDING_TYPE_UNKNOWN -1
29 
30 #ifndef BINDING_TYPE
31 #define BINDING_TYPE BINDING_TYPE_UNKNOWN
32 #endif
33 
34 #if (BINDING_TYPE == BINDING_TYPE_CLI) // This is a command-line executable.
35 
36 // Matrices are transposed on load/save.
37 #define BINDING_MATRIX_TRANSPOSED true
38 
41 
46 #define PRINT_PARAM_STRING mlpack::bindings::cli::ParamString
47 
52 #define PRINT_PARAM_VALUE mlpack::bindings::cli::PrintValue
53 
61 #define PRINT_CALL mlpack::bindings::cli::ProgramCall
62 
67 #define PRINT_DATASET mlpack::bindings::cli::PrintDataset
68 
73 #define PRINT_MODEL mlpack::bindings::cli::PrintModel
74 
79 #define BINDING_IGNORE_CHECK mlpack::bindings::cli::IgnoreCheck
80 
81 namespace mlpack {
82 namespace util {
83 
84 template<typename T>
86 
87 }
88 }
89 
90 static const std::string testName = "";
94 
95 static void mlpackMain(); // This is typically defined after this include.
96 
97 int main(int argc, char** argv)
98 {
99  // Parse the command-line options; put them into CLI.
101  // Enable timing.
103 
104  // A "total_time" timer is run by default for each mlpack program.
105  mlpack::Timer::Start("total_time");
106 
107  mlpackMain();
108 
109  // Print output options, print verbose information, save model parameters,
110  // clean up, and so forth.
112 }
113 
114 #elif(BINDING_TYPE == BINDING_TYPE_TEST) // This is a unit test.
115 
116 // Matrices are not transposed on load/save.
117 #define BINDING_MATRIX_TRANSPOSED false
118 
122 
123 // These functions will do nothing.
124 #define PRINT_PARAM_STRING(A) std::string(" ")
125 #define PRINT_PARAM_VALUE(A, B) std::string(" ")
126 #define PRINT_DATASET(A) std::string(" ")
127 #define PRINT_MODEL(A) std::string(" ")
128 
136 #define PRINT_CALL(...) std::string(" ")
137 
142 #define BINDING_IGNORE_CHECK mlpack::bindings::tests::IgnoreCheck
143 
144 namespace mlpack {
145 namespace util {
146 
147 template<typename T>
149 
150 }
151 }
152 
153 // testName symbol should be defined in each binding test file
155 
156 #elif(BINDING_TYPE == BINDING_TYPE_PYX) // This is a Python binding.
157 
158 // Matrices are transposed on load/save.
159 #define BINDING_MATRIX_TRANSPOSED true
160 
163 
168 #define PRINT_PARAM_STRING mlpack::bindings::python::ParamString
169 
174 #define PRINT_PARAM_VALUE mlpack::bindings::python::PrintValue
175 
180 #define PRINT_DATASET mlpack::bindings::python::PrintDataset
181 
186 #define PRINT_MODEL mlpack::bindings::python::PrintModel
187 
195 #define PRINT_CALL mlpack::bindings::python::ProgramCall
196 
201 #define BINDING_IGNORE_CHECK mlpack::bindings::python::IgnoreCheck
202 
203 namespace mlpack {
204 namespace util {
205 
206 template<typename T>
208 
209 }
210 }
211 
212 static const std::string testName = "";
214 
215 #undef BINDING_NAME
216 #define BINDING_NAME(NAME) static \
217  mlpack::util::ProgramName \
218  io_programname_dummy_object = mlpack::util::ProgramName(NAME); \
219  namespace mlpack { \
220  namespace bindings { \
221  namespace python { \
222  std::string programName = NAME; \
223  } \
224  } \
225  }
226 
227 PARAM_FLAG("verbose", "Display informational messages and the full list of "
228  "parameters and timers at the end of execution.", "v");
229 PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
230  " copied before the method is run. This is useful for debugging problems "
231  "where the input parameters are being modified by the algorithm, but can "
232  "slow down the code.", "");
233 PARAM_FLAG("check_input_matrices", "If specified, the input matrix is checked for"
234  " NaN and inf values; an exception is thrown if any are found.", "");
235 
236 // Nothing else needs to be defined---the binding will use mlpackMain() as-is.
237 
238 #elif(BINDING_TYPE == BINDING_TYPE_JL) // This is a Julia binding.
239 
240 // Matrices are transposed on load/save.
241 #define BINDING_MATRIX_TRANSPOSED true
242 
245 
246 #define PRINT_PARAM_STRING mlpack::bindings::julia::ParamString
247 #define PRINT_PARAM_VALUE mlpack::bindings::julia::PrintValue
248 #define PRINT_DATASET mlpack::bindings::julia::PrintDataset
249 #define PRINT_MODEL mlpack::bindings::julia::PrintModel
250 #define PRINT_CALL mlpack::bindings::julia::ProgramCall
251 #define BINDING_IGNORE_CHECK mlpack::bindings::julia::IgnoreCheck
252 
253 namespace mlpack {
254 namespace util {
255 
256 template<typename T>
258 
259 }
260 }
261 
262 static const std::string testName = "";
264 
265 #undef BINDING_NAME
266 #define BINDING_NAME(NAME) static \
267  mlpack::util::ProgramName \
268  io_programname_dummy_object = mlpack::util::ProgramName(NAME); \
269  namespace mlpack { \
270  namespace bindings { \
271  namespace julia { \
272  std::string programName = NAME; \
273  } \
274  } \
275  }
276 
277 PARAM_FLAG("verbose", "Display informational messages and the full list of "
278  "parameters and timers at the end of execution.", "v");
279 
280 // Nothing else needs to be defined---the binding will use mlpackMain() as-is.
281 
282 #elif(BINDING_TYPE == BINDING_TYPE_GO) // This is a Go binding.
283 
284 // Matrices are transposed on load/save.
285 #define BINDING_MATRIX_TRANSPOSED true
286 
289 
290 #define PRINT_PARAM_STRING mlpack::bindings::go::ParamString
291 #define PRINT_PARAM_VALUE mlpack::bindings::go::PrintValue
292 #define PRINT_DATASET mlpack::bindings::go::PrintDataset
293 #define PRINT_MODEL mlpack::bindings::go::PrintModel
294 #define PRINT_CALL mlpack::bindings::go::ProgramCall
295 #define BINDING_IGNORE_CHECK mlpack::bindings::go::IgnoreCheck
296 
297 namespace mlpack {
298 namespace util {
299 
300 template<typename T>
301 using Option = mlpack::bindings::go::GoOption<T>;
302 
303 }
304 }
305 
306 static const std::string testName = "";
308 
309 #undef BINDING_NAME
310 #define BINDING_NAME(NAME) static \
311  mlpack::util::ProgramName \
312  io_programname_dummy_object = mlpack::util::ProgramName(NAME); \
313  namespace mlpack { \
314  namespace bindings { \
315  namespace go { \
316  std::string programName = NAME; \
317  } \
318  } \
319  }
320 
321 PARAM_FLAG("verbose", "Display informational messages and the full list of "
322  "parameters and timers at the end of execution.", "v");
323 
324 // Nothing else needs to be defined---the binding will use mlpackMain() as-is.
325 
326 #elif(BINDING_TYPE == BINDING_TYPE_R) // This is a R binding.
327 
328 // This doesn't actually matter for this binding type.
329 #define BINDING_MATRIX_TRANSPOSED true
330 
333 
334 #define PRINT_PARAM_STRING mlpack::bindings::r::ParamString
335 #define PRINT_PARAM_VALUE mlpack::bindings::r::PrintValue
336 #define PRINT_DATASET mlpack::bindings::r::PrintDataset
337 #define PRINT_MODEL mlpack::bindings::r::PrintModel
338 #define PRINT_CALL(...) mlpack::bindings::r::ProgramCall(false, __VA_ARGS__)
339 #define BINDING_IGNORE_CHECK mlpack::bindings::r::IgnoreCheck
340 
341 namespace mlpack {
342 namespace util {
343 
344 template<typename T>
345 using Option = mlpack::bindings::r::ROption<T>;
346 
347 }
348 }
349 
350 static const std::string testName = "";
352 
353 PARAM_FLAG("verbose", "Display informational messages and the full list of "
354  "parameters and timers at the end of execution.", "v");
355 
356 // Nothing else needs to be defined---the binding will use mlpackMain() as-is.
357 
358 #elif BINDING_TYPE == BINDING_TYPE_MARKDOWN
359 
360 // We use MARKDOWN_BINDING_NAME in BINDING_NAME(), BINDING_SHORT_DESC(),
361 // BINDING_LONG_DESC(), BINDING_EXAMPLE() and BINDING_SEE_ALSO()
362 // so it needs to be defined.
363 #ifndef MARKDOWN_BINDING_NAME
364  #error "MARKDOWN_BINDING_NAME must be defined when BINDING_TYPE is Markdown!"
365 #endif
366 
367 // This value doesn't actually matter, but it needs to be defined as something.
368 #define BINDING_MATRIX_TRANSPOSED true
369 
372 
377 #define PRINT_PARAM_STRING mlpack::bindings::markdown::ParamString
378 
383 #define PRINT_PARAM_VALUE mlpack::bindings::markdown::PrintValue
384 
389 #define PRINT_DATASET mlpack::bindings::markdown::PrintDataset
390 
395 #define PRINT_MODEL mlpack::bindings::markdown::PrintModel
396 
404 #define PRINT_CALL mlpack::bindings::markdown::ProgramCall
405 
410 #define BINDING_IGNORE_CHECK mlpack::bindings::markdown::IgnoreCheck
411 
412 // This doesn't actually matter for this binding type.
413 #define BINDING_MATRIX_TRANSPOSED true
414 
415 namespace mlpack {
416 namespace util {
417 
418 template<typename T>
420 
421 }
422 }
423 
426 
427 #undef BINDING_NAME
428 #undef BINDING_SHORT_DESC
429 #undef BINDING_LONG_DESC
430 #undef BINDING_EXAMPLE
431 #undef BINDING_SEE_ALSO
432 
433 #define BINDING_NAME(NAME) static \
434  mlpack::bindings::markdown::ProgramNameWrapper \
435  io_programname_dummy_object = \
436  mlpack::bindings::markdown::ProgramNameWrapper( \
437  MARKDOWN_BINDING_NAME, NAME);
438 
439 #define BINDING_SHORT_DESC(SHORT_DESC) static \
440  mlpack::bindings::markdown::ShortDescriptionWrapper \
441  io_programshort_desc_dummy_object = \
442  mlpack::bindings::markdown::ShortDescriptionWrapper( \
443  MARKDOWN_BINDING_NAME, SHORT_DESC);
444 
445 #define BINDING_LONG_DESC(LONG_DESC) static \
446  mlpack::bindings::markdown::LongDescriptionWrapper \
447  io_programlong_desc_dummy_object = \
448  mlpack::bindings::markdown::LongDescriptionWrapper( \
449  MARKDOWN_BINDING_NAME, []() { return std::string(LONG_DESC); });
450 
451 #ifdef __COUNTER__
452  #define BINDING_EXAMPLE(EXAMPLE) static \
453  mlpack::bindings::markdown::ExampleWrapper \
454  JOIN(io_programexample_dummy_object_, __COUNTER__) = \
455  mlpack::bindings::markdown::ExampleWrapper(MARKDOWN_BINDING_NAME, \
456  []() { return(std::string(EXAMPLE)); });
457 
458  #define BINDING_SEE_ALSO(DESCRIPTION, LINK) static \
459  mlpack::bindings::markdown::SeeAlsoWrapper \
460  JOIN(io_programsee_also_dummy_object_, __COUNTER__) = \
461  mlpack::bindings::markdown::SeeAlsoWrapper(MARKDOWN_BINDING_NAME, \
462  DESCRIPTION, LINK);
463 #else
464  #define BINDING_EXAMPLE(EXAMPLE) static \
465  mlpack::bindings::markdown::ExampleWrapper \
466  JOIN(JOIN(io_programexample_dummy_object_, __LINE__), opt) = \
467  mlpack::bindings::markdown::ExampleWrapper(MARKDOWN_BINDING_NAME, \
468  []() { return(std::string(EXAMPLE)); });
469 
470  #define BINDING_SEE_ALSO(DESCRIPTION, LINK) static \
471  mlpack::bindings::markdown::SeeAlsoWrapper \
472  JOIN(JOIN(io_programsee_also_dummy_object_, __LINE__), opt) = \
473  mlpack::bindings::markdown::SeeAlsoWrapper(MARKDOWN_BINDING_NAME, \
474  DESCRIPTION, LINK);
475 #endif
476 
477 PARAM_FLAG("verbose", "Display informational messages and the full list of "
478  "parameters and timers at the end of execution.", "v");
479 
480 // CLI-specific parameters.
481 PARAM_FLAG("help", "Default help info.", "h");
482 PARAM_STRING_IN("info", "Print help on a specific option.", "", "");
483 PARAM_FLAG("version", "Display the version of mlpack.", "V");
484 
485 // Python-specific parameters.
486 PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
487  " copied before the method is run. This is useful for debugging problems "
488  "where the input parameters are being modified by the algorithm, but can "
489  "slow down the code.", "");
490 
491 #else
492 
493 #error "Unknown binding type! Be sure BINDING_TYPE is defined if you are " \
494  "including <mlpack/core/util/mlpack_main.hpp>.";
495 
496 #endif
497 
498 #include "param_checks.hpp"
499 
500 #endif
void EndProgram()
Handle command-line program termination.
Definition: end_program.hpp:26
Linear algebra utility functions, generally performed on matrices or vectors.
The Markdown option class.
Definition: md_option.hpp:33
The Go option class.
Definition: go_option.hpp:39
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:335
#define PARAM_FLAG(ID, DESC, ALIAS)
Define a flag parameter.
Definition: param.hpp:189
A static object whose constructor registers a parameter with the IO class.
Definition: cli_option.hpp:48
A static object whose constructor registers a parameter with the IO class.
Definition: test_option.hpp:40
static void Start(const std::string &name)
Start the given timer.
The R option class.
Definition: R_option.hpp:31