Adding support for reading wrong assumptions
[satlib.git] / glucose-syrup / parallel / SharedCompanion.cc
1 /***************************************************************************************[SharedCompanion.cc]
2  Glucose -- Copyright (c) 2009-2014, Gilles Audemard, Laurent Simon
3                                 CRIL - Univ. Artois, France
4                                 LRI  - Univ. Paris Sud, France (2009-2013)
5                                 Labri - Univ. Bordeaux, France
6
7  Syrup (Glucose Parallel) -- Copyright (c) 2013-2014, Gilles Audemard, Laurent Simon
8                                 CRIL - Univ. Artois, France
9                                 Labri - Univ. Bordeaux, France
10
11 Glucose sources are based on MiniSat (see below MiniSat copyrights). Permissions and copyrights of
12 Glucose (sources until 2013, Glucose 3.0, single core) are exactly the same as Minisat on which it 
13 is based on. (see below).
14
15 Glucose-Syrup sources are based on another copyright. Permissions and copyrights for the parallel
16 version of Glucose-Syrup (the "Software") are granted, free of charge, to deal with the Software
17 without restriction, including the rights to use, copy, modify, merge, publish, distribute,
18 sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is 
19 furnished to do so, subject to the following conditions:
20
21 - The above and below copyrights notices and this permission notice shall be included in all
22 copies or substantial portions of the Software;
23 - The parallel version of Glucose (all files modified since Glucose 3.0 releases, 2013) cannot
24 be used in any competitive event (sat competitions/evaluations) without the express permission of 
25 the authors (Gilles Audemard / Laurent Simon). This is also the case for any competitive event
26 using Glucose Parallel as an embedded SAT engine (single core or not).
27
28
29 --------------- Original Minisat Copyrights
30
31 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
32 Copyright (c) 2007-2010, Niklas Sorensson
33
34 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
35 associated documentation files (the "Software"), to deal in the Software without restriction,
36 including without limitation the rights to use, copy, modify, merge, publish, distribute,
37 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
38 furnished to do so, subject to the following conditions:
39
40 The above copyright notice and this permission notice shall be included in all copies or
41 substantial portions of the Software.
42
43 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
44 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
45 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
46 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
47 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48  **************************************************************************************************/
49
50 #include "core/Solver.h"
51 #include "parallel/ParallelSolver.h"
52 #include "core/SolverTypes.h"
53 #include "parallel/ClausesBuffer.h"
54 #include "parallel/SharedCompanion.h"
55
56
57 using namespace Glucose;
58
59 SharedCompanion::SharedCompanion(int _nbThreads) :
60     nbThreads(_nbThreads), 
61     bjobFinished(false),
62     jobFinishedBy(NULL),
63     panicMode(false), // The bug in the SAT2014 competition :)
64     jobStatus(l_Undef),
65     random_seed(9164825) {
66
67         pthread_mutex_init(&mutexSharedClauseCompanion,NULL); // This is the shared companion lock
68         pthread_mutex_init(&mutexSharedUnitCompanion,NULL); // This is the shared companion lock
69         pthread_mutex_init(&mutexSharedCompanion,NULL); // This is the shared companion lock
70         pthread_mutex_init(&mutexJobFinished,NULL); // This is the shared companion lock
71         if (_nbThreads> 0)  {
72             setNbThreads(_nbThreads);
73             fprintf(stdout,"c Shared companion initialized: handling of clauses of %d threads.\nc %d ints for the sharing clause buffer (not expandable) .\n", _nbThreads, clausesBuffer.maxSize());
74         }
75
76 }
77
78 void SharedCompanion::setNbThreads(int _nbThreads) {
79    nbThreads = _nbThreads;
80    clausesBuffer.setNbThreads(_nbThreads); 
81 }
82
83 void SharedCompanion::printStats() {
84 }
85
86 // No multithread safe
87 bool SharedCompanion::addSolver(ParallelSolver* s) {
88         watchedSolvers.push(s);
89         pthread_mutex_t* mu = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
90         pthread_mutex_init(mu,NULL);
91         assert(s->thn == watchedSolvers.size()-1); // all solvers must have been registered in the good order
92         nextUnit.push(0);
93
94         return true;
95 }
96 void SharedCompanion::newVar(bool sign) {
97    isUnary .push(l_Undef);
98 }
99
100 void SharedCompanion::addLearnt(ParallelSolver *s,Lit unary) {
101   pthread_mutex_lock(&mutexSharedUnitCompanion);
102   if (isUnary[var(unary)]==l_Undef) {
103       unitLit.push(unary);
104       isUnary[var(unary)] = sign(unary)?l_False:l_True;
105   } 
106   pthread_mutex_unlock(&mutexSharedUnitCompanion);
107 }
108
109 Lit SharedCompanion::getUnary(ParallelSolver *s) {
110   int sn = s->thn;
111   Lit ret = lit_Undef;
112
113   pthread_mutex_lock(&mutexSharedUnitCompanion);
114   if (nextUnit[sn] < unitLit.size())
115       ret = unitLit[nextUnit[sn]++];
116   pthread_mutex_unlock(&mutexSharedUnitCompanion);
117  return ret;
118 }
119
120 // Specialized functions for this companion
121 // must be multithread safe
122 // Add a clause to the threads-wide clause database (all clauses, through)
123 bool SharedCompanion::addLearnt(ParallelSolver *s, Clause & c) { 
124   int sn = s->thn; // thread number of the solver
125   bool ret = false;
126   assert(watchedSolvers.size()>sn);
127
128   pthread_mutex_lock(&mutexSharedClauseCompanion);
129   ret = clausesBuffer.pushClause(sn, c);
130   pthread_mutex_unlock(&mutexSharedClauseCompanion);
131   return ret;
132 }
133
134
135 bool SharedCompanion::getNewClause(ParallelSolver *s, int & threadOrigin, vec<Lit>& newclause) { // gets a new interesting clause for solver s 
136   int sn = s->thn;
137   
138     // First, let's get the clauses on the big blackboard
139     pthread_mutex_lock(&mutexSharedClauseCompanion);
140     bool b = clausesBuffer.getClause(sn, threadOrigin, newclause);
141     pthread_mutex_unlock(&mutexSharedClauseCompanion);
142  
143   return b;
144 }
145
146 bool SharedCompanion::jobFinished() {
147     bool ret = false;
148     pthread_mutex_lock(&mutexJobFinished);
149     ret = bjobFinished;
150     pthread_mutex_unlock(&mutexJobFinished);
151     return ret;
152 }
153
154 bool SharedCompanion::IFinished(ParallelSolver *s) {
155     bool ret = false;
156     pthread_mutex_lock(&mutexJobFinished);
157     if (!bjobFinished) {
158         ret = true;
159         bjobFinished = true;
160         jobFinishedBy = s;
161     }
162     pthread_mutex_unlock(&mutexJobFinished);
163     return ret;
164 }
165
166
167