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 
39 #define PRINT_PARAM_STRING mlpack::bindings::cli::ParamString
40 #define PRINT_PARAM_VALUE mlpack::bindings::cli::PrintValue
41 #define PRINT_CALL mlpack::bindings::cli::ProgramCall
42 #define PRINT_DATASET mlpack::bindings::cli::PrintDataset
43 #define PRINT_MODEL mlpack::bindings::cli::PrintModel
44 #define BINDING_IGNORE_CHECK mlpack::bindings::cli::IgnoreCheck
45 
46 namespace mlpack {
47 namespace util {
48 
49 template<typename T>
51 
52 }
53 }
54 
55 static const std::string testName = "";
59 
60 static void mlpackMain(); // This is typically defined after this include.
61 
62 int main(int argc, char** argv)
63 {
64  // Parse the command-line options; put them into CLI.
66  // Enable timing.
68 
69  // A "total_time" timer is run by default for each mlpack program.
70  mlpack::Timer::Start("total_time");
71 
72  mlpackMain();
73 
74  // Print output options, print verbose information, save model parameters,
75  // clean up, and so forth.
77 }
78 
79 #elif(BINDING_TYPE == BINDING_TYPE_TEST) // This is a unit test.
80 
81 // Matrices are not transposed on load/save.
82 #define BINDING_MATRIX_TRANSPOSED false
83 
87 
88 // These functions will do nothing.
89 #define PRINT_PARAM_STRING(A) std::string(" ")
90 #define PRINT_PARAM_VALUE(A, B) std::string(" ")
91 #define PRINT_DATASET(A) std::string(" ")
92 #define PRINT_MODEL(A) std::string(" ")
93 #define PRINT_CALL(...) std::string(" ")
94 #define BINDING_IGNORE_CHECK mlpack::bindings::tests::IgnoreCheck
95 
96 namespace mlpack {
97 namespace util {
98 
99 template<typename T>
101 
102 }
103 }
104 
105 // testName symbol should be defined in each binding test file
107 
108 #undef PROGRAM_INFO
109 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) \
110  static mlpack::util::ProgramDoc \
111  cli_programdoc_dummy_object = mlpack::util::ProgramDoc(NAME, SHORT_DESC, \
112  []() { return DESC; }, { __VA_ARGS__ })
113 
114 #elif(BINDING_TYPE == BINDING_TYPE_PYX) // This is a Python binding.
115 
116 // Matrices are transposed on load/save.
117 #define BINDING_MATRIX_TRANSPOSED true
118 
121 
122 #define PRINT_PARAM_STRING mlpack::bindings::python::ParamString
123 #define PRINT_PARAM_VALUE mlpack::bindings::python::PrintValue
124 #define PRINT_DATASET mlpack::bindings::python::PrintDataset
125 #define PRINT_MODEL mlpack::bindings::python::PrintModel
126 #define PRINT_CALL mlpack::bindings::python::ProgramCall
127 #define BINDING_IGNORE_CHECK mlpack::bindings::python::IgnoreCheck
128 
129 namespace mlpack {
130 namespace util {
131 
132 template<typename T>
134 
135 }
136 }
137 
138 static const std::string testName = "";
140 
141 #undef PROGRAM_INFO
142 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) \
143  static mlpack::util::ProgramDoc \
144  cli_programdoc_dummy_object = mlpack::util::ProgramDoc(NAME, SHORT_DESC, \
145  []() { return DESC; }, { __VA_ARGS__ }); \
146  namespace mlpack { \
147  namespace bindings { \
148  namespace python { \
149  std::string programName = NAME; \
150  } \
151  } \
152  }
153 
154 PARAM_FLAG("verbose", "Display informational messages and the full list of "
155  "parameters and timers at the end of execution.", "v");
156 PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
157  " copied before the method is run. This is useful for debugging problems "
158  "where the input parameters are being modified by the algorithm, but can "
159  "slow down the code.", "");
160 
161 // Nothing else needs to be defined---the binding will use mlpackMain() as-is.
162 
163 #elif BINDING_TYPE == BINDING_TYPE_MARKDOWN
164 
165 // We use BINDING_NAME in PROGRAM_INFO() so it needs to be defined.
166 #ifndef BINDING_NAME
167  #error "BINDING_NAME must be defined when BINDING_TYPE is Markdown!"
168 #endif
169 
172 
173 #define PRINT_PARAM_STRING mlpack::bindings::markdown::ParamString
174 #define PRINT_PARAM_VALUE mlpack::bindings::markdown::PrintValue
175 #define PRINT_DATASET mlpack::bindings::markdown::PrintDataset
176 #define PRINT_MODEL mlpack::bindings::markdown::PrintModel
177 #define PRINT_CALL mlpack::bindings::markdown::ProgramCall
178 #define BINDING_IGNORE_CHECK mlpack::bindings::markdown::IgnoreCheck
179 
180 // This doesn't actually matter for this binding type.
181 #define BINDING_MATRIX_TRANSPOSED true
182 
183 namespace mlpack {
184 namespace util {
185 
186 template<typename T>
188 
189 }
190 }
191 
194 
195 #undef PROGRAM_INFO
196 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) static \
197  mlpack::bindings::markdown::ProgramDocWrapper \
198  cli_programdoc_dummy_object = \
199  mlpack::bindings::markdown::ProgramDocWrapper(BINDING_NAME, NAME, \
200  SHORT_DESC, []() { return DESC; }, { __VA_ARGS__ }); \
201 
202 PARAM_FLAG("verbose", "Display informational messages and the full list of "
203  "parameters and timers at the end of execution.", "v");
204 
205 // CLI-specific parameters.
206 PARAM_FLAG("help", "Default help info.", "h");
207 PARAM_STRING_IN("info", "Print help on a specific option.", "", "");
208 PARAM_FLAG("version", "Display the version of mlpack.", "V");
209 
210 // Python-specific parameters.
211 PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
212  " copied before the method is run. This is useful for debugging problems "
213  "where the input parameters are being modified by the algorithm, but can "
214  "slow down the code.", "");
215 
216 #else
217 
218 #error "Unknown binding type! Be sure BINDING_TYPE is defined if you are " \
219  "including <mlpack/core/util/mlpack_main.hpp>.";
220 
221 #endif
222 
223 #include "param_checks.hpp"
224 
225 #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:223
#define PARAM_FLAG(ID, DESC, ALIAS)
Define a flag parameter.
Definition: param.hpp:93
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.