forked from Tencent/plato
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hanp.cc
138 lines (109 loc) · 4.56 KB
/
hanp.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
/*
Tencent is pleased to support the open source community by making
Plato available.
Copyright (C) 2019 THL A29 Limited, a Tencent company.
All rights reserved.
Licensed under the BSD 3-Clause License (the "License"); you may
not use this file except in compliance with the License. You may
obtain a copy of the License at
https://opensource.org/licenses/BSD-3-Clause
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
See the AUTHORS file for names of contributors.
*/
#include <cstdint>
#include <cstdlib>
#include <unordered_set>
#include "glog/logging.h"
#include "gflags/gflags.h"
#include "boost/format.hpp"
#include "boost/iostreams/stream.hpp"
#include "boost/iostreams/filter/gzip.hpp"
#include "boost/iostreams/filtering_stream.hpp"
#include "plato/graph/graph.hpp"
#include "plato/algo/hanp/hanp.hpp"
#include "plato/util/perf.hpp"
#include "plato/util/atomic.hpp"
#include "plato/util/spinlock.hpp"
DEFINE_string(input, "", "input file, in csv format, without edge data");
DEFINE_string(output, "", "output directory");
DEFINE_bool(is_directed, true, "is graph directed or not");
DEFINE_bool(part_by_in, true, "partition by in-degree");
DEFINE_int32(alpha, -1, "alpha value used in sequence balance partition");
DEFINE_uint32(iterations, 20, "number of iterations");
DEFINE_double(preference, 1.0, "is any arbitrary comparable characteristic for any node");
DEFINE_double(hop_att, 0.1, "a new attenuated score");
bool string_not_empty(const char*, const std::string& value) {
if (0 == value.length()) { return false; }
return true;
}
DEFINE_validator(input, &string_not_empty);
DEFINE_validator(output, &string_not_empty);
void init(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
google::LogToStderr();
}
void print_flags(){
LOG(INFO) << "input : " << FLAGS_input;
LOG(INFO) << "output : " << FLAGS_output;
LOG(INFO) << "is_directed : " << FLAGS_is_directed;
LOG(INFO) << "part_by_in : " << FLAGS_part_by_in;
LOG(INFO) << "alpha : " << FLAGS_alpha;
LOG(INFO) << "iterations : " << FLAGS_iterations;
LOG(INFO) << "preference : " << FLAGS_preference;
LOG(INFO) << "hot_att : " << FLAGS_hop_att;
}
int main(int argc, char** argv) {
plato::stop_watch_t watch;
auto& cluster_info = plato::cluster_info_t::get_instance();
init(argc, argv);
cluster_info.initialize(&argc, &argv);
if (0 == cluster_info.partition_id_) {
print_flags();
}
watch.mark("t0");
plato::graph_info_t graph_info(FLAGS_is_directed);
auto pdcsc = plato::create_dcsc_seqd_from_path<float>(
&graph_info, FLAGS_input, plato::edge_format_t::CSV,
plato::float_decoder, FLAGS_alpha, FLAGS_part_by_in
);
using graph_spec_t = std::remove_reference<decltype(*pdcsc)>::type;
plato::algo::hanp_opts_t opts;
opts.iteration_ = FLAGS_iterations;
opts.preference = FLAGS_preference;
opts.hop_att = FLAGS_hop_att;
opts.dis = 1e-5;
auto labels = plato::algo::hanp<graph_spec_t>(*pdcsc, graph_info, opts);
if (0 == cluster_info.partition_id_) {
LOG(INFO) << "hanp calculation done: " << watch.show("t0") / 1000.0 << "s";
}
watch.mark("t0");
{
// save result to hdfs
std::vector<std::unique_ptr<plato::hdfs_t::fstream>> fs_v(cluster_info.threads_);
std::vector<std::unique_ptr<boost::iostreams::filtering_stream<boost::iostreams::output>>>
fs_output_v(cluster_info.threads_);
for (int i = 0; i < cluster_info.threads_; ++i) {
fs_v[i].reset(new plato::hdfs_t::fstream(plato::hdfs_t::get_hdfs(FLAGS_output),
(boost::format("%s/%04d_%04d.csv.gz") % FLAGS_output.c_str() % cluster_info.partition_id_ % i).str(), true));
fs_output_v[i].reset(new boost::iostreams::filtering_stream<boost::iostreams::output>());
fs_output_v[i]->push(boost::iostreams::gzip_compressor());
fs_output_v[i]->push(*fs_v[i]);
}
labels.template foreach<int> (
[&](plato::vid_t v_i, plato::vid_t* pval) {
static thread_local auto& fs_output = fs_output_v[omp_get_thread_num()];
*fs_output << v_i << "," << *pval << "\n";
return 0;
}
);
}
if (0 == cluster_info.partition_id_) {
LOG(INFO) << "save result cost: " << watch.show("t1") / 1000.0 << "s";
}
return 0;
}