Painless
A framework to ease parallelization of sequential CDCL SAT solvers
Loading...
Searching...
No Matches
SharingEntity.hpp
1#pragma once
2
3#include "containers/ClauseExchange.hpp"
4#include "utils/Logger.hpp"
5#include "utils/Parameters.hpp"
6
7#include <atomic>
8#include <memory>
9#include <mutex>
10#include <set>
11#include <shared_mutex>
12
28class SharingEntity : public std::enable_shared_from_this<SharingEntity>
29{
30 public:
40 : m_sharingId(s_currentSharingId.fetch_add(1))
41 , m_clients(0)
42 {
43 LOGDEBUG1("I am sharing entity %d", m_sharingId);
44 }
45
54 SharingEntity(const std::vector<std::shared_ptr<SharingEntity>>& clients)
55 : m_sharingId(s_currentSharingId.fetch_add(1))
56 , m_clients(clients.begin(), clients.end())
57 {
58 LOGDEBUG1("I am sharing entity %d", m_sharingId);
59 }
60
64 virtual ~SharingEntity() {}
65
74 virtual bool importClause(const ClauseExchangePtr& clause) = 0;
75
83 virtual void importClauses(const std::vector<ClauseExchangePtr>& v_clauses) = 0;
84
89 int getSharingId() const { return this->m_sharingId; }
90
95 void setSharingId(int _id) { this->m_sharingId = _id; }
96
102 virtual void addClient(std::shared_ptr<SharingEntity> client)
103 {
104 std::unique_lock<std::shared_mutex> lock(m_clientsMutex);
105 LOGDEBUG3("Sharing Entity %d: new client %p (counts: %d)", m_sharingId, client.get(), client.use_count());
106 m_clients.push_back(client);
107 }
108
109
116 virtual void removeClient(std::shared_ptr<SharingEntity> client)
117 {
118 std::unique_lock<std::shared_mutex> lock(m_clientsMutex);
119 auto initialSize = m_clients.size();
120 m_clients.erase(
121 std::remove_if(m_clients.begin(),
122 m_clients.end(),
123 [&client](const std::weak_ptr<SharingEntity>& wp) { return wp.lock() == client; }),
124 m_clients.end());
125 if (m_clients.size() < initialSize) {
126 LOGDEBUG3("Sharing Entity %d: removed client %p", m_sharingId, client.get());
127 }
128 }
129
137 size_t getClientCount() const
138 {
139 std::shared_lock<std::shared_mutex> lock(m_clientsMutex);
140 return m_clients.size();
141 }
142
147 {
148 std::unique_lock<std::shared_mutex> lock(m_clientsMutex);
149 m_clients.clear();
150 }
151
152 protected:
164 virtual bool exportClauseToClient(const ClauseExchangePtr& clause, std::shared_ptr<SharingEntity> client)
165 {
166 return client->importClause(clause);
167 }
168
177 bool exportClause(const ClauseExchangePtr& clause)
178 {
179 std::shared_lock<std::shared_mutex> lock(m_clientsMutex);
180 bool exported = false;
181 for (const auto& client : m_clients) {
182 if (auto sharedClient = client.lock()) {
183 if (exportClauseToClient(clause, sharedClient))
184 exported = true;
185 }
186 }
187 return exported;
188 }
189
197 void exportClauses(const std::vector<ClauseExchangePtr>& clauses)
198 {
199 std::shared_lock<std::shared_mutex> lock(m_clientsMutex);
200 for (const auto& weakClient : m_clients) {
201 if (auto client = weakClient.lock()) {
202 for (const ClauseExchangePtr& clause : clauses) {
203 exportClauseToClient(clause, client);
204 }
205 }
206 }
207 }
208
209 private:
211 int m_sharingId;
212
214 inline static std::atomic<int> s_currentSharingId{ 0 };
215
216 protected:
218 std::vector<std::weak_ptr<SharingEntity>> m_clients;
219
221 mutable std::shared_mutex m_clientsMutex;
222};
223
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
#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
SharingEntity()
Construct a new SharingEntity object.
Definition SharingEntity.hpp:39
void setSharingId(int _id)
Set the sharing ID of this entity.
Definition SharingEntity.hpp:95
virtual void importClauses(const std::vector< ClauseExchangePtr > &v_clauses)=0
Import multiple clauses to this sharing entity.
void clearClients()
Remove all clients.
Definition SharingEntity.hpp:146
virtual bool importClause(const ClauseExchangePtr &clause)=0
Import a single clause to this sharing entity.
virtual ~SharingEntity()
Destroy the SharingEntity object.
Definition SharingEntity.hpp:64
size_t getClientCount() const
Get the current number of clients.
Definition SharingEntity.hpp:137
void exportClauses(const std::vector< ClauseExchangePtr > &clauses)
Export multiple clauses to all registered clients.
Definition SharingEntity.hpp:197
virtual bool exportClauseToClient(const ClauseExchangePtr &clause, std::shared_ptr< SharingEntity > client)
Export a clause to a specific client.
Definition SharingEntity.hpp:164
SharingEntity(const std::vector< std::shared_ptr< SharingEntity > > &clients)
Construct a new SharingEntity object.
Definition SharingEntity.hpp:54
virtual void removeClient(std::shared_ptr< SharingEntity > client)
Remove a specific client from this entity.
Definition SharingEntity.hpp:116
bool exportClause(const ClauseExchangePtr &clause)
Export a clause to all registered clients.
Definition SharingEntity.hpp:177
int getSharingId() const
Get the sharing ID of this entity.
Definition SharingEntity.hpp:89
virtual void addClient(std::shared_ptr< SharingEntity > client)
Add a client to this entity.
Definition SharingEntity.hpp:102
std::vector< std::weak_ptr< SharingEntity > > m_clients
List of weak pointers to client SharingEntities.
Definition SharingEntity.hpp:218
std::shared_mutex m_clientsMutex
Mutex to protect access to m_clients.
Definition SharingEntity.hpp:221