Repository navigation
Expand file tree
/
Copy pathsampleTest.cpp
More file actions
323 lines (270 loc) · 9.63 KB
/
Copy pathsampleTest.cpp
File metadata and controls
323 lines (270 loc) · 9.63 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
#include <functional>
#include <random>
#include <unordered_set>
#include <map>
#include <set>
#include <chrono>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <unistd.h>
//each node has four elements - weight, pathWeight, level, children
struct Node {
int weight;
int level;
std::atomic<bool> markedBit = false; //remove ID
long long pathWeight = LLONG_MAX;
std::vector<Node*> children;
std::vector<Node*> parents;
Node(int weight, int level) : weight(weight), level(level) {}
};
//n is the graph size that is desired
//randomly generates values from 0 to n-1 for every node and starts with level 0
//sets the 0 (starting node) pathWeight as 0
//generates a unique set of parents from the nodes that have already been considered
//for each index it connects it and increments the level of the child
std::vector<Node*> generateAcyclicGraph(int n, unsigned int seed) {
//n = number of nodes
std::vector<Node*> nodes;
nodes.reserve(n);
//random objects
std::mt19937 gen(seed);
std::uniform_int_distribution<int> dis(0, n-1);
// Create nodes
for (int i = 0; i < n; ++i) {
nodes.push_back(new Node(dis(gen), 0)); // Level will be updated later
}
if (!nodes.empty()) {
nodes[0]->pathWeight = 0;
}
// Create an acyclic graph
for (int i = 1; i < n; ++i) {
// Randomly choose a set of unique parent from the existing nodes
std::set<int> parentIndices;
std::uniform_int_distribution<int> levelDist(1, std::min(i,14));
int levelOfMyNode = levelDist(gen);
std::uniform_int_distribution<int> parentDist(0, levelOfMyNode-1);
std::uniform_int_distribution<int> parentChoosingDist(0, i-1);
int howManyParents = parentDist(gen)+1;
//std::cout<<i<<" " <<levelOfMyNode<< " "<< howManyParents<< std::endl;
while(((int) parentIndices.size()) < howManyParents){
int indCons = parentChoosingDist(gen);
if(nodes[indCons]->level<levelOfMyNode){
parentIndices.insert(indCons);
}
}
//for each index in the set just creating that graph
int maxParentLevel = 0;
for (int parentIndex : parentIndices) {
maxParentLevel = std::max(nodes[parentIndex]->level, maxParentLevel);
nodes[i]->pathWeight = std::min(nodes[i]->pathWeight, nodes[parentIndex]->pathWeight + nodes[i]->weight);
nodes[parentIndex]->children.push_back(nodes[i]);
nodes[i]->parents.push_back(nodes[parentIndex]);
}
nodes[i]->level = maxParentLevel + 1;
}
return nodes;
}
//n is the seed set size
//generates <=n nodes of the graph then reassign there value with a value from 0 to nodeSize - 1 like above
std::vector<Node*> generateSeedSet(int n, int nodeSize, std::vector<Node*> &graph, unsigned int seed){
//n = seed set size
//nodeSize = node size of the graph
//want to return a vector of Node that are the nodes which have been changed to a different random weight
std::vector<Node*> seedSet;
seedSet.reserve(n);
//random objects
std::mt19937 gen(seed);
std::uniform_int_distribution<int> dis(0, nodeSize-1);
std::set<int> indices;//optimizie gen with hash map
while((int)indices.size()<n){
int indToChange = dis(gen);
indices.insert(indToChange);
}
for(int ind: indices){
Node* changingNode = graph[ind];
int newWeight = dis(gen);
changingNode->weight = newWeight;
seedSet.push_back(changingNode);
}
return seedSet;
}
void printGraph(Node* root) {
if (!root) return;
std::map<Node*, int> nodeIds; // Map nodes to unique IDs
int nodeId = 0; // Start assigning IDs from 0
std::queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* current = q.front();
q.pop();
if (nodeIds.find(current) == nodeIds.end()) {
nodeIds[current] = nodeId++;
}
std::cout << "Node ID: " << nodeIds[current] << ", Level: " << current->level << ", Children: ";
for (Node* child : current->children) {
if (nodeIds.find(child) == nodeIds.end()) {
nodeIds[child] = nodeId++;
q.push(child);
}
std::cout << nodeIds[child] << " ";
}
std::cout << std::endl;
}
}
//computes a node
//returns true if new path weight is different from old path weight
bool calcNode(Node* nd){
long long oldPathWeight = nd->pathWeight;
long long newPathWeight = LLONG_MAX;
for(Node* parent : nd->parents){
newPathWeight= std::min(parent->pathWeight, newPathWeight);
}
newPathWeight+= nd->weight;
nd->markedBit = false;
if(newPathWeight==oldPathWeight){
return false;
}else{
nd->pathWeight = newPathWeight;
return true;
}
}
//bfs using seedSet
void sequentialBFS(std::vector<Node*> &graph, std::vector<Node*> seedSet, int maxLevel){
std::vector<std::queue<Node*>> vq(maxLevel+1);
for(Node* nd : seedSet){
if(!nd->markedBit){
vq[nd->level].push(nd);
nd->markedBit = true;
}
}
for(int level = 0; level<=maxLevel; level++){
std::queue<Node*>& q = vq[level];
if(vq[level].size()==0){
continue;
}
while(!q.empty()){
Node* current = q.front();
q.pop();
usleep(10);
if(calcNode(current)){
for(Node* child : current->children){
if(!child->markedBit){
vq[child->level].push(child);
child->markedBit = true;
}
}
}
}
}
}
void testingSequential(std::vector<Node*> &graph, int seedSetSize, int maxLevel, int seed, int n){
std::vector<Node*> seedSet = generateSeedSet(seedSetSize, n, graph, seed);
auto beg = std::chrono::high_resolution_clock::now();
sequentialBFS(graph, seedSet, maxLevel);
auto end = std::chrono::high_resolution_clock::now();
std::cout << "Sequential BFS for " << n << " graph size with seed set size of "<< seedSetSize <<" : "
<< std::chrono::duration_cast<std::chrono::microseconds>(end - beg).count()
<< " microseconds" << std::endl;
}
void processLevel(std::queue<Node*>& q, std::mutex& qMutex, std::atomic<bool>& done, std::vector<std::queue<Node*>> vq, std::queue<Node*>& threadQueue) {
while (true) {
Node* current = nullptr;
{
std::lock_guard<std::mutex> lock(qMutex);
if (q.empty()) {
done = true;
return;
}
current = q.front();
q.pop();
}
using namespace std::chrono_literals;
std::this_thread::sleep_for(10ms);
if (calcNode(current)) {
for (Node* child : current->children) {
if (!child->markedBit) {
{
threadQueue.push(child);
}
child->markedBit = true; //atomic
}
}
}
}
}
void multithreadedBFS(std::vector<Node*>& graph, std::vector<Node*> seedSet, int maxLevel, size_t numThreads, std::vector<std::thread>& threads) {
std::vector<std::queue<Node*>> vq(maxLevel+1);
std::mutex qMutex;
for(Node* nd : seedSet){
if(!nd->markedBit){
vq[nd->level].push(nd);
nd->markedBit = true;
}
}
for(int level = 0; level<=maxLevel; level++){
std::queue<Node*>& q = vq[level];
if(vq[level].size()==0) continue;
std::atomic<bool> done(false);
std::vector<std::queue<Node*>> tq(numThreads);
for (int i = 0; i < (int) numThreads; ++i) {
threads[i]= std::thread(processLevel, std::ref(q), std::ref(qMutex), std::ref(done), std::ref(vq), std::ref(tq[i]));
}
for (auto& thread : threads) {
if (thread.joinable()) {
thread.join();
}
}
while (!done.load()) {
std::this_thread::yield();
}
for (int i = 0; i < (int) numThreads; ++i) {
while(!tq[i].empty()){
Node* top = tq[i].front();
tq[i].pop();
vq[top->level].push(top);
}
}
}
}
void testingMultithreadedBFS(std::vector<Node*> &graph, int seedSetSize, int maxLevel, size_t numThreads,int seed, int n){
std::vector<Node*> seedSet = generateSeedSet(seedSetSize, n, graph, seed);
std::vector<std::thread> threads((int)numThreads);
auto beg = std::chrono::high_resolution_clock::now();
multithreadedBFS(graph, seedSet, maxLevel, numThreads, threads);
auto end = std::chrono::high_resolution_clock::now();
std::cout << "PoolScheduling MT BFS for " << n << " graph size with seed set size of "<< seedSetSize << " with thread count of "<< numThreads << " : "
<< std::chrono::duration_cast<std::chrono::microseconds>(end - beg).count()
<< " microseconds" << std::endl;
}
int main() {
int n = 10000; // Number of nodes
std::vector<Node*> graphSeq = generateAcyclicGraph(n, 42);
//printGraph(graphSeq[0]);
std::vector<Node*> graphMT1 = generateAcyclicGraph(n, 42);
int maxLevelSeq = 0;
for (Node* nd : graphSeq) {
maxLevelSeq = std::max(nd->level, maxLevelSeq);
}
int maxLevelMT1 = 0;
for (Node* nd : graphMT1) {
maxLevelMT1 = std::max(nd->level, maxLevelMT1);
}
std::cout<<"Max level for the graph " <<maxLevelSeq<<std::endl;
//int seed set size is 1000
testingSequential(graphSeq, 1000, maxLevelSeq, 45, n);
testingMultithreadedBFS(graphMT1, 1000, maxLevelMT1, 16, 48, n);
// Clean up
for (Node* node : graphSeq) {
delete node;
}
for (Node* node : graphMT1) {
delete node;
}
return 0;
}