mlpack 3.4.2
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
81namespace mlpack {
82namespace util {
83
84template<typename T>
86
87}
88}
89
90static const std::string testName = "";
94
95static void mlpackMain(); // This is typically defined after this include.
96
97int 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
144namespace mlpack {
145namespace util {
146
147template<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
203namespace mlpack {
204namespace util {
205
206template<typename T>
208
209}
210}
211
212static 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
227PARAM_FLAG("verbose", "Display informational messages and the full list of "
228 "parameters and timers at the end of execution.", "v");
229PARAM_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
234// Nothing else needs to be defined---the binding will use mlpackMain() as-is.
235
236#elif(BINDING_TYPE == BINDING_TYPE_JL) // This is a Julia binding.
237
238// Matrices are transposed on load/save.
239#define BINDING_MATRIX_TRANSPOSED true
240
243
244#define PRINT_PARAM_STRING mlpack::bindings::julia::ParamString
245#define PRINT_PARAM_VALUE mlpack::bindings::julia::PrintValue
246#define PRINT_DATASET mlpack::bindings::julia::PrintDataset
247#define PRINT_MODEL mlpack::bindings::julia::PrintModel
248#define PRINT_CALL mlpack::bindings::julia::ProgramCall
249#define BINDING_IGNORE_CHECK mlpack::bindings::julia::IgnoreCheck
250
251namespace mlpack {
252namespace util {
253
254template<typename T>
256
257}
258}
259
260static const std::string testName = "";
262
263#undef BINDING_NAME
264#define BINDING_NAME(NAME) static \
265 mlpack::util::ProgramName \
266 io_programname_dummy_object = mlpack::util::ProgramName(NAME); \
267 namespace mlpack { \
268 namespace bindings { \
269 namespace julia { \
270 std::string programName = NAME; \
271 } \
272 } \
273 }
274
275PARAM_FLAG("verbose", "Display informational messages and the full list of "
276 "parameters and timers at the end of execution.", "v");
277
278// Nothing else needs to be defined---the binding will use mlpackMain() as-is.
279
280#elif(BINDING_TYPE == BINDING_TYPE_GO) // This is a Go binding.
281
282// Matrices are transposed on load/save.
283#define BINDING_MATRIX_TRANSPOSED true
284
287
288#define PRINT_PARAM_STRING mlpack::bindings::go::ParamString
289#define PRINT_PARAM_VALUE mlpack::bindings::go::PrintValue
290#define PRINT_DATASET mlpack::bindings::go::PrintDataset
291#define PRINT_MODEL mlpack::bindings::go::PrintModel
292#define PRINT_CALL mlpack::bindings::go::ProgramCall
293#define BINDING_IGNORE_CHECK mlpack::bindings::go::IgnoreCheck
294
295namespace mlpack {
296namespace util {
297
298template<typename T>
300
301}
302}
303
304static const std::string testName = "";
306
307#undef BINDING_NAME
308#define BINDING_NAME(NAME) static \
309 mlpack::util::ProgramName \
310 io_programname_dummy_object = mlpack::util::ProgramName(NAME); \
311 namespace mlpack { \
312 namespace bindings { \
313 namespace go { \
314 std::string programName = NAME; \
315 } \
316 } \
317 }
318
319PARAM_FLAG("verbose", "Display informational messages and the full list of "
320 "parameters and timers at the end of execution.", "v");
321
322// Nothing else needs to be defined---the binding will use mlpackMain() as-is.
323
324#elif(BINDING_TYPE == BINDING_TYPE_R) // This is a R binding.
325
326// This doesn't actually matter for this binding type.
327#define BINDING_MATRIX_TRANSPOSED true
328
331
332#define PRINT_PARAM_STRING mlpack::bindings::r::ParamString
333#define PRINT_PARAM_VALUE mlpack::bindings::r::PrintValue
334#define PRINT_DATASET mlpack::bindings::r::PrintDataset
335#define PRINT_MODEL mlpack::bindings::r::PrintModel
336#define PRINT_CALL(...) mlpack::bindings::r::ProgramCall(false, __VA_ARGS__)
337#define BINDING_IGNORE_CHECK mlpack::bindings::r::IgnoreCheck
338
339namespace mlpack {
340namespace util {
341
342template<typename T>
344
345}
346}
347
348static const std::string testName = "";
350
351PARAM_FLAG("verbose", "Display informational messages and the full list of "
352 "parameters and timers at the end of execution.", "v");
353
354// Nothing else needs to be defined---the binding will use mlpackMain() as-is.
355
356#elif BINDING_TYPE == BINDING_TYPE_MARKDOWN
357
358// We use MARKDOWN_BINDING_NAME in BINDING_NAME(), BINDING_SHORT_DESC(),
359// BINDING_LONG_DESC(), BINDING_EXAMPLE() and BINDING_SEE_ALSO()
360// so it needs to be defined.
361#ifndef MARKDOWN_BINDING_NAME
362 #error "MARKDOWN_BINDING_NAME must be defined when BINDING_TYPE is Markdown!"
363#endif
364
365// This value doesn't actually matter, but it needs to be defined as something.
366#define BINDING_MATRIX_TRANSPOSED true
367
370
375#define PRINT_PARAM_STRING mlpack::bindings::markdown::ParamString
376
381#define PRINT_PARAM_VALUE mlpack::bindings::markdown::PrintValue
382
387#define PRINT_DATASET mlpack::bindings::markdown::PrintDataset
388
393#define PRINT_MODEL mlpack::bindings::markdown::PrintModel
394
402#define PRINT_CALL mlpack::bindings::markdown::ProgramCall
403
408#define BINDING_IGNORE_CHECK mlpack::bindings::markdown::IgnoreCheck
409
410// This doesn't actually matter for this binding type.
411#define BINDING_MATRIX_TRANSPOSED true
412
413namespace mlpack {
414namespace util {
415
416template<typename T>
418
419}
420}
421
424
425#undef BINDING_NAME
426#undef BINDING_SHORT_DESC
427#undef BINDING_LONG_DESC
428#undef BINDING_EXAMPLE
429#undef BINDING_SEE_ALSO
430
431#define BINDING_NAME(NAME) static \
432 mlpack::bindings::markdown::ProgramNameWrapper \
433 io_programname_dummy_object = \
434 mlpack::bindings::markdown::ProgramNameWrapper( \
435 MARKDOWN_BINDING_NAME, NAME);
436
437#define BINDING_SHORT_DESC(SHORT_DESC) static \
438 mlpack::bindings::markdown::ShortDescriptionWrapper \
439 io_programshort_desc_dummy_object = \
440 mlpack::bindings::markdown::ShortDescriptionWrapper( \
441 MARKDOWN_BINDING_NAME, SHORT_DESC);
442
443#define BINDING_LONG_DESC(LONG_DESC) static \
444 mlpack::bindings::markdown::LongDescriptionWrapper \
445 io_programlong_desc_dummy_object = \
446 mlpack::bindings::markdown::LongDescriptionWrapper( \
447 MARKDOWN_BINDING_NAME, []() { return std::string(LONG_DESC); });
448
449#ifdef __COUNTER__
450 #define BINDING_EXAMPLE(EXAMPLE) static \
451 mlpack::bindings::markdown::ExampleWrapper \
452 JOIN(io_programexample_dummy_object_, __COUNTER__) = \
453 mlpack::bindings::markdown::ExampleWrapper(MARKDOWN_BINDING_NAME, \
454 []() { return(std::string(EXAMPLE)); });
455
456 #define BINDING_SEE_ALSO(DESCRIPTION, LINK) static \
457 mlpack::bindings::markdown::SeeAlsoWrapper \
458 JOIN(io_programsee_also_dummy_object_, __COUNTER__) = \
459 mlpack::bindings::markdown::SeeAlsoWrapper(MARKDOWN_BINDING_NAME, \
460 DESCRIPTION, LINK);
461#else
462 #define BINDING_EXAMPLE(EXAMPLE) static \
463 mlpack::bindings::markdown::ExampleWrapper \
464 JOIN(JOIN(io_programexample_dummy_object_, __LINE__), opt) = \
465 mlpack::bindings::markdown::ExampleWrapper(MARKDOWN_BINDING_NAME, \
466 []() { return(std::string(EXAMPLE)); });
467
468 #define BINDING_SEE_ALSO(DESCRIPTION, LINK) static \
469 mlpack::bindings::markdown::SeeAlsoWrapper \
470 JOIN(JOIN(io_programsee_also_dummy_object_, __LINE__), opt) = \
471 mlpack::bindings::markdown::SeeAlsoWrapper(MARKDOWN_BINDING_NAME, \
472 DESCRIPTION, LINK);
473#endif
474
475PARAM_FLAG("verbose", "Display informational messages and the full list of "
476 "parameters and timers at the end of execution.", "v");
477
478// CLI-specific parameters.
479PARAM_FLAG("help", "Default help info.", "h");
480PARAM_STRING_IN("info", "Print help on a specific option.", "", "");
481PARAM_FLAG("version", "Display the version of mlpack.", "V");
482
483// Python-specific parameters.
484PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
485 " copied before the method is run. This is useful for debugging problems "
486 "where the input parameters are being modified by the algorithm, but can "
487 "slow down the code.", "");
488
489#else
490
491#error "Unknown binding type! Be sure BINDING_TYPE is defined if you are " \
492 "including <mlpack/core/util/mlpack_main.hpp>.";
493
494#endif
495
496#include "param_checks.hpp"
497
498#endif
static void EnableTiming()
Enable timing of mlpack programs.
static void Start(const std::string &name)
Start the given timer.
A static object whose constructor registers a parameter with the IO class.
Definition: cli_option.hpp:49
The Go option class.
Definition: go_option.hpp:40
The Markdown option class.
Definition: md_option.hpp:34
The Python option class.
Definition: py_option.hpp:38
The R option class.
Definition: R_option.hpp:32
A static object whose constructor registers a parameter with the IO class.
Definition: test_option.hpp:41
void EndProgram()
Handle command-line program termination.
Definition: end_program.hpp:26
void ParseCommandLine(int argc, char **argv)
Parse the command line, setting all of the options inside of the CLI object to their appropriate give...
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
#define PARAM_FLAG(ID, DESC, ALIAS)
Define a flag parameter.
Definition: param.hpp:189
#define PARAM_STRING_IN(ID, DESC, ALIAS, DEF)
Define a string input parameter.
Definition: param.hpp:335