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/io.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  std::map& parameters = IO::Parameters();
33  for (auto& it : parameters)
34  {
35  util::ParamData& d = it.second;
36  if (!d.input)
37  IO::GetSingleton().functionMap[d.tname]["OutputParam"](d, NULL, NULL);
38  }
39 
40  if (IO::HasParam("verbose"))
41  {
42  Log::Info << std::endl << "Execution parameters:" << std::endl;
43 
44  // Print out all the values.
45  for (auto& it : parameters)
46  {
47  // Now, figure out what type it is, and print it.
48  // We can handle strings, ints, bools, doubles.
49  util::ParamData& data = it.second;
50  std::string cliName;
51  IO::GetSingleton().functionMap[data.tname]["MapParameterName"](data,
52  NULL, (void*) &cliName);
53  Log::Info << " " << cliName << ": ";
54 
55  std::string printableParam;
56  IO::GetSingleton().functionMap[data.tname]["GetPrintableParam"](data,
57  NULL, (void*) &printableParam);
58  Log::Info << printableParam << std::endl;
59  }
60 
61  Log::Info << "Program timers:" << std::endl;
62  for (auto& it2 : IO::GetSingleton().timer.GetAllTimers())
63  {
64  Log::Info << " " << it2.first << ": ";
65  IO::GetSingleton().timer.PrintTimer(it2.first);
66  }
67  }
68 
69  // Lastly clean up any memory. If we are holding any pointers, then we "own"
70  // them. But we may hold the same pointer twice, so we have to be careful to
71  // not delete it multiple times.
72  std::unordered_map memoryAddresses;
73  for (auto& it : parameters)
74  {
75  util::ParamData& data = it.second;
76 
77  void* result;
78  IO::GetSingleton().functionMap[data.tname]["GetAllocatedMemory"](data,
79  NULL, (void*) &result);
80  if (result != NULL && memoryAddresses.count(result) == 0)
81  memoryAddresses[result] = &data;
82  }
83 
84  // Now we have all the unique addresses that need to be deleted.
85  std::unordered_map::const_iterator it2;
86  it2 = memoryAddresses.begin();
87  while (it2 != memoryAddresses.end())
88  {
89  util::ParamData& data = *(it2->second);
90 
91  IO::GetSingleton().functionMap[data.tname]["DeleteAllocatedMemory"](data,
92  NULL, NULL);
93 
94  ++it2;
95  }
96 }
97 
98 } // namespace cli
99 } // namespace bindings
100 } // namespace mlpack
101 
102 #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.
void PrintTimer(const std::string &timerName)
Prints the specified timer.
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
static IO & GetSingleton()
Retrieve the singleton.
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.
Timers timer
Holds the timer objects.
Definition: io.hpp:315
FunctionMapType functionMap
Definition: io.hpp:299
static std::map< std::string, util::ParamData > & Parameters()
Return a modifiable list of parameters that IO knows about.