-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanonical.cpp
More file actions
307 lines (249 loc) · 8.15 KB
/
Copy pathcanonical.cpp
File metadata and controls
307 lines (249 loc) · 8.15 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "canonical.h"
tbb::atomic<int> Canonical::hits = 0;
tbb::atomic<int> Canonical::misses = 0;
tbb::concurrent_hash_map<size_t, size_t> Canonical::pattern_to_canonical = tbb::concurrent_hash_map<size_t, size_t> ();
//tbb::concurrent_hash_map<size_t, Canonical::PatternInfo*> Canonical::naives = tbb::concurrent_hash_map<size_t, Canonical::PatternInfo*> ();
//tbb::concurrent_hash_map<size_t, Canonical::PatternInfo*> Canonical::canonicals = tbb::concurrent_hash_map<size_t, Canonical::PatternInfo*> ();
/*size_t Canonical::getHashScratch(bliss::Graph &bg) {
bliss::Stats stats;
//size_t N = bg.get_nof_vertices();
const unsigned int* perm = bg.canonical_form(stats, NULL, NULL);
bliss::Graph *bc = bg.permute(perm);
size_t canCode = bc->get_hash();
delete bc;
return canCode;
}*/
//get canonical with embedding directly
size_t Canonical::getHash(BasicEmbedding &e) {
size_t naiveCode = e.getNaiveCodeHashValue();
tbb::concurrent_hash_map<size_t, size_t>::accessor a;
bool r = Canonical::pattern_to_canonical.insert(a, naiveCode); // creates by default if not exists, acquires lock
if (r) { // it means that it is new
bliss::Graph bg = e.getBlissGraph();
bliss::Stats stats;
const unsigned int* perm = bg.canonical_form(stats, NULL, NULL);
bliss::Graph *bc = bg.permute(perm);
a->second = bc->get_hash();
delete bc;
Canonical::misses++;
}
else {
Canonical::hits++;
}
size_t code = a->second;
a.release();
if (Canonical::hits+Canonical::misses%10000==0) {
std::cout << "Caninocal hashmap hits: " << Canonical::hits << " misses: " << Canonical::misses << std::endl;
}
return code;
}
/*size_t Canonical::getMyHash(BasicEmbedding &e) {
size_t naiveCode = e.getNaiveCodeHashValue();
tbb::concurrent_hash_map<size_t, size_t>::accessor a;
bool r = Canonical::pattern_to_canonical.insert(a, naiveCode); // creates by default if not exists, acquires lock
if (r) { // it means that it is new
//build adj-list for edges
std::vector<int> &edges = e.getEdges();
int adjlist[edges.size()][edges.size()+1];
for (uint i = 0; i < edges.size(); i++) {
adjlist[i][0]=0;
for (uint j = (i+1); j < edgegs.size(); j++) {
if (g->isNeighborEdge(edges[i], edges[j])) {
adjlist[i][++adjlist[i][0]] = j;
adjlist[j][++adjlist[i][0]] = i;
}
}
}
Canonical::misses++;
}
else {
Canonical::hits++;
}
size_t code = a->second;
a.release();
if (Canonical::hits+Canonical::misses%10000==0) {
std::cout << "Caninocal hashmap hits: " << Canonical::hits << " misses: " << Canonical::misses << std::endl;
}
return code;
}*/
//get canonical with graph directly
size_t Canonical::getHash(Graph &g) {
size_t naiveCode = g.getNaiveCodeHashValue();
tbb::concurrent_hash_map<size_t, size_t>::accessor a;
bool r = Canonical::pattern_to_canonical.insert(a, naiveCode); // creates by default if not exists, acquires lock
if (r) { // it means that it is new
bliss::Graph bg = g.getBlissGraph();
bliss::Stats stats;
const unsigned int* perm = bg.canonical_form(stats, NULL, NULL);
bliss::Graph *bc = bg.permute(perm);
a->second = bc->get_hash();
delete bc;
Canonical::misses++;
}
else {
Canonical::hits++;
}
size_t code = a->second;
a.release();
if (Canonical::hits+Canonical::misses%10000==0) {
std::cout << "Caninocal hashmap hits: " << Canonical::hits << " misses: " << Canonical::misses << std::endl;
}
return code;
}
size_t Canonical::getHash(bliss::Graph &bg) {
//return Canonical::getHashScratch(bg);
size_t naiveCode = bg.get_hash();
tbb::concurrent_hash_map<size_t, size_t>::accessor a;
bool r = Canonical::pattern_to_canonical.insert(a, naiveCode); // creates by default if not exists, acquires lock
PatternInfo *naiveInfo = NULL;
PatternInfo *canonicalInfo = NULL;
if (r) { // it means that it is new
bliss::Stats stats;
size_t N = bg.get_nof_vertices();
//naiveInfo->init(N);
//canonicalInfo->init(N);
naiveInfo = new PatternInfo(N);
canonicalInfo = new PatternInfo(N);
//structures to keep automorphic info
for (unsigned int i = 0; i < N ; i ++) {
canonicalInfo->array[i] = i;
}
//running canonical form func
//perm[i] is the vertex idx from the original graph keep in position i of the canonical one.
const unsigned int* perm = bg.canonical_form(stats, &report_aut, canonicalInfo->array);
memcpy(naiveInfo->array, perm, sizeof(unsigned int) * N);
bliss::Graph *bc = bg.permute(perm);
a->second = bc->get_hash();
//put parent to canonical format
unsigned int parentAux[N];
memcpy(parentAux, canonicalInfo->array, sizeof(unsigned int) * N);
for (unsigned int i = 0; i < N ; i ++) {
canonicalInfo->array[i] = parentAux[perm[i]];
}
//bg.write_dimacs(stdout);
//bc->write_dimacs(stdout);
//std::cout << "canonical perm: [";
//for (uint i = 0; i < N ; i ++) {
// std::cout << " " << naiveInfo->array[i];
//}
//std::cout << " ]\n";
//std::cout << "parent: [";
//for (uint i = 0; i < N ; i ++) {
// std::cout << " " << canonicalInfo->array[i];
//}
//std::cout << " ]\n";
delete bc;
}
size_t code = a->second;
a.release();
/*//create accessor to fill other maps
tbb::concurrent_hash_map<size_t, PatternInfo*>::accessor b;
//fill map naive info
bool r1 = Canonical::naives.insert(b, naiveCode); // creates by default if not exists, acquires lock
if (r1) b->second = naiveInfo;
else if (naiveInfo) delete naiveInfo;
b.release();
//fill map canonical info
bool r2 = Canonical::canonicals.insert(b, code); // creates by default if not exists, acquires lock
if (r2) b->second = canonicalInfo;
else if (canonicalInfo) delete canonicalInfo;
b.release();
//sanaty check
if (r!=r1 || r!=r2) {
std::cout << "error: canonical hashmaps are incorrect!" << std::endl;
exit(1);
}*/
// canonical code
return code;
}
/*unsigned int* Canonical::getPermutation(bliss::Graph &bg) {
size_t naiveCode = bg.get_hash();
tbb::concurrent_hash_map<size_t, PatternInfo*>::accessor b;
bool r = Canonical::naives.find(b, naiveCode); // creates by default if not exists, acquires lock
if (r) { //means that was found
unsigned int *array = b->second->array;
b.release();
return array;
}
else {
b.release();
getHash(bg);
return getPermutation(bg);
}
}*/
/**
* The hook function that prints the found automorphisms.
* \a param must be a file descriptor (FILE *).
*/
void Canonical::report_aut(void* param, const unsigned int n, const unsigned int* aut) {
//fprintf((FILE*)param, "Generator: ");
//fprintf(stdout, "Generator: ");
//bliss::print_permutation((FILE*)param, n, aut, 0);
get_automorphism_map((unsigned int*)param, n, aut, 0);
/*std::cout << "[";
for (int i = 0; i < n ; i ++) {
std::cout << " " << aut[i];
}
std::cout << " ]\n"; */
//fprintf(stdout, "\n");
}
void Canonical::get_automorphism_map(unsigned int* parent, const unsigned int N, const unsigned int* perm, const unsigned int offset) {
assert(N > 0);
assert(perm);
for(unsigned int i = 0; i < N; i++) {
unsigned int j = perm[i];
if(j == i) continue;
while(parent[i]>j) {
parent[i] = j;
j = parent[j];
}
}
/*for(unsigned int i = 0; i < N; i++) {
unsigned int j = perm[i];
if(j == i)
continue;
bool is_first = true;
while(j != i) {
if(j < i) {
is_first = false;
break;
}
j = perm[j];
}
if(!is_first)
continue;
fprintf(stdout, "(%u,", i+offset);
j = perm[i];
while(j != i) {
if (parent[j] > i) parent[j] = i;
fprintf(stdout, "%u", j+offset);
j = perm[j];
if(j != i)
fprintf(stdout, ",");
}
fprintf(stdout, ")");
}*/
/*std::cout << "#####canonical perm####: [";
for (int i = 0; i < N ; i ++) {
std::cout << " " << perm[i];
std::cout << " (" << parent[i] << ") ";
}
std::cout << " ]\n";*/
}
/*void Canonical::merge_sets(int *parent, size_t N) {
for (int i = 0; i < N; i++) {
std::cout << "checking parent of id " << i << std::endl;
if (parent[i] == -1) {
parent[i] = i;
continue;
}
int j = parent[i];
//while(j != i) {
while(parent[j]!=j) {
j = parent[j];
std::cout << "new parent " << j << std::endl;
}
std::cout << "idx " << i << " parent " << j << std::endl;
parent[i] = j;
}
}*/