Skip to content

Commit 168d82f

Browse files
committed
Refactor code
1 parent 785609b commit 168d82f

File tree

3 files changed

+118
-95
lines changed

3 files changed

+118
-95
lines changed

core/command_line.h

+49-24
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <vector>
1515

1616

17-
1817
/*
1918
FaultyRank
2019
Class: CLBase
@@ -27,11 +26,11 @@ Handles command line argument parsing
2726

2827

2928
class CLBase {
30-
protected:
29+
protected:
3130
int argc_;
32-
char** argv_;
31+
char **argv_;
3332
std::string get_args_ = "N:E:f:u:i:t:";
34-
std::vector<std::string> help_strings_;
33+
std::vector <std::string> help_strings_;
3534

3635
int64_t num_nodes_ = 0;
3736
int64_t num_edges_ = 0;
@@ -49,18 +48,18 @@ class CLBase {
4948
if (def != "")
5049
def = "[" + def + "]";
5150
snprintf(buf, kBufLen, " -%c %-9s: %-54s%10s", opt, opt_arg.c_str(),
52-
text.c_str(), def.c_str());
51+
text.c_str(), def.c_str());
5352
help_strings_.push_back(buf);
5453
}
5554

56-
public:
57-
CLBase(int argc, char** argv) : argc_(argc), argv_(argv) {
55+
public:
56+
CLBase(int argc, char **argv) : argc_(argc), argv_(argv) {
5857
AddHelpLine('h', "", "print this help message");
5958
AddHelpLine('N', "base_filename_", "load base-graph from file");
6059
AddHelpLine('E', "dynamic_filename_", "load dynamic-graph from file");
6160
AddHelpLine('f', "input_file", "load graph from file");
6261
AddHelpLine('u', "unfilled_property_file", "load unfilled property vertices from file");
63-
AddHelpLine('i', "i", "perform at most i iterations",std::to_string(max_iters_));
62+
AddHelpLine('i', "i", "perform at most i iterations", std::to_string(max_iters_));
6463
AddHelpLine('t', "t", "use tolerance t", std::to_string(tolerance_));
6564
}
6665

@@ -81,58 +80,84 @@ class CLBase {
8180
return true;
8281
}
8382

84-
void virtual HandleArg(signed char opt, char* opt_arg) {
83+
void virtual HandleArg(signed char opt, char *opt_arg) {
8584
switch (opt) {
86-
case 'N': num_nodes_ = atol(opt_arg); break;
87-
case 'E': num_edges_ = atol(opt_arg); break;
88-
case 'f': input_filename_ = std::string(opt_arg); break;
89-
case 'u': up_filename_ = std::string(opt_arg); break;
90-
case 'i': max_iters_ = atoi(opt_arg); break;
91-
case 't': tolerance_ = std::stod(opt_arg); break;
85+
case 'N':
86+
num_nodes_ = atol(opt_arg);
87+
break;
88+
case 'E':
89+
num_edges_ = atol(opt_arg);
90+
break;
91+
case 'f':
92+
input_filename_ = std::string(opt_arg);
93+
break;
94+
case 'u':
95+
up_filename_ = std::string(opt_arg);
96+
break;
97+
case 'i':
98+
max_iters_ = atoi(opt_arg);
99+
break;
100+
case 't':
101+
tolerance_ = std::stod(opt_arg);
102+
break;
92103
}
93104
}
94105

95106
void PrintUsage() {
96-
for (std::string h : help_strings_)
107+
for (std::string h: help_strings_)
97108
std::cout << h << std::endl;
98109
std::exit(0);
99110
}
100111

101112
int64_t num_nodes() const { return num_nodes_; }
113+
102114
int64_t num_edges() const { return num_edges_; }
115+
103116
std::string input_filename() const { return input_filename_; }
117+
104118
std::string up_filename() const { return up_filename_; }
119+
105120
bool has_unfilled_property() const { return (up_filename_ != ""); }
121+
106122
int max_iters() const { return max_iters_; }
123+
107124
double tolerance() const { return tolerance_; }
108125
};
109126

110127

111-
112128
class CLApp : public CLBase {
113129
bool do_analysis_ = false;
114130
int num_trials_ = 16;
115131
bool do_verify_ = false;
116132

117-
public:
118-
CLApp(int argc, char** argv) : CLBase(argc, argv) {
133+
public:
134+
CLApp(int argc, char **argv) : CLBase(argc, argv) {
119135
get_args_ += "an:v";
120136
AddHelpLine('a', "", "output analysis of last run", "false");
121137
AddHelpLine('n', "n", "perform n trials", std::to_string(num_trials_));
122138
AddHelpLine('v', "", "verify the output of each run", "false");
123139
}
124140

125-
void HandleArg(signed char opt, char* opt_arg) override {
141+
void HandleArg(signed char opt, char *opt_arg) override {
126142
switch (opt) {
127-
case 'a': do_analysis_ = true; break;
128-
case 'n': num_trials_ = atoi(opt_arg); break;
129-
case 'v': do_verify_ = true; break;
130-
default: CLBase::HandleArg(opt, opt_arg);
143+
case 'a':
144+
do_analysis_ = true;
145+
break;
146+
case 'n':
147+
num_trials_ = atoi(opt_arg);
148+
break;
149+
case 'v':
150+
do_verify_ = true;
151+
break;
152+
default:
153+
CLBase::HandleArg(opt, opt_arg);
131154
}
132155
}
133156

134157
bool do_analysis() const { return do_analysis_; }
158+
135159
int num_trials() const { return num_trials_; }
160+
136161
bool do_verify() const { return do_verify_; }
137162
};
138163

core/faultyrank.cpp

+67-69
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ typedef float ScoreT;
1616
using namespace std::chrono;
1717

1818
/// Sugar for printing the rank-values (@id_rank and @p_rank) after @iter iterations
19-
void print_ranks(const Graph &g, const Graph &rg, int iter, vector<ScoreT>& id_rank, vector<ScoreT>& p_rank) {
19+
void print_ranks(const Graph &g, const Graph &rg, int iter, vector <ScoreT> &id_rank, vector <ScoreT> &p_rank) {
2020
cout << "\nafter " << iter << " iteration" << endl;
2121
cout << "id-rank: ";
22-
for(NodeID u=0; u<g.num_nodes(); u+=1) cout << id_rank[u] << " ";
22+
for (NodeID u = 0; u < g.num_nodes(); u += 1) cout << id_rank[u] << " ";
2323
cout << endl;
2424

2525
cout << "p-rank: ";
26-
for(NodeID u=0; u<rg.num_nodes(); u+=1) cout << p_rank[u] << " ";
26+
for (NodeID u = 0; u < rg.num_nodes(); u += 1) cout << p_rank[u] << " ";
2727
cout << endl;
2828
}
2929

@@ -32,10 +32,10 @@ void print_graph_property(const Graph &g, const Graph &rg) {
3232
int64_t count_zero_in_g = 0;
3333
int64_t count_zero_in_rg = 0;
3434

35-
for (NodeID u=0; u < g.num_nodes(); u++) {
36-
if(g.out_degree(u) == 0 && rg.out_degree(u) == 0) count_isolated_nodes += 1;
37-
else if(g.out_degree(u)) count_zero_in_g += 1;
38-
else if(rg.out_degree(u)) count_zero_in_rg += 1;
35+
for (NodeID u = 0; u < g.num_nodes(); u++) {
36+
if (g.out_degree(u) == 0 && rg.out_degree(u) == 0) count_isolated_nodes += 1;
37+
else if (g.out_degree(u)) count_zero_in_g += 1;
38+
else if (rg.out_degree(u)) count_zero_in_rg += 1;
3939
else {
4040
cout << "ERROR: Should not happen!" << endl;
4141
exit(-1);
@@ -52,91 +52,89 @@ void FaultyPageRank(const Graph &g, const Graph &rg,
5252
const ScoreT init_score = 1.0f;
5353
const ScoreT zero_score = 0.0f;
5454

55-
vector<ScoreT> p_rank_prev(g.num_nodes(), init_score);
56-
vector<ScoreT> id_rank(g.num_nodes(),zero_score);
57-
vector<ScoreT> p_rank(g.num_nodes(),zero_score);
55+
vector <ScoreT> p_rank_prev(g.num_nodes(), init_score);
56+
vector <ScoreT> id_rank(g.num_nodes(), zero_score);
57+
vector <ScoreT> p_rank(g.num_nodes(), zero_score);
5858

5959
/// going to update @id_rank values from Graph @G
60-
for (int iter=0; iter < max_iters; iter+=1) {
61-
///update id rank
62-
for (NodeID u=0; u < g.num_nodes(); u++) {
63-
if (g.out_degree(u) == 0) {
64-
cout<<"Sink node in G"<<u<<endl;
65-
ScoreT share = p_rank_prev[u] / (g.num_nodes() - 1);
66-
for(NodeID v=0; v < g.num_nodes(); v++) {
67-
if (u != v) id_rank[v] += share;
68-
}
69-
}
70-
else {
71-
cout<<"Nodes except sink node in G"<<u<<endl;
72-
ScoreT share = p_rank_prev[u] / g.out_degree(u);
73-
for (EdgeItem neigh: g.graph_[u].neighbors) {
74-
id_rank[neigh.v] += share;
75-
}
76-
}
77-
}
78-
///update property rank
79-
for (NodeID u=0; u < rg.num_nodes(); u++) {
80-
if (rg.out_degree(u) == 0) {
81-
cout<<"Sink node in RG"<<u<<endl;
82-
ScoreT share = id_rank[u] / (rg.num_nodes() - 1);
83-
for(NodeID v=0; v < rg.num_nodes(); v++) {
84-
if (u != v) p_rank[v] += share;
85-
}
86-
}
87-
else if (rg.out_degree(u) > rg.out_degree_paired(u) && !rg.graph_[u].unfilled_property_flag && rg.out_degree_paired(u)) {
88-
cout<<"Weighted node in RG"<<u<<endl;
89-
ScoreT share = id_rank[u] / rg.out_degree_paired(u);
90-
for (EdgeItem neigh: rg.graph_[u].neighbors) {
91-
if(neigh.paired_flag) p_rank[neigh.v] += share;
92-
}
93-
}
94-
else {
95-
cout<<"remaining node in RG"<<u<<endl;
96-
ScoreT share = id_rank[u] / rg.out_degree(u);
97-
for (EdgeItem neigh: rg.graph_[u].neighbors) {
98-
p_rank[neigh.v] += share;
99-
}
100-
}
101-
}
102-
103-
print_ranks(g, rg, iter, id_rank, p_rank);
104-
//#pragma omp parallel for
105-
for (NodeID u=0; u < g.num_nodes(); u++) {
106-
p_rank_prev[u] = p_rank[u];
107-
p_rank[u] = 0.0;
108-
id_rank[u] = 0.0;
109-
}
110-
111-
cout << "Done " << iter << " iteration." << endl;
60+
for (int iter = 0; iter < max_iters; iter += 1) {
61+
///update id rank
62+
for (NodeID u = 0; u < g.num_nodes(); u++) {
63+
if (g.out_degree(u) == 0) {
64+
cout << "Sink node in G: " << u << endl;
65+
ScoreT share = p_rank_prev[u] / (g.num_nodes() - 1);
66+
for (NodeID v = 0; v < g.num_nodes(); v++) {
67+
if (u != v) id_rank[v] += share;
68+
}
69+
} else {
70+
cout << "Nodes except sink node in G: " << u << endl;
71+
ScoreT share = p_rank_prev[u] / g.out_degree(u);
72+
for (EdgeItem neigh: g.graph_[u].neighbors) {
73+
id_rank[neigh.v] += share;
74+
}
75+
}
76+
}
77+
///update property rank
78+
for (NodeID u = 0; u < rg.num_nodes(); u++) {
79+
if (rg.out_degree(u) == 0) {
80+
cout << "Sink node in RG" << u << endl;
81+
ScoreT share = id_rank[u] / (rg.num_nodes() - 1);
82+
for (NodeID v = 0; v < rg.num_nodes(); v++) {
83+
if (u != v) p_rank[v] += share;
84+
}
85+
} else if (rg.out_degree(u) > rg.out_degree_paired(u) && !rg.graph_[u].unfilled_property_flag &&
86+
rg.out_degree_paired(u)) {
87+
cout << "Weighted node in RG" << u << endl;
88+
ScoreT share = id_rank[u] / rg.out_degree_paired(u);
89+
for (EdgeItem neigh: rg.graph_[u].neighbors) {
90+
if (neigh.paired_flag) p_rank[neigh.v] += share;
91+
}
92+
} else {
93+
cout << "remaining node in RG" << u << endl;
94+
ScoreT share = id_rank[u] / rg.out_degree(u);
95+
for (EdgeItem neigh: rg.graph_[u].neighbors) {
96+
p_rank[neigh.v] += share;
97+
}
98+
}
99+
}
100+
101+
print_ranks(g, rg, iter, id_rank, p_rank);
102+
//#pragma omp parallel for
103+
for (NodeID u = 0; u < g.num_nodes(); u++) {
104+
p_rank_prev[u] = p_rank[u];
105+
p_rank[u] = 0.0;
106+
id_rank[u] = 0.0;
107+
}
108+
109+
cout << "Done " << iter << " iteration." << endl;
112110
}
113111
}
114112

115-
int main(int argc, char* argv[]) {
113+
int main(int argc, char *argv[]) {
116114
CLApp cli(argc, argv);
117115
if (!cli.ParseArgs()) return -1;
118116

119117
/// building the graph from @file
120118
auto start_g = std::chrono::high_resolution_clock::now();
121119
Graph G(cli.input_filename(), cli.num_nodes(), cli.num_edges());
122-
G.PrintTopology();
120+
G.PrintTopology();
123121
auto end_g = std::chrono::high_resolution_clock::now();
124122
double duration_g = std::chrono::duration_cast<std::chrono::nanoseconds>(end_g - start_g).count();
125-
cout << "time to build the original graph: " << duration_g/1000000000 << " seconds." << endl;
123+
cout << "time to build the original graph: " << duration_g / 1000000000 << " seconds." << endl;
126124

127125
/// building the reverse graph of Graph @G
128126
auto start_rg = std::chrono::high_resolution_clock::now();
129127
Graph RG(G);
130-
if(cli.has_unfilled_property()) RG.mark_unfilled_property_vertices(cli.up_filename());
131-
RG.PrintTopology();
128+
if (cli.has_unfilled_property()) RG.mark_unfilled_property_vertices(cli.up_filename());
129+
RG.PrintTopology();
132130
auto end_rg = std::chrono::high_resolution_clock::now();
133131
double duration_rg = std::chrono::duration_cast<std::chrono::nanoseconds>(end_rg - start_rg).count();
134-
cout << "time to build the reverse graph: " << duration_rg/1000000000 << " seconds." << endl;
132+
cout << "time to build the reverse graph: " << duration_rg / 1000000000 << " seconds." << endl;
135133

136134
/// call faulty page-rank algorithm
137135
auto start = std::chrono::high_resolution_clock::now();
138136
FaultyPageRank(G, RG, cli.max_iters());
139137
auto end = std::chrono::high_resolution_clock::now();
140138
double duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
141-
cout << "time to run the faulty-rank algorithm: " << duration/1000000000 << " seconds." << endl;
139+
cout << "time to run the faulty-rank algorithm: " << duration / 1000000000 << " seconds." << endl;
142140
}

core/graph.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ class Graph {
185185
void BenchmarkFullGraphAccess() const {
186186
volatile int count = 0;
187187
auto start = std::chrono::high_resolution_clock::now();
188-
for (NodeID u = 0; u < num_nodes_; u+=1) {
188+
for (NodeID u = 0; u < num_nodes_; u += 1) {
189189
for (EdgeItem v: graph_[u].neighbors) {
190190
count += (v.paired_flag ? 1 : 0);
191191
}
192192
}
193193

194194
auto end = std::chrono::high_resolution_clock::now();
195195
double duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
196-
cout << count << "# time to run the full access benchmark: " << duration/1000000000 << " seconds." << endl;
196+
cout << count << "# time to run the full access benchmark: " << duration / 1000000000 << " seconds." << endl;
197197
}
198198

199199
private:

0 commit comments

Comments
 (0)