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(util::Params& params, util::Timers& timers)
27 {
28  // Stop the timers.
29  timers.StopAllTimers();
30 
31  // Print any output.
32  std::map<std::string, util::ParamData>& parameters = params.Parameters();
33  for (auto& it : parameters)
34  {
35  util::ParamData& d = it.second;
36  if (!d.input)
37  params.functionMap[d.tname]["OutputParam"](d, NULL, NULL);
38  }
39 
40  if (params.Has("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  params.functionMap[data.tname]["MapParameterName"](data, NULL,
52  (void*) &cliName);
53  Log::Info << " " << cliName << ": ";
54 
55  std::string printableParam;
56  params.functionMap[data.tname]["GetPrintableParam"](data, NULL,
57  (void*) &printableParam);
58  Log::Info << printableParam << std::endl;
59  }
60 
61  Log::Info << "Program timers:" << std::endl;
62 
63  // Merge the global timers with the binding-specific ones.
64  std::map<std::string, std::chrono::microseconds> timerMap =
65  timers.GetAllTimers();
66  std::map<std::string, std::chrono::microseconds> globalTimerMap =
68  for (auto& it : globalTimerMap)
69  {
70  if (timerMap.count(it.first) == 1)
71  timerMap[it.first] += it.second;
72  else
73  timerMap[it.first] = it.second;
74  }
75 
76  for (auto& it2 : timerMap)
77  {
78  Log::Info << " " << it2.first << ": " << timers.Print(it2.second);
79  }
80  }
81 
82  // Lastly clean up any memory. If we are holding any pointers, then we "own"
83  // them. But we may hold the same pointer twice, so we have to be careful to
84  // not delete it multiple times.
85  std::unordered_map<void*, util::ParamData*> memoryAddresses;
86  for (auto& it : parameters)
87  {
88  util::ParamData& data = it.second;
89 
90  void* result;
91  params.functionMap[data.tname]["GetAllocatedMemory"](data, NULL,
92  (void*) &result);
93  if (result != NULL && memoryAddresses.count(result) == 0)
94  memoryAddresses[result] = &data;
95  }
96 
97  // Now we have all the unique addresses that need to be deleted.
98  std::unordered_map<void*, util::ParamData*>::const_iterator it2;
99  it2 = memoryAddresses.begin();
100  while (it2 != memoryAddresses.end())
101  {
102  util::ParamData& data = *(it2->second);
103 
104  params.functionMap[data.tname]["DeleteAllocatedMemory"](data, NULL, NULL);
105 
106  ++it2;
107  }
108 }
109 
110 } // namespace cli
111 } // namespace bindings
112 } // namespace mlpack
113 
114 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
static std::map< std::string, std::chrono::microseconds > GetAllTimers()
Returns a copy of all the timers used via this interface.
static std::string Print(const std::chrono::microseconds &totalDuration)
Prints the specified timer.
void StopAllTimers()
Stop all timers.
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
std::map< std::string, ParamData > & Parameters()
Get the map of parameters.
Definition: params.hpp:96
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
bool Has(const std::string &identifier) const
Return true if the specified parameter was given.
std::string tname
Type information of this parameter.
Definition: param_data.hpp:61
std::map< std::string, std::chrono::microseconds > GetAllTimers()
Returns a copy of all the timers used via this interface.
FunctionMapType functionMap
Map for functions and types.
Definition: params.hpp:130
void EndProgram(util::Params &params, util::Timers &timers)
Handle command-line program termination.
Definition: end_program.hpp:26
The Params class holds all information about the parameters passed to a specific binding.
Definition: params.hpp:20