-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmy_kahip.h
179 lines (139 loc) · 4.62 KB
/
my_kahip.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
#ifndef MY_KAHIP_H
#define MY_KAHIP_H
#include "array_id_func.h"
#include "id_func.h"
#include "tiny_id_func.h"
#include "back_arc.h"
#include "geo_pos.h"
#include "inertial_flow.h"
#include <vector>
#include <cassert>
#include <kaHIP_interface.h>
namespace my_kahip{
inline
::inertial_flow::Cut compute_my_kahip_cut(
const RangeIDIDMultiFunc&inv_tail, const ArrayIDIDFunc&head,
double min_balance
){
int node_count = head.image_count();
//int arc_count = head.preimage_count();
//std::vector<int>one_node_weights(node_count, 1);
//std::vector<int>one_arc_weights(arc_count, 1);
std::vector<int>part_of_node(node_count);
int part_count = 2;
int cut_size;
int* n = &node_count;
int* vwgt = /*&one_node_weights[0]*/nullptr;
int* xadj = (int*)&inv_tail.range_begin[0];
int* adjcwgt = /*&one_arc_weights[0]*/nullptr;
int* adjncy = head.data_;
int* nparts = &part_count;
double* imbalance = &min_balance;
bool suppress_output = true;
int seed = 42;
int mode = STRONG;
int* edgecut = &cut_size;
int* part = &part_of_node[0];
kaffpa(n, vwgt, xadj, adjcwgt, adjncy,nparts, imbalance, suppress_output, seed, mode, edgecut, part);
BitIDFunc reachable = id_func(node_count, [&](unsigned x){return part[x] == 1;});
int reachable_count = 0;
for(int x=0; x<node_count; ++x)
if(reachable(x))
++reachable_count;
cut_size = 0;
for(int x=0; x<node_count; ++x)
for(auto xy:inv_tail(x))
if(reachable(x) != reachable(head(xy)))
++cut_size;
cut_size /= 2;
if(reachable_count <= node_count/2)
return ::inertial_flow::Cut{std::move(reachable), reachable_count, cut_size};
else
return ::inertial_flow::Cut{~std::move(reachable), node_count-reachable_count, cut_size};
}
inline
::inertial_flow::Cut compute_my_kahip_cut(
const ArrayIDIDFunc&tail, const ArrayIDIDFunc&head,
double min_balance
){
if(!std::is_sorted(tail.begin(), tail.end()))
throw std::runtime_error("tail must be sorted");
return compute_my_kahip_cut(invert_sorted_id_id_func(tail), head, min_balance);
}
inline
std::vector<int> compute_my_kahip_separator(const ArrayIDIDFunc&tail, const ArrayIDIDFunc&head, double min_balance){
const int arc_count = head.preimage_count();
const int node_count = head.image_count();
std::vector<int>sep;
if(node_count == 1){
sep = {0};
} else {
::inertial_flow::Cut c = compute_my_kahip_cut(tail, head, min_balance);
for(int i=0; i<arc_count; ++i)
if(c.is_on_smaller_side(tail(i)) && !c.is_on_smaller_side(head(i)))
sep.push_back(head(i));
}
return sep; // NVRO
}
struct MyKahipSeparator{
MyKahipSeparator(double min_balance):
min_balance(min_balance){}
template<class InputNodeID, class ArcWeight>
std::vector<int>operator()(const ArrayIDIDFunc&tail, const ArrayIDIDFunc&head, const InputNodeID& input_node_id, const ArcWeight&arc_weight)const{
return compute_my_kahip_separator(
tail, head,
min_balance
);
}
double min_balance;
};
inline
MyKahipSeparator
ComputeSeparator(double min_balance){
return {min_balance};
}
inline
std::vector<int> compute_my_kahip2_separator(const ArrayIDIDFunc&tail, const ArrayIDIDFunc&head, double min_balance){
RangeIDIDMultiFunc inv_tail = invert_sorted_id_id_func(tail);
int node_count = head.image_count();
//int arc_count = head.preimage_count();
//std::vector<int>one_node_weights(node_count, 1);
//std::vector<int>one_arc_weights(arc_count, 1);
int part_count = 2;
int separator_size;
int*separator_pointer;
int* n = &node_count;
int* vwgt = /*&one_node_weights[0]*/nullptr;
int* xadj = (int*)&inv_tail.range_begin[0];
int* adjcwgt = /*&one_arc_weights[0]*/nullptr;
int* adjncy = head.data_;
int* nparts = &part_count;
double* imbalance = &min_balance;
bool suppress_output = true;
int seed = 42;
int mode = STRONG;
node_separator(n, vwgt, xadj, adjcwgt, adjncy, nparts, imbalance, suppress_output, seed, mode, &separator_size, &separator_pointer);
std::vector<int>separator_list(separator_size);
std::copy(separator_pointer, separator_pointer + separator_size, separator_list.begin());
delete[]separator_pointer;
return separator_list;
}
struct MyKahip2Separator{
MyKahip2Separator(double min_balance):
min_balance(min_balance){}
template<class InputNodeID, class ArcWeight>
std::vector<int>operator()(const ArrayIDIDFunc&tail, const ArrayIDIDFunc&head, const InputNodeID& input_node_id, const ArcWeight&arc_weight)const{
return compute_my_kahip2_separator(
tail, head,
min_balance
);
}
double min_balance;
};
inline
MyKahip2Separator
ComputeSeparator2(double min_balance){
return {min_balance};
}
}
#endif