ClauseExchange
ClauseExchange is a memory-efficient class for storing and managing SAT clauses of varying sizes. It uses a flexible array member for optimized memory usage and intrusive reference counting for efficient memory management.
Key Features
- Memory Layout
- Uses a flexible array member (
lits[0]
) for storing literals
- Fixed-size header containing:
unsigned int lbd;
int from;
unsigned int size;
std::atomic<unsigned int> refCounter;
- Smart Pointer Management
- Uses
boost::intrusive_ptr
for reference counting
- Type alias:
using ClauseExchangePtr = boost::intrusive_ptr<ClauseExchange>
- Custom reference counting through:
Represents an exchangeable clause with flexible array member.
Definition ClauseExchange.hpp:42
- Iterator Support
- STL-compatible iterators
- Supports range-based for loops
int* begin();
int* end();
const int* begin() const;
const int* end() const;
- Utility Functions
int& operator[](unsigned int index);
const int& operator[](unsigned int index) const;
void sortLiterals();
void sortLiteralsDescending();
std::string toString() const;
- LBD Handling
- Forces LBD ≥ 2 for non-unit clauses
- Unit clauses can have LBD of 0 or 1 (I am thinking of forcing it to 0).
Usage Example
clause->lits[0] = 1;
clause->lits[1] = -2;
clause->lits[2] = 3;
for (int& lit : *clause) {
}
static ClauseExchangePtr create(const unsigned int size, const unsigned int lbd=0, const int from=-1)
Create a new ClauseExchange object.
ClauseBuffer
ClauseBuffer is a thread-safe buffer for exchanging clauses between producers and consumers. It's built on top of boost::lockfree::queue
for efficient multiple producers-multiple consumers operations.
Key Features
- Lock-Free Operations
- Based on
boost::lockfree::queue
- Non-fixed size queue
- Multiple producer/consumer safe
- Atomic Size Tracking
std::atomic<size_t> m_size;
- Clause Addition
bool addClause(ClauseExchangePtr clause);
size_t addClauses(const std::vector<ClauseExchangePtr>& clauses);
bool tryAddClauseBounded(ClauseExchangePtr clause);
size_t tryAddClausesBounded(const std::vector<ClauseExchangePtr>& clauses);
- Clause Retrieval
bool getClause(ClauseExchangePtr& clause);
void getClauses(std::vector<ClauseExchangePtr>& clauses);
- Utility Functions
size_t size() const;
bool empty() const;
void clear();
Usage Example
if (buffer.addClause(clause)) {
}
ClauseExchangePtr received;
if (buffer.getClause(received)) {
}
std::vector<ClauseExchangePtr> clauses;
buffer.getClauses(clauses);
Wrapper for boost::lockfree::queue to manage ClauseExchange objects.
Definition ClauseBuffer.hpp:26
toRawPtr and fromRawPtr
ClauseBuffer internally uses boost::lockfree::queue
which operates on raw pointers, while the external interface uses ClauseExchangePtr
(smart pointers). The toRawPtr
and fromRawPtr
methods of ClauseExchange bridge this gap safely.
refCounter.fetch_add(1, std::memory_order_relaxed);
return this;
}
return ClauseExchangePtr(ptr, false);
}
if (queue.push(raw)) {
m_size.fetch_add(1, std::memory_order_release);
return true;
} else {
return false;
}
}
if (queue.pop(raw)) {
m_size.fetch_sub(1, std::memory_order_release);
return true;
}
return false;
}
bool addClause(ClauseExchangePtr clause)
Adds a single clause to the buffer.
Definition ClauseBuffer.hpp:82
bool getClause(ClauseExchangePtr &clause)
Retrieves a single clause from the buffer.
Definition ClauseBuffer.hpp:158
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
toRawPtr()
: Increments reference count before storing in queue
fromRawPtr()
: Creates smart pointer without additional increment
- Important
- A ClauseExchangePtr should not be gotten through
.get()
: Be careful to not leave hanging raw pointers, otherwise the ClauseExchange instance will never be freed.
{
queue.addClause(raw);
}
ClauseExchangePtr newClause;
queue.getClause(newClause);