Skip to content

Commit 5c84e27

Browse files
authored
Merge pull request #25 from kwrx/dev
Iterazioni
2 parents 0661b9d + 0d41faf commit 5c84e27

22 files changed

+326
-59
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ set(HAVE_IDLV 0)
1313
set(HAVE_WASP 1)
1414
set(HAVE_CLASP 0)
1515

16-
## Multithreading
17-
set(HAVE_THREADS 1)
16+
## QASP
17+
set(HAVE_THREADS 1)
18+
set(HAVE_ITERATIONS 1)
1819

1920
## Wasp
2021
set(HAVE_WASP_ASSUMPTIONS 1)
22+
set(HAVE_WASP_RESET 1)
2123

2224
## Logger
2325
set(HAVE_PRETTY_LOGGER 1)

config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
#cmakedefine HAVE_GRINGO @HAVE_GRINGO@
2828
#cmakedefine HAVE_CLASP @HAVE_CLASP@
2929
#cmakedefine HAVE_THREADS @HAVE_THREADS@
30+
#cmakedefine HAVE_ITERATIONS @HAVE_ITERATIONS@
3031
#cmakedefine HAVE_WASP_ASSUMPTIONS @HAVE_WASP_ASSUMPTIONS@
32+
#cmakedefine HAVE_WASP_RESET @HAVE_WASP_RESET@
3133
#cmakedefine HAVE_PRETTY_LOGGER @HAVE_PRETTY_LOGGER@
3234

3335

include/qasp/qasp.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323

2424
#include <stdint.h>
2525

26-
#define QASP_OPTIONS_DEFAULT_CPUS 8
2726
#define QASP_OPTIONS_DEFAULT_QUIET 0
27+
#define QASP_OPTIONS_DEFAULT_CPUS 8
28+
#define QASP_OPTIONS_DEFAULT_ITERATIONS 1
2829

2930
#define QASP_RESULT_COHERENT 1
3031
#define QASP_RESULT_INCOHERENT 2
3132
#define QASP_RESULT_UNKNOWN 0
3233

3334

35+
typedef uint64_t qasp_iteration_t;
36+
37+
38+
3439
#if defined(__cplusplus)
3540

3641
#include <vector>
@@ -40,16 +45,19 @@ namespace qasp {
4045

4146
struct Options {
4247

43-
uint16_t cpus;
4448
uint16_t quiet;
49+
uint16_t cpus;
50+
qasp_iteration_t iterations;
4551

46-
Options(uint16_t cpus, uint16_t quiet)
47-
: cpus(cpus)
48-
, quiet(quiet) {}
52+
Options(uint16_t quiet, uint16_t cpus, qasp_iteration_t iterations)
53+
: quiet(quiet)
54+
, cpus(cpus)
55+
, iterations(iterations) {}
4956

5057
Options()
51-
: cpus(QASP_OPTIONS_DEFAULT_CPUS)
52-
, quiet(QASP_OPTIONS_DEFAULT_QUIET) {}
58+
: quiet(QASP_OPTIONS_DEFAULT_QUIET)
59+
, cpus(QASP_OPTIONS_DEFAULT_CPUS)
60+
, iterations(QASP_OPTIONS_DEFAULT_ITERATIONS) {}
5361

5462
};
5563

@@ -74,11 +82,11 @@ namespace qasp {
7482
__options = std::move(options);
7583
}
7684

77-
inline const auto& options() {
85+
inline const auto& options() const {
7886
return __options;
7987
}
8088

81-
inline const auto& sources() {
89+
inline const auto& sources() const {
8290
return __sources;
8391
}
8492

lib/wasp/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ add_library(wasp ${SOURCES} ${HEADERS})
77
target_link_libraries(wasp PUBLIC ${Boost_LIBRARIES})
88
target_include_directories(wasp PUBLIC ${Boost_INCLUDE_DIRS})
99

10-
target_compile_options(wasp PRIVATE -Wno-error -w)
10+
target_compile_options(wasp PRIVATE -Wno-error -w)
11+
target_compile_definitions(wasp PRIVATE __qasp__)

lib/wasp/util/VariableNames.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@
2020
#include "WaspAssert.h"
2121
#include <sstream>
2222
using namespace std;
23+
24+
25+
#if defined(__qasp__)
26+
thread_local
27+
#endif
2328
vector< string > VariableNames::variables;
29+
30+
#if defined(__qasp__)
31+
thread_local
32+
#endif
2433
bool VariableNames::toBePrinted = false;
2534

35+
2636
bool
2737
VariableNames::isHidden(
2838
Var v )

lib/wasp/util/VariableNames.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#ifndef WASP_VARIABLENAMES_H
2020
#define WASP_VARIABLENAMES_H
2121

22+
#if defined(__qasp__)
23+
#include <thread>
24+
#endif
25+
2226
#include <cassert>
2327
#include <iostream>
2428
#include <vector>
@@ -36,9 +40,23 @@ class VariableNames
3640
static bool hasToBePrinted( Var v );
3741
static void addVariable();
3842
static const vector< string >& getVariables();
43+
44+
#if defined(__qasp__)
45+
static inline void reset() {
46+
variables.clear();
47+
}
48+
#endif
3949

4050
private:
51+
52+
#if defined(__qasp__)
53+
thread_local
54+
#endif
4155
static vector< string > variables;
56+
57+
#if defined(__qasp__)
58+
thread_local
59+
#endif
4260
static bool toBePrinted;
4361
};
4462

src/main.cc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ static void show_usage(int argc, char** argv) {
3535
std::cout
3636
<< "Use: " << QASP_PROGRAM_NAME << " [OPTIONS] SOURCES...\n"
3737
<< "Process qasp SOURCES and blabla...\n\n"
38-
<< " -q, --quiet hide log information\n"
39-
<< " --help show this help\n"
40-
<< " --version print version info and exit\n";
38+
<< " -j N, --parallel=N allow N jobs at once.\n"
39+
<< " -n N, --iterations=N iterate solving N times (infinite: N <- 0).\n"
40+
<< " -q, --quiet hide log information.\n"
41+
<< " --help print this message and exit.\n"
42+
<< " --version print version info and exit.\n";
4143

42-
exit(1);
44+
exit(EXIT_FAILURE);
4345

4446
}
4547

@@ -63,7 +65,7 @@ static void show_version(int argc, char** argv) {
6365
<< __TIMESTAMP__ << ")\n";
6466

6567

66-
exit(1);
68+
exit(EXIT_FAILURE);
6769

6870
}
6971

@@ -83,7 +85,7 @@ int main(int argc, char** argv) {
8385
qasp::Options options;
8486

8587
int c, idx;
86-
while((c = getopt_long(argc, argv, "qj:hv", long_options, &idx)) != -1) {
88+
while((c = getopt_long(argc, argv, "qj:n:hv", long_options, &idx)) != -1) {
8789

8890
switch(c) {
8991
case 'q':
@@ -92,6 +94,9 @@ int main(int argc, char** argv) {
9294
case 'j':
9395
options.cpus = atoi(optarg);
9496
break;
97+
case 'n':
98+
options.iterations = atoll(optarg);
99+
break;
95100
case 'v':
96101
show_version(argc, argv);
97102
break;
@@ -111,6 +116,11 @@ int main(int argc, char** argv) {
111116
abort();
112117
}
113118

119+
if(unlikely(options.iterations <= 0)) {
120+
options.iterations = std::numeric_limits<decltype(options.iterations)>().max();
121+
}
122+
123+
114124

115125
std::vector<std::string> sources;
116126

@@ -153,6 +163,6 @@ int main(int argc, char** argv) {
153163

154164
}
155165

156-
return 0;
166+
return EXIT_SUCCESS;
157167

158168
}

src/qasp/AnswerSet.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ namespace qasp {
3434
class AnswerSet : public std::vector<Atom> {
3535
public:
3636

37+
inline friend bool operator==(const AnswerSet& a, const AnswerSet& b) {
38+
39+
if(a.size() != b.size())
40+
return false;
41+
42+
43+
std::vector<Atom> v1 = a;
44+
std::vector<Atom> v2 = b;
45+
46+
std::sort(v1.begin(), v1.end(), [] (const auto& i, const auto& j) { return i.predicate() < j.predicate(); });
47+
std::sort(v2.begin(), v2.end(), [] (const auto& i, const auto& j) { return i.predicate() < j.predicate(); });
48+
49+
return std::equal(v1.begin(), v1.end(), v2.begin());
50+
51+
}
52+
3753
inline friend std::ostream& operator <<(std::ostream& os, const AnswerSet& a) {
3854

3955
os << "{";
@@ -51,6 +67,11 @@ namespace qasp {
5167

5268
}
5369

70+
71+
inline const bool contains(const Atom& atom) const {
72+
return std::find(this->begin(), this->end(), atom) != this->end();
73+
}
74+
5475
};
5576

5677
}

src/qasp/Assumptions.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ namespace qasp {
6060

6161
}
6262

63+
64+
inline const bool contains(const Atom& atom) const {
65+
return this->std::unordered_set<std::string>::find(atom.predicate()) != this->std::unordered_set<std::string>::end();
66+
}
67+
68+
6369
template <typename ...T>
6470
inline void emplace_back(T&&... args) {
6571

src/qasp/Program.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ std::tuple<ProgramModel, std::vector<AnswerSet>> Program::solve(const AnswerSet&
128128

129129
for(const auto& i : answer) {
130130

131-
auto found = std::find(assumptions().begin(), assumptions().end(), i); // FIXME: Da rivedere...
132-
133-
if(unlikely(found != assumptions().end()))
131+
if(unlikely(assumptions().contains(i)))
134132
continue;
135133

136134

@@ -150,9 +148,7 @@ std::tuple<ProgramModel, std::vector<AnswerSet>> Program::solve(const AnswerSet&
150148

151149
for(const auto& i : assumptions()) {
152150

153-
auto found = std::find(answer.begin(), answer.end(), i);
154-
155-
if(found != answer.end())
151+
if(answer.contains(i))
156152
positive.emplace_back(map_index(i), i.predicate());
157153
else
158154
negative.emplace_back(map_index(i), i.predicate());

0 commit comments

Comments
 (0)