DiSMEC++
dismec::stats::Tracked Class Reference

A base class to be used for all types that implement some for of statistics tracking. More...

#include <tracked.h>

Inheritance diagram for dismec::stats::Tracked:
dismec::ResultStatsGatherer dismec::init::WeightsInitializer dismec::objective::Objective dismec::postproc::PostProcessor dismec::solvers::Minimizer anonymous_namespace{statistics.cpp}::DefaultGatherer anonymous_namespace{cascade.cpp}::CombinedWeightInitializer dismec::init::ConstantInitializer dismec::init::NumpyInitializer dismec::init::PreTrainedInitializer dismec::init::SubsetFeatureMeanInitializer dismec::init::ZeroInitializer dismec::objective::PointWiseRegularizer< HuberRegularizer > dismec::objective::PointWiseRegularizer< ElasticNetRegularizer > dismec::objective::PointWiseRegularizer< SquaredNormRegularizer > anonymous_namespace{minimizer.cpp}::MockObjective dismec::objective::DenseAndSparseLinearBase dismec::objective::LinearClassifierBase dismec::objective::PointWiseRegularizer< CRTP > dismec::postproc::CombinePostProcessor dismec::postproc::CullingPostProcessor dismec::postproc::IdentityPostProc dismec::postproc::ReorderPostProc dismec::postproc::Sparsify anonymous_namespace{minimizer.cpp}::MockMinimizer dismec::solvers::NewtonWithLineSearch dismec::solvers::NullOptimizer

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< StatisticsCollectionget_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< StatisticsCollectionm_Collection
 The internal collection. Filled with a new StatisticsCollection in the constructor. More...
 

Detailed Description

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:

  • Since this is expected to be used as base class in many fundamental types of the library, we want to keep the include dependencies as little as possible. In order to define the interface, it is not necessary to know any details of the statistics gathering implementation.
  • Introduce as little overhead as possible. This is only relevant to the statistics gathering functions 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.

Definition at line 42 of file tracked.h.

Constructor & Destructor Documentation

◆ Tracked()

Tracked::Tracked ( )

Default constructor, creates the internal stats::StatisticsCollection.

Definition at line 13 of file tracked.cpp.

◆ ~Tracked()

Tracked::~Tracked ( )
protecteddefault

Non-virtual destructor. Declared protected, so we don't accidentally try to do a polymorphic delete.

Member Function Documentation

◆ declare_stat()

◆ declare_tag()

void Tracked::declare_tag ( tag_id_t  index,
std::string  name 
)
protected

Declares a new tag. This function just forwards all its arguments to the internal StatisticsCollection.

Note
Since this function will not be called in performance critical code, it is not implemented inline.
See also
stats::StatisticsCollection::declare_tag.

Definition at line 24 of file tracked.cpp.

References m_Collection.

Referenced by dismec::solvers::NewtonWithLineSearch::NewtonWithLineSearch(), and dismec::solvers::NullOptimizer::NullOptimizer().

◆ get_stats()

std::shared_ptr<StatisticsCollection> dismec::stats::Tracked::get_stats ( ) const
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().

◆ make_dependent()

template<class T , class... Args>
T& dismec::stats::Tracked::make_dependent ( T &  t)
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().

◆ make_timer()

◆ record()

template<class T >
void dismec::stats::Tracked::record ( stat_id_t  stat,
T &&  value 
)
inlineprotected

◆ register_stat()

void Tracked::register_stat ( const std::string &  name,
std::unique_ptr< Statistics stat 
)

Registers a tracker for the statistics name.

Note
This function will not be called in performance-critical code, and is thus not defined inline.
See also
stats::StatisticsCollection::register_stat

Definition at line 20 of file tracked.cpp.

References m_Collection.

◆ set_tag()

template<class... Args>
void dismec::stats::Tracked::set_tag ( tag_id_t  tag,
long  value 
)
inlineprotected

Set value of tag. This function just forwards all its arguments to the internal StatisticsCollection.

Template Parameters
ArgsDummy template parameter pack. Needs to be empty
See also
stats::StatisticsCollection::set_tag

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().

Member Data Documentation

◆ m_Collection

std::shared_ptr<StatisticsCollection> dismec::stats::Tracked::m_Collection
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().


The documentation for this class was generated from the following files: