Painless
A framework to ease parallelization of sequential CDCL SAT solvers
Loading...
Searching...
No Matches
ClauseBuffer.hpp
1#pragma once
2
3#include "containers/ClauseExchange.hpp"
4#include "utils/Logger.hpp"
5#include "utils/Parameters.hpp"
6#include <atomic>
7#include <boost/lockfree/policies.hpp>
8#include <boost/lockfree/queue.hpp>
9#include <vector>
26{
27 private:
28 boost::lockfree::queue<ClauseExchange*, boost::lockfree::fixed_sized<false>> queue;
29 std::atomic<size_t> m_size;
30
31 public:
35 ClauseBuffer() = delete;
36
41 explicit ClauseBuffer(size_t size)
42 : queue(size)
43 , m_size(0)
44 {
45 }
46
50 ClauseBuffer(const ClauseBuffer&) = delete;
51
55 ClauseBuffer(const ClauseBuffer&&) = delete;
56
61
66
71
76
82 bool addClause(ClauseExchangePtr clause)
83 {
84 ClauseExchange* raw = clause->toRawPtr();
85 if (queue.push(raw)) {
86 m_size.fetch_add(1, std::memory_order_release);
87 return true;
88 } else {
89 // Reference count is decremented, since we do not store the returned ClauseExchangePtr, thus it goes out of scope
91 return false;
92 }
93 }
94
100 size_t addClauses(const std::vector<ClauseExchangePtr>& clauses)
101 {
102 size_t old_size = m_size.load(std::memory_order_relaxed);
103
104 for (const auto& clause : clauses) {
105 addClause(clause);
106 }
107
108 return m_size.load(std::memory_order_acquire) - old_size;
109 }
110
120 bool tryAddClauseBounded(ClauseExchangePtr clause)
121 {
122 ClauseExchange* raw = clause->toRawPtr();
123 if (queue.bounded_push(raw)) {
124 m_size.fetch_add(1, std::memory_order_release);
125 return true;
126 } else {
128 return false;
129 }
130 }
131
141 size_t tryAddClausesBounded(const std::vector<ClauseExchangePtr>& clauses)
142 {
143 size_t old_size = m_size.load(std::memory_order_relaxed);
144 for (const auto& clause : clauses) {
145 if (!tryAddClauseBounded(clause)) {
146 // If a push fails, we've reached the capacity, so we break
147 break;
148 }
149 }
150 return m_size.load(std::memory_order_acquire) - old_size;
151 }
152
158 bool getClause(ClauseExchangePtr& clause)
159 {
160 LOGDEBUG3("Size before pop %ld", this->size());
161 ClauseExchange* raw;
162 if (queue.pop(raw)) {
163 clause = ClauseExchange::fromRawPtr(raw);
164 m_size.fetch_sub(1, std::memory_order_release);
165 return true;
166 }
167 return false;
168 }
169
174 void getClauses(std::vector<ClauseExchangePtr>& clauses)
175 {
176 ClauseExchange* raw;
177 while (queue.pop(raw)) {
178 clauses.push_back(ClauseExchange::fromRawPtr(raw));
179 m_size.fetch_sub(1, std::memory_order_release);
180 }
181 }
182
187 size_t size() const { return m_size.load(std::memory_order_acquire); }
188
192 void clear()
193 {
194 ClauseExchange* raw;
195 while (queue.pop(raw)) {
197 }
198 m_size.store(0, std::memory_order_release);
199 }
200
205 bool empty() const { return m_size.load(std::memory_order_acquire) == 0; }
206};
207
Defines logging functions and macros for the SAT solver.
#define LOGDEBUG3(...)
Log a debug message with verbosity level 4 and magenta color.
Definition Logger.hpp:229
Wrapper for boost::lockfree::queue to manage ClauseExchange objects.
Definition ClauseBuffer.hpp:26
ClauseBuffer()=delete
Default Constructor deleted to enforce parameterized constructor.
bool empty() const
Checks if the buffer is empty.
Definition ClauseBuffer.hpp:205
void getClauses(std::vector< ClauseExchangePtr > &clauses)
Retrieves all available clauses from the buffer.
Definition ClauseBuffer.hpp:174
ClauseBuffer(ClauseBuffer &&)=default
Move constructor.
~ClauseBuffer()
Destructor.
Definition ClauseBuffer.hpp:75
bool addClause(ClauseExchangePtr clause)
Adds a single clause to the buffer.
Definition ClauseBuffer.hpp:82
size_t size() const
Returns the current number of clauses in the buffer.
Definition ClauseBuffer.hpp:187
ClauseBuffer(size_t size)
Constructs a ClauseBuffer with the specified size.
Definition ClauseBuffer.hpp:41
bool tryAddClauseBounded(ClauseExchangePtr clause)
Attempts to add a single clause to the buffer using bounded push.
Definition ClauseBuffer.hpp:120
void clear()
Clears all clauses from the buffer.
Definition ClauseBuffer.hpp:192
ClauseBuffer(const ClauseBuffer &)=delete
Copy constructor (deleted).
size_t tryAddClausesBounded(const std::vector< ClauseExchangePtr > &clauses)
Attempts to add multiple clauses to the buffer using bounded push.
Definition ClauseBuffer.hpp:141
size_t addClauses(const std::vector< ClauseExchangePtr > &clauses)
Adds multiple clauses to the buffer.
Definition ClauseBuffer.hpp:100
bool getClause(ClauseExchangePtr &clause)
Retrieves a single clause from the buffer.
Definition ClauseBuffer.hpp:158
ClauseBuffer & operator=(const ClauseBuffer &)=delete
Assignment operator (deleted).
ClauseBuffer(const ClauseBuffer &&)=delete
Move constructor (deleted).
ClauseBuffer & operator=(ClauseBuffer &&)=default
Move assignment operator.
Represents an exchangeable clause with flexible array member.
Definition ClauseExchange.hpp:42
ClauseExchange * toRawPtr()
Convert to a raw pointer and increment the reference count.
Definition ClauseExchange.hpp:151
static ClauseExchangePtr fromRawPtr(ClauseExchange *ptr)
Create an intrusive_ptr from a raw pointer.
Definition ClauseExchange.hpp:162