Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#include <synchronization.h>
Public Member Functions | |
UniqueClauseStream () | |
UniqueClauseStream (const UniqueClauseStream &)=delete | |
Move only - this is an expensive class to copy. | |
UniqueClauseStream (UniqueClauseStream &&)=default | |
bool | Add (absl::Span< const int > clause) ABSL_LOCKS_EXCLUDED(mutex_) |
bool | Delete (absl::Span< const int > clause) ABSL_LOCKS_EXCLUDED(mutex_) |
CompactVectorVector< int > | NextBatch () ABSL_LOCKS_EXCLUDED(mutex_) |
int | FillUpstreamBuffer (UniqueClauseStream &upstream, int clause_size, int max_clauses_to_export) ABSL_LOCKS_EXCLUDED(mutex_) |
int | NumBufferedLiteralsOfSize (int size) const ABSL_LOCKS_EXCLUDED(mutex_) |
int | NumBufferedLiterals () const ABSL_LOCKS_EXCLUDED(mutex_) |
bool | CanAccept (int size, int lbd) const |
void | RemoveWorstClauses () |
int | lbd_threshold () const ABSL_LOCKS_EXCLUDED(mutex_) |
void | set_lbd_threshold (int lbd) ABSL_LOCKS_EXCLUDED(mutex_) |
Static Public Member Functions | |
static size_t | HashClause (absl::Span< const int > clause, size_t hash_seed=0) |
Computes a hash that is independent of the order of literals in the clause. | |
Static Public Attributes | |
static constexpr int | kMinClauseSize = 3 |
static constexpr int | kMaxClauseSize = 8 |
static constexpr int | kMaxLiteralsPerBatch = 4096 / sizeof(int) |
Export 4KiB of clauses per batch. | |
static constexpr int | kMaxBufferedLiterals = kMaxLiteralsPerBatch |
Emit a stream of clauses in batches without duplicates. Each batch has a fixed number of literals, containing the smallest clauses added. It has a finite size internal buffer that is a small multiple of the batch size.
This class is thread-safe, the idea is to have one per worker plus a global one to deduplicate between workers to minimize contention.
This uses a finite buffer, so some clauses may be dropped if we generate too many more than we export, but that is rarely a problem because we never overfill the "global" stream, and if we drop a clause on a worker, one of the following will most likely happen:
Definition at line 589 of file synchronization.h.
operations_research::sat::UniqueClauseStream::UniqueClauseStream | ( | ) |
Definition at line 1037 of file synchronization.cc.
|
delete |
Move only - this is an expensive class to copy.
|
default |
bool operations_research::sat::UniqueClauseStream::Add | ( | absl::Span< const int > | clause | ) |
Adds the clause to a future batch and returns true if the clause was added. Otherwise returns false. This may return false if the buffer is full. It will not block the clause if it is dropped to avoid unbounded growth of the hash table.
This is just a safety check, the caller should have called CanAccept().
Definition at line 1043 of file synchronization.cc.
bool operations_research::sat::UniqueClauseStream::CanAccept | ( | int | size, |
int | lbd ) const |
Returns true if the stream can accept a clause of the specified size and LBD without dropping it.
Definition at line 1116 of file synchronization.cc.
bool operations_research::sat::UniqueClauseStream::Delete | ( | absl::Span< const int > | clause | ) |
Lazily deletes a clause with the same hash, returns true if it was present. The deleted clause will not be exported (either via NextBatch or FillUpstreamBuffer). A clause with the same hash may be re-added after calling Delete. If another clause with the same hash is added before the deleted clause is emitted then both clauses may be emitted.
Note a clause with this hash may be buffered, but not yet exported.
Definition at line 1064 of file synchronization.cc.
int operations_research::sat::UniqueClauseStream::FillUpstreamBuffer | ( | UniqueClauseStream & | upstream, |
int | clause_size, | ||
int | max_clauses_to_export ) |
Adds up to max_clauses_to_export clauses of a given size to upstream and removes them from the internal buffer.
Don't emit deleted clauses.
Definition at line 1090 of file synchronization.cc.
|
static |
Computes a hash that is independent of the order of literals in the clause.
Definition at line 1152 of file synchronization.cc.
|
inline |
Definition at line 646 of file synchronization.h.
CompactVectorVector< int > operations_research::sat::UniqueClauseStream::NextBatch | ( | ) |
Returns a set of clauses totalling up to kMaxLiteralsPerBatch and removes exported clauses from the internal buffer.
Definition at line 1071 of file synchronization.cc.
int operations_research::sat::UniqueClauseStream::NumBufferedLiterals | ( | ) | const |
Definition at line 1107 of file synchronization.cc.
|
inline |
Returns the number of literals in the buffer in clauses with size <= max_size.
Definition at line 630 of file synchronization.h.
void operations_research::sat::UniqueClauseStream::RemoveWorstClauses | ( | ) |
Delete longest clauses while keeping at least kMaxBufferedLiterals. This guarantees that CanAccept will return the same result as before, and at least the next batch will contain the same clauses, but we will emit fewer old, long clauses in the future.
Stop if removing one more clause of the current size would leave the buffer under full. Otherwise we might remove a shorter clause later!
Definition at line 1127 of file synchronization.cc.
void operations_research::sat::UniqueClauseStream::set_lbd_threshold | ( | int | lbd | ) |
Definition at line 1147 of file synchronization.cc.
|
staticconstexpr |
Bound the total literals we buffer, approximately enforced so shorter clauses can replace longer ones. This can be larger than kMaxLiteralsPerBatch (hence the separate constant), but experiments suggest that this doesn't help.
Definition at line 599 of file synchronization.h.
|
staticconstexpr |
Definition at line 592 of file synchronization.h.
|
staticconstexpr |
Export 4KiB of clauses per batch.
Definition at line 594 of file synchronization.h.
|
staticconstexpr |
Definition at line 591 of file synchronization.h.