/
GossipMap.cpp
1884 lines (1388 loc) · 59.3 KB
/
GossipMap.cpp
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
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>
#include <string>
#include "malloc.h"
#include "google/malloc_extension.h"
#include <graphlab.hpp>
#include <graphlab/rpc/dht.hpp>
inline double pLogP(double pr) {
return (pr > 0.0 ? pr * log(pr) : 0.0);
}
void findAssignedPart(int* start, int* end, int numAll, int numProc, int myID);
// Global variables...
double RESET_PROB = 0.15; // random reset probability.
double TOLERANCE = 1.0E-15;
double THRESHOLD = 1.0E-3;
double TOTALWEIGHT = 1.0; // Should be updated after reading graph.
long N_NODE = 1; // Should be updated after reading graph. number of nodes.
double SUM_DANGLING_SIZE = 1.0;
double SUM_ALL_SIZE = 1.0; // sum of all size before normalize, and will be used for normalize node-size.
int MAX_ITER = 10; // maximum iteration for graph findCommunity vertex program.
int MAX_SP_ITER = 3; // maximum iteration for sp-graph findCommunity vertex program.
int MAX_ITER_PGRANK = 200; // maximum iteration for calculating ergodic state (pagerank) of vertices.
int INTERVAL = 3; // time interval for checking whether movement message valid or not.
int nextEmptyModID = 100000000; // temporary emptyMod target ID.
// This will be a container of the information of each module.
// This ModuleInfo will be stored in ditributed hash table (dht),
// so the index will be the key of dht.
struct ModuleInfo : public graphlab::IS_POD_TYPE {
int index; // Index number of this module.
// Index will be a key of dht for accessing corresponding ModuleInfo object.
double exitPr; // exit probability of this module.
double stayPr; // stay probability of this module, which is sum of p_alpha + exit probability.
double sumPr; // sum of p_alpha. alpha is in this module i.
double sumTPWeight; // sum of teleport weight. SUM (tau_a).
double sumDangling; // sum of dangling nodes weight.
int numMembers; // number of members of this Module.
double modOutFlow; // outFlow from the module. == sum of outFlow to other modules from members.
//std::vector<int> members; // This vector will keep the id() of this module's members.
ModuleInfo(): index(1), exitPr(0.0), stayPr(0.0), sumPr(0.0), sumTPWeight(0.0), sumDangling(0.0), numMembers(0), modOutFlow(0.0) {}
explicit ModuleInfo(int idx, double exitProb, double sumProb, double sumTPW, double sumDang, int numMem):
index(idx), exitPr(exitProb), sumPr(sumProb), stayPr(exitProb + sumProb),
sumTPWeight(sumTPW), sumDangling(sumDang), numMembers(numMem), modOutFlow(0.0) {}
};
struct ModuleInfos {
std::map<int, ModuleInfo> modInfoHash;
void save(graphlab::oarchive& oarc) const {
oarc << modInfoHash;
}
void load(graphlab::iarchive& iarc) {
iarc >> modInfoHash;
}
ModuleInfos& operator+=(const ModuleInfos& other) {
for (std::map<int, ModuleInfo>::const_iterator it = other.modInfoHash.begin(); it != other.modInfoHash.end(); it++) {
//if (modInfoHash.count(it->first) > 0) {
if (modInfoHash.find(it->first) != modInfoHash.end()) {
modInfoHash[it->first].sumPr += it->second.sumPr;
modInfoHash[it->first].sumTPWeight += it->second.sumTPWeight;
modInfoHash[it->first].sumDangling += it->second.sumDangling;
modInfoHash[it->first].numMembers += it->second.numMembers;
modInfoHash[it->first].exitPr += it->second.exitPr;
modInfoHash[it->first].stayPr += it->second.stayPr;
modInfoHash[it->first].modOutFlow += it->second.modOutFlow;
}
else {
ModuleInfo mInfo;
mInfo.index = it->second.index;
mInfo.sumPr = it->second.sumPr;
mInfo.sumTPWeight = it->second.sumTPWeight;
mInfo.sumDangling = it->second.sumDangling;
mInfo.numMembers = it->second.numMembers;
mInfo.exitPr += it->second.exitPr;
mInfo.stayPr += it->second.stayPr;
mInfo.modOutFlow += it->second.modOutFlow;
modInfoHash[it->first] = mInfo;
}
}
return *this;
}
};
ModuleInfos* modInfoPtr;
class distributed_ModInfos {
private:
graphlab::dc_dist_object<distributed_ModInfos> rmi; // Local RMI object.
size_t nProcs;
size_t myID;
graphlab::mutex lock;
public:
ModuleInfos localModInfos; // local storage for EdgeMap.
distributed_ModInfos(graphlab::distributed_control& dc) : rmi(dc, this) {
nProcs = rmi.dc().numprocs();
myID = rmi.dc().procid();
rmi.barrier(); // make sure all machines finish constructing this object.
}
void combineModInfos(size_t procTo, ModuleInfos other) {
if (procTo == myID) {
lock.lock();
localModInfos += other;
lock.unlock();
}
else {
// this function will call remote_call to the procTo.
rmi.remote_call(procTo, &distributed_ModInfos::combineModInfos, procTo, other);
}
}
// Must be called by all machine simultaneously.
void clearLocalModInfos() {
rmi.barrier();
//std::map<int, ModuleInfo>().swap(localModInfos.modInfoHash);
localModInfos.modInfoHash.clear();
rmi.barrier();
}
};
/*
* Module State struct, which is based on gossiping protocol.
*/
struct ModState : public graphlab::IS_POD_TYPE {
int modID;
int numMembers; // the number of members in this module.
double modPr; // sum of pagerank of member vertices.
double sumTPWeight; // sum of teleport weight of member vertices.
double sumDangSize; // sum of pagerank of dangling vertices in the module.
double modOutFlow; // out-flow from this module to other modules.
//double inModFlow; // in-flow from other modules to this module.
ModState(): modID(1), numMembers(1), modPr(0.0), sumTPWeight(0.0), sumDangSize(0.0),
modOutFlow(0.0) {}
//explicit ModState(int id, int nMem, double sumPr, double sumTPW, double sumDang, double outF, double inF):
explicit ModState(int id, int nMem, double sumPr, double sumTPW, double sumDang, double outF):
modID(id), numMembers(nMem), modPr(sumPr), sumTPWeight(sumTPW), sumDangSize(sumDang),
modOutFlow(outF) {}
explicit ModState(const ModuleInfo modInfo) {
modID = modInfo.index;
numMembers = modInfo.numMembers;
modPr = modInfo.sumPr;
sumTPWeight = modInfo.sumTPWeight;
sumDangSize = modInfo.sumDangling;
modOutFlow = modInfo.modOutFlow;
}
// This will be called during the gather phase of findCommunity vertex-program
// to sum ModState values for averaging them.
ModState& operator+=(const ModState& other) {
//this->numMembers += other.numMembers;
//this->modPr += other.modPr;
//this->sumTPWeight += other.sumTPWeight;
//this->sumDangSize += other.sumDangSize;
//this->modOutFlow += other.modOutFlow;
if (this->modPr < other.modPr) {
this->numMembers = other.numMembers;
this->modPr = other.modPr;
this->sumTPWeight = other.sumTPWeight;
this->sumDangSize = other.sumDangSize;
this->modOutFlow = other.modOutFlow;
}
return *this;
}
};
/*
* move message struct, for a vertex (v) from srcMod to dstMod.
* NOTE that the move-unit could be superNode later..
*/
struct MoveMsg : public graphlab::IS_POD_TYPE {
int vid; // vertex id.
int atIter; // the iteration number at the movement of the vertex. (can be used as a timestamp!)
int srcMod; // source mod ID.
int dstMod; // destination mod ID
int nVertices; // the number of vertices in this movement.
double ndSize; // the size (pagerank) of the moved vertex.
double ndTPWeight; // the teleport weight of the vertex. (Usually, it is propotional to the number of vertices in this move-unit.
double ndDangSize; // the size of dangling vertex.
double deltaSrcOutFlow; // the change of outflow of the srcMod.
double deltaDstOutFlow; // the change of outflow of the dstMod.
MoveMsg(): vid(1), srcMod(1), dstMod(1), atIter(1), nVertices(1),
ndSize(1.0), ndTPWeight(1.0), ndDangSize(0.0), deltaSrcOutFlow(0.0), deltaDstOutFlow(0.0) {}
explicit MoveMsg(int id, int src, int dst, int iter, int nv, double size, double tpw, double dSize, double dSrc, double dDst):
vid(id), srcMod(src), dstMod(dst), atIter(iter), nVertices(nv),
ndSize(size), ndTPWeight(tpw), ndDangSize(dSize), deltaSrcOutFlow(dSrc), deltaDstOutFlow(dDst) {}
};
typedef std::pair<int, int> msg_key; // msg_key contains (vid, iter).
typedef std::map<msg_key, MoveMsg> message_map; // this will be the storage of the messages received from neighbors in node_data.
/*
* This will be actual message which is sent by signaling,
* so this should implement +operator function, for adding all messages since last run.
* Simply, we will have a message_map as a container for combined MoveMsg objects.
*/
struct Messages {
message_map msgs;
void save(graphlab::oarchive& oarc) const {
oarc << msgs;
}
void load(graphlab::iarchive& iarc) {
iarc >> msgs;
}
// Addition of two Messages objects.
Messages& operator+=(const Messages& other) {
// add ONLY MoveMsg objects which are not in msgs container.
for (message_map::const_iterator it = other.msgs.begin(); it != other.msgs.end(); it++) {
if (msgs.find(it->first) == msgs.end())
msgs.insert((*it));
}
return *this;
}
};
// storage for the msg_keys which represent the messages already seen.
typedef std::set<msg_key> received_keys; // first : vID, second: iter.
// Define vertex data structure.
struct node_data {
double size; // p_alpha.
int nMembers; // number of members in this node, which is for sp-node case. otherwise, 1.
double exitPr; // exitPr for superNode or node...
double ndOutFlow; // sum of out-flow of this node...
double nodeWeight; // node weight for non-uniform teleport weight..
int modIdx; // module index number.
int prevModId; // previous module id. It will be used for rolling back to the previous module assignment.
bool isSuper; // true: if it is superNode (it is a group of nodes in the current level)
// false: otherwise
double teleportWeight; // teleportWeight = normalized nodeWeight (nodeWeight/totalNodeWeight)
double danglingSize; // danglingSize will be equivalent to sumDangliing of corresponding module.
bool isDangling; // true: if it is a dangling node, false: otherwise.
int pgIter; // iteration counter for pagerank calculation.
int iter; // iteration counter for finding community vertex program.
ModState myModStat; // this will be updated based on received messages.
void save(graphlab::oarchive& oarc) const {
oarc << size << nMembers << exitPr << ndOutFlow << nodeWeight << modIdx << prevModId << isSuper << teleportWeight << danglingSize << isDangling << pgIter << iter << myModStat;
}
void load(graphlab::iarchive& iarc) {
iarc >> size >> nMembers >> exitPr >> ndOutFlow >> nodeWeight >> modIdx >> prevModId >> isSuper >> teleportWeight >> danglingSize >> isDangling >> pgIter >> iter >> myModStat;
}
node_data(): nodeWeight(1.0), nMembers(1), isSuper(false), danglingSize(0.0), isDangling(false), pgIter(0), iter(0) {}
explicit node_data(double ndWeight) : nodeWeight(ndWeight), nMembers(1), isSuper(false), danglingSize(0.0), isDangling(false), pgIter(0), iter(0) {}
explicit node_data(const ModuleInfo& modInfo) {
size = modInfo.sumPr;
nMembers = modInfo.numMembers;
ndOutFlow = modInfo.modOutFlow;
exitPr = modInfo.exitPr;
nodeWeight = modInfo.sumTPWeight;
modIdx = modInfo.index;
prevModId = modIdx;
isSuper = true;
teleportWeight = modInfo.sumTPWeight;
danglingSize = modInfo.sumDangling;
isDangling = size == danglingSize ? true : false;
pgIter = iter = 0;
myModStat = ModState(modInfo);
}
};
typedef std::map<int, double> flowmap; // <modID, flow>
typedef std::map<int, double> edgeFlow; // <vID, edgeFlow>
// Define flow_type data structure as the gather_type of the core-algorithm...
struct flow_type {
flowmap outFlowToMod; // outFlowToMod[modID] = outflow
flowmap inFlowFromMod; // inFlowFromMod[modID] = inflow
std::map<int, ModState> neighborModStates; // <modID, ModState>
std::map<int, int> numModOccurs; // <modID, #occurrence>
flow_type() {}
void save(graphlab::oarchive& oarc) const {
oarc << outFlowToMod << inFlowFromMod << neighborModStates << numModOccurs;
}
void load(graphlab::iarchive& iarc) {
iarc >> outFlowToMod >> inFlowFromMod >> neighborModStates >> numModOccurs;
}
flow_type& operator+=(const flow_type& other) {
for (flowmap::const_iterator it = other.outFlowToMod.begin(); it != other.outFlowToMod.end(); it++) {
if(outFlowToMod.find(it->first) != outFlowToMod.end()) {
outFlowToMod[it->first] += it->second;
}
else {
outFlowToMod[it->first] = it->second;
}
}
for (flowmap::const_iterator it = other.inFlowFromMod.begin(); it != other.inFlowFromMod.end(); it++) {
if(inFlowFromMod.find(it->first) != inFlowFromMod.end()) {
inFlowFromMod[it->first] += it->second;
}
else {
inFlowFromMod[it->first] = it->second;
}
}
for (std::map<int, ModState>::const_iterator it = other.neighborModStates.begin(); it != other.neighborModStates.end(); it++) {
if (neighborModStates.find(it->first) != neighborModStates.end()) {
neighborModStates[it->first] += it->second;
numModOccurs[it->first] += other.numModOccurs.find(it->first)->second;
}
else {
neighborModStates[it->first] = it->second;
numModOccurs[it->first] = other.numModOccurs.find(it->first)->second;
}
}
return *this;
}
};
typedef node_data vertex_data_type;
typedef double edge_data_type; // edge-weight is an edge data (double).
// the graph type is determined by the vertex and edge data types.
typedef graphlab::distributed_graph<vertex_data_type, edge_data_type> graph_type;
struct OutFlows {
std::map<int, double> outFlow;
void save(graphlab::oarchive& oarc) const {
oarc << outFlow;
}
void load(graphlab::iarchive& iarc) {
iarc >> outFlow;
}
void addEdge(const graph_type::edge_type& e) {
int src = e.source().data().modIdx;
int dst = e.target().data().modIdx;
if (src != dst) {
if (outFlow.find(src) != outFlow.end())
outFlow[src] += e.data();
else
outFlow[src] = e.data();
if (outFlow.find(dst) == outFlow.end())
outFlow[dst] = 0.0;
}
else {
if (outFlow.find(src) == outFlow.end())
outFlow[src] = 0.0;
}
}
OutFlows& operator+=(const OutFlows& other) {
for (std::map<int, double>::const_iterator it = other.outFlow.begin(); it != other.outFlow.end(); it++) {
if (outFlow.find(it->first) != outFlow.end()) {
outFlow[it->first] += it->second;
}
else {
outFlow[it->first] = it->second;
}
}
return *this;
}
};
class distributed_OutFlows {
private:
graphlab::dc_dist_object<distributed_OutFlows> rmi; // Local RMI object.
size_t nProcs;
size_t myID;
graphlab::mutex lock;
public:
OutFlows localOutFlows; // local storage for EdgeMap.
distributed_OutFlows(graphlab::distributed_control& dc) : rmi(dc, this) {
nProcs = rmi.dc().numprocs();
myID = rmi.dc().procid();
rmi.barrier(); // make sure all machines finish constructing this object.
}
void combineOutFlows(size_t procTo, OutFlows other) {
if (procTo == myID) {
lock.lock();
localOutFlows += other;
lock.unlock();
}
else {
// this function will call remote_call to the procTo.
rmi.remote_call(procTo, &distributed_OutFlows::combineOutFlows, procTo, other);
}
}
// Must be called by all machine simultaneously.
void clearLocalOutFlowsMap() {
rmi.barrier();
//std::map<int, double>().swap(localOutFlows.outFlow);
localOutFlows.outFlow.clear();
rmi.barrier();
}
};
/*
* A simple function used by graph.transform_vertices(init_vertex);
* to initialize the vertex data.
*/
void init_vertex(graph_type::vertex_type& v) {
v.data().modIdx = (int) v.id();
v.data().size = 1.0 / N_NODE;
v.data().teleportWeight = v.data().nodeWeight / TOTALWEIGHT;
if (v.num_out_edges() == 0) {
v.data().isDangling = true;
v.data().danglingSize = v.data().size;
}
}
void reset_modInfo(graph_type::vertex_type& v) {
v.data().modIdx = (int) v.id();
v.data().iter = 0;
v.data().myModStat = ModState((int)v.id(), v.data().nMembers, v.data().size, v.data().teleportWeight, v.data().danglingSize, v.data().ndOutFlow);
}
bool is_dangling(const graph_type::vertex_type& v) {
return v.data().isDangling;
}
// This map-reduce function called for only dangling-vertex set.
double dangling_vertex_size(const graph_type::vertex_type& vertex) {
//double dSize = 0.0;
//if (vertex.data().isDangling)
// dSize = vertex.data().size;
//return dSize;
return vertex.data().size; // This is the case when restricted the dangling-vertex set.
}
double sum_size(const graph_type::vertex_type& vertex) {
return vertex.data().size;
}
void normalize_vertex_size(graph_type::vertex_type& vertex) {
vertex.data().size /= SUM_ALL_SIZE;
}
// This should be updated for general cases, but for SNAP dataset, this will work correctly.
void normalize_edge_value(graph_type::edge_type& e) {
e.data() = 1.0 / e.source().num_out_edges(); // e.weight / sumEdgeWeights ....
}
void update_edge_flow(graph_type::edge_type& edge) {
edge.data() *= edge.source().data().size; // p_a * e.weight <-- for faster running...
}
/********************
* map-functions for the related map-reduce functions...
********************/
double sum_plogp_vertex(const graph_type::vertex_type& vertex) {
return pLogP(vertex.data().size);
}
double sumExitPr(const graph_type::vertex_type& v) {
return v.data().exitPr;
}
double sum_exitLogExit(const graph_type::vertex_type& v) {
return pLogP(v.data().exitPr);
}
double sum_stayLogStay(const graph_type::vertex_type& v) {
return pLogP(v.data().exitPr + v.data().size);
}
void addVertexToModInfos(ModuleInfos& mInfos, const graph_type::vertex_type& v) {
int modIdx = v.data().modIdx;
if (mInfos.modInfoHash.find(modIdx) != mInfos.modInfoHash.end()) {
ModuleInfo& mInfoRef = mInfos.modInfoHash[modIdx];
mInfoRef.sumPr += v.data().size;
mInfoRef.sumTPWeight += v.data().teleportWeight;
mInfoRef.sumDangling += v.data().danglingSize;
mInfoRef.numMembers += v.data().nMembers;
}
else {
ModuleInfo mInfo;
mInfo.sumPr = v.data().size;
mInfo.index = v.data().modIdx;
mInfo.sumTPWeight = v.data().teleportWeight;
mInfo.sumDangling = v.data().danglingSize;
mInfo.numMembers = v.data().nMembers;
mInfos.modInfoHash[modIdx] = mInfo;
}
}
// update mod-state after converged.
void updateModState(graph_type::vertex_type& v) {
ModState& myModRef = v.data().myModStat;
int mid = v.data().modIdx;
v.data().prevModId = v.data().modIdx;
myModRef.modID = mid;
myModRef.numMembers = modInfoPtr->modInfoHash[mid].numMembers;
myModRef.modPr = modInfoPtr->modInfoHash[mid].sumPr;
myModRef.sumTPWeight = modInfoPtr->modInfoHash[mid].sumTPWeight;
myModRef.sumDangSize = modInfoPtr->modInfoHash[mid].sumDangling;
myModRef.modOutFlow = modInfoPtr->modInfoHash[mid].modOutFlow;
v.data().iter = 0; // iteration number should be reset for running next loop.
}
// roll-back module assignment when MDL is increased...
void rollBackModID(graph_type::vertex_type& v) {
v.data().modIdx = v.data().prevModId;
}
struct ModIDs {
std::map<int, int> modIDs; // <vid, mod_id>
void save(graphlab::oarchive& oarc) const {
oarc << modIDs;
}
void load(graphlab::iarchive& iarc) {
iarc >> modIDs;
}
ModIDs& operator+=(const ModIDs other) {
for (std::map<int, int>::const_iterator it = other.modIDs.begin(); it != other.modIDs.end(); it++) {
// This would concatenate two different maps of <vid, mod_id>s, and each vid will appear only once,
// so don't need to check existence test.
modIDs.insert((*it));
}
return *this;
}
};
ModIDs* modAssignPtr; // used for hold global pointer for ModIDs to use within transform_vertex() functor.
ModIDs aggregateModAssign(const graph_type::vertex_type& v) {
ModIDs mID;
mID.modIDs[(int)v.id()] = v.data().modIdx;
return mID;
}
void updateModAssign(graph_type::vertex_type& v) {
// The previous modIdx is a vertex id of corresponding sp_node.
int sp_id = v.data().modIdx;
v.data().modIdx = modAssignPtr->modIDs[sp_id];
}
typedef std::map<std::pair<int, int>, edge_data_type> edgeList; //map<<src, dst>, edgeflow>
struct EdgeMap {
edgeList eList; //edge list.
void save(graphlab::oarchive& oarc) const {
oarc << eList;
}
void load(graphlab::iarchive& iarc) {
iarc >> eList;
}
void add(const graph_type::local_edge_type& e) {
int src = e.source().data().modIdx;
int dst = e.target().data().modIdx;
if (src != dst) {
if (eList.find(std::make_pair(src, dst)) != eList.end()) {
eList[std::make_pair(src, dst)] += e.data();
}
else {
eList[std::make_pair(src, dst)] = e.data();
}
}
}
EdgeMap& operator+=(const EdgeMap& other) {
for (edgeList::const_iterator it = other.eList.begin(); it != other.eList.end(); it++) {
if (eList.find(it->first) != eList.end()) {
eList[it->first] += it->second;
}
else {
eList[it->first] = it->second;
}
}
return *this;
}
};
class global_EdgeMap {
private:
graphlab::dc_dist_object<global_EdgeMap> rmi; // Local RMI object.
size_t nProcs;
size_t myID;
graphlab::mutex lock;
public:
EdgeMap localEdgeMap; // local storage for EdgeMap.
global_EdgeMap(graphlab::distributed_control& dc) : rmi(dc, this) {
nProcs = rmi.dc().numprocs();
myID = rmi.dc().procid();
rmi.barrier(); // make sure all machines finish constructing this object.
}
void combineEdgeMap(size_t procTo, EdgeMap other) {
if (procTo == myID) {
lock.lock();
localEdgeMap += other;
lock.unlock();
}
else {
// this function will call remote_call to the procTo.
rmi.remote_call(procTo, &global_EdgeMap::combineEdgeMap, procTo, other);
}
}
// Must be called by all machine simultaneously.
void clearLocalEdgeMap() {
rmi.barrier();
//edgeList().swap(localEdgeMap.eList);
localEdgeMap.eList.clear();
rmi.barrier();
}
};
// This is the function of calculating ergodic node size (probability).
// The ergodic node probability is practically the same as the normalized PageRank value of each node.
class calculateErgodicNodeSize :
public graphlab::ivertex_program<graph_type, double>,
public graphlab::IS_POD_TYPE {
//double sumDanglingSize = 0.0;
double last_size;
public:
// gather: we are going to gather information from in-edges
edge_dir_type gather_edges(icontext_type& context, const vertex_type& vertex) const {
return graphlab::IN_EDGES;
}
// Gather the weighted rank of the in-edge adjacent page .
double gather(icontext_type& context, const vertex_type& vertex, edge_type& edge) const {
return edge.source().data().size * edge.data();
}
// Use the total rank of the adjacent pages from gather() phase to update this page.
void apply(icontext_type& context, vertex_type& vertex, const gather_type& total) {
double newval = (1.0 - RESET_PROB) * total;
newval += (RESET_PROB + (1.0 - RESET_PROB) * SUM_DANGLING_SIZE) * vertex.data().teleportWeight;
last_size = vertex.data().size;
vertex.data().size = newval;
vertex.data().pgIter++;
}
// The scatter edges depends on the last_change value..
edge_dir_type scatter_edges(icontext_type& context, const vertex_type& vertex) const {
//if (std::fabs(last_size - vertex.data().size) > TOLERANCE && context.iteration() < MAX_ITER_PGRANK)
if (std::fabs(last_size - vertex.data().size) > TOLERANCE && vertex.data().pgIter < MAX_ITER_PGRANK)
return graphlab::OUT_EDGES;
else
return graphlab::NO_EDGES;
}
// the scatter function just signal adjacent pages.
void scatter(icontext_type& context, const vertex_type& vertex, edge_type& edge) const {
context.signal(edge.target());
}
};
// This vertex program calculates initial exit-probability of each vertex,
// after calculating the ergodic probability of each vertex.
class update_exitPr :
public graphlab::ivertex_program<graph_type, double>,
public graphlab::IS_POD_TYPE {
public:
edge_dir_type gather_edges(icontext_type& context, const vertex_type& vertex) const {
return graphlab::OUT_EDGES;
}
double gather(icontext_type& context, const vertex_type& vertex, edge_type& edge) const {
return edge.data();
}
void apply(icontext_type& context, vertex_type& v, const gather_type& sumExitFlow) {
if (!v.data().isDangling) {
v.data().exitPr = RESET_PROB * (1.0 - v.data().teleportWeight) * v.data().size
+ (1.0 - RESET_PROB) * sumExitFlow;
v.data().ndOutFlow = sumExitFlow;
}
else {
v.data().exitPr = (1.0 - v.data().teleportWeight) * v.data().size;
v.data().danglingSize = v.data().size;
v.data().ndOutFlow = 0.0; // dangling vertex, no direct out-flow.
}
// initialize myModStat in v.data()
//ModState(modID, nMember,sumPr,sumTPW, sumDang, outF);
v.data().myModStat = ModState((int)v.id(), 1, v.data().size, v.data().teleportWeight, v.data().danglingSize, v.data().ndOutFlow);
}
edge_dir_type scatter_edges(icontext_type& context, const vertex_type& vertex) const {
return graphlab::NO_EDGES;
}
void scatter(icontext_type& context, const vertex_type& vertex, edge_type& edge) const {
}
};
/*
* This is the vertex-program for the core algorithm of GossipMap.
*/
class findCommunity :
public graphlab::ivertex_program<graph_type, flow_type, Messages> {
bool moved; // if this vertex moved, it will set to true. Otherwise, false.
Messages myMessages; // This will contain messages sent to neighbors.
Messages oneMsg; // This will contain only the message about current movement.
int myOldMod;
MoveMsg myMoveMsg;
msg_key mykey;
public:
void save(graphlab::oarchive& oarc) const {
oarc << moved << myMessages << myOldMod << myMoveMsg << mykey;
}
void load(graphlab::iarchive& iarc) {
iarc >> moved >> myMessages >> myOldMod >> myMoveMsg >> mykey;
}
// init() : will receive messages from signaling neighbors, and concatenating them.
void init(icontext_type& context, const vertex_type& v, const message_type& msg) {
//message_map().swap(myMessages.msgs); // initialize myMessages container.
myMessages.msgs.clear();
if (v.data().iter >= (v.data().isSuper ? MAX_SP_ITER : MAX_ITER))
return;
// check the message was already seen or not.
for (message_map::const_iterator it = msg.msgs.begin(); it != msg.msgs.end(); it++) {
msg_key key = it->first;
MoveMsg mvMsg = it->second;
// first look at the message is related to myMod and is not from vertex itself.
if ((mvMsg.srcMod == v.data().modIdx || mvMsg.dstMod == v.data().modIdx) && key.first != v.id() && (mvMsg.atIter + INTERVAL > v.data().iter)) {
myMessages.msgs[key] = mvMsg;
}
}
}
edge_dir_type gather_edges(icontext_type& context, const vertex_type& vertex) const {
return (vertex.data().iter >= (vertex.data().isSuper? MAX_SP_ITER : MAX_ITER)) ? graphlab::NO_EDGES : graphlab::ALL_EDGES;
}
flow_type gather(icontext_type& context, const vertex_type& v, edge_type& edge) const {
// Calculate flow and return it. If edge == OUT_EDGE, then it will be out-flow. Otherwise, in-flow.
flow_type flow;
if( edge.source().id() == v.id() ) {
// OUT_EDGE case
int modID = edge.target().data().modIdx;
flow.outFlowToMod[modID] = (1.0 - RESET_PROB) * edge.data();
flow.inFlowFromMod[modID] = 0.0; // since the edge related to out-flow.
ModState& nghbrStateRef = edge.target().data().myModStat;
ModState cpState;
cpState.modID = modID;
cpState.numMembers = nghbrStateRef.numMembers;
cpState.modPr = nghbrStateRef.modPr;
cpState.sumTPWeight = nghbrStateRef.sumTPWeight;
cpState.sumDangSize = nghbrStateRef.sumDangSize;
cpState.modOutFlow = nghbrStateRef.modOutFlow;
flow.neighborModStates[modID] = cpState;
flow.numModOccurs[modID] = 1;
}
else {
// IN_EDGE case
int modID = edge.source().data().modIdx;
flow.inFlowFromMod[modID] = (1.0 - RESET_PROB) * edge.data();
flow.outFlowToMod[modID] = 0.0; // since the edge related to in-flow.
ModState& nghbrStateRef = edge.source().data().myModStat;
ModState cpState;
cpState.modID = modID;
cpState.numMembers = nghbrStateRef.numMembers;
cpState.modPr = nghbrStateRef.modPr;
cpState.sumTPWeight = nghbrStateRef.sumTPWeight;
cpState.sumDangSize = nghbrStateRef.sumDangSize;
cpState.modOutFlow = nghbrStateRef.modOutFlow;
flow.neighborModStates[modID] = cpState;
flow.numModOccurs[modID] = 1;
}
return flow;
}
// Based on in-/out-flow information, and other module information,
// this method will find a new module which could improve the quality of communities.
void apply(icontext_type& context, vertex_type& v, const gather_type& inOutFlows) {
moved = false; // Initially set as false, and it will be set 'true' if this vertex moves.
if (v.data().iter >= (v.data().isSuper? MAX_SP_ITER : MAX_ITER))
return;
flowmap outFlowToMod = inOutFlows.outFlowToMod; // copy from gather result.
flowmap inFlowFromMod = inOutFlows.inFlowFromMod; // copy from gather result.
//flowmap moduleFlow = inOutFlows.modFlow;
// 1st strategy: average neighbor ModStates
std::map<int, ModState> nghbrModStates = inOutFlows.neighborModStates;
std::map<int, int> numOccurs = inOutFlows.numModOccurs;
const int emptyTarget = context.num_vertices() + 1; // This will be an indicator of moving to emptyModule.
////////////////////////////////////////////////
// update messages from signaling neighbors. //
////////////////////////////////////////////////
ModState& myState = v.data().myModStat;
for (message_map::iterator it = myMessages.msgs.begin(); it != myMessages.msgs.end(); it++) {
//case 1 - myMod = it->srcMod : removing the vertex from myModStat.
if ((it->second).srcMod == myState.modID) {
myState.numMembers -= (it->second).nVertices;
myState.modPr -= (it->second).ndSize;
myState.sumTPWeight -= (it->second).ndTPWeight;
myState.sumDangSize -= (it->second).ndDangSize;
myState.modOutFlow += (it->second).deltaSrcOutFlow;
}
else if ((it->second).dstMod == myState.modID) {
myState.numMembers += (it->second).nVertices;
myState.modPr += (it->second).ndSize;
myState.sumTPWeight += (it->second).ndTPWeight;
myState.sumDangSize += (it->second).ndDangSize;
myState.modOutFlow += (it->second).deltaDstOutFlow;
}
}
// copy node specific values
double ndSize = v.data().size;
double ndExitPr = v.data().exitPr;
double ndTPWeight = v.data().teleportWeight;
double ndDanglingSize = v.data().danglingSize;
int srcMod = v.data().modIdx;
double srcModFlow = (outFlowToMod.find(srcMod) != outFlowToMod.end()) ? outFlowToMod[srcMod] + inFlowFromMod[srcMod] : 0.0;
// add information of the current vertex 'v' here.
if (nghbrModStates.find(srcMod) != nghbrModStates.end()) {
nghbrModStates[srcMod] += v.data().myModStat;
numOccurs[srcMod]++;
}
else {
nghbrModStates[srcMod] = v.data().myModStat;
numOccurs[srcMod] = 1;
}
ModState& srcModState = nghbrModStates[srcMod];
//if (numOccurs[srcMod] > 1) {
// int nOcc = numOccurs[srcMod];
// srcModState.numMembers /= nOcc;
// srcModState.modPr /= nOcc;
// srcModState.sumTPWeight /= nOcc;
// srcModState.sumDangSize /= nOcc;
// srcModState.modOutFlow /= nOcc;
//}
// update myModState w.r.t. neighbors info..
myState.numMembers = srcModState.numMembers;
myState.modPr = srcModState.modPr;
myState.sumTPWeight = srcModState.sumTPWeight;
myState.sumDangSize = srcModState.sumDangSize;
myState.modOutFlow = srcModState.modOutFlow;
double alpha = RESET_PROB;
double beta = 1.0 - RESET_PROB;
// add approximated teleportation flow for srcModule...
// if srcModFlow == 0, it means that no neighbors are in the same module.
// And we assume that is the case of only one vertex is in the srcMod --> teleportation flow == 0.0.
if (srcModFlow > 0) {
srcModFlow += (alpha * ndSize + beta * ndDanglingSize) * (srcModState.sumTPWeight - ndTPWeight);
srcModFlow += (alpha * (srcModState.modPr - ndSize) + beta * (srcModState.sumDangSize - ndDanglingSize)) * ndTPWeight;
}
/////////// Calculate oldExitPr w.r.t. neighbors' information ////////////
double oldExitPrSrcMod = ndExitPr;
if (srcModState.numMembers > 1) {
oldExitPrSrcMod = alpha * (1.0 - srcModState.sumTPWeight) * srcModState.modPr
+ beta * (srcModState.modOutFlow + (1.0 - srcModState.sumTPWeight) * srcModState.sumDangSize);