-
Notifications
You must be signed in to change notification settings - Fork 0
/
DDSolver.h
230 lines (189 loc) · 7.03 KB
/
DDSolver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// Created by nandgate on 10/24/2024.
//
#ifndef DDSOLVER_H
#define DDSOLVER_H
#include <thread>
#include <condition_variable>
#include <mutex>
#include <atomic>
#include "DD.h"
#include "grb.h"
#include "NodeExplorer.h"
#include <queue>
#include <stack>
const unsigned int NUM_WORKERS = 3;
enum STATUS {
WORKER_NEEDS_NODES = 0x1,
MASTER_NEEDS_NODES = 0x2,
WORKER_WORKING = 0x4,
MASTER_ASSIGNED_NODES = 0x8,
WORKER_SHARED_NODES = 0x10,
NOT_ENOUGH_NODES_TO_SHARE = 0x20,
SOLVER_FINISHED = 0x40,
MASTER_RECEIVED_NODES = 0x80
};
class Payload {
// std::condition_variable cv;
// std::mutex lock;
/*
* 0 : worker working in progress.
* 1 : worker needs nodes.
* 2 : master needs nodes.
* 4 : worker working in progress.
* 8 : master assigned nodes to worker.
* 16 : worker shared nodes to master (load balance).
* 32 : not enough nodes to share to master.
*/
public:
Payload() = default;
vector<Node_t> nodes_;
// std::atomic<uint8_t> status{}; // worker's current status.
volatile uint8_t status = 0;
std::mutex lock; // around nodes vector.
std::condition_variable cv; // to wake up the worker waiting for nodes.
// uint8_t payloadStatus = 0;
vector<Node_t> getNodes(bool &done); // called by worker.
void addNodesToWorker(vector<Node_t> nodes); // called by master.
bool masterRequireNodes() const noexcept; // called by master.
void askWorkerForNodes(); // called by master.
uint8_t getStatus() const noexcept;
vector<Node_t> getNodesFromWorker();
void addNodesToMaster(vector<Node_t> nodes);
void setStatus(uint8_t status_);
};
class DDSolver {
friend class Worker;
friend class Master;
enum STATUS_FLAGS {
ASSIGNED,
REQUESTED = 8,
REQUEST_MASTER = 1,
REQUEST_WORKER = 2,
PROCESSING = 0
};
class WorkerElement {
std::condition_variable cv;
std::mutex lock;
vector<Node_t> nodes_;
std::atomic<uint8_t> status{};
/*
* 0 : worker working in progress.
* 1 : worker needs nodes.
* 2 : master needs nodes.
* 4 : master assigned nodes to worker.
* 8 : worker assigned nodes to master.
* 16 : not enough nodes to return to master.
*/
public:
WorkerElement() = default;
/**
* Worker calls this function to collect the nodes from master.
*/
vector<Node_t> getWork() {
{
std::scoped_lock l{lock};
if (!nodes_.empty()) {
/* either master placed some nodes initially, or worker placed some nodes previously
for master on master's request. Status flag should be zero. */
auto nodes = move(nodes_);
// status.store(0, memory_order_release); // really necessary?
return nodes;
}
}
// indicate master and wait.
status.store(1,memory_order_release);
while (true) {
std::unique_lock ul{lock};
cv.wait(ul, [&]{return !nodes_.empty();});
vector<Node_t> work = std::move(nodes_);
status.store(0b0, memory_order_release);
// nodes_.clear();
return work;
}
}
/**
* Master adds the nodes to the element.
*/
void addWork(vector<Node_t> work) {
// should be done by the master.
{
std::unique_lock<std::mutex> ul{lock};
nodes_ = move(work);
}
status.store(4, memory_order_release);
cv.notify_one();
}
bool submitWorkRequest() { // called by master.
// if worker itself require work?
auto val = status.load(memory_order_acquire);
if (val & 0b1000) return false;
status.store(2, memory_order_release);
return true;
}
};
class NodeQueue {
struct comparator {
bool operator() (const Node_t& node1, const Node_t& node2) const {
// return (node1.ub + node2.lb) < (node2.ub + node2.lb); // need to optimize this with ints
return node1.globalLayer > node2.globalLayer;
}
};
// use either queue or vector or priority queue.
// use mutex
priority_queue<Node_t, vector<Node_t>, comparator> q;
// stack<Node_t> q;
public:
NodeQueue() = default;
explicit NodeQueue(vector<Node_t> nodes): q{nodes.begin(), nodes.end()}{}
void pushNodes(vector<Node_t> nodes);
void pushNode(Node_t node);
Node_t getNode();
vector<Node_t> getNodes(size_t n);
[[nodiscard]] bool empty() const { return q.empty();}
[[nodiscard]] size_t size() const {return q.size();}
};
NodeQueue nodeQueue; // global queue.
double optimalLB;
const shared_ptr<Network> networkPtr;
#ifdef SOLVER_STATS
size_t numNodesExplored = 0;
size_t numNodesFound = 0;
size_t numPrunedByBound = 0;
size_t numNodesUnnecessary = 0;
size_t numQueueEntered = 0;
void displayStats() const {
cout << "************************ Stats for nerds **************************" << endl;
cout << "Total number of nodes added to queue: " << numQueueEntered << endl;
cout << "Number of nodes processed: " << numNodesExplored << endl;
cout << "Number of nodes discarded by feasibility: " << numPrunedByBound << endl;
cout << "Number of unnecessarily processed nodes: " << numNodesUnnecessary << endl;
cout << "********************************************************************" << endl;
}
#endif
void process(NodeExplorer explorer);
void processWork(unsigned int id, pair<CutContainer, CutContainer> cuts);
void processWork2(unsigned int id, pair<CutContainer, CutContainer> cuts);
void startMaster2();
void startMaster();
void startMaster3();
void processWork3(unsigned int id, pair<CutContainer, CutContainer> cuts);
std::mutex queueLock;
std::atomic<double> globalLB{numeric_limits<double>::lowest()};
std::atomic_bool isCompleted{false};
vector<Payload> workers{NUM_WORKERS};
public:
explicit DDSolver(const shared_ptr<Network>& networkPtr_):networkPtr{networkPtr_}, optimalLB{std::numeric_limits<double>::lowest()} { }
// DDSolver() : optimalLB{std::numeric_limits<double>::lowest()}{}
[[nodiscard]] Node_t getNode();
[[nodiscard]] double getOptimalLB() const;
void setLB(double lb);
void initialize();
void start();
void startSolve(optional<pair<CutContainer, CutContainer>> initialCuts);
void startSolveParallel(optional<pair<CutContainer,CutContainer>> initialCuts);
pair<CutContainer, CutContainer> initializeCuts();
pair<CutContainer, CutContainer> initializeCuts2(size_t n = 50);
void startPThreadSolver();
};
#endif //DDSOLVER_H