forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkPBGLDistributedGraphHelper.cxx
1099 lines (991 loc) · 39.7 KB
/
vtkPBGLDistributedGraphHelper.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPBGLDistributedGraphHelper.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*
* Copyright (C) 2008 The Trustees of Indiana University.
* Use, modification and distribution is subject to the Boost Software
* License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt)
*/
#include "vtkPBGLDistributedGraphHelper.h"
#include "assert.h"
#include "vtkGraph.h"
#include "vtkGraphInternals.h"
#include "vtkInformation.h"
#include "vtkObjectFactory.h"
#include "vtkPBGLGraphAdapter.h"
#include "vtkDataSetAttributes.h"
#include "vtkVariantArray.h"
#include "vtkVariantBoostSerialization.h"
#include "vtkDataArray.h"
#include "vtkStringArray.h"
#include <boost/graph/distributed/mpi_process_group.hpp>
#include <boost/bind.hpp>
#include <utility>
//----------------------------------------------------------------------------
// private class vtkPBGLDistributedGraphHelperMessageTypes
//----------------------------------------------------------------------------
class vtkPBGLDistributedGraphHelperMessageTypes
{
public:
//----------------------------------------------------------------------------
// Edge message bundle class for Id:Id edge types
//----------------------------------------------------------------------------
class EdgeIIMessageBundle
{
public:
EdgeIIMessageBundle() { uDistributedId=0; vDistributedId=0; propertyArr=NULL; }
EdgeIIMessageBundle(vtkIdType u, vtkIdType v, vtkVariantArray* prop=NULL)
: uDistributedId(u), vDistributedId(v), propertyArr(prop)
{ };
~EdgeIIMessageBundle() {};
vtkIdType uDistributedId;
vtkIdType vDistributedId;
vtkVariantArray * propertyArr;
private:
friend class boost::serialization::access;
template<typename Archiver>
void serialize(Archiver& ar, const unsigned int version)
{
ar & uDistributedId & vDistributedId & propertyArr;
}
};
//----------------------------------------------------------------------------
// Edge message bundle class for Id:Name edge types
//----------------------------------------------------------------------------
class EdgeINMessageBundle
{
public:
EdgeINMessageBundle() { uDistributedId=0; propertyArr=NULL; }
EdgeINMessageBundle(vtkIdType u, const vtkVariant& v, vtkVariantArray* prop=NULL)
: uDistributedId(u), vPedigreeId(v), propertyArr(prop)
{ };
~EdgeINMessageBundle() {};
vtkIdType uDistributedId;
vtkVariant vPedigreeId;
vtkVariantArray * propertyArr;
private:
friend class boost::serialization::access;
template<typename Archiver>
void serialize(Archiver& ar, const unsigned int version)
{
ar & uDistributedId & vPedigreeId & propertyArr;
}
};
//----------------------------------------------------------------------------
// Edge message bundle class for Name:Id edge types
//----------------------------------------------------------------------------
class EdgeNIMessageBundle
{
public:
EdgeNIMessageBundle() { vDistributedId=0; propertyArr=NULL; }
EdgeNIMessageBundle(const vtkVariant& u, vtkIdType v, vtkVariantArray* prop=NULL)
: uPedigreeId(u), vDistributedId(v), propertyArr(prop)
{ };
~EdgeNIMessageBundle() {};
vtkVariant uPedigreeId;
vtkIdType vDistributedId;
vtkVariantArray * propertyArr;
private:
friend class boost::serialization::access;
template<typename Archiver>
void serialize(Archiver& ar, const unsigned int version)
{
ar & uPedigreeId & vDistributedId & propertyArr;
}
};
//----------------------------------------------------------------------------
// Edge message bundle class for Name:Name edge types
//----------------------------------------------------------------------------
class EdgeNNMessageBundle
{
public:
EdgeNNMessageBundle() { propertyArr=NULL; }
EdgeNNMessageBundle(const vtkVariant& u, const vtkVariant& v, vtkVariantArray* prop=NULL)
: uPedigreeId(u), vPedigreeId(v), propertyArr(prop)
{ };
~EdgeNNMessageBundle() {};
vtkVariant uPedigreeId;
vtkVariant vPedigreeId;
vtkVariantArray * propertyArr;
private:
friend class boost::serialization::access;
template<typename Archiver>
void serialize(Archiver& ar, const unsigned int version)
{
ar & uPedigreeId & vPedigreeId & propertyArr;
}
};
};
typedef vtkPBGLDistributedGraphHelperMessageTypes::EdgeIIMessageBundle EdgeIIMessageBundle;
typedef vtkPBGLDistributedGraphHelperMessageTypes::EdgeINMessageBundle EdgeINMessageBundle;
typedef vtkPBGLDistributedGraphHelperMessageTypes::EdgeNIMessageBundle EdgeNIMessageBundle;
typedef vtkPBGLDistributedGraphHelperMessageTypes::EdgeNNMessageBundle EdgeNNMessageBundle;
//----------------------------------------------------------------------------
// private class vtkPBGLDistributedGraphHelperInternals
//----------------------------------------------------------------------------
class vtkPBGLDistributedGraphHelperInternals : public vtkObject
{
public:
static vtkPBGLDistributedGraphHelperInternals *New();
vtkTypeMacro(vtkPBGLDistributedGraphHelperInternals, vtkObject);
// Description:
// Handle a FIND_VERTEX_TAG message.
vtkIdType HandleFindVertex(const vtkVariant& pedigreeId);
// Description:
// Handle a FIND_EDGE_SOURCE_TARGET_TAG message.
std::pair<vtkIdType, vtkIdType> HandleFindEdgeSourceTarget(vtkIdType id);
// Description:
// Add a vertex with the given pedigree, if a vertex with that
// pedigree ID does not already exist. Returns the ID for that
// vertex.
vtkIdType HandleAddVertex(const vtkVariant& pedigreeId);
// Description:
// Add a vertex with properties, if a vertex with the
// pedigree ID (assuming there is one in the properties) does
// not already exist. Returns the ID for that vertex.
vtkIdType HandleAddVertexProps(vtkVariantArray *propArray);
// Description:
// Handle a ADD_DIRECTED_BACK_EDGE_TAG or ADD_UNDIRECTED_BACK_END_TAG
// message.
void HandleAddBackEdge(vtkEdgeType edge, bool directed);
// Description:
// Handle ADD_*DIRECTED_EDGE_*_REPLY_TAG messages.
vtkEdgeType
HandleAddEdge(const EdgeIIMessageBundle& msg, bool directed);
// Description:
// Handle ADD_*DIRECTED_EDGE_NI_*_REPLY_TAG messages.
vtkEdgeType
HandleAddEdgeNI(const EdgeNIMessageBundle& msg, bool directed);
// Description:
// Handle ADD_*DIRECTED_EDGE_IN_*_REPLY_TAG messages
vtkEdgeType
HandleAddEdgeIN(const EdgeINMessageBundle& msg, bool directed);
// Description:
// Handle ADD_*DIRECTED_EDGE_NN_*_REPLY_TAG messages.
vtkEdgeType
HandleAddEdgeNN(const EdgeNNMessageBundle& msg, bool directed);
// Description:
// The helper class of which this structure is a part.
vtkPBGLDistributedGraphHelper *Helper;
// Description:
// Process group used by this helper
boost::graph::distributed::mpi_process_group process_group;
private:
vtkPBGLDistributedGraphHelperInternals()
: process_group(GetRootProcessGroup()) { }
// Retrieve the root process group.
static boost::graph::distributed::mpi_process_group&
GetRootProcessGroup()
{
if (!root_process_group)
{
root_process_group = new boost::graph::distributed::mpi_process_group();
}
return *root_process_group;
}
// Description:
// The "root" process group, to which all of the process groups in
// VTK's distributed graphs will eventually attach.
static boost::graph::distributed::mpi_process_group *root_process_group;
};
// Definition
boost::graph::distributed::mpi_process_group *
vtkPBGLDistributedGraphHelperInternals::root_process_group;
vtkStandardNewMacro(vtkPBGLDistributedGraphHelperInternals);
//----------------------------------------------------------------------------
// class vtkPBGLDistributedGraphHelper
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkPBGLDistributedGraphHelper);
//----------------------------------------------------------------------------
vtkPBGLDistributedGraphHelper::vtkPBGLDistributedGraphHelper()
{
this->Internals = vtkPBGLDistributedGraphHelperInternals::New();
this->Internals->Helper = this;
}
//----------------------------------------------------------------------------
vtkPBGLDistributedGraphHelper::~vtkPBGLDistributedGraphHelper()
{
this->Internals->Delete();
}
//----------------------------------------------------------------------------
void vtkPBGLDistributedGraphHelper::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "vtkPBGLDistributedGraphHelper" << endl;
}
//----------------------------------------------------------------------------
void vtkPBGLDistributedGraphHelper::Synchronize()
{
synchronize(this->Internals->process_group);
}
//----------------------------------------------------------------------------
vtkDistributedGraphHelper *vtkPBGLDistributedGraphHelper::Clone()
{
return vtkPBGLDistributedGraphHelper::New();
}
//----------------------------------------------------------------------------
boost::graph::distributed::mpi_process_group vtkPBGLDistributedGraphHelper::GetProcessGroup()
{
return this->Internals->process_group.base();
}
//----------------------------------------------------------------------------
void
vtkPBGLDistributedGraphHelper::AddVertexInternal(vtkVariantArray *propertyArr,
vtkIdType *vertex)
{
vtkIdType rank
= this->Graph->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER());
vtkAbstractArray *peds = this->Graph->GetVertexData()->GetPedigreeIds();
vtkIdType owner = -1;
if (peds != NULL)
{
vtkIdType pedIdx = this->Graph->GetVertexData()->SetPedigreeIds(peds);
vtkVariant pedigreeId = propertyArr->GetValue(pedIdx);
owner = this->GetVertexOwnerByPedigreeId(pedigreeId);
}
if ((peds != NULL) && (owner == rank))
{
// This little dance keeps us from having to make
// vtkPBGLDistributedGraphHelper a friend of vtkGraph. It also
// makes sure that users don't try to be sneaky about adding
// vertices to non-mutable vtkGraphs.
if (vtkMutableDirectedGraph *graph
= vtkMutableDirectedGraph::SafeDownCast(this->Graph))
{
if (vertex)
{
*vertex = graph->AddVertex(propertyArr);
}
else
{
graph->LazyAddVertex(propertyArr);
}
}
else if (vtkMutableUndirectedGraph *graph
= vtkMutableUndirectedGraph::SafeDownCast(this->Graph))
{
if (vertex)
{
*vertex = graph->AddVertex(propertyArr);
}
else
{
graph->LazyAddVertex(propertyArr);
}
}
else
{
vtkErrorMacro("Cannot add vertices to a non-mutable, distributed graph");
}
return;
}
if (vertex)
{
// Request immediate addition of the vertex, with a reply.
send_oob_with_reply(this->Internals->process_group, owner,
ADD_VERTEX_PROPS_WITH_REPLY_TAG, propertyArr, *vertex);
}
else
{
// Request addition of the vertex, eventually.
send(this->Internals->process_group, owner, ADD_VERTEX_PROPS_NO_REPLY_TAG, propertyArr);
}
}
//----------------------------------------------------------------------------
void
vtkPBGLDistributedGraphHelper::AddVertexInternal(const vtkVariant& pedigreeId,
vtkIdType *vertex)
{
vtkIdType rank
= this->Graph->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER());
vtkIdType owner = this->GetVertexOwnerByPedigreeId(pedigreeId);
if (owner == rank)
{
// This little dance keeps us from having to make
// vtkPBGLDistributedGraphHelper a friend of vtkGraph. It also
// makes sure that users don't try to be sneaky about adding
// vertices to non-mutable vtkGraphs.
if (vtkMutableDirectedGraph *graph
= vtkMutableDirectedGraph::SafeDownCast(this->Graph))
{
if (vertex)
{
*vertex = graph->AddVertex(pedigreeId);
}
else
{
graph->LazyAddVertex(pedigreeId);
}
}
else if (vtkMutableUndirectedGraph *graph
= vtkMutableUndirectedGraph::SafeDownCast(this->Graph))
{
if (vertex)
{
*vertex = graph->AddVertex(pedigreeId);
}
else
{
graph->LazyAddVertex(pedigreeId);
}
}
else
{
vtkErrorMacro("Cannot add vertices to a non-mutable, distributed graph");
}
return;
}
if (vertex)
{
// Request immediate addition of the vertex, with a reply.
send_oob_with_reply(this->Internals->process_group, owner,
ADD_VERTEX_WITH_REPLY_TAG, pedigreeId, *vertex);
}
else
{
// Request addition of the vertex, eventually.
send(this->Internals->process_group, owner, ADD_VERTEX_NO_REPLY_TAG, pedigreeId);
}
}
//----------------------------------------------------------------------------
void
vtkPBGLDistributedGraphHelper::AddEdgeInternal(vtkIdType uDistributedId,
vtkIdType vDistributedId,
bool directed,
vtkVariantArray *propertyArr,
vtkEdgeType *edge)
{
int rank = this->Graph->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER());
int numProcs = this->Graph->GetInformation()->Get(vtkDataObject::DATA_NUMBER_OF_PIECES());
int uOwner = this->GetVertexOwner (uDistributedId);
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << "]\tAddEdgeInternal<uDist,vDist>("
<< "u={"<< this->GetVertexOwner(uDistributedId)
<< ":" << this->GetVertexIndex(uDistributedId) << "}, "
<< "v={"<< this->GetVertexOwner(vDistributedId)
<< ":" << this->GetVertexIndex(vDistributedId) << "}, "
<< "prop=" << propertyArr << ", "
<< "edge=" << edge
<< ")" << endl;
if(propertyArr)
{
cout << "[" << rank << "]\t-\tpropertyArr: NumberOfValues="
<< propertyArr->GetNumberOfValues() << endl;
cout << "[" << rank << "]\t-\tpropertyArr: NumberOfArrays="
<< this->Graph->GetEdgeData()->GetNumberOfArrays() << endl;
}
fflush(stdout);
#endif
if (uOwner == rank)
{
// The source of the edge is local.
vtkGraphInternals* GraphInternals = this->Graph->GetGraphInternals(true);
// The edge ID involves our rank and the local number of edges.
vtkIdType edgeId = this->MakeDistributedId(rank, GraphInternals->NumberOfEdges);
if (propertyArr) // Add edge properties
{
vtkDataSetAttributes *edgeData = this->Graph->GetEdgeData();
int numProps = propertyArr->GetNumberOfValues(); // # of properties = # of arrays
assert(numProps == edgeData->GetNumberOfArrays());
for (int iprop=0; iprop<numProps; iprop++)
{
vtkAbstractArray* arr = edgeData->GetAbstractArray(iprop);
if (vtkDataArray::SafeDownCast(arr))
{
vtkDataArray::SafeDownCast(arr)->InsertNextTuple1(propertyArr->GetPointer(iprop)->ToDouble());
}
else if (vtkStringArray::SafeDownCast(arr))
{
vtkStringArray::SafeDownCast(arr)->InsertNextValue(propertyArr->GetPointer(iprop)->ToString());
}
else
{
vtkErrorMacro("Unsupported array type");
}
}
}
// Add the forward edge.
GraphInternals->Adjacency[this->GetVertexIndex(uDistributedId)]
.OutEdges.push_back(vtkOutEdgeType(vDistributedId, edgeId));
// We've added an edge.
GraphInternals->NumberOfEdges++;
int vOwner = this->GetVertexOwner(vDistributedId);
if (vOwner == rank)
{
// The target vertex is local. Add the appropriate back edge.
if (directed)
{
GraphInternals->Adjacency[this->GetVertexIndex(vDistributedId)]
.InEdges.push_back(vtkInEdgeType(uDistributedId, edgeId));
}
else if (uDistributedId != vDistributedId)
{
// Avoid storing self-loops twice in undirected graphs
GraphInternals->Adjacency[this->GetVertexIndex(vDistributedId)]
.OutEdges.push_back(vtkOutEdgeType(uDistributedId, edgeId));
}
}
else
{
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << "]\tsend("
<< "{"<<this->GetVertexOwner(uDistributedId)<<":"<<this->GetVertexIndex(uDistributedId)<<"}, "
<< "{"<<this->GetVertexOwner(vDistributedId)<<":"<<this->GetVertexIndex(vDistributedId)<<"}"
<< ") to process " << vOwner
<< " ADD_*_BACK_EDGE_TAG"
<< endl;
fflush(stdout);
#endif
// WCMCLEN: enabling this seems to cause problems sometimes?
send_oob(this->Internals->process_group, vOwner,
directed ? ADD_DIRECTED_BACK_EDGE_TAG
: ADD_UNDIRECTED_BACK_EDGE_TAG,
vtkEdgeType(uDistributedId, vDistributedId, edgeId));
}
if (edge)
{
*edge = vtkEdgeType(uDistributedId, vDistributedId, edgeId);
}
}
else // uOwner != rank
{
// The source of the edge is non-local.
if (edge)
{
// Send an AddEdge request to the owner of "u", and wait
// patiently for the reply.
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << "]\tsend_oob_with_reply("
<< "{" << this->GetVertexOwner(uDistributedId) <<":"<<this->GetVertexIndex(uDistributedId)<<"}, "
<< "{" << this->GetVertexOwner(vDistributedId) <<":"<<this->GetVertexIndex(vDistributedId)<<"}"
<< ") to process " << uOwner
<< " ADD_*_EDGE_WITH_REPLY"
<< endl;
fflush(stdout);
#endif
send_oob_with_reply(this->Internals->process_group, uOwner,
directed ? ADD_DIRECTED_EDGE_WITH_REPLY_TAG
: ADD_UNDIRECTED_EDGE_WITH_REPLY_TAG,
EdgeIIMessageBundle(uDistributedId,vDistributedId,propertyArr),
*edge);
}
else
{
// We're adding a remote edge, but we don't need to wait
// until the edge has been added. Just send a message to the
// owner of the source; we don't need (or want) a reply.
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << "]\tsend("
<< "{"<<this->GetVertexOwner(uDistributedId) <<":"<<this->GetVertexIndex(uDistributedId)<<"}, "
<< "{"<<this->GetVertexOwner(vDistributedId) <<":"<<this->GetVertexIndex(vDistributedId)<<"}"
<< ") to process " << uOwner
<< " ADD_*_EDGE_NO_REPLY"
<< endl;
fflush(stdout);
#endif
send(this->Internals->process_group, uOwner,
directed? ADD_DIRECTED_EDGE_NO_REPLY_TAG
: ADD_UNDIRECTED_EDGE_NO_REPLY_TAG,
EdgeIIMessageBundle(uDistributedId, vDistributedId, propertyArr));
}
}
}
//----------------------------------------------------------------------------
void
vtkPBGLDistributedGraphHelper::AddEdgeInternal(const vtkVariant& uPedigreeId,
vtkIdType vDistributedId,
bool directed,
vtkVariantArray *propertyArr,
vtkEdgeType *edge)
{
vtkIdType rank = this->Graph->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER());
vtkIdType uOwner = this->GetVertexOwnerByPedigreeId(uPedigreeId);
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << "]\tAddEdgeInternal<uPed, vDist>("
<< "u="<< uPedigreeId.ToString() << ", "
<< "v={"<< this->GetVertexOwner(vDistributedId)
<< ":" << this->GetVertexIndex(vDistributedId) << "}, "
<< "prop=" << propertyArr << ", "
<< "edge=" << edge
<< ")" << endl;
#endif
if (uOwner == rank)
{
// Resolve the pedigreeId for u immediately and add the edge locally.
vtkIdType uDistributedId;
this->AddVertexInternal(uPedigreeId, &uDistributedId);
uDistributedId = this->MakeDistributedId(rank, uDistributedId);
this->AddEdgeInternal(uDistributedId, vDistributedId, directed, propertyArr, edge);
return;
}
// Edge is remote: request its addition.
if (edge)
{
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << "]\tsend_oob_with_reply("
<< uPedigreeId.ToString()
<< "{"<<this->GetVertexOwner(vDistributedId) <<":"<<this->GetVertexIndex(vDistributedId)<<"}"
<< ") to process " << uOwner
<< " ADD_*_EDGE_NI_WITH_REPLY"
<< endl;
fflush(stdout);
#endif
send_oob_with_reply(this->Internals->process_group, uOwner,
directed ? ADD_DIRECTED_EDGE_NI_WITH_REPLY_TAG
: ADD_UNDIRECTED_EDGE_NI_WITH_REPLY_TAG,
EdgeNIMessageBundle(uPedigreeId,vDistributedId,propertyArr),
*edge);
}
else
{
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << "]\tsend("
<< uPedigreeId.ToString()
<< "{"<<this->GetVertexOwner(vDistributedId) <<":"<<this->GetVertexIndex(vDistributedId)<<"}"
<< ") to process " << uOwner
<< " ADD_*_EDGE_NI_NO_REPLY"
<< endl;
fflush(stdout);
#endif
send(this->Internals->process_group, uOwner,
directed ? ADD_DIRECTED_EDGE_NI_NO_REPLY_TAG
: ADD_UNDIRECTED_EDGE_NI_NO_REPLY_TAG,
EdgeNIMessageBundle(uPedigreeId, vDistributedId, propertyArr));
}
}
//----------------------------------------------------------------------------
void
vtkPBGLDistributedGraphHelper::AddEdgeInternal(vtkIdType uDistributedId,
const vtkVariant& vPedigreeId,
bool directed,
vtkVariantArray *propertyArr,
vtkEdgeType *edge)
{
vtkIdType rank
= this->Graph->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER());
vtkIdType vOwner = this->GetVertexOwnerByPedigreeId(vPedigreeId);
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << "]\tAddEdgeInternal<uDist,vPed>("
<< "u={"<< this->GetVertexOwner(uDistributedId)
<< ":" << this->GetVertexIndex(uDistributedId) << "}, "
<< "v=" << vPedigreeId.ToString() << ", "
<< "prop=" << propertyArr << ", "
<< "edge=" << edge
<< ")" << endl;
fflush(stdout);
#endif
if (vOwner == rank || edge)
{
// Resolve the pedigree ID for v immediately and add the edge.
vtkIdType vDistributedId;
this->AddVertexInternal(vPedigreeId, &vDistributedId);
vDistributedId=this->MakeDistributedId(vOwner,vDistributedId); // pass vOwner, NOT rank
this->AddEdgeInternal(uDistributedId, vDistributedId, directed, propertyArr, edge);
return;
}
// v is remote and we don't care when the edge is added. Ask the
// owner of v to resolve the pedigree ID of v and add the edge.
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << "]\tsend("
<< "{"<<this->GetVertexOwner(uDistributedId) <<":"<<this->GetVertexIndex(uDistributedId)<<"}"
<< ", " << vPedigreeId.ToString() << ") to process " << vOwner
<< " ADD_*_EDGE_IN_NO_REPLY"
<< endl;
fflush(stdout);
#endif
send(this->Internals->process_group, vOwner,
directed ? ADD_DIRECTED_EDGE_IN_NO_REPLY_TAG
: ADD_UNDIRECTED_EDGE_IN_NO_REPLY_TAG,
EdgeINMessageBundle(uDistributedId,vPedigreeId,propertyArr));
}
//----------------------------------------------------------------------------
void
vtkPBGLDistributedGraphHelper::AddEdgeInternal(const vtkVariant& uPedigreeId,
const vtkVariant& vPedigreeId,
bool directed,
vtkVariantArray *propertyArr,
vtkEdgeType *edge)
{
vtkIdType rank
= this->Graph->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER());
vtkIdType uOwner = this->GetVertexOwnerByPedigreeId(uPedigreeId);
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << "]\tAddEdgeInternal<uPed,vPed>("
<< "u="<< uPedigreeId.ToString() << ", "
<< "uOwner="<< uOwner << ", "
<< "v="<< vPedigreeId.ToString() << ", "
<< "prop=" << propertyArr << ", "
<< "edge=" << edge
<< ")" << endl;
#endif
if (uOwner == rank)
{
// Resolve the pedigree ID for u immediately and add the edge.
vtkIdType u;
this->AddVertexInternal(uPedigreeId, &u);
vtkIdType uDistributedId = this->MakeDistributedId(rank,u);
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << " uPed,vPed]("
<< "u="<< u << ", "
<< ")" << endl;
#endif
this->AddEdgeInternal(uDistributedId, vPedigreeId, directed, propertyArr, edge);
return;
}
vtkIdType vOwner = this->GetVertexOwnerByPedigreeId(vPedigreeId);
if (vOwner == rank || edge)
{
// Resolve the pedigree ID for v immediately and add the edge.
vtkIdType vLocalIndex;
this->AddVertexInternal(vPedigreeId, &vLocalIndex);
vtkIdType vDistributedId = this->MakeDistributedId(vOwner,vLocalIndex); // pass vOwner, NOT rank
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << " uPed,vPed]("
<< ")" << endl;
#endif
this->AddEdgeInternal(uPedigreeId, vDistributedId, directed, propertyArr, edge);
return;
}
// Neither u nor v is local, and we don't care when the edge is
// added, so ask the owner of v to resolve the pedigree ID of v and
// add the edge.
#if defined(DEBUG) // WCMCLEN
cout << "[" << rank << "]\tsend("
<< uPedigreeId.ToString() << ", "
<< vPedigreeId.ToString()
<< ") to process " << uOwner
<< " ADD_*_EDGE_NN_NO_REPLY"
<< endl;
fflush(stdout);
#endif
send(this->Internals->process_group, vOwner,
directed? ADD_DIRECTED_EDGE_NN_NO_REPLY_TAG
: ADD_UNDIRECTED_EDGE_NN_NO_REPLY_TAG,
EdgeNNMessageBundle(uPedigreeId, vPedigreeId, propertyArr));
}
//----------------------------------------------------------------------------
vtkIdType
vtkPBGLDistributedGraphHelper::FindVertex(const vtkVariant& pedigreeId)
{
vtkIdType rank
= this->Graph->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER());
vtkIdType owner = this->GetVertexOwnerByPedigreeId(pedigreeId);
if (owner == rank)
{
// The vertex is local; just ask the local part of the graph.
return this->Graph->FindVertex(pedigreeId);
}
// The vertex is remote; send a message looking for it.
vtkIdType result;
send_oob_with_reply(this->Internals->process_group, owner,
FIND_VERTEX_TAG, pedigreeId, result);
return result;
}
//----------------------------------------------------------------------------
void
vtkPBGLDistributedGraphHelper::FindEdgeSourceAndTarget(vtkIdType id,
vtkIdType *source,
vtkIdType *target)
{
vtkIdType rank
= this->Graph->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER());
vtkIdType owner = this->GetEdgeOwner(id);
if (owner == rank)
{
if (source)
{
*source = this->Graph->GetSourceVertex(id);
}
if (target)
{
*target = this->Graph->GetTargetVertex(id);
}
return;
}
std::pair<vtkIdType, vtkIdType> result;
send_oob_with_reply(this->Internals->process_group, owner,
FIND_EDGE_SOURCE_TARGET_TAG, id, result);
if (source)
{
*source = result.first;
}
if (target)
{
*target = result.second;
}
}
//----------------------------------------------------------------------------
void vtkPBGLDistributedGraphHelper::AttachToGraph(vtkGraph *graph)
{
this->Graph = graph;
if (graph &&
(graph->GetNumberOfVertices() != 0 || graph->GetNumberOfEdges() != 0))
{
vtkErrorMacro("Cannot attach a distributed graph helper to a non-empty vtkGraph");
}
if (this->Graph)
{
// Set the piece number and number of pieces so that the
// vtkGraph knows the layout of the graph.
this->Graph->GetInformation()->Set(
vtkDataObject::DATA_PIECE_NUMBER(),
process_id(this->Internals->process_group));
this->Graph->GetInformation()->Set(
vtkDataObject::DATA_NUMBER_OF_PIECES(),
num_processes(this->Internals->process_group));
// Add our triggers to the process group
this->Internals->process_group.make_distributed_object();
this->Internals->process_group.trigger_with_reply<vtkVariant>
(FIND_VERTEX_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleFindVertex,
this->Internals, _3));
this->Internals->process_group.trigger_with_reply<vtkIdType>
(FIND_EDGE_SOURCE_TARGET_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals
::HandleFindEdgeSourceTarget,
this->Internals, _3));
this->Internals->process_group.trigger<vtkVariant>
(ADD_VERTEX_NO_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddVertex,
this->Internals, _3));
this->Internals->process_group.trigger_with_reply<vtkVariant>
(ADD_VERTEX_WITH_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddVertex,
this->Internals, _3));
this->Internals->process_group.trigger<vtkVariantArray *>
(ADD_VERTEX_PROPS_NO_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddVertexProps,
this->Internals, _3));
this->Internals->process_group.trigger_with_reply<vtkVariantArray *>
(ADD_VERTEX_PROPS_WITH_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddVertexProps,
this->Internals, _3));
this->Internals->process_group.trigger<vtkEdgeType>
(ADD_DIRECTED_BACK_EDGE_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddBackEdge,
this->Internals, _3, true));
this->Internals->process_group.trigger<vtkEdgeType>
(ADD_UNDIRECTED_BACK_EDGE_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddBackEdge,
this->Internals, _3, false));
// Add edge for (id, id) pairs
this->Internals->process_group.trigger<EdgeIIMessageBundle>
(ADD_DIRECTED_EDGE_NO_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdge,
this->Internals, _3, true));
this->Internals->process_group.trigger<EdgeIIMessageBundle>
(ADD_UNDIRECTED_EDGE_NO_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdge,
this->Internals, _3, false));
this->Internals->process_group.trigger_with_reply<EdgeIIMessageBundle>
(ADD_DIRECTED_EDGE_WITH_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdge,
this->Internals, _3, true));
this->Internals->process_group.trigger_with_reply<EdgeIIMessageBundle>
(ADD_UNDIRECTED_EDGE_WITH_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdge,
this->Internals, _3, false));
// Add edge for (pedigree, id) pairs
this->Internals->process_group.trigger<EdgeNIMessageBundle>
(ADD_DIRECTED_EDGE_NI_NO_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdgeNI,
this->Internals, _3, true));
this->Internals->process_group.trigger<EdgeNIMessageBundle>
(ADD_UNDIRECTED_EDGE_NI_NO_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdgeNI,
this->Internals, _3, false));
this->Internals->process_group.trigger_with_reply<EdgeNIMessageBundle>
(ADD_DIRECTED_EDGE_NI_WITH_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdgeNI,
this->Internals, _3, true));
this->Internals->process_group.trigger_with_reply<EdgeNIMessageBundle>
(ADD_UNDIRECTED_EDGE_NI_WITH_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdgeNI,
this->Internals, _3, false));
// Add edge for (id, pedigree) pairs
this->Internals->process_group.trigger<EdgeINMessageBundle>
(ADD_DIRECTED_EDGE_IN_NO_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdgeIN,
this->Internals, _3, true));
this->Internals->process_group.trigger<EdgeINMessageBundle>
(ADD_UNDIRECTED_EDGE_IN_NO_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdgeIN,
this->Internals, _3, false));
// Add edge for (pedigree, pedigree) pairs
this->Internals->process_group.trigger<EdgeNNMessageBundle>
(ADD_DIRECTED_EDGE_NN_NO_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdgeNN,
this->Internals, _3, true));
this->Internals->process_group.trigger<EdgeNNMessageBundle>
(ADD_UNDIRECTED_EDGE_NN_NO_REPLY_TAG,
boost::bind(&vtkPBGLDistributedGraphHelperInternals::HandleAddEdgeNN,
this->Internals, _3, false));
}
// vtkDistributedGraphHelper will set up the appropriate masks.
this->Superclass::AttachToGraph(graph);
}
//----------------------------------------------------------------------------
//------------- vtkPBGLDistributedGraphHelperInternals methods --------------
//----------------------------------------------------------------------------
vtkIdType
vtkPBGLDistributedGraphHelperInternals::
HandleFindVertex(const vtkVariant& pedigreeId)
{
return this->Helper->FindVertex(pedigreeId);
}
//----------------------------------------------------------------------------
std::pair<vtkIdType, vtkIdType>
vtkPBGLDistributedGraphHelperInternals::
HandleFindEdgeSourceTarget(vtkIdType id)
{
std::pair<vtkIdType, vtkIdType> result;
this->Helper->FindEdgeSourceAndTarget(id, &result.first, &result.second);
return result;
}
//----------------------------------------------------------------------------
vtkIdType
vtkPBGLDistributedGraphHelperInternals::
HandleAddVertexProps(vtkVariantArray *propArr)
{
vtkIdType result;
this->Helper->AddVertexInternal(propArr, &result);
return result;
}
//----------------------------------------------------------------------------
vtkIdType
vtkPBGLDistributedGraphHelperInternals::
HandleAddVertex(const vtkVariant& pedigreeId)
{
vtkIdType result;
this->Helper->AddVertexInternal(pedigreeId, &result);
return result;
}
//----------------------------------------------------------------------------
void
vtkPBGLDistributedGraphHelperInternals::HandleAddBackEdge
(vtkEdgeType edge, bool directed)
{
#if defined(DEBUG) // WCMCLEN
cout << "["<< process_group.rank << "]\tHandleAddBackEdge"
<< "(" << edge.Source << ", " << edge.Target << ", " << edge.Id << ")"
<< endl;
fflush(stdout);
#endif
assert(edge.Source != edge.Target);
assert(this->Helper->GetVertexOwner(edge.Target)
== this->Helper->Graph->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER()));
vtkGraphInternals *GraphInternals = this->Helper->Graph->GetGraphInternals(true);
if (directed)
{
GraphInternals->Adjacency[this->Helper->GetVertexIndex(edge.Target)]
.InEdges.push_back(vtkInEdgeType(edge.Source, edge.Id));
}
else
{
GraphInternals->Adjacency[this->Helper->GetVertexIndex(edge.Target)]
.OutEdges.push_back(vtkOutEdgeType(edge.Source, edge.Id));
}
}
//----------------------------------------------------------------------------
vtkEdgeType
vtkPBGLDistributedGraphHelperInternals::HandleAddEdge