-
Notifications
You must be signed in to change notification settings - Fork 4
/
pla.cpp
254 lines (181 loc) · 7.84 KB
/
pla.cpp
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
//
// Created by Jikai Lu on 4/30/20.
//
#include "pla.h"
int optimal_length;
std::atomic<int> open_node_num;
thread_state* thread_array;
std::vector<int> start_g_value;
//std::queue<pair<int, Node *>> wait_list;
std::vector<pair<int, Node*> > start_expand(const Map* map, int node_number) {
priority_queue<pair<int, Node *>> active_nodes;
active_nodes.push({map->goal->compute_heuristic(map->start) ,map->start});
start_g_value = vector<int>(map->height*map->width, INT32_MAX);
start_g_value[map->start->node_id] = 0;
std::vector<pair<int, Node*> > expand_list;
while (!active_nodes.empty()) {
Node *current_node = active_nodes.top().second;
active_nodes.pop();
if (current_node->node_id == map->goal->node_id) {
optimal_length = start_g_value[current_node->node_id];
return std::vector<pair<int, Node*> >();
}
for (auto edge : current_node->adjacent_list)
{
Node *node = edge.first;
int weight = edge.second;
int update_g_value = weight + start_g_value[current_node->node_id];
if (update_g_value < start_g_value[node->node_id]) {
start_g_value[node->node_id] = update_g_value;
int new_f = update_g_value + map->goal->compute_heuristic(node);
active_nodes.push({new_f, node});
if (active_nodes.size() == node_number) {
while (!active_nodes.empty()) {
expand_list.push_back(active_nodes.top());
active_nodes.pop();
}
return expand_list;
}
}
}
}
while (!active_nodes.empty()) {
expand_list.push_back(active_nodes.top());
active_nodes.pop();
}
return expand_list;
}
void distribute_node(std::vector<pair<int, Node*> > node_list, int thread_count) {
srand(time(NULL));
for (int i = 0; i < node_list.size(); i++) {
int thread_id = rand() % thread_count;
thread_array[thread_id].open_list.push({node_list[i].first, {NULL, node_list[i].second}});
}
open_node_num = node_list.size();
}
void build_hypercube(int thread_count) {
int bit_num = (int)log2(thread_count);
for (int i = 0; i < thread_count; i++) {
for (int j = 0; j < bit_num; j++) {
int neighbor = i ^ (1 << j);
thread_array[i].neighbors.push_back(&thread_array[neighbor]);
}
// thread_array[i].neighbors.push_back(&thread_array[i]);
}
}
TestResult* find_path_pla(const Map* map, int thread_count) {
TestResult* test_result = new TestResult(thread_count);
auto node_list = start_expand(map, thread_count);
const int busy_threshold = 100;
if (node_list.empty()) {
std::cout << "do not need multi threads, the map is too small" << std::endl;
test_result->shortest = optimal_length;
return test_result;
}
else {
thread_array = new thread_state[thread_count];
distribute_node(node_list, thread_count);
build_hypercube(thread_count);
omp_set_num_threads(thread_count);
}
optimal_length = INT32_MAX;
#pragma omp parallel default(shared)
{
vector<int> g_value(start_g_value);
int id = omp_get_thread_num();
const int DELTA_NODE_FLUSH_PERIOD = 10;
int delta_node_flush_count = 0;
int delta_node_num = 0;
while (open_node_num > 0) {
delta_node_flush_count ++;
if (delta_node_flush_count > DELTA_NODE_FLUSH_PERIOD) {
delta_node_flush_count = 0;
open_node_num += delta_node_num;
delta_node_num = 0;
}
omp_set_lock(&thread_array[id].lock);
if (thread_array[id].open_list.size() > busy_threshold && thread_count > 1) {
thread_array[id].wait_list.push(thread_array[id].open_list.top());
thread_array[id].open_list.pop();
}
int current_open_size = thread_array[id].open_list.size();
omp_unset_lock(&thread_array[id].lock);
if (current_open_size == 0) {
// for (auto neighbor: thread_array[id].neighbors) {
// omp_set_lock(&neighbor->lock);
// }
int busiest_neighbor = -1;
int longest_wl = 0;
for (int i = 0; i < thread_array[id].neighbors.size(); i++) {
auto neighbor = thread_array[id].neighbors[i];
omp_set_lock(&neighbor->lock);
if (neighbor->wait_list.size() > longest_wl) {
busiest_neighbor = i;
longest_wl = neighbor->wait_list.size();
}
omp_unset_lock(&neighbor->lock);
}
if (longest_wl > 0) {
omp_set_lock(&thread_array[id].neighbors[busiest_neighbor]->lock);
if (thread_array[id].neighbors[busiest_neighbor]->wait_list.size() > 0) {
auto fetch_element = thread_array[id].neighbors[busiest_neighbor]->wait_list.front();
thread_array[id].neighbors[busiest_neighbor]->wait_list.pop();
thread_array[id].open_list.push(fetch_element);
int copy_g_value = fetch_element.first - map->goal->compute_heuristic(fetch_element.second.second);
if (copy_g_value < g_value[fetch_element.second.second->node_id]) {
g_value[fetch_element.second.second->node_id] = copy_g_value;
}
omp_unset_lock(&thread_array[id].neighbors[busiest_neighbor]->lock);
}
else {
omp_unset_lock(&thread_array[id].neighbors[busiest_neighbor]->lock);
continue;
}
}
else {
continue;
}
}
omp_set_lock(&thread_array[id].lock);
Node* current_node = thread_array[id].open_list.top().second.second;
Node* prev_node = thread_array[id].open_list.top().second.first;
thread_array[id].open_list.pop();
thread_array[id].count++;
if (current_node->node_id == map->goal->node_id) {
if (g_value[current_node->node_id] < optimal_length) {
optimal_length = g_value[current_node->node_id];
}
}
// open_node_num--;
// omp_unset_lock(&lock);
omp_unset_lock(&thread_array[id].lock);
delta_node_num -= 1;
for (auto edge : current_node->adjacent_list)
{
Node *node = edge.first;
if (prev_node!=NULL && prev_node->node_id==node->node_id) {
continue;
}
int weight = edge.second;
int update_g_value = weight + g_value[current_node->node_id];
if (update_g_value < g_value[node->node_id]) {
g_value[node->node_id] = update_g_value;
int new_f = update_g_value + map->goal->compute_heuristic(node);
thread_array[id].open_list.push({new_f, {current_node, node}});
delta_node_num++;
}
}
// open_node_num += delta_node_num;
// omp_set_lock(&lock);
}
// omp_unset_lock(&lock);
}
// for (int i = 0; i < thread_count; i++) {
// std::cout << "thread " << i << ": " << thread_array[i].count << std::endl;
// }
test_result->shortest = optimal_length;
for (int i = 0; i < thread_count; i++) {
test_result->thread_explore[i] = thread_array[i].count;
}
return test_result;
}