-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgraph.hpp
1908 lines (1823 loc) · 76.6 KB
/
graph.hpp
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2020 Guanyu Feng, Tsinghua University
*
* 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
*
* https://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.
*/
#pragma once
#include <cstdio>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <exception>
#include <functional>
#include <mutex>
#include <atomic>
#include <immintrin.h>
#include <algorithm>
#include <omp.h>
#include <tbb/tbb.h>
#include <sparsehash/dense_hash_map>
#include <cmath>
#include "type.hpp"
#include "bitmap.hpp"
#include "atomic.hpp"
#include "actset.hpp"
#include "mvvec.hpp"
#include "storage.hpp"
#include "io.hpp"
template <typename EdgeData = void>
class Graph
{
public:
using adjedge_type = AdjEdge<EdgeData>;
using edge_type = Edge<EdgeData>;
using Storage = IndexedEdgeStorage<adjedge_type, storage::data::Vector, storage::index::DenseHashMap>;
//using Storage = IndexOnlyStorage<adjedge_type, storage::data::Vector, storage::index::DenseHashMap>;
using adjlist_type = typename Storage::adjlist_type;
using adjlist_iter_type = typename Storage::adjlist_iter_type;
using adjlist_range_type = std::pair<adjlist_iter_type, adjlist_iter_type>;
using lock_type = typename Storage::lock_type;
template<typename VertexData>
struct VertexTree
{
adjedge_type parent;
VertexData data;
};
Graph(uint64_t _vertices, uint64_t _hint_edges, bool _symmetric, bool _dual, bool _trace_modified = false)
: vertices(_vertices), dense_threshold(vertices), symmetric(_symmetric), dual(_dual),
outgoing(), incoming(),
edges(_hint_edges==0?_vertices*16:_hint_edges),
dense_active_all(vertices),
active_in(vertices), active_out(vertices), active_tree(vertices),
invalidated(vertices), invalidated_idx(0),
offsets(), empty_parent(), modified(vertices), trace_modified(_trace_modified)
{
int ncpus = omp_get_max_threads();
cpu_set_t cpuset_full, cpuset;
pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
CPU_ZERO(&cpuset_full);
#pragma omp parallel
{
#pragma omp critical
{
cpu_set_t local_cpuset;
pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &local_cpuset);
CPU_OR(&cpuset_full, &cpuset_full, &local_cpuset);
thread_id.local() = omp_get_thread_num();
}
}
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset_full);
tbb::task_scheduler_init init(ncpus);
//task_arena.initialize(ncpus);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
fprintf(stderr, "|V|=%lu\n", _vertices);
empty_parent.nbr = vertices;
outgoing.resize(vertices);
if(!symmetric)
{
incoming.resize(vertices);
}
dense_active_all.fill();
}
uint64_t get_thread_id()
{
return thread_id.local();
}
lock_type& get_lock(uint64_t vid)
{
return outgoing.get_lock(vid);
}
std::mutex& get_global_lock()
{
return mutex;
}
ActiveSet &get_modified()
{
return modified;
}
uint64_t get_modified_length()
{
return modified.get_sparse_length();
}
void clear_modified()
{
modified.clear();
}
void transpose()
{
if(!symmetric)
{
std::swap(outgoing, incoming);
}
}
Bitmap& get_dense_active_in()
{
return active_in.get_dense_ref();
}
Bitmap& get_dense_active_out()
{
return active_out.get_dense_ref();
}
Bitmap& get_dense_active_all()
{
return dense_active_all;
}
std::vector<uint64_t>& get_sparse_active_in()
{
return active_in.get_sparse_ref();
}
std::vector<uint64_t>& get_sparse_active_out()
{
return active_out.get_sparse_ref();
}
uint64_t get_incoming_degree(uint64_t vid)
{
if(symmetric) return get_outgoing_degree(vid);
return incoming.get_degree(vid);
}
uint64_t get_outgoing_degree(uint64_t vid)
{
return outgoing.get_degree(vid);
}
adjlist_type& get_outgoing_adjlist(uint64_t vid)
{
return outgoing.get_adjlist(vid);
}
adjlist_type& get_incoming_adjlist(uint64_t vid)
{
if(symmetric) return get_outgoing_adjlist(vid);
return incoming.get_adjlist(vid);
}
adjlist_range_type get_outgoing_adjlist_range(uint64_t vid)
{
return outgoing.get_adjlist_iter(vid);
}
adjlist_range_type get_incoming_adjlist_range(uint64_t vid)
{
if(symmetric) return get_outgoing_adjlist_range(vid);
return incoming.get_adjlist_iter(vid);
}
uint64_t get_edge_num(edge_type e)
{
return outgoing.get_edge_num(e.src, e);
}
uint64_t add_edge(edge_type e, bool directed = true)
{
if(e.src >= vertices || e.dst >= vertices) throw std::runtime_error("VertexId error.");
if(!directed)
{
add_edge(e, true);
std::swap(e.src, e.dst);
return add_edge(e, true) - (e.src == e.dst);
}
uint64_t current_size = 0;
adjedge_type ae = e;
{
current_size = outgoing.update_edge(ae, e.src, 1);
}
if(!symmetric)
{
ae.nbr = e.src;
incoming.update_edge(ae, e.dst, 1);
}
return current_size;
}
uint64_t del_edge(edge_type e, bool directed = true)
{
if(e.src >= vertices || e.dst >= vertices) throw std::runtime_error("VertexId error.");
if(!directed)
{
del_edge(e, true);
std::swap(e.src, e.dst);
return del_edge(e, true);
}
uint64_t current_size = 0;
adjedge_type ae = e;
{
current_size = outgoing.update_edge(ae, e.src, -1);
}
if(!symmetric)
{
ae.nbr = e.src;
incoming.update_edge(ae, e.dst, -1);
}
return current_size;
}
template<typename VertexData>
std::vector<VertexTree<VertexData>> alloc_vertex_tree_array()
{
std::vector<VertexTree<VertexData>> ta(vertices);
#pragma omp parallel for
for(uint64_t i=0;i<vertices;i++) ta[i].parent = empty_parent;
return ta;
}
template<typename VertexData>
void fill_vertex_tree_array(std::vector<VertexTree<VertexData>> &ta, VertexData value)
{
#pragma omp parallel for
for(uint64_t i=0;i<vertices;i++) {
ta[i].data = value;
}
}
template<typename VertexData>
std::vector<VertexData> alloc_vertex_array()
{
std::vector<VertexData> a;
a.resize(vertices);
return a;
}
template<typename VertexData>
void fill_vertex_array(std::vector<VertexData> &a, VertexData value)
{
#pragma omp parallel for
for(uint64_t i=0;i<vertices;i++) {
a[i] = value;
}
}
Bitmap alloc_vertex_bitmap()
{
return Bitmap(vertices);
}
template<typename VertexData>
MVVec<VertexData> alloc_history_array()
{
return MVVec<VertexData>(vertices);
}
template<typename R>
R stream_vertices(std::function<R(uint64_t)> process, const Bitmap &active)
{
R reducer = 0;
#pragma omp parallel for schedule(dynamic, 64) reduction(+:reducer)
for(uint64_t word_i=0;word_i<WORD_OFFSET(vertices)+1;word_i++)
{
uint64_t v_i = BEGIN_OF_WORD(word_i);
uint64_t word = active.data[word_i];
while(word != 0)
{
if(word & 1)
{
reducer += process(v_i);
}
v_i++;
word >>= 1;
}
}
return reducer;
}
template<typename R>
R stream_edges(std::function<R(uint64_t, const adjlist_range_type &range)> sparse_process, std::function<R(uint64_t, const adjlist_range_type &range)> dense_process, const Bitmap &active)
{
R reducer = 0;
uint64_t active_edges = stream_vertices<uint64_t>(
[&](uint64_t vid)
{
return outgoing.get_degree(vid);
},
active
);
bool sparse = sparse_process && (active_edges < dense_threshold || active_edges < edges/20 || !dual || !dense_process);
//fprintf(stderr, "%lu %lu %s\n", active_edges, edges.load(), sparse?"sparse":"dense");
if(sparse)
{
#pragma omp parallel for schedule(dynamic, (active_edges<edges/200)?65536:64) reduction(+:reducer)
for(uint64_t word_i=0;word_i<WORD_OFFSET(vertices)+1;word_i++)
{
uint64_t v_i = BEGIN_OF_WORD(word_i);
uint64_t word = active.data[word_i];
while(word != 0)
{
if(word & 1)
{
if(outgoing.get_degree(v_i))
{
reducer += sparse_process(v_i, outgoing.get_adjlist_iter(v_i));
}
}
v_i++;
word >>= 1;
}
}
//reducer = tbb::parallel_reduce(tbb::blocked_range<uint64_t>(0lu, WORD_OFFSET(vertices)+1, (active_edges<edges/200)?65536:64), reducer,
//[&](const tbb::blocked_range<uint64_t> &range, R res) -> R
//{
// for(uint64_t word_i=range.begin();word_i!=range.end();word_i++)
// {
// uint64_t v_i = BEGIN_OF_WORD(word_i);
// uint64_t word = active.data[word_i];
// while(word != 0)
// {
// if(word & 1)
// {
// if(outgoing.get_degree(v_i)) res += sparse_process(v_i, outgoing.get_adjlist(v_i));
// }
// v_i++;
// word >>= 1;
// }
// }
// return res;
//},
//[](R x, R y) -> R
//{
// return x+y;
//});
}
else
{
#pragma omp parallel for schedule(dynamic, 64) reduction(+:reducer)
for(uint64_t v_i=0;v_i<vertices;v_i++)
{
if(symmetric)
{
if(outgoing.get_degree(v_i)) reducer += dense_process(v_i, outgoing.get_adjlist_iter(v_i));
}
else
{
if(incoming.get_degree(v_i)) reducer += dense_process(v_i, incoming.get_adjlist_iter(v_i));
}
}
}
return reducer;
}
//template<typename R>
//R stream_vertices(std::function<R(uint64_t)> process, const std::vector<uint64_t> &active, const uint64_t &length)
template<typename R, typename Process>
R stream_vertices(Process process, const std::vector<uint64_t> &active, const uint64_t &length)
{
R reducer = 0;
if(length < OPENMP_THRESHOLD)
{
for(uint64_t i=0;i<length;i++)
{
uint64_t v_i = active[i];
reducer += process(v_i);
}
}
else
{
reducer = tbb::parallel_reduce(tbb::blocked_range<uint64_t>(0lu, length, 256), reducer,
[&](const tbb::blocked_range<uint64_t> &range, R res) -> R
{
for(uint64_t i=range.begin();i!=range.end();i++)
{
uint64_t v_i = active[i];
res += process(v_i);
}
return res;
},
[](R x, R y) -> R
{
return x+y;
},
affinity_partitioner);
}
return reducer;
}
template<typename R>
R stream_edges(std::function<R(uint64_t, const adjlist_range_type &range)> sparse_process, std::function<R(uint64_t, const adjlist_range_type &range)> dense_process, const std::vector<uint64_t> &active, const uint64_t &length, uint64_t active_edges = (uint64_t)-1)
{
R reducer = 0;
if(active_edges == (uint64_t)-1)
{
active_edges = stream_vertices<uint64_t>(
[&](uint64_t vid)
{
return outgoing.get_degree(vid);
},
active, length
);
}
bool sparse = sparse_process && (active_edges < dense_threshold || active_edges < edges/20 || !dual || !dense_process);
//fprintf(stderr, "%lu %lu %s\n", active_edges, edges.load(), sparse?"sparse":"dense");
if(sparse)
{
//THRESHOLD_OPENMP("omp parallel for schedule(dynamic, 64) reduction(+:reducer)", length,
// for(uint64_t i=0;i<length;i++)
// {
// uint64_t v_i = active[i];
// if(outgoing.get_degree(v_i)) reducer += sparse_process(v_i, outgoing.get_adjlist(v_i));
// }
//);
if(length < OPENMP_THRESHOLD)
{
for(uint64_t i=0;i<length;i++)
{
uint64_t v_i = active[i];
if(outgoing.get_degree(v_i)) reducer += sparse_process(v_i, outgoing.get_adjlist_iter(v_i));
}
}
else
{
reducer = tbb::parallel_reduce(tbb::blocked_range<uint64_t>(0lu, length, 64), reducer,
[&](const tbb::blocked_range<uint64_t> &range, R res) -> R
{
for(uint64_t i=range.begin();i!=range.end();i++)
{
uint64_t v_i = active[i];
if(outgoing.get_degree(v_i)) res += sparse_process(v_i, outgoing.get_adjlist_iter(v_i));
}
return res;
},
[](R x, R y) -> R
{
return x+y;
},
affinity_partitioner);
}
}
else
{
#pragma omp parallel for schedule(dynamic, 32) reduction(+:reducer)
for(uint64_t v_i=0;v_i<vertices;v_i++)
{
if(symmetric)
{
if(outgoing.get_degree(v_i)) reducer += dense_process(v_i, outgoing.get_adjlist_iter(v_i));
}
else
{
if(incoming.get_degree(v_i)) reducer += dense_process(v_i, incoming.get_adjlist_iter(v_i));
}
}
}
return reducer;
}
//template<typename R>
//R stream_edges_sparse(std::function<R(uint64_t, const adjedge_type &)> process, const std::vector<uint64_t> &active, const uint64_t &length)
template<typename R, typename Process>
R stream_edges_sparse(Process process, const std::vector<uint64_t> &active, const uint64_t &length, uint64_t active_edges = (uint64_t)-1)
{
R reducer = 0;
if(active_edges == (uint64_t)-1)
{
active_edges = stream_vertices<uint64_t>(
[&](uint64_t vid)
{
return outgoing.get_degree(vid);
},
active, length
);
}
if(active_edges > OPENMP_THRESHOLD)
{
if(offsets.size() < length+1) offsets.resize(length+1);
for(uint64_t i=1;i<=length;i++)
{
offsets[i] = offsets[i-1] + outgoing.get_adjlist(active[i-1]).size();
}
tbb::enumerable_thread_specific<uint64_t> next_a(1);
//thread_local uint64_t next = 1;
reducer = tbb::parallel_reduce(tbb::blocked_range<uint64_t>(0lu, offsets[length], 256), reducer,
[&](const tbb::blocked_range<uint64_t> &range, R res) -> R
{
uint64_t next = next_a.local();
if(next > length || offsets[next-1] > range.begin()) next = (length < 32) ? 1 : std::upper_bound(offsets.begin(), offsets.begin()+length+1, range.begin())-offsets.begin();
for(uint64_t i=range.begin();i!=range.end();i++)
{
while(offsets[next] <= i) next++;
uint64_t v_i = active[next-1];
res += process(v_i, outgoing.get_adjlist(v_i)[i-offsets[next-1]]);
}
next_a.local() = next;
return res;
},
[](R x, R y) -> R
{
return x+y;
},
affinity_partitioner);
}
else
{
for(uint64_t i=0;i<length;i++)
{
uint64_t v_i = active[i];
if(outgoing.get_degree(v_i))
{
for(auto e:outgoing.get_adjlist(v_i)) reducer += process(v_i, e);
}
}
}
return reducer;
}
template<typename R, typename Process>
R stream_vertices_hybrid(Process process, ActiveSet &active)
{
if(active.is_dense())
{
return stream_vertices<R>(process, active.get_dense());
}
else
{
return stream_vertices<R>(process, active.get_sparse(), active.get_sparse_length());
}
}
//TODO pull
template<typename R, typename ProcessEdge>
R stream_edges_hybrid(std::function<R(uint64_t, const adjlist_range_type &range)> process_push, std::function<R(uint64_t, const adjlist_range_type &range)> process_pull, ProcessEdge process_edge, ActiveSet &active)
{
if(active.is_dense())
{
//fprintf(stderr, "stream_edges_dense >= %lu\n", active.get_sparse_length());
return stream_edges<R>(process_push, process_pull, active.get_dense());
}
else
{
//return stream_edges<R>(process_push, nullptr, active.get_sparse(), active.get_sparse_length());
//return stream_edges_sparse<R>(process_edge, active.get_sparse(), active.get_sparse_length());
uint64_t active_edges = stream_vertices<uint64_t>(
[&](uint64_t vid)
{
return outgoing.get_degree(vid);
},
active.get_sparse(), active.get_sparse_length()
);
if(active_edges < 16384 || !Storage::edge_random_accessable)
{
return stream_edges<R>(process_push, nullptr, active.get_sparse(), active.get_sparse_length());
}
double x = log(active.get_sparse_length()), y = log(active_edges);
double predict = -2.20754644*x+0.58438928*y+14.45252841;
//uint64_t active_edges = (uint64_t) -1;
//const uint64_t vertex_centric_threshold = 2048000;//1024000;
//if(active.get_sparse_length() > vertex_centric_threshold || !Storage::edge_random_accessable)
if(predict < 0)
{
//fprintf(stderr, "stream_edges_sparse_vertex %lu\n", active.get_sparse_length());
return stream_edges<R>(process_push, nullptr, active.get_sparse(), active.get_sparse_length(), active_edges);
}
else
{
return stream_edges_sparse<R>(process_edge, active.get_sparse(), active.get_sparse_length(), active_edges);
}
}
}
//template <typename R, typename DataType>
//R build_tree_raw(
// std::function<std::pair<DataType, bool>(uint64_t vid)> init_label_func,
// std::function<std::pair<bool, R>(uint64_t depth, R total_result, R local_result)> continue_reduce_func,
// std::function<std::pair<bool, DataType>(uint64_t src, uint64_t dst, DataType src_data, DataType dst_data, adjedge_type adjedge)> update_func,
// std::function<R(R old_result, uint64_t src, uint64_t dst, DataType src_data, DataType old_dst_data, DataType new_dst_data)> active_result_func,
// std::vector<DataType> &labels)
template <typename R, typename DataType, typename InitLabelFunc, typename ContinueReduceFunc, typename UpdateFunc, typename ActiveResultFunc>
R build_tree_raw(
InitLabelFunc init_label_func,
ContinueReduceFunc continue_reduce_func,
UpdateFunc update_func,
ActiveResultFunc active_result_func,
std::vector<DataType> &labels)
{
auto &active_all = get_dense_active_all();
auto &active_in = get_dense_active_in();
auto &active_out = get_dense_active_out();
active_in.clear();
stream_vertices<uint64_t>(
[&](uint64_t vid)
{
labels[vid] = init_label_func(vid).first;
if(init_label_func(vid).second) active_in.set_bit(vid);
return 0;
},
active_all
);
R total_result = 0;
for(uint64_t i=0;true;i++)
{
active_out.clear();
R local_result = stream_edges<R>(
[&](uint64_t src, const adjlist_range_type &outgoing_range)
{
R result = 0;
for(auto iter = outgoing_range.first;iter != outgoing_range.second; iter++)
{
auto edge = *iter;
if(edge.num > 0)
{
uint64_t dst = edge.nbr;
auto src_data = labels[src];
auto dst_data = labels[dst];
if(update_func(src, dst, src_data, dst_data, edge).first)
{
auto eup = edge; eup.nbr = src;
auto update_pair = update_label_raw(labels, src, dst, eup, update_func);
if(update_pair.first)
{
active_out.set_bit(dst);
result = active_result_func(result, src, dst, src_data, dst_data, update_pair.second);
}
}
}
}
return result;
},
[&](uint64_t dst, const adjlist_range_type &incoming_range)
{
DataType new_label = labels[dst];
uint64_t new_src = (uint64_t)-1;
adjedge_type new_edge;
R result = 0;
for(auto iter = incoming_range.first;iter != incoming_range.second; iter++)
{
auto edge = *iter;
if(edge.num > 0)
{
uint64_t src = edge.nbr;
auto src_data = labels[src];
auto update_pair = update_func(src, dst, src_data, new_label, edge);
if(update_pair.first)
{
new_label = update_pair.second;
new_src = src;
new_edge = edge;
}
}
}
if(new_src == (uint64_t)-1) return result;
auto src_data = labels[new_src];
auto dst_data = labels[dst];
if(update_func(new_src, dst, src_data, dst_data, new_edge).first)
{
auto src = new_src;
auto edge = new_edge;
auto eup = edge; eup.nbr = src;
auto update_pair = update_label_raw(labels, src, dst, eup, update_func);
if(update_pair.first)
{
active_out.set_bit(dst);
result = active_result_func(result, src, dst, src_data, dst_data, update_pair.second);
}
}
return result;
},
active_in
);
std::swap(active_in, active_out);
bool is_continue;
std::tie(is_continue, total_result) = continue_reduce_func(i, total_result, local_result);
if(!is_continue) break;
}
return total_result;
}
//template <typename R, typename DataType>
//R update_tree_add(
// std::function<std::pair<bool, R>(uint64_t depth, R total_result, R local_result)> continue_reduce_func,
// std::function<std::pair<bool, DataType>(uint64_t src, uint64_t dst, DataType src_data, DataType dst_data, adjedge_type adjedge)> update_func,
// std::function<R(R old_result, uint64_t src, uint64_t dst, DataType src_data, DataType old_dst_data, DataType new_dst_data)> active_result_func,
// std::vector<DataType> &labels, edge_type edge, bool directed = true)
template <typename R, typename DataType, typename ContinueReduceFunc, typename UpdateFunc, typename ActiveResultFunc>
R update_tree_add_raw(
ContinueReduceFunc continue_reduce_func,
UpdateFunc update_func,
ActiveResultFunc active_result_func,
std::vector<DataType> &labels, edge_type edge, bool directed = true)
{
if(!directed)
{
R retl = update_tree_add_raw<R, DataType>(continue_reduce_func, update_func, active_result_func, labels, edge, true);
std::swap(edge.src, edge.dst);
R retr = update_tree_add_raw<R, DataType>(continue_reduce_func, update_func, active_result_func, labels, edge, true);
return continue_reduce_func(0, retl, retr).second;
}
R total_result = 0;
if(update_func(edge.src, edge.dst, labels[edge.src], labels[edge.dst], edge).first)
{
active_in.clear();
adjedge_type eup = edge; eup.nbr = edge.src;
auto src_data = labels[edge.src];
auto dst_data = labels[edge.dst];
auto update_pair = update_label_raw(labels, edge.src, edge.dst, eup, update_func);
if(update_pair.first)
{
active_in.active(edge.dst);
if(trace_modified) modified.active(edge.dst);
total_result = active_result_func(total_result, edge.src, edge.dst, src_data, dst_data, update_pair.second);
}
}
else
{
return total_result;
}
for(uint64_t i=0;true;i++)
{
active_out.clear();
R local_result = stream_edges_hybrid<R>(
[&](uint64_t src, const adjlist_range_type &outgoing_range)
{
R result = 0;
for(auto iter = outgoing_range.first;iter != outgoing_range.second; iter++)
{
auto edge = *iter;
if(edge.num > 0)
{
uint64_t dst = edge.nbr;
auto src_data = labels[src];
auto dst_data = labels[dst];
if(update_func(src, dst, src_data, dst_data, edge).first)
{
auto eup = edge; eup.nbr = src;
auto update_pair = update_label_raw(labels, src, dst, eup, update_func);
if(update_pair.first)
{
active_out.active(dst);
if(trace_modified) modified.active(dst);
result = active_result_func(result, src, dst, src_data, dst_data, update_pair.second);
}
}
}
}
return result;
},
[&](uint64_t dst, const adjlist_range_type &incoming_range)
{
DataType new_label = labels[dst];
uint64_t new_src = (uint64_t)-1;
adjedge_type new_edge;
R result = 0;
for(auto iter = incoming_range.first;iter != incoming_range.second; iter++)
{
auto edge = *iter;
if(edge.num > 0)
{
uint64_t src = edge.nbr;
auto src_data = labels[src];
auto update_pair = update_func(src, dst, src_data, new_label, edge);
if(update_pair.first)
{
new_label = update_pair.second;
new_src = src;
new_edge = edge;
}
}
}
if(new_src == (uint64_t)-1) return result;
auto src_data = labels[new_src];
auto dst_data = labels[dst];
if(update_func(new_src, dst, src_data, dst_data, new_edge).first)
{
auto src = new_src;
auto edge = new_edge;
auto eup = edge; eup.nbr = src;
auto update_pair = update_label_raw(labels, src, dst, eup, update_func);
if(update_pair.first)
{
active_out.active(dst);
if(trace_modified) modified.active(dst);
result = active_result_func(result, src, dst, src_data, dst_data, update_pair.second);
}
}
return result;
},
[&](uint64_t src, const adjedge_type &edge)
{
R result = 0;
if(edge.num > 0)
{
uint64_t dst = edge.nbr;
auto src_data = labels[src];
auto dst_data = labels[dst];
if(update_func(src, dst, src_data, dst_data, edge).first)
{
auto eup = edge; eup.nbr = src;
auto update_pair = update_label_raw(labels, src, dst, eup, update_func);
if(update_pair.first)
{
active_out.active(dst);
if(trace_modified) modified.active(dst);
result = active_result_func(result, src, dst, src_data, dst_data, update_pair.second);
}
}
}
return result;
},
active_in
);
std::swap(active_in, active_out);
bool is_continue;
std::tie(is_continue, total_result) = continue_reduce_func(i, total_result, local_result);
if(!is_continue) break;
}
return total_result;
}
//template <typename R, typename DataType>
//R build_tree(
// std::function<std::pair<DataType, bool>(uint64_t vid)> init_label_func,
// std::function<std::pair<bool, R>(uint64_t depth, R total_result, R local_result)> continue_reduce_func,
// std::function<std::pair<bool, DataType>(uint64_t src, uint64_t dst, DataType src_data, DataType dst_data, adjedge_type adjedge)> update_func,
// std::function<R(R old_result, uint64_t src, uint64_t dst, DataType src_data, DataType old_dst_data, DataType new_dst_data)> active_result_func,
// std::vector<VertexTree<DataType>> &labels)
template <typename R, typename DataType, typename InitLabelFunc, typename ContinueReduceFunc, typename UpdateFunc, typename ActiveResultFunc>
R build_tree(
InitLabelFunc init_label_func,
ContinueReduceFunc continue_reduce_func,
UpdateFunc update_func,
ActiveResultFunc active_result_func,
std::vector<VertexTree<DataType>> &labels)
{
//static std::vector<VertexTree<DataType>> bak_labels = alloc_vertex_tree_array<DataType>();
auto &active_all = get_dense_active_all();
auto &active_in = get_dense_active_in();
auto &active_out = get_dense_active_out();
active_in.clear();
stream_vertices<uint64_t>(
[&](uint64_t vid)
{
labels[vid].data = init_label_func(vid).first;
labels[vid].parent = empty_parent;
//bak_labels[vid] = labels[vid];
if(init_label_func(vid).second) active_in.set_bit(vid);
return 0;
},
active_all
);
R total_result = 0;
for(uint64_t i=0;true;i++)
{
//stream_vertices<uint64_t>(
// [&](uint64_t vid)
// {
// bak_labels[vid] = labels[vid];
// return 0;
// },
// active_in
//);
active_out.clear();
R local_result = stream_edges<R>(
[&](uint64_t src, const adjlist_range_type &outgoing_range)
{
R result = 0;
for(auto iter = outgoing_range.first;iter != outgoing_range.second; iter++)
{
auto edge = *iter;
if(edge.num > 0)
{
uint64_t dst = edge.nbr;
//auto src_data = bak_labels[src].data;
auto src_data = labels[src].data;
auto dst_data = labels[dst].data;
if(update_func(src, dst, src_data, dst_data, edge).first)
{
auto eup = edge; eup.nbr = src;
auto update_pair = update_label(labels, src, dst, eup, update_func, src_data);
if(update_pair.first)
{
active_out.set_bit(dst);
result = active_result_func(result, src, dst, src_data, dst_data, update_pair.second);
}
}
}
}
return result;
},
[&](uint64_t dst, const adjlist_range_type &incoming_range)
{
DataType new_label = labels[dst].data;
uint64_t new_src = (uint64_t)-1;
adjedge_type new_edge;
R result = 0;
for(auto iter = incoming_range.first;iter != incoming_range.second; iter++)
{
auto edge = *iter;
if(edge.num > 0)
{
uint64_t src = edge.nbr;
//auto src_data = bak_labels[src].data;
auto src_data = labels[src].data;
auto update_pair = update_func(src, dst, src_data, new_label, edge);
if(update_pair.first)
{
new_label = update_pair.second;
new_src = src;
new_edge = edge;
}
}
}
if(new_src == (uint64_t)-1) return result;
//auto src_data = bak_labels[new_src].data;
auto src_data = labels[new_src].data;
auto dst_data = labels[dst].data;
if(update_func(new_src, dst, src_data, dst_data, new_edge).first)
{
auto src = new_src;
auto edge = new_edge;
auto eup = edge; eup.nbr = src;
auto update_pair = update_label(labels, src, dst, eup, update_func, src_data);
if(update_pair.first)
{
active_out.set_bit(dst);
result = active_result_func(result, src, dst, src_data, dst_data, update_pair.second);
}
}
return result;
},
active_in
);
std::swap(active_in, active_out);
bool is_continue;
std::tie(is_continue, total_result) = continue_reduce_func(i, total_result, local_result);
if(!is_continue) break;
}
return total_result;
}
template <typename DataType, typename UpdateFunc>
bool need_update_tree_add(
UpdateFunc update_func,
std::vector<VertexTree<DataType>> &labels, edge_type edge, bool directed = true)
{
if(!directed)
{
bool retl = need_update_tree_add<DataType>(update_func, labels, edge, true);
std::swap(edge.src, edge.dst);
bool retr = need_update_tree_add<DataType>(update_func, labels, edge, true);
return retl || retr;
}
return (update_func(edge.src, edge.dst, labels[edge.src].data, labels[edge.dst].data, edge).first);
}