mlpack  3.1.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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_MARKDOWN 128
26 #define BINDING_TYPE_UNKNOWN -1
27 
28 #ifndef BINDING_TYPE
29 #define BINDING_TYPE BINDING_TYPE_UNKNOWN
30 #endif
31 
32 #if (BINDING_TYPE == BINDING_TYPE_CLI) // This is a command-line executable.
33 
34 // Matrices are transposed on load/save.
35 #define BINDING_MATRIX_TRANSPOSED true
36 
39 
44 #define PRINT_PARAM_STRING mlpack::bindings::cli::ParamString
45 
50 #define PRINT_PARAM_VALUE mlpack::bindings::cli::PrintValue
51 
59 #define PRINT_CALL mlpack::bindings::cli::ProgramCall
60 
65 #define PRINT_DATASET mlpack::bindings::cli::PrintDataset
66 
71 #define PRINT_MODEL mlpack::bindings::cli::PrintModel
72 
77 #define BINDING_IGNORE_CHECK mlpack::bindings::cli::IgnoreCheck
78 
79 namespace mlpack {
80 namespace util {
81 
82 template<typename T>
84 
85 }
86 }
87 
88 static const std::string testName = "";
92 
93 static void mlpackMain(); // This is typically defined after this include.
94 
95 int main(int argc, char** argv)
96 {
97  // Parse the command-line options; put them into CLI.
99  // Enable timing.
101 
102  // A "total_time" timer is run by default for each mlpack program.
103  mlpack::Timer::Start("total_time");
104 
105  mlpackMain();
106 
107  // Print output options, print verbose information, save model parameters,
108  // clean up, and so forth.
110 }
111 
112 #elif(BINDING_TYPE == BINDING_TYPE_TEST) // This is a unit test.
113 
114 // Matrices are not transposed on load/save.
115 #define BINDING_MATRIX_TRANSPOSED false
116 
120 
121 // These functions will do nothing.
122 #define PRINT_PARAM_STRING(A) std::string(" ")
123 #define PRINT_PARAM_VALUE(A, B) std::string(" ")
124 #define PRINT_DATASET(A) std::string(" ")
125 #define PRINT_MODEL(A) std::string(" ")
126 
134 #define PRINT_CALL(...) std::string(" ")
135 
140 #define BINDING_IGNORE_CHECK mlpack::bindings::tests::IgnoreCheck
141 
142 namespace mlpack {
143 namespace util {
144 
145 template<typename T>
147 
148 }
149 }
150 
151 // testName symbol should be defined in each binding test file
153 
154 #undef PROGRAM_INFO
155 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) \
156  static mlpack::util::ProgramDoc \
157  cli_programdoc_dummy_object = mlpack::util::ProgramDoc(NAME, SHORT_DESC, \
158  []() { return DESC; }, { __VA_ARGS__ })
159 
160 #elif(BINDING_TYPE == BINDING_TYPE_PYX) // This is a Python binding.
161 
162 // Matrices are transposed on load/save.
163 #define BINDING_MATRIX_TRANSPOSED true
164 
167 
172 #define PRINT_PARAM_STRING mlpack::bindings::python::ParamString
173 
178 #define PRINT_PARAM_VALUE mlpack::bindings::python::PrintValue
179 
184 #define PRINT_DATASET mlpack::bindings::python::PrintDataset
185 
190 #define PRINT_MODEL mlpack::bindings::python::PrintModel
191 
199 #define PRINT_CALL mlpack::bindings::python::ProgramCall
200 
205 #define BINDING_IGNORE_CHECK mlpack::bindings::python::IgnoreCheck
206 
207 namespace mlpack {
208 namespace util {
209 
210 template<typename T>
212 
213 }
214 }
215 
216 static const std::string testName = "";
218 
219 #undef PROGRAM_INFO
220 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) \
221  static mlpack::util::ProgramDoc \
222  cli_programdoc_dummy_object = mlpack::util::ProgramDoc(NAME, SHORT_DESC, \
223  []() { return DESC; }, { __VA_ARGS__ }); \
224  namespace mlpack { \
225  namespace bindings { \
226  namespace python { \
227  std::string programName = NAME; \
228  } \
229  } \
230  }
231 
232 PARAM_FLAG("verbose", "Display informational messages and the full list of "
233  "parameters and timers at the end of execution.", "v");
234 PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
235  " copied before the method is run. This is useful for debugging problems "
236  "where the input parameters are being modified by the algorithm, but can "
237  "slow down the code.", "");
238 
239 // Nothing else needs to be defined---the binding will use mlpackMain() as-is.
240 
241 #elif(BINDING_TYPE == BINDING_TYPE_JL) // This is a Julia binding.
242 
243 // Matrices are transposed on load/save.
244 #define BINDING_MATRIX_TRANSPOSED true
245 
248 
249 #define PRINT_PARAM_STRING mlpack::bindings::julia::ParamString
250 #define PRINT_PARAM_VALUE mlpack::bindings::julia::PrintValue
251 #define PRINT_DATASET mlpack::bindings::julia::PrintDataset
252 #define PRINT_MODEL mlpack::bindings::julia::PrintModel
253 #define PRINT_CALL mlpack::bindings::julia::ProgramCall
254 #define BINDING_IGNORE_CHECK mlpack::bindings::julia::IgnoreCheck
255 
256 namespace mlpack {
257 namespace util {
258 
259 template<typename T>
261 
262 }
263 }
264 
265 static const std::string testName = "";
267 
268 #undef PROGRAM_INFO
269 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) static \
270  mlpack::util::ProgramDoc \
271  cli_programdoc_dummy_object = mlpack::util::ProgramDoc(NAME, SHORT_DESC, \
272  []() { return DESC; }, { __VA_ARGS__ }); \
273  namespace mlpack { \
274  namespace bindings { \
275  namespace julia { \
276  std::string programName = NAME; \
277  } \
278  } \
279  }
280 
281 PARAM_FLAG("verbose", "Display informational messages and the full list of "
282  "parameters and timers at the end of execution.", "v");
283 
284 #elif BINDING_TYPE == BINDING_TYPE_MARKDOWN
285 
286 // We use BINDING_NAME in PROGRAM_INFO() so it needs to be defined.
287 #ifndef BINDING_NAME
288  #error "BINDING_NAME must be defined when BINDING_TYPE is Markdown!"
289 #endif
290 
291 // This value doesn't actually matter, but it needs to be defined as something.
292 #define BINDING_MATRIX_TRANSPOSED true
293 
296 
301 #define PRINT_PARAM_STRING mlpack::bindings::markdown::ParamString
302 
307 #define PRINT_PARAM_VALUE mlpack::bindings::markdown::PrintValue
308 
313 #define PRINT_DATASET mlpack::bindings::markdown::PrintDataset
314 
319 #define PRINT_MODEL mlpack::bindings::markdown::PrintModel
320 
328 #define PRINT_CALL mlpack::bindings::markdown::ProgramCall
329 
334 #define BINDING_IGNORE_CHECK mlpack::bindings::markdown::IgnoreCheck
335 
336 // This doesn't actually matter for this binding type.
337 #define BINDING_MATRIX_TRANSPOSED true
338 
339 namespace mlpack {
340 namespace util {
341 
342 template<typename T>
344 
345 }
346 }
347 
350 
351 #undef PROGRAM_INFO
352 #define PROGRAM_INFO(NAME, SHORT_DESC, DESC, ...) static \
353  mlpack::bindings::markdown::ProgramDocWrapper \
354  cli_programdoc_dummy_object = \
355  mlpack::bindings::markdown::ProgramDocWrapper(BINDING_NAME, NAME, \
356  SHORT_DESC, []() { return DESC; }, { __VA_ARGS__ }); \
357 
358 PARAM_FLAG("verbose", "Display informational messages and the full list of "
359  "parameters and timers at the end of execution.", "v");
360 
361 // CLI-specific parameters.
362 PARAM_FLAG("help", "Default help info.", "h");
363 PARAM_STRING_IN("info", "Print help on a specific option.", "", "");
364 PARAM_FLAG("version", "Display the version of mlpack.", "V");
365 
366 // Python-specific parameters.
367 PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
368  " copied before the method is run. This is useful for debugging problems "
369  "where the input parameters are being modified by the algorithm, but can "
370  "slow down the code.", "");
371 
372 #else
373 
374 #error "Unknown binding type! Be sure BINDING_TYPE is defined if you are " \
375  "including <mlpack/core/util/mlpack_main.hpp>.";
376 
377 #endif
378 
379 #include "param_checks.hpp"
380 
381 #endif
void EndProgram()
Handle command-line program termination.
Definition: end_program.hpp:26
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.
string(REGEX REPLACE".*#define MLPACK_VERSION_MINOR ([0-9]+).*""\\1"MLPACK_VERSION_MINOR"${VERSION_HPP_CONTENTS}") string(REGEX REPLACE".* "\\1" MLPACK_VERSION_PATCH "$
Definition: CMakeLists.txt:35