Skip to content

Commit

Permalink
Solvers is now a singleton, and expose solvers.hpp to API
Browse files Browse the repository at this point in the history
  • Loading branch information
trolando committed Jul 24, 2024
1 parent 73cb0dc commit 0c7ee84
Show file tree
Hide file tree
Showing 27 changed files with 199 additions and 189 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(OINK_HDRS
include/oink/error.hpp
include/oink/game.hpp
include/oink/solvers.hpp
include/oink/solver.hpp
include/oink/bitset.hpp
include/oink/uintqueue.hpp
include/oink/libpopcnt.h
Expand Down
7 changes: 4 additions & 3 deletions include/oink/oink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

#include <iostream>
#include <vector>
#include <optional>

#include "oink/error.hpp"
#include "oink/game.hpp"
#include "oink/uintqueue.hpp"
#include "oink/solvers.hpp"

namespace pg {

Expand All @@ -46,8 +48,7 @@ class Oink
/**
* Instruct Oink to use the given solver.
*/
void setSolver(int solverid);
void setSolver(std::string label);
void setSolver(std::string solver);

/**
* Instruct Oink to inflate as a preprocessing step.
Expand Down Expand Up @@ -158,7 +159,7 @@ class Oink

Game *game; // game being solved
std::ostream &logger; // logger for trace/debug messages
int solver = -1; // which solver to use
std::optional<std::string> solver; // which solver to use
int workers = -1; // number of workers, 0 = autodetect, -1 = use non parallel
int trace = 0; // verbosity (0 for normal, 1 for trace, 2 for debug)
bool inflate = false; // inflate the game before solving
Expand Down
File renamed without changes.
85 changes: 64 additions & 21 deletions include/oink/solvers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <iostream>
#include <functional>
#include <vector>
#include <map>
#include <set>

#ifndef SOLVERS_HPP
#define SOLVERS_HPP
Expand All @@ -30,49 +32,90 @@ class Solver;
class Solvers
{
public:
Solvers();
using SolverConstructor = std::function<Solver*(Oink&, Game&)>;

Solvers(const Solvers&) = delete;
Solvers& operator=(const Solvers&) = delete;

/**
* Get number of solvers
*/
unsigned count() { return labels.size(); }
static unsigned count() { return instance().solvers.size(); }

/**
* Get label of solver <id>
* Get description of solver <id>
*/
std::string label(int id) { return labels[id]; }
static std::string desc(const std::string& id)
{
return instance().solvers[id].description;
}

/**
* Get description of solver <id>
* Get whether solver <id> can be run in parallel
*/
std::string desc(int id) { return descriptions[id]; }
static bool isParallel(const std::string& id)
{
return instance().solvers[id].isParallel;
}

/**
* Get whether solver <id> can be run in parallel
* Construct solver with the given parameters
*/
bool isParallel(int id) { return ispar[id]; }
static Solver* construct(const std::string& id, Oink& oink, Game& game)
{
return instance().solvers[id].constructor(oink, game);
}

/**
* Construct solver <id> with the given parameters
* Write a formatted list of all solvers to the given ostream
*/
Solver* construct(int id, Oink* oink, Game* game) { return constructors[id](oink, game); }
static void list(std::ostream &out);

/**
* Obtain the id matching the given solver label
* Add a solver to the set of solvers
*/
int id(std::string label);
static void add(const std::string& id, const std::string& description, bool isParallel, const SolverConstructor& constructor)
{
instance().solvers[id] = {description, isParallel, constructor};
}

static SolverConstructor get(const std::string& id)
{
return instance().solvers[id].constructor;
}

static std::set<std::string> getSolverIDs()
{
std::set<std::string> ids;
for (const auto& entry : instance().solvers) {
ids.insert(entry.first);
}
return ids;
}

private:
struct SolverInfo {
std::string description;
bool isParallel;
SolverConstructor constructor;
};

std::map<std::string, SolverInfo> solvers;

Solvers();

static Solvers& instance() {
static Solvers instance;
return instance;
}

/**
* Write a formatted list of all solvers to the given ostream
* Add a solver to the set of solvers
*/
void list(std::ostream &out);

protected:
std::vector<std::string> labels;
std::vector<std::string> descriptions;
std::vector<bool> ispar;
std::vector<std::function<Solver*(Oink*, Game*)>> constructors;
void add(std::string, std::string, int, std::function<Solver*(Oink*, Game*)>);
void _add(const std::string& id, const std::string& description, bool isParallel, const SolverConstructor& constructor)
{
solvers[id] = {description, isParallel, constructor};
}
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/dtl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef DTL_HPP
#define DTL_HPP

#include "solver.hpp"
#include "oink/solver.hpp"

namespace pg {

Expand Down
2 changes: 1 addition & 1 deletion src/fpi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <queue>

#include "solver.hpp"
#include "oink/solver.hpp"
#include "lace.h"

namespace pg {
Expand Down
2 changes: 1 addition & 1 deletion src/fpj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef FPJ_HPP
#define FPJ_HPP

#include "solver.hpp"
#include "oink/solver.hpp"
#include "lace.h"

namespace pg {
Expand Down
2 changes: 1 addition & 1 deletion src/mspm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <queue>

#include "solver.hpp"
#include "oink/solver.hpp"

namespace pg {

Expand Down
2 changes: 1 addition & 1 deletion src/npp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <deque>
#include <vector>

#include "solver.hpp"
#include "oink/solver.hpp"

namespace pg
{
Expand Down
25 changes: 8 additions & 17 deletions src/oink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "oink/oink.hpp"
#include "oink/solvers.hpp"
#include "solver.hpp"
#include "oink/solver.hpp"
#include "lace.h"

namespace pg {
Expand Down Expand Up @@ -352,15 +352,9 @@ Oink::flush()
}

void
Oink::setSolver(int solverid)
Oink::setSolver(std::string solver)
{
solver = solverid;
}

void
Oink::setSolver(std::string label)
{
solver = Solvers().id(label);
this->solver = solver;
}

void _solve_loop(Oink* s)
Expand All @@ -379,8 +373,6 @@ Oink::solveLoop()
/**
* Report chosen solver.
*/
Solvers solvers;

if (bottomSCC) {
do {
// disable all solved vertices
Expand All @@ -397,7 +389,7 @@ Oink::solveLoop()
logger << game->count_unsolved() << " nodes left)" << std::endl;

// solve current subgame
Solver *s = solvers.construct(solver, this, game);
auto s = Solvers::construct(*solver, *this, *game);
if (!s->parseOptions(options)) {
logger << "error parsing options: " << options << std::endl;
exit(-1);
Expand All @@ -414,7 +406,7 @@ Oink::solveLoop()
disabled = game->solved;

// solve current subgame
Solver *s = solvers.construct(solver, this, game);
auto s = Solvers::construct(*solver, *this, *game);
bool fullSolver = s->isFullSolver();
if (!s->parseOptions(options)) {
logger << "error parsing options: " << options << std::endl;
Expand Down Expand Up @@ -500,7 +492,7 @@ Oink::run()
return;
}

if (solver == -1) {
if (solver == std::nullopt) {
logger << "no solver selected!" << std::endl;
return;
}
Expand All @@ -515,10 +507,9 @@ Oink::run()
* - if sequential solver, run sequantial
*/

Solvers solvers;
logger << "solving using " << solvers.desc(solver) << std::endl;
logger << "solving using " << Solvers::desc(*solver) << std::endl;

if (solvers.isParallel(solver)) {
if (Solvers::isParallel(*solver)) {
if (workers >= 0) {
if (lace_workers() == 0) {
lace_start(workers, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/pp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <queue>

#include "solver.hpp"
#include "oink/solver.hpp"

namespace pg {

Expand Down
2 changes: 1 addition & 1 deletion src/ppq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <vector>

#include "solver.hpp"
#include "oink/solver.hpp"

namespace pg {

Expand Down
2 changes: 1 addition & 1 deletion src/psi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <queue>

#include "solver.hpp"
#include "oink/solver.hpp"
#include "lace.h"

namespace pg {
Expand Down
2 changes: 1 addition & 1 deletion src/ptl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef PTL_HPP
#define PTL_HPP

#include "solver.hpp"
#include "oink/solver.hpp"

namespace pg {

Expand Down
2 changes: 1 addition & 1 deletion src/qpt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef QPT_HPP
#define QPT_HPP

#include "solver.hpp"
#include "oink/solver.hpp"

namespace pg {

Expand Down
2 changes: 1 addition & 1 deletion src/rtl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <map>
#include <tuple>

#include "solver.hpp"
#include "oink/solver.hpp"

namespace pg {

Expand Down
Loading

0 comments on commit 0c7ee84

Please sign in to comment.