end_program.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_BINDINGS_CLI_END_PROGRAM_HPP
14 #define MLPACK_BINDINGS_CLI_END_PROGRAM_HPP
15 
16 #include <mlpack/core/util/cli.hpp>
17 
18 namespace mlpack {
19 namespace bindings {
20 namespace cli {
21 
26 inline void EndProgram()
27 {
28  // Stop the CLI timers.
30 
31  // Print any output.
32  const std::map<std::string, util::ParamData>& parameters = CLI::Parameters();
33  std::map<std::string, util::ParamData>::const_iterator it =
34  parameters.begin();
35  while (it != parameters.end())
36  {
37  const util::ParamData& d = it->second;
38  if (!d.input)
39  CLI::GetSingleton().functionMap[d.tname]["OutputParam"](d, NULL, NULL);
40 
41  ++it;
42  }
43 
44  if (CLI::HasParam("verbose"))
45  {
46  Log::Info << std::endl << "Execution parameters:" << std::endl;
47 
48  // Print out all the values.
49  it = parameters.begin();
50  while (it != parameters.end())
51  {
52  // Now, figure out what type it is, and print it.
53  // We can handle strings, ints, bools, doubles.
54  const util::ParamData& data = it->second;
55  std::string boostName;
56  CLI::GetSingleton().functionMap[data.tname]["MapParameterName"](data,
57  NULL, (void*) &boostName);
58  Log::Info << " " << boostName << ": ";
59 
60  std::string printableParam;
61  CLI::GetSingleton().functionMap[data.tname]["GetPrintableParam"](data,
62  NULL, (void*) &printableParam);
63  Log::Info << printableParam << std::endl;
64 
65  ++it;
66  }
67 
68  Log::Info << "Program timers:" << std::endl;
69  for (auto it2 : CLI::GetSingleton().timer.GetAllTimers())
70  {
71  Log::Info << " " << it2.first << ": ";
72  CLI::GetSingleton().timer.PrintTimer(it2.first);
73  }
74  }
75 
76  // Lastly clean up any memory. If we are holding any pointers, then we "own"
77  // them. But we may hold the same pointer twice, so we have to be careful to
78  // not delete it multiple times.
79  std::unordered_map<void*, const util::ParamData*> memoryAddresses;
80  it = parameters.begin();
81  while (it != parameters.end())
82  {
83  const util::ParamData& data = it->second;
84 
85  void* result;
86  CLI::GetSingleton().functionMap[data.tname]["GetAllocatedMemory"](data,
87  NULL, (void*) &result);
88  if (result != NULL && memoryAddresses.count(result) == 0)
89  memoryAddresses[result] = &data;
90 
91  ++it;
92  }
93 
94  // Now we have all the unique addresses that need to be deleted.
95  std::unordered_map<void*, const util::ParamData*>::const_iterator it2;
96  it2 = memoryAddresses.begin();
97  while (it2 != memoryAddresses.end())
98  {
99  const util::ParamData& data = *(it2->second);
100 
101  CLI::GetSingleton().functionMap[data.tname]["DeleteAllocatedMemory"](data,
102  NULL, NULL);
103 
104  ++it2;
105  }
106 }
107 
108 } // namespace cli
109 } // namespace bindings
110 } // namespace mlpack
111 
112 #endif
void EndProgram()
Handle command-line program termination.
Definition: end_program.hpp:26
std::map< std::string, std::chrono::microseconds > GetAllTimers()
Returns a copy of all the timers used via this interface.
void StopAllTimers()
Stop all timers.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_po.hpp:21
static CLI & GetSingleton()
Retrieve the singleton.
void PrintTimer(const std::string &timerName)
Prints the specified timer.
Timers timer
Holds the timer objects.
Definition: cli.hpp:318
static std::map< std::string, util::ParamData > & Parameters()
Return a modifiable list of parameters that CLI knows about.
bool input
True if this option is an input option (otherwise, it is output).
Definition: param_data.hpp:73
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
std::string tname
Type information of this parameter.
Definition: param_data.hpp:61
static bool HasParam(const std::string &identifier)
See if the specified flag was found while parsing.
FunctionMapType functionMap
Definition: cli.hpp:302