forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkStructuredImplicitConnectivity.cxx
1580 lines (1366 loc) · 48.1 KB
/
vtkStructuredImplicitConnectivity.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: vtkStructuredImplicitConnectivity.h
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.
=========================================================================*/
#include "vtkStructuredImplicitConnectivity.h"
// VTK includes
#include "vtkDataArray.h"
#include "vtkFieldDataSerializer.h"
#include "vtkImageData.h"
#include "vtkMPIController.h"
#include "vtkMultiProcessController.h"
#include "vtkMultiProcessStream.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkRectilinearGrid.h"
#include "vtkStructuredData.h"
#include "vtkStructuredExtent.h"
#include "vtkStructuredGrid.h"
// C/C++ includes
#include <algorithm>
#include <cassert>
#include <map>
#include <sstream>
#include <vector>
//==============================================================================
// INTERNAL DATASTRUCTURES & DEFINITIONS
//==============================================================================
// Some useful extent macros
#define IMIN(ext) ext[0]
#define IMAX(ext) ext[1]
#define JMIN(ext) ext[2]
#define JMAX(ext) ext[3]
#define KMIN(ext) ext[4]
#define KMAX(ext) ext[5]
#define I(ijk) ijk[0]
#define J(ijk) ijk[1]
#define K(ijk) ijk[2]
namespace vtk
{
namespace detail
{
// Given two intervals A=[a1,a2] and B[b1,b2] the IntervalsConnect struct
// enumerates the cases where interval A connects to Interval B.
struct IntervalsConnect
{
// NOTE: This enum is arranged s.t., negating a value in [-4,4] will yield
// the mirror inverse
enum connectivity_t
{
IMPLICIT_LO = -4, // Interval A implicitly connects with B on A's low end
SUBSET = -3, // Interval A is completely inside interval B
OVERLAP_LO = -2, // Interval A intersects with B on A's low end
LO = -1, // A's low end touches B's high end A.Low() == B.High()
ONE_TO_ONE = 0, // Intervals A,B are exactly the same.
HI = 1, // A's high end touches B's low end A.High() == B.Low()
OVERLAP_HI = 2, // Interval A intersects with B on A's high end
SUPERSET = 3, // Interval A *contains* all of interval B
IMPLICIT_HI = 4, // Interval A implicitly connects with B on its high end.
DISJOINT = 5, // Intervals A,B are completely disjoint.
UNDEFINED = 6 // Undefined
};
static
std::string OrientationToString( int orient[3] )
{
std::ostringstream oss;
oss << "(";
for(int i=0; i < 3; ++i)
{
if(i==1 || i==2)
{
oss << ", ";
}
switch( orient[i] )
{
case IMPLICIT_LO:
oss << "IMPLICIT_LO";
break;
case SUBSET:
oss << "SUBSET";
break;
case OVERLAP_LO:
oss << "OVERLAP_LO";
break;
case LO:
oss << "LO";
break;
case ONE_TO_ONE:
oss << "ONE_TO_ONE";
break;
case HI:
oss << "HI";
break;
case OVERLAP_HI:
oss << "OVERLAP_HI";
break;
case SUPERSET:
oss << "SUPERSET";
break;
case IMPLICIT_HI:
oss << "IMPLICIT_HI";
break;
case DISJOINT:
oss << "DISJOINT";
break;
case UNDEFINED:
oss << "UNDEFINED";
break;
default:
oss << "*UNKNOWN*";
} // END switch
} // END for
oss << ")";
return( oss.str() );
}
}; // END struct IntervalsConnect
//------------------------------------------------------------------------------
// Interval class Definition
//------------------------------------------------------------------------------
class Interval
{
public:
Interval() : lo(0), hi(-1) {};
Interval(const int l, const int h) : lo(l), hi(h) {};
~Interval() {};
int Low() const { return this->lo; };
int High() const { return this->hi; };
int Cardinality() const { return(this->hi-this->lo+1); };
bool Valid() const { return(this->lo <= this->hi); };
void Set(const int l, const int h) { this->lo=l; this->hi=h; };
void Invalidate() {this->Set(0,-1);}
bool Within(const Interval& B) const
{ return( (this->lo >= B.Low()) && (this->hi <= B.High()) ); };
bool ImplicitNeighbor(const Interval& B, int& type);
static bool ImplicitNeighbors(
const Interval& A, const Interval& B, int& type);
bool Intersects(const Interval& B, Interval& Overlap, int& type);
static bool Intersects(const Interval& A, const Interval& B,
Interval& Overlap, int& type);
private:
int lo;
int hi;
};
//------------------------------------------------------------------------------
bool Interval::ImplicitNeighbors(const Interval& A, const Interval& B, int& t)
{
assert("pre: interval is not valid!" && A.Valid());
assert("pre: B interval is not valid!" && B.Valid() );
bool status = false;
if( A.High()+1 == B.Low() )
{
status = true;
t = IntervalsConnect::IMPLICIT_HI;
}
else if( B.High()+1 == A.Low() )
{
status = true;
t = IntervalsConnect::IMPLICIT_LO;
}
return( status );
}
//------------------------------------------------------------------------------
bool Interval::ImplicitNeighbor(const Interval& B, int& type)
{
return( Interval::ImplicitNeighbors(*this,B,type) );
}
//------------------------------------------------------------------------------
bool Interval::Intersects(const Interval& A, const Interval& B,
Interval& Overlap, int& type)
{
assert("pre: interval is not valid!" && A.Valid());
assert("pre: B interval is not valid!" && B.Valid() );
bool status = false;
// Disjoint cases
if( A.High() < B.Low() )
{
type = IntervalsConnect::DISJOINT;
Overlap.Invalidate();
status = false;
}
else if( B.High() < A.Low() )
{
type = IntervalsConnect::DISJOINT;
Overlap.Invalidate();
status = false;
}
// ONE_TO_ONE case
else if( A.Cardinality()==B.Cardinality() &&
A.Low()==B.Low() &&
A.High()==B.High() )
{
type = IntervalsConnect::ONE_TO_ONE;
Overlap.Set(A.Low(),A.High());
status = true;
}
// A is a SUBSET of B
else if( A.Within(B) )
{
type = IntervalsConnect::SUBSET;
Overlap.Set(A.Low(),A.High());
status = true;
}
// A is a superset of B
else if( B.Within(A) )
{
type = IntervalsConnect::SUPERSET;
Overlap.Set(B.Low(),B.High());
status = true;
}
// A touches B on the high end
else if( A.High() == B.Low() )
{
type = IntervalsConnect::HI;
Overlap.Set(A.High(),A.High());
status = true;
}
// A touches B on the low end
else if( A.Low() == B.High() )
{
type = IntervalsConnect::LO;
Overlap.Set(A.Low(),A.Low());
status = true;
}
// A intersects B on its low end
else if( (A.Low() >= B.Low()) && (A.Low() <= B.High()) )
{
type = IntervalsConnect::OVERLAP_LO;
Overlap.Set(A.Low(),B.High());
status = true;
}
// A intersects B on its high end
else if( (A.High() >= B.Low() ) && (A.High() <= B.High()) )
{
type = IntervalsConnect::OVERLAP_HI;
Overlap.Set(B.Low(),A.High());
status = true;
}
else
{
vtkGenericWarningMacro(
<< "Undefined interval intersection!"
<< "Code should not reach here!!!");
type = IntervalsConnect::UNDEFINED;
status = false;
Overlap.Invalidate();
}
return( status );
}
//------------------------------------------------------------------------------
bool Interval::Intersects(const Interval& B, Interval& Overlap, int& type)
{
return( Interval::Intersects(*this, B, Overlap, type) );
}
//------------------------------------------------------------------------------
struct ImplicitNeighbor
{
int Rank; // the rank of the neighbor
int Extent[6]; // the extent of the neighbor
int Orientation[3]; // the orientation w.r.t the local extent
int Overlap[6]; // the overlap extent
std::string ToString()
{
std::ostringstream oss;
oss << "rank=" << this->Rank << " ";
oss << "extent=[";
oss << this->Extent[0] << ", ";
oss << this->Extent[1] << ", ";
oss << this->Extent[2] << ", ";
oss << this->Extent[3] << ", ";
oss << this->Extent[4] << ", ";
oss << this->Extent[5] << "] ";
oss << "overlap=[";
oss << this->Overlap[0] << ", ";
oss << this->Overlap[1] << ", ";
oss << this->Overlap[2] << ", ";
oss << this->Overlap[3] << ", ";
oss << this->Overlap[4] << ", ";
oss << this->Overlap[5] << "] ";
oss << "orientation=";
oss << IntervalsConnect::OrientationToString(this->Orientation);
return( oss.str() );
}
};
//------------------------------------------------------------------------------
struct DomainMetaData
{
int WholeExtent[6]; // Extent of the entire domain
int DataDescription; // Data-description of the distributed dataset.
int NDim; // Number of dimensions according to DataDescription.
int DimIndex[3]; // Stores the dimensions of the dataset in the
// the right order. This essentially allows to
// process 2-D (XY,XZ,YZ) and 3-D datasets in a
// transparent way.
int GlobalImplicit[3]; // indicates for each dimension if there is globally
// implicit connectivity. Any value > 0 indicates
// implicit connectivity in the given direction.
// Flat list of extents. Extents are organized as follows:
// [id, imin, imax, jmin, jmax, kmin, kmax]
std::vector< int > ExtentListInfo;
/// \brief Checks if a grid with the given extent is within this domain
/// \param ext the extent of the grid in query
/// \return status true if the grid is insided, else false.
bool HasGrid(int ext[6])
{ return( vtkStructuredExtent::Smaller(ext,this->WholeExtent) ); };
/// \brief Initializes the domain metadata.
void Initialize(int wholeExt[6])
{
memcpy(this->WholeExtent,wholeExt,6*sizeof(int));
this->DataDescription =
vtkStructuredData::GetDataDescriptionFromExtent(wholeExt);
if (this->DataDescription == VTK_EMPTY)
{
return;
}
// Sanity checks!
assert( "pre: data description is VTK_EMPTY!" &&
(this->DataDescription != VTK_EMPTY) );
assert( "pre: dataset must be 2-D or 3-D" &&
(this->DataDescription >= VTK_XY_PLANE) );
this->NDim = -1;
std::fill(this->DimIndex,this->DimIndex+3,-1);
std::fill(this->GlobalImplicit,this->GlobalImplicit+3,0);
switch( this->DataDescription )
{
case VTK_XY_PLANE:
this->NDim = 2;
this->DimIndex[0] = 0;
this->DimIndex[1] = 1;
break;
case VTK_XZ_PLANE:
this->NDim = 2;
this->DimIndex[0] = 0;
this->DimIndex[1] = 2;
break;
case VTK_YZ_PLANE:
this->NDim = 2;
this->DimIndex[0] = 1;
this->DimIndex[1] = 2;
break;
case VTK_XYZ_GRID:
this->NDim = 3;
this->DimIndex[0] = 0;
this->DimIndex[1] = 1;
this->DimIndex[2] = 2;
break;
default:
vtkGenericWarningMacro(
<< "Cannot handle data description: "
<< this->DataDescription << "\n");
} // END switch
assert( "post: NDim==2 || NDim==3" && ( this->NDim==2 || this->NDim==3 ) );
}
};
//------------------------------------------------------------------------------
struct StructuredGrid
{
int ID;
int Extent[6];
int DataDescription;
int Grow[3]; // indicates if the grid grows to the right along each dim.
int Implicit[3]; // indicates implicit connectivity alone each dim.
vtkPoints* Nodes;
vtkPointData* PointData;
// arrays used if the grid is a rectilinear grid
vtkDataArray* X_Coords;
vtkDataArray* Y_Coords;
vtkDataArray* Z_Coords;
std::vector< ImplicitNeighbor > Neighbors;
//------------------------------------------------------------------------------
bool IsRectilinearGrid()
{
if( (this->X_Coords != nullptr) &&
(this->Y_Coords != nullptr) &&
(this->Z_Coords != nullptr) )
{
return true;
}
return false;
}
//------------------------------------------------------------------------------
void Clear()
{
if( this->Nodes != nullptr )
{
this->Nodes->Delete();
this->Nodes = nullptr;
}
if( this->PointData != nullptr )
{
this->PointData->Delete();
this->PointData = nullptr;
}
if( this->X_Coords != nullptr )
{
this->X_Coords->Delete();
this->X_Coords = nullptr;
}
if( this->Y_Coords != nullptr )
{
this->Y_Coords->Delete();
this->Y_Coords = nullptr;
}
if( this->Z_Coords != nullptr )
{
this->Z_Coords->Delete();
this->Z_Coords = nullptr;
}
this->Neighbors.clear();
}
//------------------------------------------------------------------------------
void Initialize(StructuredGrid* grid)
{
assert("pre: input grid is nullptr!" && (grid != nullptr) );
this->Initialize(grid->ID,grid->Extent,nullptr,nullptr);
// Grow the extent in each dimension as needed
for(int i=0; i < 3; ++i)
{
if( grid->Grow[i]==1 )
{
this->Extent[i*2+1] += 1;
} // END if
} // END for all dimensions
// the number of nodes in the grown extent
vtkIdType nnodes = vtkStructuredData::GetNumberOfPoints(
this->Extent,grid->DataDescription);
// Allocate coordinates, if needed
if( grid->Nodes != nullptr )
{
this->Nodes = vtkPoints::New();
this->Nodes->SetDataType( grid->Nodes->GetDataType() );
this->Nodes->SetNumberOfPoints( nnodes );
} // END if has points
else
{
this->Nodes = nullptr;
}
// Allocate rectilinear grid coordinates, if needed
if( (grid->X_Coords != nullptr) &&
(grid->Y_Coords != nullptr) &&
(grid->Z_Coords != nullptr) )
{
int dims[3];
vtkStructuredData::GetDimensionsFromExtent(
this->Extent,dims,this->DataDescription);
this->X_Coords = vtkDataArray::CreateDataArray(
grid->X_Coords->GetDataType());
this->X_Coords->SetNumberOfTuples( dims[0] );
for(vtkIdType idx=0; idx < grid->X_Coords->GetNumberOfTuples(); ++idx)
{
this->X_Coords->SetTuple(idx,idx,grid->X_Coords);
}
this->Y_Coords = vtkDataArray::CreateDataArray(
grid->Y_Coords->GetDataType());
this->Y_Coords->SetNumberOfTuples( dims[1] );
for(vtkIdType idx=0; idx < grid->Y_Coords->GetNumberOfTuples(); ++idx)
{
this->Y_Coords->SetTuple(idx,idx,grid->Y_Coords);
}
this->Z_Coords = vtkDataArray::CreateDataArray(
grid->Z_Coords->GetDataType());
this->Z_Coords->SetNumberOfTuples( dims[2] );
for(vtkIdType idx=0; idx < grid->Z_Coords->GetNumberOfTuples(); ++idx)
{
this->Z_Coords->SetTuple(idx,idx,grid->Z_Coords);
}
} // END if rectilinear grid
else
{
grid->X_Coords = nullptr;
grid->Y_Coords = nullptr;
grid->Z_Coords = nullptr;
}
// Allocate fields, if needed
if( grid->PointData != nullptr )
{
this->PointData = vtkPointData::New();
this->PointData->CopyAllocate(grid->PointData,nnodes);
// NOTE: CopyAllocate, allocates the buffers internally, but, does not
// set the number of tuples of each array to nnodes.
for(int array=0; array < this->PointData->GetNumberOfArrays(); ++array)
{
vtkDataArray* a = this->PointData->GetArray( array );
a->SetNumberOfTuples(nnodes);
} // END for all arrays
}
else
{
this->PointData = nullptr;
}
// copy everything from the given grid
int desc = grid->DataDescription;
int ijk[3] = {0,0,0};
for( I(ijk)=IMIN(grid->Extent); I(ijk) <= IMAX(grid->Extent); ++I(ijk) )
{
for( J(ijk)=JMIN(grid->Extent); J(ijk) <= JMAX(grid->Extent); ++J(ijk) )
{
for( K(ijk)=KMIN(grid->Extent); K(ijk) <= KMAX(grid->Extent); ++K(ijk) )
{
// Compute the source index
vtkIdType srcIdx =
vtkStructuredData::ComputePointIdForExtent(grid->Extent,ijk,desc);
// Compute the target index
vtkIdType targetIdx =
vtkStructuredData::ComputePointIdForExtent(this->Extent,ijk,desc);
// Copy nodes
if( this->Nodes != nullptr )
{
this->Nodes->SetPoint(targetIdx,grid->Nodes->GetPoint(srcIdx));
}
// Copy node-centered fields
if( this->PointData != nullptr )
{
this->PointData->CopyData(grid->PointData,srcIdx,targetIdx);
}
} // END for all k
} // END for all j
} // END for all i
}
//------------------------------------------------------------------------------
void Initialize(int id, int ext[6], vtkDataArray* x_coords,
vtkDataArray* y_coords, vtkDataArray* z_coords, vtkPointData* fields)
{
assert("pre: nullptr x_coords!" && (x_coords != nullptr) );
assert("pre: nullptr y_coords!" && (y_coords != nullptr) );
assert("pre: nullptr z_coords!" && (z_coords != nullptr) );
this->ID = id;
memcpy(this->Extent,ext,6*sizeof(int));
this->DataDescription = vtkStructuredData::GetDataDescriptionFromExtent(ext);
std::fill(this->Grow,this->Grow+3,0);
std::fill(this->Implicit,this->Implicit+3,0);
this->Nodes = nullptr;
// Effectively, shallow copy the coordinate arrays and maintain ownership
// of these arrays in the caller.
this->X_Coords = vtkDataArray::CreateDataArray(x_coords->GetDataType());
this->X_Coords->SetVoidArray(
x_coords->GetVoidPointer(0),x_coords->GetNumberOfTuples(),1);
this->Y_Coords = vtkDataArray::CreateDataArray(y_coords->GetDataType());
this->Y_Coords->SetVoidArray(
y_coords->GetVoidPointer(0),y_coords->GetNumberOfTuples(),1);
this->Z_Coords = vtkDataArray::CreateDataArray(z_coords->GetDataType());
this->Z_Coords->SetVoidArray(
z_coords->GetVoidPointer(0),z_coords->GetNumberOfTuples(),1);
if(fields != nullptr)
{
this->PointData = vtkPointData::New();
this->PointData->ShallowCopy(fields);
}
else
{
this->PointData = nullptr;
}
}
//------------------------------------------------------------------------------
void Initialize(int id, int ext[6], vtkPoints* nodes, vtkPointData* fields)
{
this->ID = id;
memcpy(this->Extent,ext,6*sizeof(int));
this->DataDescription = vtkStructuredData::GetDataDescriptionFromExtent(ext);
std::fill(this->Grow,this->Grow+3,0);
std::fill(this->Implicit,this->Implicit+3,0);
this->X_Coords = nullptr;
this->Y_Coords = nullptr;
this->Z_Coords = nullptr;
if(nodes != nullptr)
{
this->Nodes = vtkPoints::New();
this->Nodes->ShallowCopy(nodes);
}
else
{
this->Nodes = nullptr;
}
if(fields != nullptr)
{
this->PointData = vtkPointData::New();
this->PointData->ShallowCopy(fields);
}
else
{
this->PointData = nullptr;
}
}
};
//------------------------------------------------------------------------------
// CommManager class Definition
//------------------------------------------------------------------------------
class CommunicationManager
{
public:
CommunicationManager() {};
~CommunicationManager() { this->Clear(); };
unsigned char* GetRcvBuffer(const int fromRank);
unsigned int GetRcvBufferSize(const int fromRank);
void EnqueueRcv(const int fromRank);
void EnqueueSend(const int toRank, unsigned char* data, unsigned int nbytes);
void Exchange(vtkMPIController* comm);
int NumMsgs();
void Clear();
private:
// map send/rcv buffers based on rank.
std::map< int, unsigned char* > Send;
std::map< int, int> SendByteSize;
std::map< int, unsigned char* > Rcv;
std::map< int, int> RcvByteSize;
std::vector< vtkMPICommunicator::Request > Requests;
// exchanges buffer-sizes
void AllocateRcvBuffers(vtkMPIController* comm);
};
//------------------------------------------------------------------------------
void CommunicationManager::Clear()
{
this->Requests.clear();
this->SendByteSize.clear();
this->RcvByteSize.clear();
std::map<int,unsigned char*>::iterator it;
for(it=this->Send.begin(); it != this->Send.end(); ++it)
{
delete [] it->second;
}
this->Send.clear();
for(it=this->Rcv.begin(); it != this->Rcv.end(); ++it)
{
delete [] it->second;
}
this->Rcv.clear();
}
//------------------------------------------------------------------------------
unsigned char* CommunicationManager::GetRcvBuffer(const int fromRank)
{
assert( "pre: cannot find buffer for requested rank!" &&
(this->Rcv.find( fromRank ) != this->Rcv.end()) );
return( this->Rcv[ fromRank ] );
}
//------------------------------------------------------------------------------
unsigned int CommunicationManager::GetRcvBufferSize(const int fromRank)
{
assert( "pre: cannot find bytesize size of requested rank!" &&
(this->RcvByteSize.find( fromRank ) != this->RcvByteSize.end()) );
return( this->RcvByteSize[ fromRank ] );
}
//------------------------------------------------------------------------------
int CommunicationManager::NumMsgs()
{
return static_cast<int>( this->Send.size()+this->Rcv.size() );
}
//------------------------------------------------------------------------------
void CommunicationManager::EnqueueRcv(const int fromRank)
{
assert("pre: rcv from rank has already been enqueued!" &&
(this->Rcv.find(fromRank)==this->Rcv.end()) );
this->Rcv[ fromRank ] = nullptr;
this->RcvByteSize[ fromRank ] = 0;
}
//------------------------------------------------------------------------------
void CommunicationManager::EnqueueSend(
const int toRank, unsigned char* data, unsigned int nbytes)
{
assert("pre: send to rank has already been enqueued!" &&
(this->Send.find(toRank)==this->Send.end()));
this->Send[ toRank ] = data;
this->SendByteSize[ toRank ] = nbytes;
}
//------------------------------------------------------------------------------
void CommunicationManager::AllocateRcvBuffers(vtkMPIController* comm)
{
std::map<int,int>::iterator it;
// STEP 0: Allocate vector to store request objects for non-blocking comm.
int rqstIdx = 0;
this->Requests.resize(this->NumMsgs());
// STEP 1: Post receives
for(it=this->RcvByteSize.begin(); it != this->RcvByteSize.end(); ++it)
{
int fromRank = it->first;
int* dataPtr = &(it->second);
comm->NoBlockReceive(dataPtr,1,fromRank,0,this->Requests[rqstIdx]);
++rqstIdx;
}
// STEP 2: Post Sends
for(it=this->SendByteSize.begin(); it != this->SendByteSize.end(); ++it)
{
int toRank = it->first;
int* dataPtr = &(it->second);
comm->NoBlockSend(dataPtr,1,toRank,0,this->Requests[rqstIdx]);
++rqstIdx;
}
// STEP 3: WaitAll
if (!this->Requests.empty())
{
comm->WaitAll(this->NumMsgs(),&this->Requests[0]);
}
this->Requests.clear();
// STEP 4: Allocate rcv buffers
std::map<int,unsigned char*>::iterator bufferIter = this->Rcv.begin();
for( ;bufferIter != this->Rcv.end(); ++bufferIter)
{
int fromRank = bufferIter->first;
assert("pre: rcv buffer should be nullptr!" && (this->Rcv[fromRank]==nullptr) );
this->Rcv[ fromRank ] = new unsigned char[ this->RcvByteSize[fromRank] ];
}
}
//------------------------------------------------------------------------------
void CommunicationManager::Exchange(vtkMPIController* comm)
{
std::map<int,unsigned char*>::iterator it;
// STEP 0: exchange & allocate buffer sizes
this->AllocateRcvBuffers(comm);
// STEP 1: Allocate vector to store request objects for non-blocking comm.
int rqstIdx = 0;
this->Requests.resize(this->NumMsgs());
// STEP 2: Post Rcvs
for(it=this->Rcv.begin(); it != this->Rcv.end(); ++it)
{
int fromRank = it->first;
unsigned char* buffer = it->second;
assert("pre: rcv buffer size not found!" &&
this->RcvByteSize.find(fromRank) != this->RcvByteSize.end() );
unsigned int bytesize = this->RcvByteSize[ fromRank ];
comm->NoBlockReceive(buffer,bytesize,fromRank,0,this->Requests[rqstIdx]);
++rqstIdx;
}
// STEP 3: Post Sends
for(it=this->Send.begin(); it != this->Send.end(); ++it)
{
int toRank = it->first;
unsigned char* buffer = it->second;
assert("pre: rcv buffer size not found!" &&
this->SendByteSize.find(toRank) != this->SendByteSize.end() );
unsigned int bytesize = this->SendByteSize[ toRank ];
comm->NoBlockSend(buffer,bytesize,toRank,0,this->Requests[rqstIdx]);
++rqstIdx;
}
// STEP 4: WaitAll
if (!this->Requests.empty())
{
comm->WaitAll(this->NumMsgs(),&this->Requests[0]);
}
this->Requests.clear();
}
} // END namespace detail
} // END namespace vtk
//==============================================================================
// END INTERNAL DATASTRUCTURE DEFINITIONS
//==============================================================================
vtkStandardNewMacro(vtkStructuredImplicitConnectivity);
//------------------------------------------------------------------------------
vtkStructuredImplicitConnectivity::vtkStructuredImplicitConnectivity()
{
this->DomainInfo = nullptr;
this->InputGrid = nullptr;
this->OutputGrid = nullptr;
this->CommManager = nullptr;
this->Controller = vtkMPIController::SafeDownCast(
vtkMultiProcessController::GetGlobalController());
}
//------------------------------------------------------------------------------
vtkStructuredImplicitConnectivity::~vtkStructuredImplicitConnectivity()
{
delete this->DomainInfo;
this->DomainInfo = nullptr;
if( this->InputGrid != nullptr )
{
this->InputGrid->Clear();
delete this->InputGrid;
this->InputGrid = nullptr;
}
if( this->OutputGrid != nullptr )
{
this->OutputGrid->Clear();
delete this->OutputGrid;
this->OutputGrid = nullptr;
}
if( this->CommManager != nullptr )
{
this->CommManager->Clear();
delete this->CommManager;
this->CommManager = nullptr;
}
this->Controller = nullptr;
}
//------------------------------------------------------------------------------
void vtkStructuredImplicitConnectivity::PrintSelf(ostream& os,vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << "Controller: " << this->Controller << std::endl;
if( this->Controller != nullptr )
{
os << "Number of Ranks: " << this->Controller->GetNumberOfProcesses();
os << std::endl;
} // END if Controller != nullptr
os << "Input Grid: " << this->InputGrid << std::endl;
if( this->InputGrid != nullptr )
{
os << "Extent: [" << this->InputGrid->Extent[0];
os << ", " << this->InputGrid->Extent[1];
os << ", " << this->InputGrid->Extent[2];
os << ", " << this->InputGrid->Extent[3];
os << ", " << this->InputGrid->Extent[4];
os << ", " << this->InputGrid->Extent[5];
os << "] " << std::endl;
os << "Grow: [" << this->InputGrid->Grow[0];
os << ", " << this->InputGrid->Grow[1];
os << ", " << this->InputGrid->Grow[2];
os << "] " << std::endl;
os << "Number of Neighbors: " << this->InputGrid->Neighbors.size();
os << std::endl;
size_t N = this->InputGrid->Neighbors.size();
for(size_t nei=0; nei < N; ++nei)
{
os << "\t" << this->InputGrid->Neighbors[ nei ].ToString();
os << std::endl;
} // END for all neighbors
} // END if InputGrid != nullptr
}
//------------------------------------------------------------------------------
void vtkStructuredImplicitConnectivity::SetWholeExtent(int wholeExt[6])
{
delete this->DomainInfo;
this->DomainInfo = new vtk::detail::DomainMetaData();
this->DomainInfo->Initialize(wholeExt);
assert("post: Domain description does not match across ranks!" &&
this->GlobalDataDescriptionMatch() );
}
//------------------------------------------------------------------------------
void vtkStructuredImplicitConnectivity::RegisterGrid(
const int gridID,
int extent[6],
vtkPoints* gridNodes,
vtkPointData* pointData)
{
// Sanity Checks!
assert("pre: nullptr Domain, whole extent is not set!" &&
(this->DomainInfo != nullptr) );
assert("pre: input not nullptr in this process!" &&
(this->InputGrid == nullptr) );
assert("pre: input grid ID should be >= 0" && (gridID >= 0) );
delete this->InputGrid;
this->InputGrid = nullptr;
// Only add if the grid falls within the output extent. Processes that do
// not contain the VOI will fail this test.
if (this->DomainInfo->HasGrid(extent))
{
this->InputGrid = new vtk::detail::StructuredGrid();
this->InputGrid->Initialize(gridID,extent,gridNodes,pointData);
}
}
//------------------------------------------------------------------------------
void vtkStructuredImplicitConnectivity::RegisterRectilinearGrid(
const int gridID,
int extent[6],
vtkDataArray* xcoords,
vtkDataArray* ycoords,
vtkDataArray* zcoords,
vtkPointData* pointData)
{
// Sanity Checks!
assert("pre: nullptr Domain, whole extent is not set!" &&
(this->DomainInfo != nullptr) );
assert("pre: input not nullptr in this process!" &&
(this->InputGrid == nullptr) );
assert("pre: input grid ID should be >= 0" && (gridID >= 0) );
delete this->InputGrid;
this->InputGrid = nullptr;
// Only add if the grid falls within the output extent. Processes that do
// not contain the VOI will fail this test.