PLSSVM - Parallel Least Squares Support Vector Machine  2.0.0
A Least Squares Support Vector Machine implementation using different backends.
performance_tracker.hpp
Go to the documentation of this file.
1 
13 #ifndef PLSSVM_DETAIL_PERFORMANCE_TRACKER_HPP_
14 #define PLSSVM_DETAIL_PERFORMANCE_TRACKER_HPP_
15 #pragma once
16 
17 #include "plssvm/detail/cmd/parser_predict.hpp" // plssvm::detail::cmd::parser_predict
18 #include "plssvm/detail/cmd/parser_scale.hpp" // plssvm::detail::cmd::parser_scale
19 #include "plssvm/detail/cmd/parser_train.hpp" // plssvm::detail::cmd::parser_train
20 #include "plssvm/detail/type_traits.hpp" // plssvm::detail::remove_cvref_t
21 
22 #include "fmt/chrono.h" // format std::chrono types
23 #include "fmt/core.h" // fmt::format
24 #include "fmt/ostream.h" // format types with an operator<< overload
25 
26 #include <memory> // std::shared_ptr
27 #include <string> // std::string
28 #include <string_view> // std::string_view
29 #include <type_traits> // std::false_type, std::true_type
30 #include <unordered_map> // std::unordered_multimap
31 #include <utility> // std::move
32 
33 namespace plssvm::detail {
34 
39 template <typename T>
47  tracking_entry(const std::string_view category, const std::string_view name, T value) :
48  entry_category{ category }, entry_name{ name }, entry_value{ std::move(value) } {}
49 
51  const std::string entry_category{};
53  const std::string_view entry_name{};
55  const T entry_value{};
56 };
57 
65 template <typename T>
66 std::ostream &operator<<(std::ostream &out, const tracking_entry<T> &entry) {
67  return out << fmt::format("{}", entry.entry_value);
68 }
69 
70 namespace impl {
71 
75 template <typename T>
76 struct is_tracking_entry : std::false_type {};
80 template <typename T>
81 struct is_tracking_entry<tracking_entry<T>> : std::true_type {};
82 
83 } // namespace impl
84 
89 template <typename T>
90 struct is_tracking_entry : impl::is_tracking_entry<detail::remove_cvref_t<T>> {};
95 template <typename T>
97 
103  public:
112 
119  template <typename T>
120  void add_tracking_entry(const tracking_entry<T> &entry);
156 
163  void save(const std::string &filename);
168  void save(std::ostream &out);
169 
173  void pause_tracking() noexcept;
177  void resume_tracking() noexcept;
182  [[nodiscard]] bool is_tracking() noexcept;
183 
188  [[nodiscard]] const std::unordered_multimap<std::string, std::string> &get_tracking_entries() noexcept;
189 
190  private:
192  std::unordered_multimap<std::string, std::string> tracking_statistics{};
194  bool is_tracking_{ true };
195 };
196 
197 template <typename T>
199  if (is_tracking()) {
200  tracking_statistics.emplace(entry.entry_category, fmt::format("{}{}: {}\n", entry.entry_category.empty() ? "" : " ", entry.entry_name, entry.entry_value));
201  }
202 }
203 
205 extern std::shared_ptr<performance_tracker> global_tracker;
206 
227 #if defined(PLSSVM_PERFORMANCE_TRACKER_ENABLED)
228 
229  #define PLSSVM_DETAIL_PERFORMANCE_TRACKER_PAUSE() \
230  ::plssvm::detail::global_tracker->pause_tracking()
231 
232  #define PLSSVM_DETAIL_PERFORMANCE_TRACKER_RESUME() \
233  ::plssvm::detail::global_tracker->resume_tracking()
234 
235  #define PLSSVM_DETAIL_PERFORMANCE_TRACKER_SAVE(filename) \
236  ::plssvm::detail::global_tracker->save(filename)
237 
238  #define PLSSVM_DETAIL_PERFORMANCE_TRACKER_ADD_TRACKING_ENTRY(entry) \
239  ::plssvm::detail::global_tracker->add_tracking_entry(entry);
240 #else
241 
242  #define PLSSVM_DETAIL_PERFORMANCE_TRACKER_PAUSE()
243  #define PLSSVM_DETAIL_PERFORMANCE_TRACKER_RESUME()
244  #define PLSSVM_DETAIL_PERFORMANCE_TRACKER_SAVE(filename)
245  #define PLSSVM_DETAIL_PERFORMANCE_TRACKER_ADD_TRACKING_ENTRY(entry)
246 
247 #endif
248 
249 } // namespace plssvm::detail
250 
251 #endif // PLSSVM_DETAIL_PERFORMANCE_TRACKER_HPP_
Store the tracked information during calls to plssvm-train, plssvm-predict, and plssvm-scale.
Definition: performance_tracker.hpp:102
void add_tracking_entry(const tracking_entry< std::string > &entry)
Add a tracking_entry encapsulating a std::string to this performance tracker.
bool is_tracking_
The tracking is enabled by default.
Definition: performance_tracker.hpp:194
const std::unordered_multimap< std::string, std::string > & get_tracking_entries() noexcept
Return the currently available tracking entries.
void add_tracking_entry(const tracking_entry< cmd::parser_scale > &entry)
Add a tracking_entry encapsulating a plssvm::detail::cmd::parser_scale to this performance tracker.
void pause_tracking() noexcept
Pause the current tracking, i.e., all calls to add_tracking_entry do nothing.
void save(std::ostream &out)
Write all stored tracking entries to the output stream out.
void add_tracking_entry(const tracking_entry< cmd::parser_train > &entry)
Add a tracking_entry encapsulating a plssvm::detail::cmd::parser_train to this performance tracker.
bool is_tracking() noexcept
Check whether tracking is currently active or paused.
void save(const std::string &filename)
Write all stored tracking entries to the YAML file filename.
void resume_tracking() noexcept
Resume the tracking, i.e., all calls to add_tracking_entry do track the values again.
void add_tracking_entry(const tracking_entry< T > &entry)
Add a tracking_entry to this performance tracker.
Definition: performance_tracker.hpp:198
void add_tracking_entry(const tracking_entry< cmd::parser_predict > &entry)
Add a tracking_entry encapsulating a plssvm::detail::cmd::parser_predict to this performance tracker.
void add_tracking_entry(const tracking_entry<::plssvm::parameter > &entry)
Add a tracking_entry encapsulating a plssvm::parameter to this performance tracker.
~performance_tracker()
Default destructor. Must be implemented in .cpp file.
std::unordered_multimap< std::string, std::string > tracking_statistics
All performance statistics grouped by their specified categories.
Definition: performance_tracker.hpp:192
Namespace containing implementation details. Should not directly be used by users.
Definition: csvm.hpp:27
std::shared_ptr< performance_tracker > global_tracker
The global performance tracker instance used for the default tracking.
std::ostream & operator<<(std::ostream &out, const execution_range &range)
Output the execution range to the given output-stream out.
constexpr bool is_tracking_entry_v
Definition: performance_tracker.hpp:96
constexpr std::string_view name
The name of the library.
Definition: version.hpp:26
Implements a class encapsulating all necessary parameters for predicting using the C-SVM possibly pro...
Implements a class encapsulating all necessary parameters for scaling a data set possibly provided th...
Implements a class encapsulating all necessary parameters for training the C-SVM possibly provided th...
Sets the value to false since it isn't a tracking entry.
Definition: performance_tracker.hpp:76
Check whether T is a tracking entry. Ignores all top-level const, volatile, and reference qualifiers.
Definition: performance_tracker.hpp:90
A single tracking entry containing a specific category, a unique name, and the actual value to be tra...
Definition: performance_tracker.hpp:40
const std::string_view entry_name
The name of the tracking entry displayed in the YAML file.
Definition: performance_tracker.hpp:53
const std::string entry_category
The category to which this tracking entry belongs; used for grouping in the resulting YAML file.
Definition: performance_tracker.hpp:51
const T entry_value
The tracked value in the YAML file.
Definition: performance_tracker.hpp:55
tracking_entry(const std::string_view category, const std::string_view name, T value)
Create a new tracking entry.
Definition: performance_tracker.hpp:47
Defines some generic type traits used in the PLSSVM library.