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

*** ERROR: (clasp): Function requires valid conflict #85

Closed
LasseBlaauwbroek opened this issue Sep 2, 2022 · 11 comments
Closed

*** ERROR: (clasp): Function requires valid conflict #85

LasseBlaauwbroek opened this issue Sep 2, 2022 · 11 comments

Comments

@LasseBlaauwbroek
Copy link

LasseBlaauwbroek commented Sep 2, 2022

gringo.zip

Given the attached input file and the command clasp --opt-strategy=usc --stats=2 gringo.out I get the following error:

clasp version 3.3.8
Reading from gringo.out
Solving...
*** ERROR: (clasp): Function requires valid conflict
UNKNOWN

INTERRUPTED  : 1
Models       : 0+
Calls        : 1
Time         : 0.172s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time     : 0.172s

Choices      : 0       
Conflicts    : 1        (Analyzed: 0)
Restarts     : 0       
Problems     : 1        (Average Length: 0.00 Splits: 0)
Lemmas       : 0        (Deleted: 0)
  Binary     : 0        (Ratio:   0.00%)
  Ternary    : 0        (Ratio:   0.00%)
  Conflict   : 0        (Average Length:    0.0 Ratio:   0.00%) 
  Loop       : 0        (Average Length:    0.0 Ratio:   0.00%) 
  Other      : 0        (Average Length:    0.0 Ratio:   0.00%) 
Backjumps    : 0        (Average:  0.00 Max:   0 Sum:      0)
  Executed   : 0        (Average:  0.00 Max:   0 Sum:      0 Ratio:   0.00%)
  Bounded    : 0        (Average:  0.00 Max:   0 Sum:      0 Ratio: 100.00%)

Rules        : 988072   (Original: 978382)
  Choice     : 5038    
  Minimize   : 2       
Atoms        : 856968   (Original: 853606 Auxiliary: 3362)
Bodies       : 26984    (Original: 20796)
  Count      : 36       (Original: 468)
Equivalences : 43401    (Atom=Atom: 12039 Body=Body: 808 Other: 30554)
Tight        : Yes
Variables    : 13322    (Eliminated:    0 Frozen:  144)
Constraints  : 82543    (Binary:  90.5% Ternary:   3.5% Other:   6.1%)

On the other hand, with the default --opt-strategy there is no error. I was working with similar input files a few months ago, and I don't remember getting such problems. Any help would be appreciated!

@rkaminsk
Copy link
Member

rkaminsk commented Sep 2, 2022

I cannot reproduce the error.

clasp --opt-strategy=usc gringo.out -q 
clasp version 3.3.8
Reading from gringo.out
Solving...
OPTIMUM FOUND

Models       : 1
  Optimum    : yes
Optimization : -135 -1
Calls        : 1
Time         : 0.190s (Solving: 0.02s 1st Model: 0.02s Unsat: 0.00s)
CPU Time     : 0.190s

@LasseBlaauwbroek
Copy link
Author

Mhm, this is very strange. I tried building Clasp from source, and now I also cannot reproduce the error. It only exists in the version of Clasp installed through my package manager (pacman). For the record, here is the package building file: https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=clingo Does anything look suspicious there?

@rkaminsk
Copy link
Member

rkaminsk commented Sep 2, 2022

The build file should set the cmake build type because otherwise it will automatically be set to release. You could set it to none for example. Otherwise, the issue might be triggered by the CXXFLAGS or compiler being used.

I'ld need both the flags and the compiler version to investigate further.

@LasseBlaauwbroek
Copy link
Author

Thanks for your quick repsonses @rkaminsk !

The build file should set the cmake build type because otherwise it will automatically be set to release.

Isn't release a good thing for a package build?

I'm not an expert on Cmake so you'll have to bear with me here. Here are the CXXFLAGS and output of cmake on a build that exhibits the error. Is that enough information?
CXXFLAGS:
-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -Wp,-D_GLIBCXX_ASSERTIONS
Cmake:

-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is Clang 14.0.6
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- No build type selected - using 'Release'
-- Found Python: /usr/bin/python3.10 (found suitable version "3.10.6", minimum required is "3.6") found components: Interpreter Development Development.Module Development.Embed 
-- Found Lua: /usr/lib/liblua5.4.so;/usr/lib/libm.so (found suitable version "5.4.4", minimum required is "5.0") 
-- Found BISON: /usr/bin/bison (found suitable version "3.8.2", minimum required is "2.5") 
-- Found RE2C: /usr/bin/re2c (found suitable version "0.300.0", minimum required is "0.13") 
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Performing Test CLASP_HAS_WORKING_LIBATOMIC
-- Performing Test CLASP_HAS_WORKING_LIBATOMIC - Success
-- Potassco is not installed - using local copy
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) 
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) 
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    CLINGO_BUILD_PY_SHARED
    CLINGO_REQUIRE_LUA
    CLINGO_REQUIRE_PYTHON
    PYCLINGO_USER_INSTALL

@LasseBlaauwbroek
Copy link
Author

After further experimentation, the simplest way to reproduce the problem is as such (with Clang 14.0.6):

git clone [email protected]:potassco/clasp.git
cd clasp
export CXXFLAGS="-Wp,-D_FORTIFY_SOURCE=2"
cmake -H. -Bbuild -DCMAKE_CXX_COMPILER=clang++
cmake --build build
/build/bin/clasp --opt-strategy=usc gringo.out

@rkaminsk
Copy link
Member

rkaminsk commented Sep 2, 2022

Isn't release a good thing for a package build?

Sure but the CXXFLAGS are already set to something like release flags. Just -DNDEBUG is set additionally. If release options are intended, then maybe add -DCMAKE_BUILD_TYPE=release. The options

CLINGO_BUILD_PY_SHARED
CLINGO_REQUIRE_LUA
CLINGO_REQUIRE_PYTHON
PYCLINGO_USER_INSTALL

also have been replaced. The following could be used

-DCLINGO_BUILD_WITH_PYTHON=On
-DCLINGO_BUILD_WITH_LUA=On

to enforce python and lua support. If lua and python are installed the build should still pick them up correctly because they default to auto.

After further experimentation, the simplest way to reproduce the problem is as such (with Clang 14.0.6):

Thanks, I'll have a look. So it is maybe some ill-formed code in clasp or a compiler bug.

@rkaminsk
Copy link
Member

rkaminsk commented Sep 2, 2022

git clone [email protected]:potassco/clasp.git
cd clasp
export CXXFLAGS="-Wp,-D_FORTIFY_SOURCE=2"
cmake -H. -Bbuild -DCMAKE_CXX_COMPILER=clang++
cmake --build build
/build/bin/clasp --opt-strategy=usc gringo.out

I can reproduce with clang 14 and values 1 and 2 for _FORTIFY_SOURCE.

@rkaminsk
Copy link
Member

rkaminsk commented Sep 2, 2022

It really looks like a compiler/library bug. With the patch below the code works as expected. The original code is perfectly fine though.

diff --git a/src/solver.cpp b/src/solver.cpp
index 4bcc1d2..20a8f12 100644
--- a/src/solver.cpp
+++ b/src/solver.cpp
@@ -23,6 +23,7 @@
 //
 #include <clasp/solver.h>
 #include <clasp/clause.h>
+
 #include POTASSCO_EXT_INCLUDE(unordered_set)
 namespace Clasp {
 namespace {
@@ -1151,8 +1152,8 @@ bool Solver::resolveToFlagged(const LitVec& in, const uint8 vf, LitVec& out, uin
 }
 void Solver::resolveToCore(LitVec& out) {
        POTASSCO_REQUIRE(hasConflict() && !hasStopConflict(), "Function requires valid conflict");
-       cc_.clear();
        std::swap(cc_, conflict_);
+       conflict_.clear();
        if (searchMode() == SolverStrategies::no_learning) {
                for (uint32 i = 1, end = decisionLevel() + 1; i != end; ++i) { cc_.push_back(decision(i)); }
        

or the following one works too:

diff --git a/src/solver.cpp b/src/solver.cpp
index 4bcc1d2..08098e7 100644
--- a/src/solver.cpp
+++ b/src/solver.cpp
@@ -23,6 +23,7 @@
 //
 #include <clasp/solver.h>
 #include <clasp/clause.h>
+
 #include POTASSCO_EXT_INCLUDE(unordered_set)
 namespace Clasp {
 namespace {
@@ -1152,7 +1153,7 @@ bool Solver::resolveToFlagged(const LitVec& in, const uint8 vf, LitVec& out, uin
 void Solver::resolveToCore(LitVec& out) {
        POTASSCO_REQUIRE(hasConflict() && !hasStopConflict(), "Function requires valid conflict");
        cc_.clear();
-       std::swap(cc_, conflict_);
+       cc_.swap(conflict_);
        if (searchMode() == SolverStrategies::no_learning) {
                for (uint32 i = 1, end = decisionLevel() + 1; i != end; ++i) { cc_.push_back(decision(i)); }
        }

@rkaminsk
Copy link
Member

rkaminsk commented Sep 2, 2022

@BenKaufmann, isn't it inefficient to use std::swap here? Wouldn't just swap(a, b) be required for ADL to find bk_lib::swap?

@BenKaufmann
Copy link
Contributor

isn't it inefficient to use std::swap here? Wouldn't just swap(a, b) be required for ADL to find bk_lib::swap?

@rkaminsk Yes, it is completely brain-dead and breaks the (thin) abstraction of LitVec (either std::vector or bk_lib::pod_vector). The generic way to swap would be to use C++'s classic two-step dance:

using std::swap; // pull in the "default" algorithm
swap(a, b);      // allow ADL to find the "best" algorithm

However, given that we are not in a generic context and that bk_lib::pod_vector is intended to be a drop-in replacement for std::vector (same interface), I prefer your second version, i.e. directly calling the member function.

It really looks like a compiler/library bug.

I agree. While the original code is a little stupid, it is perfectly legal.

Anyway, I'll fix this and #84 later today.

Many thanks for investigating and solving this issue!

BenKaufmann added a commit that referenced this issue Sep 3, 2022
* Call swap member function of LitVec directly instead of relying on
  std::swap, which does not call the container's own version in case
  LitVec is an alias for bk_lib::pod_vector<Literal>.

* Coincidentally, this also prevents a code gen bug in clang 14 when
  clasp is built with option _FORTIFY_SOURCE.

* NOTE: At this point, using bk_lib::pod_vector is probably a
  pessimization given that it was never updated to C++11 (rvalues
  and/or noexcept) - but this is a problem for another day.
@LasseBlaauwbroek
Copy link
Author

Thanks for the quick investigation and fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants