-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph.h
2187 lines (2018 loc) · 55.4 KB
/
graph.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
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
// -*- C++ -*-
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "parallel.h"
#include "partitioner.h"
#include <assert.h>
//#include <type_traits>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#include <cstring>
#include <string>
#include <utility>
#include <algorithm>
#include <sys/mman.h>
#ifndef PULL_EDGES
#define PULL_EDGES 0
#endif
#ifndef EDGES_HILBERT
#define EDGES_HILBERT 0
#endif
#ifndef PARTITION_RANGE
#define PARTITION_RANGE 1
#endif
#if 0
#include "cilkpub/sort.h"
template<typename It, typename Cmp>
void mysort( It begin, It end, Cmp cmp )
{
cilkpub::cilk_sort( begin, end, cmp );
}
#else
template<typename It, typename Cmp>
void mysort( It begin, It end, Cmp cmp )
{
std::sort( begin, end, cmp );
}
#endif
using namespace std;
//#define PAGESIZE (32)
//#define PAGESIZE (2048)
#define PAGESIZE (4096)
// To be used in boolean expressions
// **************************************************************
// ADJACENCY ARRAY REPRESENTATION
// **************************************************************
class symmetricVertex
{
private:
intE* neighbors;
intT degree;
#if !COMPRESSED_VERTICES
intT id;
#endif
public:
void del()
{
delete [] neighbors;
}
symmetricVertex() {}
symmetricVertex(intE* n, intT d
#if !COMPRESSED_VERTICES
, intT vertex_id
#endif
) : neighbors(n), degree(d)
#if !COMPRESSED_VERTICES
, id(vertex_id)
#endif
{
}
#ifndef WEIGHTED
intE getInNeighbor(intT j)
{
return neighbors[j];
}
intE getOutNeighbor(intT j)
{
return neighbors[j];
}
intE getInWeight(intT j)
{
return 1;
}
intE getOutWeight(intT j)
{
return 1;
}
#else
//weights are stored in the entry after the neighbor ID
//so size of neighbor list is twice the degree
intE getInNeighbor(intT j)
{
return neighbors[2*j];
}
intE getOutNeighbor(intT j)
{
return neighbors[2*j];
}
intE getInWeight(intT j)
{
return neighbors[2*j+1];
}
intE getOutWeight(intT j)
{
return neighbors[2*j+1];
}
#endif
intT getInDegree()
{
return degree;
}
intT getOutDegree()
{
return degree;
}
void setInNeighbors(intE* _i)
{
neighbors = _i;
}
void setOutNeighbors(intE* _i)
{
neighbors=_i;
}
void setInDegree(intT _d)
{
degree = _d;
}
void setOutDegree(intT _d)
{
degree = _d;
}
#if !COMPRESSED_VERTICES
void setInDegId(intT _vertex_id)
{
id = _vertex_id;
}
void setOutDegId(intT _vertex_id)
{
id= _vertex_id;
}
intT getInDegId()
{
return id;
}
intT getOutDegId()
{
return id;
}
#endif
intE* getInNeighborPtr()
{
return neighbors;
}
intE* getOutNeighborPtr()
{
return neighbors;
}
void flipEdges() {}
};
class asymmetricVertex
{
private:
intE* inNeighbors;
intE* outNeighbors;
intT outDegree;
intT inDegree;
#if !COMPRESSED_VERTICES
intT inDegId;
intT outDegId;
#endif
public:
intE* getInNeighborPtr()
{
return inNeighbors;
}
intE* getOutNeighborPtr()
{
return outNeighbors;
}
void del()
{
delete [] inNeighbors;
delete [] outNeighbors;
}
asymmetricVertex() {}
asymmetricVertex(intE* iN, intE* oN, intT id, intT od
#if !COMPRESSED_VERTICES
, intT iId, intT oId
#endif
) : inNeighbors(iN), outNeighbors(oN), inDegree(id), outDegree(od)
#if !COMPRESSED_VERTICES
, inDegId(iId), outDegId(oId)
#endif
{}
#ifndef WEIGHTED
intE getInNeighbor(intT j)
{
return inNeighbors[j];
}
intE getOutNeighbor(intT j)
{
return outNeighbors[j];
}
intE getInWeight(intT j)
{
return 1;
}
intE getOutWeight(intT j)
{
return 1;
}
#else
intE getInNeighbor(intT j)
{
return inNeighbors[2*j];
}
intE getOutNeighbor(intT j)
{
return outNeighbors[2*j];
}
intE getInWeight(intT j)
{
return inNeighbors[2*j+1];
}
intE getOutWeight(intT j)
{
return outNeighbors[2*j+1];
}
#endif
intT getInDegree()
{
return inDegree;
}
intT getOutDegree()
{
return outDegree;
}
void setInNeighbors(intE* _i)
{
inNeighbors = _i;
}
void setOutNeighbors(intE* _i)
{
outNeighbors = _i;
}
void setInDegree(intT _d)
{
inDegree = _d;
}
void setOutDegree(intT _d)
{
outDegree = _d;
}
#if !COMPRESSED_VERTICES
void setInDegId(intT _vertex_id)
{
inDegId = _vertex_id;
}
void setOutDegId(intT _vertex_id)
{
outDegId= _vertex_id;
}
intT getInDegId()
{
return inDegId;
}
intT getOutDegId()
{
return outDegId;
}
#endif
void flipEdges()
{
swap(inNeighbors,outNeighbors);
swap(inDegree,outDegree);
#if !COMPRESSED_VERTICES
swap(inDegId,outDegId);
#endif
}
};
#if PULL_EDGES
class Edge
{
private:
intT src, dst;
#ifdef WEIGHTED
intE weight;
#endif
public:
Edge() { }
#ifdef WEIGHTED
Edge( intT s, intT d, intE w ) : src( s ), dst( d ), weight( w ) { }
#else
Edge( intT s, intT d, intE w ) : src( s ), dst( d ) { }
Edge( intT s, intT d ) : src( s ), dst( d ) { }
#endif
Edge( const Edge & e ) : src( e.src ), dst( e.dst )
#ifdef WEIGHTED
, weight( e.weight )
#endif
{ }
intE getSource() const
{
return src;
}
intE getDestination() const
{
return dst;
}
#ifdef WEIGHTED
intT getWeight() const
{
return weight;
}
#else
intT getWeight() const
{
return 0;
}
#endif
};
template<typename T>
typename std::make_unsigned<T>::type roundUpPow2( T n_u )
{
const unsigned int num_bits = sizeof(T) * 8;
typename std::make_unsigned<T>::type n = n_u;
--n;
for( unsigned int shift=1; shift < num_bits; shift <<= 1 )
n |= n >> shift;
++n;
return n;
}
// Source code based on https://en.wikipedia.org/wiki/Hilbert_curve
class HilbertEdgeSort
{
intT n;
public:
HilbertEdgeSort( intT n_ ) : n( roundUpPow2(n_) ) { }
bool operator () ( const Edge & l, const Edge & r ) const
{
return e2d( l ) < e2d( r );
}
private:
intT e2d( const Edge & e ) const
{
return xy2d( e.getSource(), e.getDestination() );
}
//convert (x,y) to d
intT xy2d( intT x, intT y ) const
{
intT rx, ry, s, d=0;
for (s=n/2; s>0; s/=2)
{
rx = (x & s) > 0;
ry = (y & s) > 0;
d += s * s * ((3 * rx) ^ ry);
rot(s, &x, &y, rx, ry);
}
return d;
}
//convert d to (x,y)
void d2xy(intT d, intT *x, intT *y) const
{
intT rx, ry, s, t=d;
*x = *y = 0;
for (s=1; s<n; s*=2)
{
rx = 1 & (t/2);
ry = 1 & (t ^ rx);
rot(s, x, y, rx, ry);
*x += s * rx;
*y += s * ry;
t /= 4;
}
}
static void rot( intT n, intT *x, intT *y, intT rx, intT ry )
{
if (ry == 0)
{
if (rx == 1)
{
*x = n-1 - *x;
*y = n-1 - *y;
}
std::swap( *x, *y );
}
}
};
template<class Edge>
class EdgeList
{
private:
Edge * edges;
intE num_edges;
intT num_vertices;
public:
EdgeList( intE m=0, intT n=0) : num_edges( m ) , num_vertices( n )
{
edges = m > 0 ? new Edge[m] : (Edge *)0;
}
EdgeList( const EdgeList<Edge> & el ) : num_edges( el.num_edges ), num_vertices(el.num_vertices)
{
edges = new Edge[num_edges];
std::copy( &el.edges[0], &el.edges[num_edges], edges );
}
EdgeList( EdgeList<Edge> && el ) : num_edges( el.num_edges ) ,num_vertices(el.num_vertices)
{
edges = el.edges;
el.num_edges = 0;
el.edges = 0;
el.num_vertices=0;
}
~EdgeList()
{
if( edges )
delete[] edges;
}
const EdgeList<Edge> & operator = ( const EdgeList<Edge> & el )
{
num_edges = el.num_edges;
num_vertices = el.num_vertices;
edges = new Edge[num_edges];
std::copy( &el.edges[0], &el.edges[num_edges], edges );
return *this;
}
const EdgeList<Edge> & operator = ( EdgeList<Edge> && el )
{
num_edges = el.num_edges;
num_vertices = el.num_vertices;
edges = el.edges;
el.num_edges = 0;
el.edges = 0;
el.num_vertices = 0;
return *this;
}
typedef Edge * iterator;
typedef const Edge * const_iterator;
iterator begin()
{
return &edges[0];
}
iterator end()
{
return &edges[num_edges];
}
const_iterator begin() const
{
return &edges[0];
}
const_iterator end() const
{
return &edges[num_edges];
}
const_iterator cbegin() const
{
return &edges[0];
}
const_iterator cend() const
{
return &edges[num_edges];
}
size_t get_num_edges() const
{
return num_edges;
}
size_t get_num_vertices() const
{
return num_vertices;
}
Edge & operator[] ( long i )
{
return edges[i];
}
const Edge & operator[] ( long i ) const
{
return edges[i];
}
void hilbert_sort()
{
mysort( begin(), end(), HilbertEdgeSort(num_vertices) );
}
};
#endif
//This is the function for hugepage mmap allocation
//Based on the NUMA Awareness
template <class vertex>
class wholeGraph
{
public:
vertex *V; //mmap
intT n;
intT m;
intE* allocatedInplace; //mmap
intE* inEdges; //mmap
intT* flags; //mmap
bool transposed;
bool isSymmetric;
wholeGraph() {}
wholeGraph(intT nn, intT mm, bool issym)
: n(nn), m(mm), isSymmetric(issym),
flags(NULL), transposed(false)
{
V = new vertex [n];
#ifndef WEIGHTED
allocatedInplace = new intE [m];
#else//WEIGHTED
allocatedInplace = new intE [2*m];
#endif
if(!isSymmetric)
{
#ifndef WEIGHTED
inEdges = new intE [m];
#else//WEIGHTED
inEdges = new intE [2*m];
#endif
}
else
inEdges = NULL;
}
void del()
{
if (flags != NULL)
delete [] flags;
if(allocatedInplace!=NULL) delete [] allocatedInplace;
delete [] V;
if(inEdges != NULL) delete [] inEdges;
}
void transpose()
{
if(!isSymmetric)
{
parallel_for(intT i=0; i<n; i++)
V[i].flipEdges();
transposed = !transposed;
}
}
};
template <class vertex>
class graph
{
public:
vertex *V; //mmap
intT n;
intT m;
intE* allocatedInplace; //mmap
intE* inEdges; //mmap
intT* flags; //mmap
bool transposed;
intT n_src_start, n_src_end, n_dst_start, n_dst_end;
bool isSymmetric;
int numanode;
#if COMPRESSED_CSCCSR
pair<intT,vertex>* CSCV; //mmap
pair<intT,vertex>* CSRV; //mmap
intT CSCVn;
intT CSRVn;
#endif
#if COMPRESSED_VERTICES
pair<intT,vertex> * CV; //mmap
intT CVn;
#endif
graph() {}
graph(intT nn, intT mm, bool issym,int pp)
: n(nn), m(mm), isSymmetric(issym),
flags(NULL), transposed(false),
n_src_start(0), n_src_end(nn),
n_dst_start(0), n_dst_end(nn)
,numanode(pp)
{
V = 0;
#ifndef WEIGHTED
allocatedInplace = new intE [m];
#else
allocatedInplace = new intE [2*m];
#endif
if(!isSymmetric)
{
#ifndef WEIGHTED
inEdges = new intE [m];
#else
inEdges = new intE [2*m];
#endif
}
else
inEdges = NULL;
#if COMPRESSED_CSCCSR
CSCV=0;
CSRV=0;
CSCVn=0;
CSRVn=0;
#endif
#if COMPRESSED_VERTICES
CV = 0;
CVn =0;
#endif
}
void del()
{
if (flags != NULL)
delete [] flags;
if(allocatedInplace!=NULL) delete [] allocatedInplace;
if(inEdges != NULL) delete [] inEdges;
#if COMPRESSED_CSCCSR
if(CSCV) delete [] CSCV;
if(CSRV) delete [] CSRV;
#endif
#if COMPRESSED_VERTICES
if(CV)
delete [] CV;
#endif
}
void reorder_vertices( intT * __restrict reorder );
// asymmetricVertex * newV = new asymmetricVertex, n);
void transpose()
{
if(!isSymmetric)
{
#if COMPRESSED_CSCCSR
if(CSCV)
{
parallel_for (intT i=0; i<CSCVn; i++)
CSCV[i].second.flipEdges();
}
if(CSRV)
{
parallel_for (intT i=0; i<CSRVn; i++)
CSRV[i].second.flipEdges();
}
#endif
#if COMPRESSED_VERTICES
if(CV)
{
parallel_for (intT i=0; i<CVn; i++)
CV[i].second.flipEdges();
}
#endif
transposed = !transposed;
}
}
#if PULL_EDGES
#if DEST_EDGES
EdgeList<Edge> build_edgelist() const
{
EdgeList<Edge> el (m,n);
long k = 0;
for( intT i=0; i<n; i++ )
{
vertex v = V[i];
for( intT j=0; j < v.getOutDegree(); ++j )
{
intT d = v.getOutNeighbor( j );
#ifndef WEIGHTED
el[k++] = Edge( i, d );
#else
el[k++] = Edge( i, d, v.getOutWeight( j ) );
#endif
}
}
assert( k == m );
return el;
}
#else//DEST_EDGES
EdgeList<Edge> build_edgelist() const
{
EdgeList<Edge> el (m,n);
long k = 0;
for( intT i=0; i<n; i++ )
{
vertex v = V[i];
for( intT j=0; j < v.getInDegree(); ++j )
{
intT d = v.getInNeighbor( j );
#ifndef WEIGHTED
el[k++] = Edge( d, i );
#else
el[k++] = Edge( d, i, v.getInWeight( j ) );
#endif
}
}
assert( k == m );
return el;
}
#endif//DEST_EDGES
#endif//PULL_EDGES
};
template <class vertex>
class partitioned_graph
{
public:
typedef vertex vertex_type;
//typedef Allocator allocator_type;
vertex *Ver;
partitioner partition;
graph<vertex> * localG;
intT m,n;
bool source;
#if PULL_EDGES
EdgeList<Edge> * localEdgeList;
#endif
partitioned_graph( wholeGraph<vertex> & GA, int num_part, long start,
bool partition_source )
: partition(num_part,GA.n),m(GA.m),n(GA.n),source(partition_source)
{
localG = new graph<vertex> [num_part];
#if PULL_EDGES
localEdgeList = new EdgeList<Edge>[num_part];
#endif
Ver = GA.V;
// partition.set_num_elements(GA.n);
//std::cerr << "partition graph: n=" << GA.n << " m=" << GA.m << std::endl;
#if PARTITION_RANGE
// Normal path
partitionByDegree( GA, num_part, partition.as_array(), sizeof(intT), partition_source );
partition.compute_starts();
#if MIX
{
partitioner by2(2,GA.n);
partitionByDegree( GA, 2, by2.as_array(), sizeof(intT), false );
by2.compute_starts();
graph<vertex> tempGA[2] =
{
PartitionByDest( GA, by2.start_of(0), by2.start_of(1), 0 ),
PartitionByDest( GA, by2.start_of(1), by2.start_of(2), 2 )
};
partitionByDegree( tempGA[0], 2, partition.as_array(), sizeof(intT), true );
partitionByDegree( tempGA[1], 2, &partition.as_array()[2], sizeof(intT), true );
//partition.compute_starts();
parallel_for_numa( int p = 0; p < 4; ++p )
{
intT src = p < 2 ? 0 : 1;
intT dst = p & 1;
// localG[p] = PartitionBySour( tempGA[src], partition.start_of(p), partition.start_of(p+1), p );
intT start = ((p&1)==0) ? 0 : partition.as_array()[p-1];
intT end = ((p&1)==0) ? partition.as_array()[p] : GA.n;
localG[p] = PartitionBySour( tempGA[src], start, end, p );
localG[p].n_dst_start = tempGA[src].n_dst_start;
localG[p].n_dst_end = tempGA[src].n_dst_end;
}
tempGA[0].del();
tempGA[1].del();
partition.as_array()[0] = GA.n/4;
partition.as_array()[1] = GA.n/4;
partition.as_array()[2] = GA.n/4;
partition.as_array()[3] = GA.n-3*(GA.n/4);
partition.compute_starts();
}
#else //MIX
parallel_for( int p=0; p < partition.get_num_partitions(); ++p )
{
if( partition_source )
localG[p] = PartitionBySour( GA, partition.start_of(p), partition.start_of(p+1),p);
else
localG[p] = PartitionByDest( GA, partition.start_of(p), partition.start_of(p+1),p);
#if PULL_EDGES
localEdgeList[p] = localG[p].build_edgelist();
#if EDGES_HILBERT
localEdgeList[p].hilbert_sort();
#endif
#endif
}
#endif // MIX
#else // PARTITION_RANGE
partitionApprox( GA, partition.as_array(), num_part );
parallel_for( int p=0; p < partition.get_num_partitions(); ++p )
{
localG[p] = graphFilter( GA, partition.as_array(), p );
intT s = sequence::reduce<intT>((intT)0,(intT)GA.n,addF<intT>(),IsPart(partition,p));
partition.set_size( p, s );
}
#endif
}
void del()
{
for( int p=0; p < partition.get_num_partitions(); ++p )
{
localG[p].del();
}
delete [] localG;
}
// Translate vertex id to partition
int partition_of( intT vertex_id )
{
return partition.partition_of( vertex_id );
}
// Get a partition of the graph to operate on
graph<vertex> get_partition( int p )
{
return localG[p];
}
#if PULL_EDGES
const EdgeList<Edge> & get_edge_list_partition( intT p )
{
return localEdgeList[p];
}
#endif
int get_num_partitions() const
{
return partition.get_num_partitions();
}
// Get the partitioner object constructed for this graph
const partitioner & get_partitioner() const
{
return partition;
}
intT getFullOutDegree(intT v) const
{
return Ver[v].getOutDegree();
}
void transpose()
{
parallel_for(unsigned i=0; i<partition.get_num_partitions(); i++)
{
localG[i].transpose();
}
}
private:
// These functions are specific to the partitioned graph. No one needs
// to know about them, I think.
// graphFilter is polymer method, partitioend graph:
// Pull: each local vertex contains its own out-degree
// Push: each local vertex contains its own in-degree
// PartitionByDest :
// Pull: each local vertex contains its own in-degree
// Push each local vertex contains its own in-degree
// PartitionBySour:
// Pull: each local vertex contains its own out-degree
// Push: each local vertex contains its own out-degree
void partitionByDegree( wholeGraph<vertex> GA, int numOfNode, intT *sizeArr,
int sizeOfOneEle, bool useOutDegree=false );
void partitionApprox(wholeGraph<vertex> & GA, short * partitions, int np);
graph<vertex> graphFilter( wholeGraph<vertex>& GA, int rangeLow, int rangeHi ,int numanode);
graph<vertex> graphFilter( wholeGraph<vertex>& GA, short * partition, intT p );
graph<vertex> PartitionByDest(wholeGraph<vertex>& GA, int rangeLow, int rangeHi ,int numanode);
graph<vertex> PartitionBySour(wholeGraph<vertex>& GA, int rangeLow, int rangeHi ,int numanode);
};
// ======================================================================
// Graph Filtering (Graph Partitioned)
// ======================================================================
//This method is partitioned by outdegree, CSR, store the all out-degree for
//all local vertices of each partitions.
//And part indegree for whole vertices
// Place edge (u,v) in partition of u
template <class vertex>
graph<vertex> partitioned_graph<vertex>::PartitionBySour(wholeGraph<vertex> &GA, int rangeLow, int rangeHi,int numanode)
{
//vertex *V = GA.getV();
vertex *V = GA.V;
const intT n = GA.n;
bool isSymmetric = GA.isSymmetric;
intT *counters = new intT [n];
intT *offsets = new intT [n];
intT *inCounters = new intT [n];
intT *inOffsets = new intT [n];
{
parallel_for (intT i = 0; i < n; i++)
{
inCounters[i] = 0;
counters[i] = 0;
if(!isSymmetric)
{
intT d = V[i].getInDegree();
for (intT j = 0; j < d; j++)
{
intT ngh = V[i].getInNeighbor(j);
if (rangeLow <= ngh && ngh < rangeHi)
inCounters[i]++;
}
}
if(rangeLow<=i && i<rangeHi)
{
intT ind = V[i].getOutDegree();
counters[i] += ind;
}
}
}
intT totalSize = 0;
intT totalInSize = 0;
for (intT i = 0; i < n; i++)
{
offsets[i] = totalSize;
totalSize += counters[i];
if(!isSymmetric)
{
inOffsets[i] = totalInSize;
totalInSize += inCounters[i];
}
}
if(!isSymmetric)
assert( totalSize == totalInSize );
graph<vertex> FG(n, totalSize, isSymmetric, numanode);
FG.V = new vertex [n];
{
parallel_for (intT i = 0; i < n; i++)
{
FG.V[i].setOutDegree(counters[i]);
if(!isSymmetric)
FG.V[i].setInDegree(inCounters[i]);
}
}
delete [] counters;
delete [] inCounters;
intE *edges = FG.allocatedInplace;
intE *inEdges = FG.inEdges;//0;
{
parallel_for (intT i = 0; i < n; i++)
{
#ifndef WEIGHTED
intE *localInEdges = &inEdges[inOffsets[i]];
#else
intE *localInEdges = &inEdges[inOffsets[i]*2];
#endif
intT incounter = 0;
if(!isSymmetric)
{
intT d = V[i].getInDegree();
for (intT j = 0; j < d; j++)
{
#ifndef WEIGHTED
intT ngh = V[i].getInNeighbor(j);
#else
intT ngh = V[i].getInNeighbor(j);
intT wgh = V[i].getInWeight(j);
#endif
if (rangeLow <= ngh && ngh < rangeHi)
{
#ifndef WEIGHTED
localInEdges[incounter] = ngh;
#else
localInEdges[incounter*2]= ngh;
localInEdges[incounter*2+1]= wgh;
#endif