-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwf-th-delay.cpp
More file actions
176 lines (141 loc) · 4.55 KB
/
Copy pathwf-th-delay.cpp
File metadata and controls
176 lines (141 loc) · 4.55 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
#include <bits/stdc++.h>
#include <semaphore.h>
using namespace std;
using namespace chrono;
int N, M;
vector<vector<int>> adj;
vector<int> dist, vis;
int num_t;
class outer_list_node {
public:
vector<vector<int>> lists;
vector<bool> done;
atomic<outer_list_node*> next = nullptr;
};
int threshold = 10;
int failure = 5, sleep_time;
int completed = 0;
void wf_bfs(outer_list_node* head, int tid, bool fail) {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
exponential_distribution<double> expRandObj(1.0/sleep_time);
outer_list_node* curr = head;
while(curr != nullptr) {
int null_count = 0, work_on = tid;
bool next_is_null = true;
while(null_count != num_t) {
if(curr->done[work_on] || curr->lists[work_on].empty()) {
null_count++;
work_on++;
if(work_on>=num_t) work_on -= num_t;
continue;
}
if(next_is_null && curr->next == nullptr) {
outer_list_node* new_node = new outer_list_node;
new_node->lists.resize(num_t);
new_node->done.resize(num_t, 0);
outer_list_node* expected = nullptr;
if(!atomic_compare_exchange_strong(&(curr->next), &expected, new_node)) {
delete new_node;
}
}
next_is_null = false;
outer_list_node* next_node = curr->next.load();
vector<int> list = curr->lists[work_on];
bool fl = 1;
if(list.size()>=threshold && work_on != tid) {
shuffle(list.begin(), list.end(), rng);
fl = 0;
}
if(fail) {
double st = expRandObj(rng);
usleep(st*1000);
}
for(auto u: list) {
if(vis[u]) continue;
if(fl && work_on!=tid) {
vector<int> neighbours = adj[u];
shuffle(neighbours.begin(), neighbours.end(), rng);
for(auto v: neighbours) {
if(dist[v] == -1) {
next_node->lists[tid].push_back(v);
dist[v] = dist[u]+1;
}
}
}
else {
for(auto v: adj[u]) {
if(dist[v] == -1) {
next_node->lists[tid].push_back(v);
dist[v] = dist[u]+1;
}
}
}
vis[u] = 1;
}
curr->done[work_on] = true;
null_count++;
work_on++;
if(work_on>=num_t) work_on -= num_t;
}
curr = curr->next;
}
completed = 1;
}
high_resolution_clock::time_point t1, t2;
void work() {
while(completed == 0);
t2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(t2 - t1).count();
cout << duration << "\n";
}
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int i, j;
FILE* f_in = fopen(argv[1], "r");
fscanf(f_in, "%d %d", &N, &M);
adj.resize(N);
dist.resize(N, -1);
vis.resize(N, 0);
for(i=0; i<M; i++) {
int x, y;
fscanf(f_in, "%d %d", &x, &y);
// x--; y--; /////////////////////////////////////////////
if(x>=N || y>=N) continue;
adj[x].push_back(y);
adj[y].push_back(x);
}
fclose(f_in);
cin >> num_t >> sleep_time;
outer_list_node* head = new outer_list_node;
head->lists.resize(num_t);
head->done.resize(num_t, 0);
head->lists[0].push_back(0);
dist[0] = 0;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<bool> fails(num_t, 0);
for(i=0; i<failure; i++) fails[i] = 1;
shuffle(fails.begin(), fails.end(), rng);
t1 = high_resolution_clock::now();
vector<thread> th;
for(i=0; i<num_t; i++) {
th.push_back(thread(wf_bfs, head, i, fails[i]));
}
thread tcheck(work);
for(i=0; i<num_t; i++) {
th[i].join();
}
tcheck.join();
while(head != nullptr) {
outer_list_node* temp = head;
head = head->next;
delete temp;
}
FILE* f_out = fopen("wf-out.txt", "w");
for(auto d: dist) {
fprintf(f_out, "%d\n", d);
}
fprintf(f_out, "\n");
fprintf(f_out, "%d\n", *max_element(dist.begin(), dist.end()));
fclose(f_out);
return 0;
}