forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkCommunicator.cxx
1691 lines (1545 loc) · 52.6 KB
/
vtkCommunicator.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: vtkCommunicator.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.
=========================================================================*/
#include "vtkCommunicator.h"
#include "vtkBoundingBox.h"
#include "vtkCharArray.h"
#include "vtkCompositeDataSet.h"
#include "vtkDataObjectTypes.h"
#include "vtkDataSetAttributes.h"
#include "vtkDataSetReader.h"
#include "vtkDataSetWriter.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkGenericDataObjectReader.h"
#include "vtkGenericDataObjectWriter.h"
#include "vtkIdTypeArray.h"
#include "vtkImageData.h"
#include "vtkIntArray.h"
#include "vtkMultiProcessController.h"
#include "vtkMultiProcessStream.h"
#include "vtkNew.h"
#include "vtkRectilinearGrid.h"
#include "vtkSmartPointer.h"
#include "vtkStructuredGrid.h"
#include "vtkStructuredPoints.h"
#include "vtkTypeTraits.h"
#include "vtkTable.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnsignedLongArray.h"
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
#include <algorithm>
#include <vector>
#define EXTENT_HEADER_SIZE 128
//=============================================================================
// Functions and classes that perform the default reduction operations.
#define STANDARD_OPERATION_DEFINITION(name, op) \
template<class T> \
void vtkCommunicator##name##Func(const T *A, T *B, vtkIdType length) \
{ \
for (vtkIdType i = 0; i < length; i++) B[i] = (op); \
} \
class vtkCommunicator##name##Class \
: public vtkCommunicator::Operation \
{ \
public: \
void Function(const void *A, void *B, vtkIdType length, int datatype) { \
switch (datatype) \
{ \
vtkTemplateMacro(vtkCommunicator##name##Func \
(reinterpret_cast<const VTK_TT *>(A), \
reinterpret_cast<VTK_TT *>(B), \
length)); \
} \
} \
int Commutative() { return 1; } \
};
#define STANDARD_OPERATION_FLOAT_OVERRIDE(name) \
static void vtkCommunicator##name##Func(const double *, double *, vtkIdType)\
{ \
vtkGenericWarningMacro(<< #name \
<< " not supported for floating point numbers"); \
} \
static void vtkCommunicator##name##Func(const float *, float *, vtkIdType) \
{ \
vtkGenericWarningMacro(<< #name \
<< " not supported for floating point numbers"); \
}
STANDARD_OPERATION_DEFINITION(Max, (A[i] < B[i] ? B[i] : A[i]));
STANDARD_OPERATION_DEFINITION(Min, (A[i] < B[i] ? A[i] : B[i]));
STANDARD_OPERATION_DEFINITION(Sum, A[i] + B[i]);
STANDARD_OPERATION_DEFINITION(Product, A[i] * B[i]);
STANDARD_OPERATION_FLOAT_OVERRIDE(LogicalAnd);
STANDARD_OPERATION_DEFINITION(LogicalAnd, A[i] && B[i]);
STANDARD_OPERATION_FLOAT_OVERRIDE(BitwiseAnd);
STANDARD_OPERATION_DEFINITION(BitwiseAnd, A[i] & B[i]);
STANDARD_OPERATION_FLOAT_OVERRIDE(LogicalOr);
STANDARD_OPERATION_DEFINITION(LogicalOr, A[i] || B[i]);
STANDARD_OPERATION_FLOAT_OVERRIDE(BitwiseOr);
STANDARD_OPERATION_DEFINITION(BitwiseOr, A[i] | B[i]);
STANDARD_OPERATION_FLOAT_OVERRIDE(LogicalXor);
STANDARD_OPERATION_DEFINITION(LogicalXor, (!A[i] && B[i]) || (A[i] && !B[i]));
STANDARD_OPERATION_FLOAT_OVERRIDE(BitwiseXor);
STANDARD_OPERATION_DEFINITION(BitwiseXor, A[i] ^ B[i]);
//=============================================================================
vtkCommunicator::vtkCommunicator()
{
this->LocalProcessId = 0;
this->NumberOfProcesses = 1;
this->MaximumNumberOfProcesses = vtkTypeTraits<int>::Max();
this->Count = 0;
}
//----------------------------------------------------------------------------
vtkCommunicator::~vtkCommunicator()
{
}
//----------------------------------------------------------------------------
int vtkCommunicator::UseCopy = 0;
void vtkCommunicator::SetUseCopy(int useCopy)
{
vtkCommunicator::UseCopy = useCopy;
}
//----------------------------------------------------------------------------
void vtkCommunicator::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "MaximumNumberOfProcesses: "
<< this->MaximumNumberOfProcesses << endl;
os << indent << "NumberOfProcesses: " << this->NumberOfProcesses << endl;
os << indent << "LocalProcessId: " << this->LocalProcessId << endl;
os << indent << "Count: " << this->Count << endl;
}
//----------------------------------------------------------------------------
void vtkCommunicator::SetNumberOfProcesses(int num)
{
if (num == this->NumberOfProcesses)
{
return;
}
if (num < 1 || num > this->MaximumNumberOfProcesses)
{
vtkErrorMacro( << num
<< " is an invalid number of processes try a number from 1 to "
<< this->NumberOfProcesses );
return;
}
this->NumberOfProcesses = num;
this->Modified();
}
//----------------------------------------------------------------------------
// Need to add better error checking
int vtkCommunicator::Send(vtkDataObject* data, int remoteHandle,
int tag)
{
// If the receiving end is using with ANY_SOURCE, we have a problem because
// some versions of MPI might deliver the multiple data objects require out of
// order. To get around this, on the first message we send the actual source
// and a mangled tag. The remote process then receives the rest of the
// messages with the specific source and mangled tag, which are guaranteed to
// be received in the correct order.
static int tagMangler = 1000;
int mangledTag = tag + tagMangler++;
int header[2];
header[0] = this->LocalProcessId; header[1] = mangledTag;
this->Send(header, 2, remoteHandle, tag);
tag = mangledTag;
int data_type = data? data->GetDataObjectType() : -1;
this->Send(&data_type, 1, remoteHandle, tag);
switch(data_type)
{
case -1:
// NULL data.
return 1;
//error on types we can't send
case VTK_DATA_OBJECT:
case VTK_DATA_SET:
case VTK_PIECEWISE_FUNCTION:
case VTK_POINT_SET:
case VTK_UNIFORM_GRID:
case VTK_GENERIC_DATA_SET:
case VTK_HYPER_OCTREE:
case VTK_COMPOSITE_DATA_SET:
case VTK_HIERARCHICAL_BOX_DATA_SET: // since we cannot send vtkUniformGrid anyways.
case VTK_MULTIGROUP_DATA_SET: // obsolete
case VTK_HIERARCHICAL_DATA_SET: //obsolete
default:
vtkWarningMacro(<< "Cannot send " << data->GetClassName());
return 0;
//send elemental data objects
case VTK_DIRECTED_GRAPH:
case VTK_UNDIRECTED_GRAPH:
case VTK_IMAGE_DATA:
case VTK_POLY_DATA:
case VTK_RECTILINEAR_GRID:
case VTK_STRUCTURED_GRID:
case VTK_STRUCTURED_POINTS:
case VTK_TABLE:
case VTK_TREE:
case VTK_UNSTRUCTURED_GRID:
case VTK_MULTIBLOCK_DATA_SET:
case VTK_UNIFORM_GRID_AMR:
return this->SendElementalDataObject(data, remoteHandle, tag);
}
}
//----------------------------------------------------------------------------
int vtkCommunicator::SendElementalDataObject(
vtkDataObject* data, int remoteHandle,
int tag)
{
VTK_CREATE(vtkCharArray, buffer);
if (vtkCommunicator::MarshalDataObject(data, buffer))
{
return this->Send(buffer, remoteHandle, tag);
}
// could not marshal data
return 0;
}
//----------------------------------------------------------------------------
int vtkCommunicator::Send(vtkDataArray* data, int remoteHandle, int tag)
{
// If the receiving end is using with ANY_SOURCE, we have a problem because
// some versions of MPI might deliver the multiple data objects require out of
// order. To get around this, on the first message we send the actual source
// and a mangled tag. The remote process then receives the rest of the
// messages with the specific source and mangled tag, which are guaranteed to
// be received in the correct order.
static int tagMangler = 1000;
int mangledTag = tag + tagMangler++;
int header[2];
header[0] = this->LocalProcessId; header[1] = mangledTag;
this->Send(header, 2, remoteHandle, tag);
tag = mangledTag;
int type = -1;
if (data == NULL)
{
this->Send( &type, 1, remoteHandle, tag);
return 1;
}
// send array type
type = data->GetDataType();
this->Send( &type, 1, remoteHandle, tag);
// send array tuples
vtkIdType numTuples = data->GetNumberOfTuples();
this->Send( &numTuples, 1, remoteHandle, tag);
// send number of components in array
int numComponents = data->GetNumberOfComponents();
this->Send( &numComponents, 1, remoteHandle, tag);
vtkIdType size = numTuples*numComponents;
const char* name = data->GetName();
int len = 0;
if (name)
{
len = static_cast<int>(strlen(name)) + 1;
}
// send length of name
this->Send( &len, 1, remoteHandle, tag);
if (len > 0)
{
// send name
this->Send( const_cast<char*>(name), len, remoteHandle, tag);
}
// do nothing if size is zero.
if (size == 0)
{
return 1;
}
// now send the raw array
this->SendVoidArray(data->GetVoidPointer(0), size, type, remoteHandle, tag);
return 1;
}
//----------------------------------------------------------------------------
int vtkCommunicator::Receive(vtkDataObject* data, int remoteHandle,
int tag)
{
//fill in the data object we are given
return this->ReceiveDataObject(data, remoteHandle, tag, -1);
}
//----------------------------------------------------------------------------
vtkDataObject *vtkCommunicator::ReceiveDataObject(int remoteHandle, int tag)
{
// If we are receiving with ANY_SOURCE, we have a problem because some
// versions of MPI might deliver the multiple data objects require out of
// order. To get around this, on the first message we receive the actual
// source and a mangled tag. We then receive the rest of the messages with
// the specific source and mangled tag, which we are guaranteed to receive in
// the correct order.
int header[2];
this->Receive(header, 2, remoteHandle, tag);
// Use the specific source and tag.
if (remoteHandle == vtkMultiProcessController::ANY_SOURCE)
{
remoteHandle = header[0];
}
tag = header[1];
int data_type = 0;
this->Receive(&data_type, 1, remoteHandle, tag);
if (data_type < 0)
{
// NULL data object.
return NULL;
}
//manufacture a data object of the proper type to fill
vtkDataObject * dObj = vtkDataObjectTypes::NewDataObject(data_type);
if (dObj != NULL)
{
if (this->ReceiveDataObject(dObj, remoteHandle, tag, data_type) == 1)
{
return dObj;
}
}
if (dObj)
{
dObj->Delete();
}
return NULL;
}
//----------------------------------------------------------------------------
int vtkCommunicator::ReceiveDataObject(vtkDataObject* data, int remoteHandle,
int tag, int dataType)
{
// If we have not yet received the data type, get the header and data type.
int data_type = dataType;
if (data_type == -1)
{
// If we are receiving with ANY_SOURCE, we have a problem because some
// versions of MPI might deliver the multiple data objects require out of
// order. To get around this, on the first message we receive the actual
// source and a mangled tag. We then receive the rest of the messages with
// the specific source and mangled tag, which we are guaranteed to receive
// in the correct order.
int header[2];
this->Receive(header, 2, remoteHandle, tag);
// Use the specific source and tag.
if (remoteHandle == vtkMultiProcessController::ANY_SOURCE)
{
remoteHandle = header[0];
}
tag = header[1];
this->Receive(&data_type, 1, remoteHandle, tag);
if (data->GetDataObjectType() != data_type)
{
vtkErrorMacro("Cannot receive object, type sent is different from destination.");
return 0;
}
}
switch(data_type)
{
//error on types we can't receive
case VTK_DATA_OBJECT:
case VTK_DATA_SET:
case VTK_PIECEWISE_FUNCTION:
case VTK_POINT_SET:
case VTK_UNIFORM_GRID:
case VTK_GENERIC_DATA_SET:
case VTK_HYPER_OCTREE:
case VTK_COMPOSITE_DATA_SET:
case VTK_HIERARCHICAL_BOX_DATA_SET: // since we cannot send vtkUniformGrid anyways.
case VTK_MULTIGROUP_DATA_SET: //obsolete.
case VTK_HIERARCHICAL_DATA_SET: // obsolete.
default:
vtkWarningMacro(
<< "Cannot receive "
<< vtkDataObjectTypes::GetClassNameFromTypeId(data_type));
return 0;
//receive elemental data objects
case VTK_DIRECTED_GRAPH:
case VTK_UNDIRECTED_GRAPH:
case VTK_IMAGE_DATA:
case VTK_POLY_DATA:
case VTK_RECTILINEAR_GRID:
case VTK_STRUCTURED_GRID:
case VTK_STRUCTURED_POINTS:
case VTK_TABLE:
case VTK_TREE:
case VTK_UNSTRUCTURED_GRID:
case VTK_MULTIBLOCK_DATA_SET:
case VTK_UNIFORM_GRID_AMR:
return this->ReceiveElementalDataObject(data, remoteHandle, tag);
}
}
//----------------------------------------------------------------------------
int vtkCommunicator::ReceiveElementalDataObject(
vtkDataObject* data, int remoteHandle,
int tag)
{
VTK_CREATE(vtkCharArray, buffer);
if (!this->Receive(buffer, remoteHandle, tag))
{
return 0;
}
return vtkCommunicator::UnMarshalDataObject(buffer, data);
}
int vtkCommunicator::Receive(vtkDataArray* data, int remoteHandle, int tag)
{
// If we are receiving with ANY_SOURCE, we have a problem because some
// versions of MPI might deliver the multiple data objects require out of
// order. To get around this, on the first message we receive the actual
// source and a mangled tag. We then receive the rest of the messages with
// the specific source and mangled tag, which we are guaranteed to receive in
// the correct order.
int header[2];
this->Receive(header, 2, remoteHandle, tag);
// Use the specific source and tag.
if (remoteHandle == vtkMultiProcessController::ANY_SOURCE)
{
remoteHandle = header[0];
}
tag = header[1];
// First receive the data type.
int type;
if (!this->Receive( &type, 1, remoteHandle, tag))
{
vtkErrorMacro("Could not receive data!");
return 0;
}
if (type == -1)
{ // This indicates a NULL object was sent. Do nothing.
return 1;
}
if (type != data->GetDataType())
{
vtkErrorMacro("Send/receive data types do not match!");
return 0;
}
// Next receive the number of tuples.
vtkIdType numTuples;
if (!this->Receive( &numTuples, 1, remoteHandle, tag))
{
vtkErrorMacro("Could not receive data!");
return 0;
}
// Next receive the number of components.
int numComponents;
this->Receive( &numComponents, 1, remoteHandle, tag);
vtkIdType size = numTuples*numComponents;
data->SetNumberOfComponents(numComponents);
data->SetNumberOfTuples(numTuples);
// Next receive the length of the name.
int nameLength;
this->Receive( &nameLength, 1, remoteHandle, tag);
if ( nameLength > 0 )
{
char *str = new char[nameLength];
// Receive the name
this->Receive(str, nameLength, remoteHandle, tag);
data->SetName(str);
}
else
{
data->SetName(NULL);
}
if (size < 0)
{
vtkErrorMacro("Bad data length");
return 0;
}
// Do nothing if size is zero.
if (size == 0)
{
return 1;
}
// now receive the raw array.
this->ReceiveVoidArray(data->GetVoidPointer(0), size, type, remoteHandle,tag);
return 1;
}
//-----------------------------------------------------------------------------
int vtkCommunicator::MarshalDataObject(vtkDataObject *object,
vtkCharArray *buffer)
{
buffer->Initialize();
buffer->SetNumberOfComponents(1);
if (object == NULL)
{
buffer->SetNumberOfTuples(0);
return 1;
}
VTK_CREATE(vtkGenericDataObjectWriter, writer);
vtkSmartPointer<vtkDataObject> copy;
copy.TakeReference(object->NewInstance());
copy->ShallowCopy(object);
writer->SetFileTypeToBinary();
// There is a problem with binary files with no data.
if (vtkDataSet::SafeDownCast(copy) != NULL)
{
vtkDataSet *ds = vtkDataSet::SafeDownCast(copy);
if (ds->GetNumberOfCells() + ds->GetNumberOfPoints() == 0)
{
writer->SetFileTypeToASCII();
}
}
writer->WriteToOutputStringOn();
writer->SetInputData(copy);
if (!writer->Write())
{
vtkGenericWarningMacro("Error detected while marshaling data object.");
return 0;
}
unsigned int size = writer->GetOutputStringLength();
if (object->GetExtentType() == VTK_3D_EXTENT)
{
// You would think that the extent information would be properly saved, but
// no, it is not.
int extent[6] = {0,0,0,0,0,0};
vtkRectilinearGrid* rg = vtkRectilinearGrid::SafeDownCast(object);
vtkStructuredGrid* sg = vtkStructuredGrid::SafeDownCast(object);
vtkImageData* id = vtkImageData::SafeDownCast(object);
if (rg)
{
rg->GetExtent(extent);
}
else if (sg)
{
sg->GetExtent(extent);
}
else if (id)
{
id->GetExtent(extent);
}
char extentHeader[EXTENT_HEADER_SIZE];
sprintf(extentHeader, "EXTENT %d %d %d %d %d %d",
extent[0], extent[1], extent[2], extent[3], extent[4], extent[5]);
buffer->SetNumberOfTuples(size+EXTENT_HEADER_SIZE);
memcpy(buffer->GetPointer(0), extentHeader, EXTENT_HEADER_SIZE);
memcpy(buffer->GetPointer(EXTENT_HEADER_SIZE), writer->GetOutputString(),
size);
}
else
{
buffer->SetArray(writer->RegisterAndGetOutputString(),
size,
0,
vtkCharArray::VTK_DATA_ARRAY_DELETE);
buffer->SetNumberOfTuples(size);
}
return 1;
}
//-----------------------------------------------------------------------------
int vtkCommunicator::UnMarshalDataObject(vtkCharArray *buffer,
vtkDataObject *object)
{
vtkIdType bufferSize = buffer->GetNumberOfTuples();
if (bufferSize <= 0)
{
object = NULL;
return 1;
}
// You would think that the extent information would be properly saved, but
// no, it is not.
int extent[6] = {0,0,0,0,0,0};
char *bufferArray = buffer->GetPointer(0);
if (strncmp(bufferArray, "EXTENT", 6) == 0)
{
sscanf(bufferArray, "EXTENT %d %d %d %d %d %d", &extent[0], &extent[1],
&extent[2], &extent[3], &extent[4], &extent[5]);
bufferArray += EXTENT_HEADER_SIZE;
bufferSize -= EXTENT_HEADER_SIZE;
}
// Make a temporary array object holding the part of the buffer that can be
// parsed by the reader.
VTK_CREATE(vtkCharArray, objectBuffer);
objectBuffer->SetNumberOfComponents(1);
objectBuffer->SetArray(bufferArray, bufferSize, 1);
VTK_CREATE(vtkGenericDataObjectReader, reader);
reader->ReadFromInputStringOn();
reader->SetInputArray(objectBuffer);
reader->Update();
if (!reader->GetOutput()->IsA(object->GetClassName()))
{
vtkGenericWarningMacro("Type mismatch while unmarshalling data.");
}
object->ShallowCopy(reader->GetOutput());
if (object->GetExtentType() == VTK_3D_EXTENT)
{
vtkRectilinearGrid* rg = vtkRectilinearGrid::SafeDownCast(object);
vtkStructuredGrid* sg = vtkStructuredGrid::SafeDownCast(object);
vtkImageData* id = vtkImageData::SafeDownCast(object);
if (rg)
{
rg->SetExtent(extent);
}
else if (sg)
{
sg->SetExtent(extent);
}
else if (id)
{
// If we fix the extent, we need to fix the origin too.
double origin[3];
id->GetOrigin(origin);
double spacing[3];
id->GetSpacing(spacing);
int readerExt[6];
id->GetExtent(readerExt);
for (int i=0; i<3; i++)
{
if (readerExt[2*i] != extent[2*i])
{
origin[i] = origin[i] - (extent[2*i] - readerExt[2*i])*spacing[i];
}
}
id->SetExtent(extent);
id->SetOrigin(origin);
}
}
return 1;
}
// The processors are views as a heap tree. The root is the processor of
// id 0.
//-----------------------------------------------------------------------------
int vtkCommunicator::GetParentProcessor(int proc)
{
int result;
if(proc%2==1)
{
result=proc>>1; // /2
}
else
{
result=(proc-1)>>1; // /2
}
return result;
}
int vtkCommunicator::GetLeftChildProcessor(int proc)
{
return (proc<<1)+1; // *2+1
}
int vtkCommunicator::ComputeGlobalBounds(int processNumber, int numProcessors,
vtkBoundingBox *bounds,
int *rhb, int *lhb,
int hasBoundsTag,
int localBoundsTag,
int globalBoundsTag)
{
int parent = 0;
int leftHasBounds = 0, rightHasBounds = 0;
int left = this->GetLeftChildProcessor(processNumber);
int right=left+1;
if(processNumber>0) // not root (nothing to do if root)
{
parent=this->GetParentProcessor(processNumber);
}
double otherBounds[6];
if(left<numProcessors)
{
this->Receive(&leftHasBounds, 1, left, hasBoundsTag);
if (lhb)
{
*lhb = leftHasBounds;
}
if(leftHasBounds)
{
this->Receive(otherBounds, 6, left, localBoundsTag);
bounds->AddBounds(otherBounds);
}
}
if(right<numProcessors)
{
// Grab the bounds from right child
this->Receive(&rightHasBounds, 1, right, hasBoundsTag);
if (rhb)
{
*rhb = rightHasBounds;
}
if(rightHasBounds)
{
this->Receive(otherBounds, 6, right, localBoundsTag);
bounds->AddBounds(otherBounds);
}
}
// If there are bounds to send do so
int boundsHaveBeenSet = bounds->IsValid();
double b[6];
// Send local to parent, Receive global from the parent.
if(processNumber > 0) // not root (nothing to do if root)
{
this->Send(&boundsHaveBeenSet, 1, parent, hasBoundsTag);
if(boundsHaveBeenSet)
{
// Copy the bounds to an array so we can send them
bounds->GetBounds(b);
this->Send(b, 6, parent, localBoundsTag);
this->Receive(b, 6, parent, globalBoundsTag);
bounds->AddBounds(b);
}
}
if(!boundsHaveBeenSet) // empty, no bounds, nothing to do
{
return 1;
}
// Send it to children.
bounds->GetBounds(b);
if(left<numProcessors)
{
if(leftHasBounds)
{
this->Send(b, 6, left, globalBoundsTag);
}
if(right<numProcessors)
{
if(rightHasBounds)
{
this->Send(b, 6, right, globalBoundsTag);
}
}
}
return 1;
}
//=============================================================================
// Collective operations.
//
// The implementations of these are very simple and probably inefficient.
// Most of the time we expect to be using an MPI controller, which has its
// own special implementations in the underlying API, so there is no good
// reason to work on creating a really good implementation here.
//-----------------------------------------------------------------------------
void vtkCommunicator::Barrier()
{
int junk = 0;
if (this->LocalProcessId == 0)
{
for (int i = 1; i < this->NumberOfProcesses; i++)
{
this->Receive(&junk, 1, i, BARRIER_TAG);
}
}
else
{
this->Send(&junk, 1, 0, BARRIER_TAG);
}
this->Broadcast(&junk, 1, 0);
}
//-----------------------------------------------------------------------------
int vtkCommunicator::BroadcastVoidArray(void *data, vtkIdType length,
int type, int srcProcessId)
{
if (srcProcessId == this->LocalProcessId)
{
int result = 1;
for (int i = 0; i < this->NumberOfProcesses; i++)
{
if (i != this->LocalProcessId)
{
result &= this->SendVoidArray(data, length, type, i, BROADCAST_TAG);
}
}
return result;
}
else
{
return this->ReceiveVoidArray(data, length, type,
srcProcessId, BROADCAST_TAG);
}
}
//-----------------------------------------------------------------------------
int vtkCommunicator::Broadcast(vtkDataObject *data, int srcProcessId)
{
VTK_CREATE(vtkCharArray, buffer);
if (this->LocalProcessId == srcProcessId)
{
if (vtkCommunicator::MarshalDataObject(data, buffer))
{
return this->Broadcast(buffer, srcProcessId);
}
else
{
// Could not marshal data.
return 0;
}
}
else
{
if (!this->Broadcast(buffer, srcProcessId))
{
return 0;
}
return vtkCommunicator::UnMarshalDataObject(buffer, data);
}
}
//-----------------------------------------------------------------------------
// We are more careful about duplicating all the metadata in the broadcast than
// the other collective operations, because it is more like a send/recv.
int vtkCommunicator::Broadcast(vtkDataArray *data, int srcProcessId)
{
int type;
vtkIdType numTuples;
int numComponents;
int nameLength = 0;
char *name = NULL;
// On the source process, extract the metadata.
if (this->LocalProcessId == srcProcessId)
{
type = data->GetDataType();
numTuples = data->GetNumberOfTuples();
numComponents = data->GetNumberOfComponents();
nameLength = 0;
name = data->GetName();
if (name)
{
nameLength = static_cast<int>(strlen(name))+1;
}
}
// Broadcast the metadata
if (!this->Broadcast(&type, 1, srcProcessId)) return 0;
if (!this->Broadcast(&numTuples, 1, srcProcessId)) return 0;
if (!this->Broadcast(&numComponents, 1, srcProcessId)) return 0;
if (!this->Broadcast(&nameLength, 1, srcProcessId)) return 0;
// On the destinations, allocate buffers.
if (this->LocalProcessId != srcProcessId)
{
if (data->GetDataType() != type)
{
vtkErrorMacro("Broadcast data types do not match!");
return 0;
}
name = (nameLength > 0) ? new char[nameLength] : NULL;
data->SetNumberOfComponents(numComponents);
data->SetNumberOfTuples(numTuples);
}
// Send the actual data.
if (nameLength > 0)
{
if (!this->Broadcast(name, nameLength, srcProcessId)) return 0;
}
if (!this->BroadcastVoidArray(data->GetVoidPointer(0),
numTuples*numComponents,
data->GetDataType(), srcProcessId)) return 0;
// Cleanup
if (this->LocalProcessId != srcProcessId)
{
if (nameLength > 0)
{
data->SetName(name);
delete[] name;
}
}
return 1;
}
//-----------------------------------------------------------------------------
int vtkCommunicator::GatherVoidArray(const void *sendBuffer,
void *recvBuffer,
vtkIdType length,
int type,
int destProcessId)
{
if (this->LocalProcessId == destProcessId)
{
int result = 1;
char *dest = reinterpret_cast<char *>(recvBuffer);
int typeSize = 1;
switch (type)
{
vtkTemplateMacro(typeSize = sizeof(VTK_TT));
}
// Copy local data first in case buffers are the same.
memmove(dest + this->LocalProcessId*length*typeSize, sendBuffer,
length*typeSize);
// Receive everything else.
for (int i = 0; i < this->NumberOfProcesses; i++)
{
if (this->LocalProcessId != i)
{
result &= this->ReceiveVoidArray(dest + i*length*typeSize,
length, type, i,
GATHER_TAG);
}
}
return result;
}
else
{
return this->SendVoidArray(sendBuffer, length, type,
destProcessId, GATHER_TAG);
}
}
//-----------------------------------------------------------------------------
int vtkCommunicator::Gather(vtkDataArray *sendBuffer,
vtkDataArray *recvBuffer,
int destProcessId)
{
int type = sendBuffer->GetDataType();
const void *sb = sendBuffer->GetVoidPointer(0);
void *rb = NULL;
int numComponents = sendBuffer->GetNumberOfComponents();
vtkIdType numTuples = sendBuffer->GetNumberOfTuples();
if (this->LocalProcessId == destProcessId)
{
if (type != recvBuffer->GetDataType())
{
vtkErrorMacro(<< "Data type mismatch.");
return 0;
}
recvBuffer->SetNumberOfComponents(numComponents);
recvBuffer->SetNumberOfTuples(numTuples*this->NumberOfProcesses);
rb = recvBuffer->GetVoidPointer(0);
}
return this->GatherVoidArray(sb, rb, numComponents*numTuples, type,
destProcessId);
}
//-----------------------------------------------------------------------------
int vtkCommunicator::GatherV(vtkDataArray *sendBuffer,
vtkDataArray* recvBuffer,
vtkSmartPointer<vtkDataArray> *recvBuffers,
int destProcessId)
{
vtkNew<vtkIdTypeArray> recvLengths;
vtkNew<vtkIdTypeArray> offsets;
int retValue = this->GatherV(
sendBuffer, recvBuffer,
recvLengths.GetPointer(), offsets.GetPointer(), destProcessId);
if (destProcessId == this->LocalProcessId)
{