Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External stats change #26

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[submodule "libpotassco"]
path = libpotassco
url = https://github.com/potassco/libpotassco.git
url = https://github.com/MaxOstrowski/libpotassco.git
branch = dev_change
2 changes: 1 addition & 1 deletion clasp/clasp_facade.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class ClaspFacade : public ModelHandler {
*/
bool read();

typedef void(*StatsCallback)(ExternalStatistics*, void*);
typedef void(*StatsCallback)(ClaspStatistics*, void*);

//! Adds a callback for defining external (i.e. user-defined) statistics.
/*!
Expand Down
123 changes: 34 additions & 89 deletions clasp/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class StatsVec : private PodVector<T*>::type {
bool own_;
};

//! A class for traversing and querying statistics.
//! A class for traversing, querying and adding statistics.
/*!
* \ingroup clingo
*/
Expand All @@ -253,75 +253,10 @@ class ClaspStatistics : public Potassco::AbstractStatistics {
~ClaspStatistics();
// Base interface
virtual Key_t root() const;
virtual Key_t userRoot() const;
virtual Type type(Key_t key) const;
virtual size_t size(Key_t key) const;
virtual Key_t at(Key_t arrK, size_t index) const;
virtual const char* key(Key_t mapK, size_t i) const;
virtual Key_t get(Key_t mapK, const char* key) const;
virtual double value(Key_t key) const;

// Register interface
Key_t setRoot(const StatisticObject&);
bool removeStat(const StatisticObject&, bool recurse);
bool removeStat(Key_t k, bool recurse);
void update();
StatisticObject findObject(Key_t root, const char* path, Key_t* track = 0) const;
StatisticObject getObject(Key_t k) const;
private:
ClaspStatistics(const ClaspStatistics&);
ClaspStatistics& operator=(const ClaspStatistics&);
Key_t root_;
struct Impl;
Impl* impl_;
};

//! A class for registering user-defined statistics.
/*!
* Functions in this interface taking a key as parameter require
* that the key is valid, i.e. the key must be reachable from root().
*/
class ExternalStatistics {
public:
typedef Potassco::Statistics_t Type;
typedef Potassco::AbstractStatistics::Key_t Key_t;

ExternalStatistics();
~ExternalStatistics();

//! Returns the root key of this object.
/*!
* The root key always exists and is of type Potassco::Statistics_t::Map.
*/
Key_t root() const;

//! Returns the type of the statistic object with the given key.
Type type(Key_t key) const;

//! Returns true if this object contains no keys beyond root().
bool empty() const;

//! Creates a statistic object under the given name in the given map.
/*!
* \pre type(object) == Potassco::Statistics_t::Map.
* \param map The map object to which the statistic object should be added.
* \param name The name under which the statistic object should be added.
* \param type The type of the statistic object to create.
* \return The key of the added statistic object.
*
* \note If a statistic object with the given name already exists in map,
* the function either returns its key provided that the types match,
* or otherwise signals failure by throwing an std::logic_error.
*/
Key_t mapAdd(Key_t map, const char* name, Type type);

//! Returns the key of the statistic object in map with the given name.
/*!
* \pre type(object) == Potassco::Statistics_t::Map.
* \note If map does not contain an object of the given name, the function
* return 0.
*/
Key_t mapGet(Key_t map, const char* name) const;

//! Creates a statistic object at the given index in the given array.
/*!
* \pre type(object) == Potassco::Statistics_t::Array.
Expand All @@ -337,36 +272,46 @@ class ExternalStatistics {
* \note Given an array A of size i, adding A[j] implicitly also creates all
* A[k] for i <= k < j.
*/
Key_t arrayAdd(Key_t array, std::size_t index, Type type);
virtual Key_t add(Key_t arrK, size_t index, Type t);

//! Returns the key of the statistic object at the given index in array.
/*!
* \pre type(object) == Potassco::Statistics_t::Array.
* \note If index is not a valid index in array, the function returns 0.
*/
Key_t arrayGet(Key_t array, std::size_t index) const;

//! Sets newValue as value for the given statistic object.
/*!
* \pre type(object) == Potassco::Statistics_t::Value.
*/
void set(Key_t object, double newValue);

//! Increments the value of object by i and returns its old value.
virtual const char* key(Key_t mapK, size_t i) const;
virtual Key_t get(Key_t mapK, const char* key) const;
//! Creates a statistic object under the given name in the given map.
/*!
* \pre type(object) == Potassco::Statistics_t::Value.
* \pre type(object) == Potassco::Statistics_t::Map.
* \param map The map object to which the statistic object should be added.
* \param name The name under which the statistic object should be added.
* \param type The type of the statistic object to create.
* \return The key of the added statistic object.
*
* \note If a statistic object with the given name already exists in map,
* the function either returns its key provided that the types match,
* or otherwise signals failure by throwing an std::logic_error.
*/
double fetchAdd(Key_t object, double i);
virtual Key_t add(Key_t mapK, const char* key, Type t);
virtual double value(Key_t key) const;
virtual void set(Key_t key, double v);

//! Converts this object to a StatisticObject of type map.
StatisticObject toStats() const;
// Register interface
Key_t setRoot(const StatisticObject&);
Key_t addUserRoot();
bool removeStat(const StatisticObject&, bool recurse);
bool removeStat(Key_t k, bool recurse);
void update();
StatisticObject findObject(Key_t root, const char* path, Key_t* track = 0) const;
StatisticObject getObject(Key_t k) const;
StatisticObject toUserStats() const;
bool userEmpty() const;
private:
ExternalStatistics(const ExternalStatistics&);
ExternalStatistics& operator=(const ExternalStatistics&);
ClaspStatistics(const ClaspStatistics&);
ClaspStatistics& operator=(const ClaspStatistics&);
Key_t root_;
struct Impl;
Impl* impl_;
Impl* impl_;
Key_t userroot_;
};


struct SolverStats;
struct JumpStats;
struct ExtendedStats;
Expand Down
2 changes: 1 addition & 1 deletion libpotassco
41 changes: 17 additions & 24 deletions src/clasp_facade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,27 +557,31 @@ double _getExhausted(const SolveResult* r) { return static_cast<double>(r->exhau
}

struct ClaspFacade::Statistics {
Statistics(ClaspFacade& f) : self_(&f), tester_(0), level_(0), clingo_(0), user_(0) {}
~Statistics() { delete user_, delete clingo_; delete solvers_.multi; }
Statistics(ClaspFacade& f) : self_(&f), tester_(0), level_(0), clingo_(0) {}
~Statistics() { delete clingo_; delete solvers_.multi; }
void start(uint32 level);
void initLevel(uint32 level);
void end();
void addTo(StatsMap& solving, StatsMap* accu) const;
void accept(StatsVisitor& out, bool final) const;
bool incremental() const { return self_->incremental(); }
Potassco::AbstractStatistics* getClingo();
void addExternal(StatsCallback, void*);
typedef StatsVec<SolverStats> SolverVec;
typedef SingleOwnerPtr<Asp::LpStats> LpStatsPtr;
typedef PrgDepGraph::NonHcfStats TesterStats;
typedef std::pair<StatsCallback, void*> Source;
typedef PodVector<Source>::type CallbackVec;
ClaspFacade* self_;
LpStatsPtr lp_; // level 0 and asp
SolverStats solvers_; // level 0
SolverVec solver_; // level > 1
SolverVec accu_; // level > 1 and incremental
TesterStats* tester_; // level > 0 and nonhcfs
uint32 level_; // active stats level
CallbackVec sources; // callbacks for user-defined stats
// For clingo stats interface
void addExternal(StatsCallback, void*);
Potassco::Span<Source> getSources() const { return Potassco::toSpan(sources); }
class ClingoView : public ClaspStatistics {
public:
explicit ClingoView(const ClaspFacade& f);
Expand All @@ -603,19 +607,6 @@ struct ClaspFacade::Statistics {
typedef SingleOwnerPtr<Accu> AccuPtr;
AccuPtr accu_;
}* clingo_; // new clingo stats interface
// For user-defined statistics
struct UserStats : ExternalStatistics {
typedef std::pair<StatsCallback, void*> Source;
typedef PodVector<Source>::type CallbackVec;
Potassco::Span<Source> getSources() const { return Potassco::toSpan(sources); }
void addSource(StatsCallback cb, void* data) { sources.push_back(Source(cb, data)); }
void update() {
for (CallbackVec::const_iterator it = sources.begin(), end = sources.end(); it != end; ++it) {
it->first(this, it->second);
}
}
CallbackVec sources;
}* user_; // user-defined statistics added on demand
};
void ClaspFacade::Statistics::initLevel(uint32 level) {
if (level_ < level) {
Expand All @@ -630,8 +621,7 @@ void ClaspFacade::Statistics::initLevel(uint32 level) {
}
}
void ClaspFacade::Statistics::addExternal(StatsCallback cb, void* data) {
if (!user_) { user_ = new UserStats(); }
user_->addSource(cb, data);
sources.push_back(Source(cb, data));
}

void ClaspFacade::Statistics::start(uint32 level) {
Expand Down Expand Up @@ -662,7 +652,6 @@ void ClaspFacade::Statistics::end() {
solver_[i]->flush();
}
if (tester_) { tester_->endStep(); }
if (user_) { user_->update(); }
if (clingo_) { clingo_->update(*this); }
}
void ClaspFacade::Statistics::addTo(StatsMap& solving, StatsMap* accu) const {
Expand All @@ -679,8 +668,8 @@ void ClaspFacade::Statistics::accept(StatsVisitor& out, bool final) const {
const SolverVec& solver = final ? accu_ : solver_;
const uint32 nThreads = final ? (uint32)accu_.size() : self_->ctx.concurrency();
const uint32 nSolver = (uint32)solver.size();
if (user_ && !user_->empty()) {
out.visitExternalStats(user_->toStats());
if (clingo_ && !clingo_->userEmpty()) {
out.visitExternalStats(clingo_->toUserStats());
}
if (nThreads > 1 && nSolver > 1 && out.visitThreads(StatsVisitor::Enter)) {
for (uint32 i = 0, end = std::min(nSolver, nThreads); i != end; ++i) {
Expand Down Expand Up @@ -728,9 +717,13 @@ ClaspFacade::Statistics::ClingoView::ClingoView(const ClaspFacade& f) {
setRoot(keys_.toStats());
}
void ClaspFacade::Statistics::ClingoView::update(const ClaspFacade::Statistics& stats) {
if (stats.user_) {
keys_.add("user_defined", stats.user_->toStats());
Key_t r = root();
setRoot(getObject(userRoot()));
for (CallbackVec::const_iterator it = stats.sources.begin(), end = stats.sources.end(); it != end; ++it) {
it->first(this, it->second);
}
setRoot(getObject(r));
if (!userEmpty()) { addUserRoot(); }
if (stats.level_ > 0 && accu_.get() && keys_.add("accu", accu_->toStats())) {
accu_->step.addTo(*accu_);
accu_->add("solving", accu_->solving_.toStats());
Expand Down Expand Up @@ -949,7 +942,7 @@ void ClaspFacade::addStatisticsCallback(StatsCallback cb, void* data) {
}

Potassco::Span<std::pair<ClaspFacade::StatsCallback, void*> > ClaspFacade::getStatisticsCallbacks() const {
return stats_.get() && stats_->user_ ? stats_->user_->getSources() : Potassco::toSpan<std::pair<StatsCallback, void*> >();
return stats_.get() ? stats_->getSources() : Potassco::toSpan<std::pair<StatsCallback, void*> >();
}

void ClaspFacade::prepare(EnumMode enumMode) {
Expand Down
Loading