-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstruct_cpd.cpp
More file actions
211 lines (154 loc) · 5.85 KB
/
construct_cpd.cpp
File metadata and controls
211 lines (154 loc) · 5.85 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
//
// Created by Bojie Shen on 6/4/21.
//
#include <iostream>
#include <graph.h>
#include <timer.h>
#include <dijkstra.h>
#include <cpd.h>
#include <omp.h>
namespace pl = polyanya;
double cpd_search(int start_id, int goal_id, const polyanya::Graph& g,const polyanya::cpd* cpd){
auto retrieve_next_move = [&](const int& source, const int& target) {
if(source == target){
return -1;
}
const int& first_move = cpd->get_first_move(source, target);
return first_move-2;
};
bool reached = false;
int cur_id = start_id;
double distance = 0;
while (!reached){
int next_move = retrieve_next_move (cur_id,goal_id);
if(next_move == -2){
return -1;
}else if(next_move == -1){
int arc_id = -1;
if( cur_id != goal_id){
for(int a = g.vertices[cur_id]; a < g.vertices[cur_id+1]; ++a){
if(g.out_vertices[a] == goal_id){
arc_id = a;
break;
}
}
if( arc_id == -1){
std::cout<<"error"<<std::endl;
}
distance = distance + g.distance_cost[arc_id];
}
reached = true;
}else{
distance = distance + g.distance_cost[g.vertices[cur_id] +next_move];
cur_id = g.out_vertices[g.vertices[cur_id]+next_move];
}
}
return distance;
}
void test_cpd(const polyanya::Graph& g,const polyanya::cpd* cpd){
pl::Dijkstra dij = pl::Dijkstra(&g);
for(int i = 0; i < g.number_of_vertices; i ++){
for(int j = 0; j < g.number_of_vertices; j ++){
const vector<double>& dis = dij.get_all_distance(i);
double dij_distance =dis[j];
double cpd_distance = cpd_search(i,j,g,cpd);
if(fabs(cpd_distance - dij_distance) > EPSILON*10){
double distance = cpd_search(i,j,g,cpd);
std::cout<<"error"<<std::endl;
}
}
}
std::cout<<"passed"<<std::endl;
}
void construct_cpd(const string& input_file, const string& output_file) {
pl::Graph g = pl::Graph();
g.load_graph(input_file);
// only need free flow cost;
warthog::timer timer1 = warthog::timer();
timer1.start();
std::cout<<"Loading visibility graph ...."<<std::endl;
vector<int> dfs_ordering =g.generate_DFS_ordering();
g.resort_graph(dfs_ordering);
cout << "Building CPD ... " << flush;
std::vector<pl::Dijkstra*> dijkstra;
for (int i = 0; i < omp_get_max_threads(); i ++){
dijkstra.push_back(new pl::Dijkstra(&g));
}
unsigned number_of_nodes = g.number_of_vertices;
pl::cpd* cpd = new pl::cpd();
{
{
pl::Dijkstra dij(&g);
dij.set_ordering(dfs_ordering);
warthog::timer t;
t.start();
dij.run_single_source_dijkstra(0);
t.stop();
double tots = t.elapsed_time_micro()*number_of_nodes / 1000000;
printf("Estimated sequential running time : %fmin\n", tots / 60.0);
}
printf("Using %d threads\n", omp_get_max_threads());
std::vector<pl::cpd>thread_cpd(omp_get_max_threads());
int progress = 0;
#pragma omp parallel
{
const int thread_count = omp_get_num_threads();
const int thread_id = omp_get_thread_num();
// const int begin_int = 0;
const int node_count = number_of_nodes;
int node_begin = (node_count*thread_id) / thread_count ;
int node_end = (node_count*(thread_id+1)) / thread_count ;
pl::Dijkstra& thread_dij = *dijkstra[thread_id];
for(int source_node=node_begin; source_node < node_end; ++source_node){
// thread_dij.full_search(source_node,weight);
// const std::vector<unsigned>& result =thread_dij.get_first_move_table(source_node);
// thread_cpd[thread_id].append_row(source_node,result);
thread_cpd[thread_id].append_row(source_node,thread_dij.run_single_source_dijkstra(source_node));
#pragma omp critical
{
++progress;
if(progress % 100 == 0) {
double ratio = (double)progress / number_of_nodes * 100.0;
std::cout << "Progress: [" << progress << "/" << number_of_nodes << "] "
<< std::setprecision(3) << ratio << "% \r";
std::cout.flush();
}
}
}
}
for(auto&x:thread_cpd)
cpd->append_rows(x);
}
timer1.stop();
for (auto i: dijkstra) delete i;
printf("Saving data to cpd.txt \n");
printf("begin size: %d, entry size: %d\n", cpd->entry_count(), cpd->get_entry_size());
std::string fname = output_file+ ".cpd";
FILE*f = fopen(fname.c_str(), "wb");
cpd->save(f);
fclose(f);
std::cout.flush();
std::cout<<"done"<<std::endl;
string mapper_file = output_file +".mapper";
cout<<"Saving mapper:"<< mapper_file<< endl;
vector<int> mapper = pl::invert_permutation(dfs_ordering);
save_vector(mapper_file,mapper);
std::cout<<std::endl;
// test_cpd(g,cpd);
}
int main(int argc, char*argv[]){
try{
const char *input_file;
const char *output_file;
if(argc != 3){
cerr << argv[0] << "input_file output_file" << endl;
return 1;
}else{
input_file = argv[1];
output_file = argv[2];
}
construct_cpd(input_file,output_file);
}catch(exception&err){
cerr << "Stopped on exception : " << err.what() << endl;
}
}