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