-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.cc
201 lines (182 loc) · 6.38 KB
/
tester.cc
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
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <stdlib.h>
#include <sys/time.h>
#include <vector>
#include "diameter.h"
#include "diamrallel.h"
using namespace std;
namespace {
// Work around for lack of pvector constructor disallowing function ptr call
enum FuncEnum {SLOW_PARA, PAPER_PARA};
void GenStar(int starsize) {
ofstream starFile;
starFile.open("graphs/star.edges");
for (int i = 0; i < starsize; i++) {
for (int j = 0; j < starsize; j++) {
if (i != j) {
starFile << i << " " << j << "\n";
}
}
}
for (int i = starsize; i < 2*starsize; i++) {
for (int j = 0; j < starsize; j++) {
// connect every "angle" node to every edge in the maximal subgraph
starFile << i << " " << j << "\n";
}
}
}
void GenPath(int pathlen) {
ofstream pathFile;
pathFile.open("graphs/path.edges");
for (int i = 0; i < pathlen; i++) {
pathFile << i << " " << i + 1 << "\n";
}
pathFile.close();
}
void GenMaximal(int size) {
ofstream maximalFile;
maximalFile.open("graphs/maximal.edges");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i != j) {
maximalFile << i << " " << j << "\n";
}
}
}
maximalFile.close();
}
// Note that if a vertex has no neighbors we must include an empty vector
// since we use index in outer vector to determine vertex number.
vector <vector<int> > GenGraph(const vector <pair<int, int> > &edges) {
int max_node = 0;
vector <vector<int> > adjlist;
for (pair<int, int> edge : edges) {
max_node = max({max_node, edge.first + 1, edge.second + 1});
}
adjlist.resize(max_node);
for (pair<int, int> edge : edges) {
adjlist[edge.first].push_back(edge.second);
}
return adjlist;
}
double GetTime() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec * 1e-6;
}
// TODO(iamabel): normalize how we call functions (enum vs. func ptr)
pair<int, double> RunTrials(const vector <vector<int> > &adjlist, const function<int(
const vector <vector<int> >)>& func, const int trials) {
double total_time = 0;
int diam = 0;
double start = GetTime();
for (int i = 0; i < trials; i++) {
diam = func(adjlist);
double end = GetTime();
total_time += end - start;
start = end;
}
return make_pair(diam, total_time/trials);
}
pair<int, double> RunTrials(const pvector <pvector<int> > &padjlist, FuncEnum func, const int trials) {
double total_time = 0;
int diam = 0;
double start = GetTime();
for (int i = 0; i < trials; i++) {
switch (func) {
case SLOW_PARA:
diam = Diameter::GetBruteDiamParallel(padjlist);
break;
case PAPER_PARA:
diam = Diameter::GetFastDiamParallel(padjlist);
break;
default:
return make_pair(-1,-1);
}
double end = GetTime();
total_time += end - start;
start = end;
}
return make_pair(diam, total_time/trials);
}
} // end namespace
int main(int argc, char** argv) {
int trials = 10; // attempt to normalize runs
char *filename = (char *)"graphs/simple.edges";
bool run_paper = false, run_slow = false, run_para_slow = false, run_para_paper = false;
for (int i = 1; i < argc; ++i) {
if (string(argv[i]) == "--trials") {
if (i + 1 < argc) {
trials = atoi(argv[++i]);
} else { // Trial flag called but unspecified
cerr << "--trials option requires one argument." << endl;
return 1;
}
} else if (string(argv[i]) == "--graph") {
if (i + 1 < argc) {
filename = argv[++i];
} else { // Graph flag called but unspecified
cerr << "--graph option requires one argument." << endl;
return 1;
}
} else if (string(argv[i]) == "--paper") run_paper = true;
else if (string(argv[i]) == "--slow") run_slow = true;
else if (string(argv[i]) == "--para_slow") run_para_slow = true;
else if (string(argv[i]) == "--para_paper") run_para_paper = true;
}
vector <pair<int, int> > edges;
{
FILE *in = fopen(filename, "r");
if (in == NULL) {
fprintf(stderr, "Can't open edges file\n");
return -1;
}
for (int from, to; fscanf(in, "%d %d", &from, &to) != EOF; ) {
edges.push_back(std::make_pair(from, to));
}
fclose(in);
}
const vector <vector<int> > adjlist = GenGraph(edges);
pvector< pvector<int> > padjlist;
if (run_para_slow || run_para_paper) {
padjlist = Diameter::BuildTSGraph(edges);
}
{
// Return of format (diameter, average time)
printf("Our graph is from file: %s\n", filename);
pair<int, double> fast_diam_time, brute_para_diam_time, brute_diam_time, paper_para_diam_time;
if (run_paper) {
fast_diam_time = RunTrials(adjlist, &Diameter::GetFastDiam, trials);
printf("\nAccording to the solution by @kawatea,"
" the diameter of the graph is: %d \n\n", fast_diam_time.first);
printf("This operation from the paper was completed in: %f seconds \n\n",
fast_diam_time.second);
}
if (run_slow) {
brute_diam_time = RunTrials(adjlist, &Diameter::GetBruteDiam, trials);
printf("A trivial, yet exact, solution says"
" the diameter of the graph is: %d \n\n", brute_diam_time.first);
printf("This brute force operation was completed in: %f seconds \n\n",
brute_diam_time.second);
}
if (run_para_slow) {
brute_para_diam_time = RunTrials(padjlist, SLOW_PARA, trials);
printf("The experimental, yet trivial solution says"
" the diameter of the graph is: %d \n\n", brute_para_diam_time.first);
printf("This parallelized brute force operation was completed in: %f seconds \n\n",
brute_para_diam_time.second);
}
if (run_para_paper) {
paper_para_diam_time = RunTrials(padjlist, PAPER_PARA, trials);
printf("The experimental, paper-modifying solution says"
" the diameter of the graph is: %d \n\n", paper_para_diam_time.first);
printf("This parallelized paper-modifying operation was completed in: %f seconds \n\n",
paper_para_diam_time.second);
}
}
return 0;
}