Painless
A framework to ease parallelization of sequential CDCL SAT solvers
Loading...
Searching...
No Matches
SharingStrategy.hpp
1#pragma once
2
3#include "SharingEntity.hpp"
4#include "containers/ClauseDatabase.hpp"
5#include "sharing/SharingStatistics.hpp"
6#include "utils/Logger.hpp"
7#include <algorithm>
8#include <atomic>
9#include <memory>
10#include <mutex>
11#include <set>
12
13
26{
27 public:
42 SharingStrategy(const std::vector<std::shared_ptr<SharingEntity>>& producers,
43 const std::vector<std::shared_ptr<SharingEntity>>& consumers,
44 const std::shared_ptr<ClauseDatabase>& clauseDB)
45 : SharingEntity(consumers)
46 , m_producers(producers.begin(), producers.end())
47 , m_clauseDB(clauseDB)
48 {
49 LOGDEBUG1("Be sure to call connectConstructorProducers after construction, this is due to the base class "
50 "shared_from_this.");
51 }
52
56 virtual ~SharingStrategy() {}
57
62 virtual bool doSharing() = 0;
63
68 virtual std::chrono::microseconds getSleepingTime() { return std::chrono::microseconds(__globalParameters__.sharingSleep); };
69
73 virtual void printStats()
74 {
75 LOGSTAT("Strategy Basic Stats: receivedCls %d, sharedCls %d, filteredAtImport: %d",
78 stats.filteredAtImport.load());
79 }
80
86 {
87 std::shared_lock<std::shared_mutex> lock(m_producersMutex);
88 for (auto& weakProducer : m_producers) {
89 if (auto producer = weakProducer.lock())
90 producer->addClient(shared_from_this());
91 }
92 }
93
98 virtual void addProducer(std::shared_ptr<SharingEntity> producer)
99 {
100 std::unique_lock<std::shared_mutex> lock(m_producersMutex);
101 m_producers.push_back(producer);
102 LOGDEBUG2("[SharingStrategy] Added new producer");
103 }
104
110 virtual void connectProducer(std::shared_ptr<SharingEntity> producer)
111 {
112 producer->addClient(shared_from_this());
113 LOGDEBUG2("[SharingStrategy] Producer %u is connected!", producer->getSharingId());
114 }
115
120 virtual void removeProducer(std::shared_ptr<SharingEntity> producer)
121 {
122 std::unique_lock<std::shared_mutex> lock(m_producersMutex);
123 producer->removeClient(shared_from_this());
124 m_producers.erase(
125 std::remove_if(m_producers.begin(),
126 m_producers.end(),
127 [&producer](const std::weak_ptr<SharingEntity>& wp) { return wp.lock() == producer; }),
128 m_producers.end());
129 LOGDEBUG2("[SharingStrategy] Removed producer");
130 }
131
132 protected:
136 bool exportClauseToClient(const ClauseExchangePtr& clause, std::shared_ptr<SharingEntity> client) override
137 {
138 if (clause->from != client->getSharingId())
139 return client->importClause(clause);
140 else
141 return false;
142 }
143
147 std::shared_ptr<ClauseDatabase> m_clauseDB;
148
151
152 /* Producers Management */
153
155 std::vector<std::weak_ptr<SharingEntity>> m_producers;
156
158 mutable std::shared_mutex m_producersMutex;
159};
Defines logging functions and macros for the SAT solver.
#define LOGDEBUG2(...)
Log a debug message with verbosity level 2 and magenta color.
Definition Logger.hpp:221
#define LOGSTAT(...)
Log a statistics message.
Definition Logger.hpp:145
#define LOGDEBUG1(...)
Log a debug message with verbosity level 1 and blue color.
Definition Logger.hpp:213
A base class representing entities that can exchange clauses between themselves.
Definition SharingEntity.hpp:29
SharingStrategy class, inheriting from SharingEntity.
Definition SharingStrategy.hpp:26
virtual bool doSharing()=0
Executes the sharing process from producers to consumers.
virtual void removeProducer(std::shared_ptr< SharingEntity > producer)
Removes a producer from this strategy.
Definition SharingStrategy.hpp:120
virtual ~SharingStrategy()
Virtual destructor.
Definition SharingStrategy.hpp:56
virtual void addProducer(std::shared_ptr< SharingEntity > producer)
Adds a producer to this strategy.
Definition SharingStrategy.hpp:98
bool exportClauseToClient(const ClauseExchangePtr &clause, std::shared_ptr< SharingEntity > client) override
A SharingStrategy doesn't send a clause to the source client (->from must store the sharingId of its ...
Definition SharingStrategy.hpp:136
virtual void printStats()
Prints the statistics of the strategy.
Definition SharingStrategy.hpp:73
std::vector< std::weak_ptr< SharingEntity > > m_producers
The set holding the references to the producers.
Definition SharingStrategy.hpp:155
SharingStatistics stats
Sharing statistics.
Definition SharingStrategy.hpp:150
void connectConstructorProducers()
Add this to the producers' clients list.
Definition SharingStrategy.hpp:85
virtual void connectProducer(std::shared_ptr< SharingEntity > producer)
Connect a producer to this strategy. Add (this) to clients of (producer)
Definition SharingStrategy.hpp:110
virtual std::chrono::microseconds getSleepingTime()
Determines the sleeping time for the sharer.
Definition SharingStrategy.hpp:68
std::shared_ptr< ClauseDatabase > m_clauseDB
Clause database where exported clauses are stored.
Definition SharingStrategy.hpp:147
std::shared_mutex m_producersMutex
Shared mutex for producers list (to become RCU ?)
Definition SharingStrategy.hpp:158
SharingStrategy(const std::vector< std::shared_ptr< SharingEntity > > &producers, const std::vector< std::shared_ptr< SharingEntity > > &consumers, const std::shared_ptr< ClauseDatabase > &clauseDB)
Constructor for SharingStrategy. Be sure to call connectConstructorProducers after construction,...
Definition SharingStrategy.hpp:42
Local Sharing statistics.Sharing statistics.
Definition SharingStatistics.hpp:10
unsigned long sharedClauses
Number of shared clauses that have been shared.
Definition SharingStatistics.hpp:12
std::atomic< unsigned long > receivedClauses
Number of shared clauses produced.
Definition SharingStatistics.hpp:15
std::atomic< unsigned long > filteredAtImport
Number of clause filtered at import.
Definition SharingStatistics.hpp:18