forked from alibaba/libgrape-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fragment_indices.h
185 lines (148 loc) Β· 5.82 KB
/
fragment_indices.h
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
/** Copyright 2020 Alibaba Group Holding Limited.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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.
*/
#ifndef EXAMPLES_GNN_SAMPLER_FRAGMENT_INDICES_H_
#define EXAMPLES_GNN_SAMPLER_FRAGMENT_INDICES_H_
#include <memory>
#include <set>
#include <utility>
#include <vector>
#include <grape/fragment/edgecut_fragment_base.h>
#include <grape/util.h>
#include <grape/utils/gcontainer.h>
#include <grape/utils/vertex_array.h>
#include "flat_hash_map/flat_hash_map.hpp"
template <typename OID_T, typename VID_T, typename VDATA_T, typename EDATA_T>
class FragmentIndicesBase {
public:
virtual ~FragmentIndicesBase() {}
static std::unique_ptr<FragmentIndicesBase<OID_T, VID_T, VDATA_T, EDATA_T>>
Create();
virtual void Init(
grape::EdgecutFragmentBase<OID_T, VID_T, VDATA_T, EDATA_T>* frag) = 0;
virtual void Insert(VID_T u, VID_T v, EDATA_T data) = 0;
virtual void Rebuild() = 0;
virtual void Resize(size_t new_size) = 0;
};
template <typename OID_T, typename VID_T, typename VDATA_T, typename EDATA_T>
class WeightIndices final
: public FragmentIndicesBase<OID_T, VID_T, VDATA_T, EDATA_T> {
public:
using vertex_t = grape::Vertex<VID_T>;
using oid_t = OID_T;
using vid_t = VID_T;
using vdata_t = VDATA_T;
using edata_t = EDATA_T;
struct WeightCollections {
ska::flat_hash_map<vid_t, edata_t> weights;
std::set<std::pair<edata_t, vid_t>> ordered_weights;
std::vector<edata_t> acc_weights;
std::vector<vid_t> ordered_ids;
};
void Init(grape::EdgecutFragmentBase<oid_t, vid_t, vdata_t, edata_t>* frag)
override {
weight_indices_.resize(frag->GetInnerVerticesNum());
for (auto& v : frag->InnerVertices()) {
int neighbor_num = frag->GetLocalOutDegree(v);
vid_t lid = v.GetValue();
auto& weights = weight_indices_[lid].weights;
auto& ordered_weights = weight_indices_[lid].ordered_weights;
auto& acc_weights = weight_indices_[lid].acc_weights;
auto& ordered_ids = weight_indices_[lid].ordered_ids;
weights.reserve(neighbor_num);
auto oes = frag->GetOutgoingAdjList(v);
for (auto e = oes.begin(); e != oes.end(); ++e) {
auto u_vid = frag->Vertex2Gid(e->neighbor);
weights.emplace(u_vid, e->data);
}
for (auto const& kv : weights) {
ordered_weights.emplace(kv.second, kv.first);
}
acc_weights.reserve(ordered_weights.size());
ordered_ids.reserve(ordered_weights.size());
edata_t acc_weight = 0;
for (auto const& kv : ordered_weights) {
acc_weight += kv.first;
acc_weights.emplace_back(acc_weight);
ordered_ids.emplace_back(kv.second);
}
}
}
void Insert(vid_t lid, vid_t nei_id, edata_t w) override {
auto& weights = weight_indices_[lid].weights;
auto& ordered_weights = weight_indices_[lid].ordered_weights;
auto iter = weights.find(nei_id);
if (iter != weights.end()) {
// update index
edata_t pw = iter->second;
weights[nei_id] = w;
ordered_weights.erase(std::make_pair(pw, iter->first));
ordered_weights.emplace(w, nei_id);
} else {
// insert index
weights.emplace(nei_id, w);
ordered_weights.emplace(w, nei_id);
}
affected_ids_.emplace(lid);
}
const ska::flat_hash_map<vid_t, edata_t>& GetWeights(
const vertex_t& v) const {
return weight_indices_[v.GetValue()].weights;
}
const std::set<std::pair<edata_t, vid_t>>& GetOrderedWeights(
const vertex_t& v) const {
return weight_indices_[v.GetValue()].ordered_weights;
}
const std::vector<edata_t>& GetAccWeights(const vertex_t& v) const {
return weight_indices_[v.GetValue()].acc_weights;
}
const std::vector<vid_t>& GetOrderedIds(const vertex_t& v) const {
return weight_indices_[v.GetValue()].ordered_ids;
}
void Rebuild() override {
for (vid_t vid : affected_ids_) {
auto& ordered_weights = weight_indices_[vid].ordered_weights;
auto& acc_weights = weight_indices_[vid].acc_weights;
auto& ordered_ids = weight_indices_[vid].ordered_ids;
acc_weights.resize(ordered_weights.size());
ordered_ids.resize(ordered_weights.size());
edata_t acc_weight = 0.0;
size_t idx = 0;
for (auto const& kv : ordered_weights) {
acc_weight += kv.first;
acc_weights[idx] = acc_weight;
ordered_ids[idx++] = kv.second;
}
}
affected_ids_.clear();
}
void Resize(size_t new_size) override { weight_indices_.resize(new_size); }
private:
std::set<vid_t> affected_ids_;
std::vector<WeightCollections> weight_indices_;
};
template <typename OID_T, typename VID_T, typename VDATA_T>
class WeightIndices<OID_T, VID_T, VDATA_T, grape::EmptyType>
: public FragmentIndicesBase<OID_T, VID_T, VDATA_T, grape::EmptyType> {
public:
void Init(grape::EdgecutFragmentBase<OID_T, VID_T, VDATA_T, grape::EmptyType>*
frag) override {}
void Insert(VID_T vid, VID_T nei_id, grape::EmptyType w) override {}
void Rebuild() override {}
void Resize(size_t new_size) override {}
};
template <typename OID_T, typename VID_T, typename VDATA_T, typename EDATA_T>
std::unique_ptr<FragmentIndicesBase<OID_T, VID_T, VDATA_T, EDATA_T>>
FragmentIndicesBase<OID_T, VID_T, VDATA_T, EDATA_T>::Create() {
return std::unique_ptr<FragmentIndicesBase<OID_T, VID_T, VDATA_T, EDATA_T>>(
new WeightIndices<OID_T, VID_T, VDATA_T, EDATA_T>());
}
#endif // EXAMPLES_GNN_SAMPLER_FRAGMENT_INDICES_H_