DiSMEC++
|
A base class to be used for all types that implement some for of statistics tracking. More...
#include <tracked.h>
Public Member Functions | |
Tracked () | |
Default constructor, creates the internal stats::StatisticsCollection . More... | |
void | register_stat (const std::string &name, std::unique_ptr< Statistics > stat) |
Registers a tracker for the statistics name . More... | |
std::shared_ptr< StatisticsCollection > | get_stats () const |
Gets an ownership-sharing reference to the StatisticsCollection . More... | |
Protected Member Functions | |
~Tracked () | |
Non-virtual destructor. Declared protected, so we don't accidentally try to do a polymorphic delete. More... | |
template<class T > | |
void | record (stat_id_t stat, T &&value) |
Record statistics. This function just forwards all its arguments to the internal StatisticsCollection . More... | |
void | declare_stat (stat_id_t index, StatisticMetaData meta) |
Declares a new statistics. This function just forwards all its arguments to the internal StatisticsCollection . More... | |
void | declare_tag (tag_id_t index, std::string name) |
Declares a new tag. This function just forwards all its arguments to the internal StatisticsCollection . More... | |
template<class... Args> | |
void | set_tag (tag_id_t tag, long value) |
Set value of tag. This function just forwards all its arguments to the internal StatisticsCollection . More... | |
template<class... Args> | |
auto | make_timer (stat_id_t id, Args... args) |
Creates a new ScopeTimer using stats::record_scope_time . More... | |
Private Member Functions | |
template<class T , class... Args> | |
T & | make_dependent (T &t) |
Given an object T, and some dummy template parameters Args, returns T unchanged. More... | |
Private Attributes | |
std::shared_ptr< StatisticsCollection > | m_Collection |
The internal collection. Filled with a new StatisticsCollection in the constructor. More... | |
A base class to be used for all types that implement some for of statistics tracking.
Provides the protected methods record()
, declare_stat()
, and make_timer()
that relay the corresponding action to the underlying stats::StatisticsCollection
, to deriving classes. Publicly available is only the option to register a Statistics
object for a given statistics name, via register_stat()
, and read-only access to the accumulator.
The implementation of Tracked is a bit tricky because it is trying to achieve too things:
record()
and make_timer()
which will appear in performance-critical code. For that reason, they are defined inline.These two goals are at odds in a naive implementation: In order to define an inline function, we cannot have an incomplete type that is accessed or returned. This is circumvented by defining these functions as templates, in such a way that the problematic call is (nominally) dependent on one of the template parameters, and thus the types only need be available when the function is actually called. This means that the collection.h
and timer.h
headers need only be included in the implementation files.
Note that this class is non-virtual. It is used as a convenient way of adding functionality to other types. It adds very convenient protected and public member functions. Its destructor is defined protected, so that one does not accidentally try to use it polymorphically.
Tracked::Tracked | ( | ) |
Default constructor, creates the internal stats::StatisticsCollection
.
Definition at line 13 of file tracked.cpp.
|
protecteddefault |
Non-virtual destructor. Declared protected, so we don't accidentally try to do a polymorphic delete.
|
protected |
Declares a new statistics. This function just forwards all its arguments to the internal StatisticsCollection
.
Definition at line 16 of file tracked.cpp.
References m_Collection.
Referenced by dismec::objective::DenseAndSparseLinearBase::DenseAndSparseLinearBase(), dismec::objective::GenericLinearClassifier::GenericLinearClassifier(), dismec::objective::LinearClassifierBase::LinearClassifierBase(), dismec::init::MeanOfFeaturesInitializer::MeanOfFeaturesInitializer(), dismec::init::MultiPosMeanInitializer< Sparse >::MultiPosMeanInitializer(), dismec::solvers::NewtonWithLineSearch::NewtonWithLineSearch(), dismec::objective::Objective::Objective(), dismec::objective::Regularized_SquaredHingeSVC::Regularized_SquaredHingeSVC(), and dismec::postproc::Sparsify::Sparsify().
|
protected |
Declares a new tag. This function just forwards all its arguments to the internal StatisticsCollection
.
Definition at line 24 of file tracked.cpp.
References m_Collection.
Referenced by dismec::solvers::NewtonWithLineSearch::NewtonWithLineSearch(), and dismec::solvers::NullOptimizer::NullOptimizer().
|
inline |
Gets an ownership-sharing reference to the StatisticsCollection
.
We return a std::shared_ptr
, instead of a plain reference, to accommodate the usage scenario in which some global object collects references to all the (thread_local) individual tracked objects, and combines their results after the threads have ended. By storing as a std::shared_ptr
, we can just collect all sub-objects during the setup phase, and do the merging after the threads have finished, without worrying about the lifetime of the involved objects. If we were to merge as each thread finishes and we are sure the objects are still alive, the (possibly costly) merging process would need to be mutex protected. To prevent this, we use shared ownership, but allow the global object only read access.
Definition at line 65 of file tracked.h.
References m_Collection.
Referenced by dismec::TrainingStatsGatherer::setup_initializer(), dismec::TrainingStatsGatherer::setup_minimizer(), and dismec::TrainingStatsGatherer::setup_postproc().
|
inlineprivate |
Given an object T, and some dummy template parameters Args, returns T unchanged.
The purpose of this function is to get a dependent value from a direct value, so that any calls on it will only be looked at in the second phase of template instantiation.
Definition at line 75 of file tracked.h.
Referenced by set_tag().
|
inlineprotected |
Creates a new ScopeTimer
using stats::record_scope_time
.
Args | Dummy template parameter pack. Needs to be empty, but has to be used in this definition so that the resulting call to record_scope_time() is only looked up in the second phase, where the function is actually instantiated. |
Definition at line 130 of file tracked.h.
References m_Collection, and dismec::stats::record_scope_time().
Referenced by dismec::objective::Objective::diag_preconditioner(), dismec::init::MeanOfFeaturesInitializer::get_initial_weight(), dismec::objective::Objective::gradient(), dismec::objective::Objective::gradient_and_pre_conditioner(), dismec::objective::Objective::gradient_at_zero(), dismec::objective::Objective::hessian_times_direction(), dismec::postproc::Sparsify::process(), dismec::objective::Objective::project_to_line(), dismec::solvers::NewtonWithLineSearch::run(), dismec::objective::Objective::value(), dismec::objective::DenseAndSparseLinearBase::x_times_w(), and dismec::objective::LinearClassifierBase::x_times_w().
|
inlineprotected |
Record statistics. This function just forwards all its arguments to the internal StatisticsCollection
.
Definition at line 90 of file tracked.h.
Referenced by dismec::init::MeanOfFeaturesInitializer::get_initial_weight(), dismec::objective::Regularized_SquaredHingeSVC::gradient_and_pre_conditioner_tpl(), dismec::solvers::NewtonWithLineSearch::record_iteration(), and dismec::solvers::NewtonWithLineSearch::run().
void Tracked::register_stat | ( | const std::string & | name, |
std::unique_ptr< Statistics > | stat | ||
) |
Registers a tracker for the statistics name
.
Definition at line 20 of file tracked.cpp.
References m_Collection.
|
inlineprotected |
Set value of tag. This function just forwards all its arguments to the internal StatisticsCollection
.
Args | Dummy template parameter pack. Needs to be empty |
Definition at line 116 of file tracked.h.
References make_dependent(), and dismec::stats::StatisticsCollection::set_tag().
Referenced by dismec::solvers::NewtonWithLineSearch::run(), and dismec::solvers::NullOptimizer::run().
|
private |
The internal collection. Filled with a new StatisticsCollection
in the constructor.
Definition at line 138 of file tracked.h.
Referenced by declare_stat(), declare_tag(), get_stats(), make_timer(), and register_stat().