DiSMEC++
collection.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_COLLECTION_H
7 #define DISMEC_COLLECTION_H
8 
9 #include "stats_base.h"
10 #include <vector>
11 #include <type_traits>
12 #include <memory>
13 #include "matrix_types.h"
14 #include "stat_id.h"
15 
16 namespace dismec::stats
17 {
48  public:
49  // declaration and registration of stats
60  void declare_stat(stat_id_t index, StatisticMetaData meta);
61 
67  void declare_tag(tag_id_t index, std::string name);
68 
78  void register_stat(const std::string& name, std::unique_ptr<Statistics> stat);
79 
80  // access stats
82  [[nodiscard]] const std::vector<StatisticMetaData>& get_statistics_meta() const { return m_MetaData; }
83 
88  [[nodiscard]] const Statistics& get_stat(const std::string& name) const;
89 
98  [[nodiscard]] TagContainer get_tag_by_name(const std::string& name) const;
99 
101  [[nodiscard]] const std::vector<TagContainer>& get_all_tags() const { return m_TagValues; }
102 
109  void provide_tags(const StatisticsCollection& other);
110 
111  // enable / disable
119  void enable(stat_id_t stat);
120 
123  void disable(stat_id_t stat);
124 
131  [[nodiscard]] bool is_enabled(stat_id_t stat) const {
132 #ifdef NDEBUG
133  return m_Enabled[stat.to_index()];
134 #else
135  // additional checking for debug builds.
136  return m_Enabled.at(stat.to_index());
137 #endif
138  }
139 
144  void enable(const std::string& stat);
145 
150  void disable(const std::string& stat);
151 
155  [[nodiscard]] bool has_stat(const std::string& name) const;
156 
163  [[nodiscard]] bool is_enabled_by_name(const std::string& name) const;
164 
165  // recording
173  template<class T, std::enable_if_t<!std::is_invocable_v<T>, bool> = true>
174  void record(stat_id_t stat, T&& value) {
175  if(is_enabled(stat)) {
176  do_record(stat.to_index(), std::forward<T>(value));
177  }
178  }
179 
197  template<class F, std::enable_if_t<std::is_invocable_v<F>, bool> = true>
198  void record(stat_id_t stat, F&& callable) {
199  if(is_enabled(stat)) {
200  // we wrap callable into an immediately invoked lambda, which is annotated as cold, so that
201  // the code-path with stat disabled is optimized.
202  do_record(stat.to_index(), [&]() __attribute__((cold)) { return callable(); }());
203  }
204  }
205 
209  void set_tag(tag_id_t tag, int value) {
210 #ifdef NDEBUG
211  m_TagValues[tag.to_index()].set_value(value);
212 #else
213  m_TagValues.at(tag.to_index()).set_value(value);
214 #endif
215  }
216 
217 
218  private:
219  template<class T>
220  void do_record(int index, T&& value) {
221  m_Statistics[index]->record(std::forward<T>(value));
222  }
223 
224  std::vector<bool> m_Enabled;
225  std::vector<StatisticMetaData> m_MetaData;
226  std::vector<std::unique_ptr<Statistics>> m_Statistics;
227 
228  std::vector<TagContainer> m_TagValues;
229  std::unordered_map<std::string, TagContainer> m_TagLookup;
230  };
231 }
232 
233 #endif //DISMEC_COLLECTION_H
constexpr T to_index() const
! Explicitly convert to an integer.
Definition: opaque_int.h:32
This class manages a collection of named Statistics objects.
Definition: collection.h:47
void declare_tag(tag_id_t index, std::string name)
Declares a new tag value.
Definition: collection.cpp:87
std::vector< StatisticMetaData > m_MetaData
Definition: collection.h:225
TagContainer get_tag_by_name(const std::string &name) const
Gets the tag with the given name.
Definition: collection.cpp:105
bool is_enabled(stat_id_t stat) const
Quickly checks whether collection of data is enabled for the given statistics.
Definition: collection.h:131
std::vector< std::unique_ptr< Statistics > > m_Statistics
Definition: collection.h:226
void do_record(int index, T &&value)
Definition: collection.h:220
const Statistics & get_stat(const std::string &name) const
Returns the Statistics object corresponding to the slot name.
Definition: collection.cpp:75
void record(stat_id_t stat, F &&callable)
Records a value that is computed only when needed.
Definition: collection.h:198
void provide_tags(const StatisticsCollection &other)
Registers all the tags of the other collection as read-only tags in this collection.
Definition: collection.cpp:109
void disable(stat_id_t stat)
Explicitly disable the collection of statistics for the given index.
Definition: collection.cpp:18
const std::vector< StatisticMetaData > & get_statistics_meta() const
Gets a vector with the declarations for all statistics.
Definition: collection.h:82
const std::vector< TagContainer > & get_all_tags() const
Gets a vector which contains all the tags owned by this collection.
Definition: collection.h:101
bool is_enabled_by_name(const std::string &name) const
Checks whether gathering data is enabled based on the statistic's name.
Definition: collection.cpp:83
void declare_stat(stat_id_t index, StatisticMetaData meta)
Declares a new statistics. Defines the corresponding index and name.
Definition: collection.cpp:40
std::vector< TagContainer > m_TagValues
Definition: collection.h:228
void enable(stat_id_t stat)
Explicitly enable the collection of statistics for the given index.
Definition: collection.cpp:12
bool has_stat(const std::string &name) const
Returns whether a stat with the given name is declared.
Definition: collection.cpp:122
void set_tag(tag_id_t tag, int value)
Sets the tag to the given integer value.
Definition: collection.h:209
void record(stat_id_t stat, T &&value)
Records an already computed value.
Definition: collection.h:174
void register_stat(const std::string &name, std::unique_ptr< Statistics > stat)
Registers a Statistics object to the named slot.
Definition: collection.cpp:59
std::unordered_map< std::string, TagContainer > m_TagLookup
Definition: collection.h:229
TODO maybe we should solve this with a variant which does the dispatch of expected type and tag.
Definition: stats_base.h:65
A tag container combines a name with a shared pointer, which points to the tag value.
Definition: stats_base.h:30
void __attribute__((hot)) htd_sum_new(const std
Data that is associated with each declared statistics.
Definition: stat_id.h:33