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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPStructuredGridConnectivity.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 "vtkPStructuredGridConnectivity.h"
#include "vtkObjectFactory.h"
#include "vtkMultiProcessController.h"
#include "vtkMPIController.h"
#include "vtkMPICommunicator.h"
#include "vtkMultiProcessStream.h"
#include <cassert>
#include <fstream>
#include <sstream>
vtkStandardNewMacro(vtkPStructuredGridConnectivity);
//------------------------------------------------------------------------------
vtkPStructuredGridConnectivity::vtkPStructuredGridConnectivity()
{
this->Controller = vtkMultiProcessController::GetGlobalController();
this->Initialized = false;
this->MPIRequests = NULL;
this->TotalNumberOfSends = 0;
this->TotalNumberOfRcvs = 0;
this->TotalNumberOfMsgs = 0;
}
//------------------------------------------------------------------------------
vtkPStructuredGridConnectivity::~vtkPStructuredGridConnectivity()
{
// STEP 0: Delete MPI requests list
delete [] this->MPIRequests;
// STEP 1: Clear all remote data
this->ClearRemoteData();
// STEP 2: Clear all raw buffers
this->ClearRawBuffers();
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::PrintSelf(
std::ostream& os,vtkIndent indent)
{
this->Superclass::PrintSelf( os, indent );
os << "Controller: " << this->Controller << std::endl;
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::Initialize()
{
if( !this->Initialized )
{
this->Rank = this->Controller->GetLocalProcessId();
this->Initialized = true;
}
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::SetNumberOfGrids( const unsigned int N )
{
if (N == 0)
{
vtkErrorMacro("Number of grids cannot be 0.");
return;
}
this->Superclass::SetNumberOfGrids( N );
this->GridRanks.resize( N,-1 );
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::RegisterGrid(
const int gridID, int extents[6],
vtkUnsignedCharArray* nodesGhostArray,
vtkUnsignedCharArray* cellGhostArray,
vtkPointData* pointData,
vtkCellData* cellData,
vtkPoints* gridNodes )
{
assert( "pre: gridID out-of-bounds!" &&
(gridID >= 0 && gridID < static_cast<int>(this->NumberOfGrids)));
this->Superclass::RegisterGrid( gridID, extents, nodesGhostArray,
cellGhostArray, pointData, cellData, gridNodes );
this->GridIds.push_back( gridID );
this->GridRanks[ gridID ] = this->Rank;
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::RegisterRemoteGrid(
const int gridID, int extents[6], int process )
{
// Sanity check
assert( "pre: gridID out-of-bounds!" &&
(gridID >= 0) && (gridID < static_cast<int>(this->GridRanks.size())));
// NOTE: remote grids only have their extents since that information is
// required to determine neighboring.
this->Superclass::RegisterGrid(
gridID, extents, NULL, NULL, NULL, NULL, NULL );
this->GridRanks[ gridID ] = process;
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::ComputeNeighbors()
{
assert( "pre: Instance has not been intialized!" && this->Initialized );
assert( "pre: Null multi-process controller" && (this->Controller != NULL) );
this->ExchangeGridExtents();
this->Controller->Barrier();
this->Superclass::ComputeNeighbors();
this->Controller->Barrier();
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::CreateGhostLayers( const int N )
{
assert( "pre: Instance has not been intialized!" && this->Initialized );
if( N==0 )
{
vtkWarningMacro(
"N=0 ghost layers requested! No ghost layers will be created!" );
this->Controller->Barrier();
return;
}
this->NumberOfGhostLayers += N;
this->AllocateInternalDataStructures();
this->GhostedExtents.resize(this->NumberOfGrids*6, -1 );
// STEP 0: Compute neighbor send and receive extent
for( unsigned int i=0; i < this->NumberOfGrids; ++i )
{
this->CreateGhostedExtent( i, N );
this->ComputeNeighborSendAndRcvExtent( i, N );
}
// BEGIN DEBUG
// std::ostringstream oss;
// oss << "Process" << this->Rank << "-neis.log";
// std::ofstream ofs;
// ofs.open( oss.str().c_str() );
// assert("ERROR: cannot write log file" && ofs.is_open() );
// this->Print( ofs );
// ofs.close();
// END DEBUG
this->Controller->Barrier();
// STEP 1: Exchange ghost-data
this->ExchangeGhostData();
this->Controller->Barrier();
// STEP 4: Create ghost-layers
for( unsigned int i=0; i < this->NumberOfGrids; ++i )
{
if( this->IsGridLocal( i ) )
{
this->CreateGhostedMaskArrays( i );
this->InitializeGhostData( i );
this->TransferRegisteredDataToGhostedData( i );
this->TransferGhostDataFromNeighbors( i );
}
} // END for all grids
// STEP 5: Synchronize
this->Controller->Barrier();
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::TransferRemoteNeighborData(
const int gridIdx, const int nei, const vtkStructuredNeighbor& Neighbor )
{
// Sanity check
assert( "pre: gridID is out-of-bounds!" &&
(gridIdx >= 0) && (gridIdx < static_cast<int>(this->NumberOfGrids)));
assert( "pre: Neighbor grid ID is out-of-bounds!" &&
(Neighbor.NeighborID >= 0) &&
(Neighbor.NeighborID < static_cast<int>(this->NumberOfGrids)));
assert( "pre: RemotePoints has not been properly allocated!" &&
this->RemotePoints.size() == this->NumberOfGrids );
assert( "pre: RemotePointData has not been properly allocated!" &&
this->RemotePointData.size() == this->NumberOfGrids );
assert( "pre: RemoteCellData has not been properly allocated!" &&
this->RemoteCellData.size() == this->NumberOfGrids );
// STEP 0: Get the ghosted grid (node) extent and cell extent
int GhostedGridExtent[6];
this->GetGhostedGridExtent( gridIdx, GhostedGridExtent );
int GhostedGridCellExtent[6];
vtkStructuredData::GetCellExtentFromPointExtent(
GhostedGridExtent, GhostedGridCellExtent );
// STEP 1: Get Neighboring cell extent
int RcvCellExtent[6];
vtkStructuredData::GetCellExtentFromPointExtent(
const_cast<int*>(Neighbor.RcvExtent), RcvCellExtent);
// STEP 2: Transfer the data
int ijk[3];
for( int i=Neighbor.RcvExtent[0]; i <= Neighbor.RcvExtent[1]; ++i )
{
for( int j=Neighbor.RcvExtent[2]; j <= Neighbor.RcvExtent[3]; ++j )
{
for( int k=Neighbor.RcvExtent[4]; k <= Neighbor.RcvExtent[5]; ++k )
{
assert( "pre: RcvExtent is outside of the GhostExtent!" &&
this->IsNodeWithinExtent(i,j,k,GhostedGridExtent) );
ijk[0]=i; ijk[1]=j; ijk[2]=k;
if( this->HasPoints(gridIdx) )
{
// Compute the source (node) index into the remote neighbor data
vtkIdType srcIdx =
vtkStructuredData::ComputePointIdForExtent(
const_cast<int*>(Neighbor.RcvExtent),ijk);
// Compute the target (node) index into the ghost data
vtkIdType targetIdx =
vtkStructuredData::ComputePointIdForExtent(
GhostedGridExtent, ijk, this->DataDescription );
this->CopyCoordinates(
this->RemotePoints[gridIdx][nei], srcIdx,
this->GhostedGridPoints[gridIdx], targetIdx );
} // END if there are grid points registered
if( this->HasPointData(gridIdx) )
{
// Compute the source (node) index into the remote neighbor data
vtkIdType srcIdx =
vtkStructuredData::ComputePointIdForExtent(
const_cast<int*>(Neighbor.RcvExtent),ijk);
// Compute the target (node) index into the ghost data
vtkIdType targetIdx =
vtkStructuredData::ComputePointIdForExtent(
GhostedGridExtent, ijk, this->DataDescription );
// Transfer node data from remote to the ghosted grid data
this->CopyFieldData(
this->RemotePointData[gridIdx][nei],srcIdx,
this->GhostedGridPointData[gridIdx],targetIdx);
} // END if has remote point data
if( this->HasCellData(gridIdx) &&
this->IsNodeWithinExtent(i,j,k,RcvCellExtent) )
{
// Compute the source cell index, Note, since we are passing a cell
// extent to ComputePointIdForExtent, the result will be a cell ID
// and not a point ID.
vtkIdType sourceCellIdx =
vtkStructuredData::ComputePointIdForExtent(
RcvCellExtent, ijk );
// Compute the target cell index. Note, since we are passing a cell
// extent to ComputePointIdForExtent, the result will be a cell ID
// and not a point ID.
vtkIdType targetCellIdx =
vtkStructuredData::ComputePointIdForExtent(
GhostedGridCellExtent, ijk, this->DataDescription);
// Transfer the cell data
this->CopyFieldData(
this->RemoteCellData[gridIdx][nei], sourceCellIdx,
this->GhostedGridCellData[gridIdx], targetCellIdx );
} // END if has remote cell data && is within the cell extent
} // END for all k
} // END for all j
} // END for all i
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::TransferGhostDataFromNeighbors(
const int gridID)
{
assert( "pre: gridID is out-of-bounds!" &&
(gridID >= 0) && (gridID < static_cast<int>(this->NumberOfGrids) ) );
assert( "pre: Neighbors are not properly allocated" &&
(this->NumberOfGrids==this->Neighbors.size() ) );
assert( "pre: grid must be local!" && this->IsGridLocal(gridID) );
int NumNeis = static_cast<int>(this->Neighbors[ gridID ].size());
for( int nei=0; nei < NumNeis; ++nei )
{
int neiGridIdx = this->Neighbors[gridID][nei].NeighborID;
if( this->IsGridLocal(neiGridIdx) )
{
this->TransferLocalNeighborData( gridID,this->Neighbors[gridID][nei] );
}
else
{
this->TransferRemoteNeighborData(
gridID, nei, this->Neighbors[gridID][nei]);
}
} // END for all neighbors
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::PackGhostData()
{
assert("pre: SendBuffers is not properly allocated!" &&
this->SendBuffers.size()==this->NumberOfGrids );
assert("pre: RcvBuffers is not properly allocated!" &&
this->RcvBuffers.size()==this->NumberOfGrids );
for( unsigned int idx=0; idx < this->GridIds.size(); ++idx )
{
int gridIdx = this->GridIds[ idx ];
assert( "ERROR: grid index is out-of-bounds!" &&
(gridIdx >= 0) &&
(gridIdx < static_cast<int>(this->NumberOfGrids)));
int NumNeis = this->GetNumberOfNeighbors( gridIdx );
this->SendBuffers[gridIdx].resize( NumNeis, NULL );
this->RcvBuffers[gridIdx].resize( NumNeis, NULL);
this->RcvBufferSizes[gridIdx].resize( NumNeis, 0 );
this->SendBufferSizes[gridIdx].resize( NumNeis, 0 );
for(int nei=0; nei < NumNeis; ++nei )
{
this->RcvBufferSizes[gridIdx][nei] = 0;
int neiGridIdx = this->Neighbors[ gridIdx ][ nei ].NeighborID;
assert( "ERROR: neighbor grid index is out-of-bounds" &&
(neiGridIdx >= 0) &&
(neiGridIdx < static_cast<int>(this->NumberOfGrids) ) );
if( this->IsGridRemote( neiGridIdx ) )
{
this->TotalNumberOfSends++;
this->TotalNumberOfRcvs++;
this->SerializeGhostData(
gridIdx, neiGridIdx,
this->Neighbors[gridIdx][nei].SendExtent,
this->SendBuffers[gridIdx][nei],
this->SendBufferSizes[gridIdx][nei] );
} // END if the neighboring grid is remote
} // END for all neighbors
} // END for all local grids
this->TotalNumberOfMsgs = this->TotalNumberOfRcvs + this->TotalNumberOfSends;
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::SerializeBufferSizes(
int *&sizesbuf, vtkIdType &N)
{
assert("pre: sizes buffer must be NULL" && (sizesbuf==NULL) );
assert("pre: Number of sends should be at least 1" &&
(this->TotalNumberOfSends >= 1) );
int k = 0;
for( unsigned int idx=0; idx < this->GridIds.size(); ++idx )
{
k += this->GetNumberOfNeighbors( this->GridIds[idx] );
}
N = 3*k;
sizesbuf = new int[ N ];
assert("pre: Cannot allocate sizes buffer" && (sizesbuf != NULL) );
int bidx = 0; // index to the buffer
for( unsigned int idx=0; idx < this->GridIds.size(); ++idx )
{
int gridIdx = this->GridIds[ idx ];
assert("pre: grid index is out-of-bounds" &&
(gridIdx >= 0) && (gridIdx < static_cast<int>(this->NumberOfGrids)));
int NumNeis = this->GetNumberOfNeighbors( gridIdx );
for( int nei=0; nei < NumNeis; ++nei )
{
// Sender grid
sizesbuf[bidx] = gridIdx;
++bidx;
// Receiver grid
sizesbuf[bidx] = this->Neighbors[gridIdx][nei].NeighborID;
++bidx;
// Buffer size
sizesbuf[bidx] = this->SendBufferSizes[gridIdx][nei];
++bidx;
} // END for all neighbors
} // END for all local grids
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::DeserializeBufferSizesForProcess(
int *buffersizes, vtkIdType N, const int vtkNotUsed(processId) )
{
assert("pre: Controller should not be NULL" && (this->Controller != NULL) );
assert("pre: Cannot deserialize empty buffer size" && (buffersizes != NULL) );
assert("pre: Buffer size should not be empty!" && (N > 0) );
assert("pre: Buffer size must be a multiple of 3" && ( (N%3)==0) );
assert("pre: RcvBuffersizes is not properly allocated!" &&
this->RcvBufferSizes.size() == this->NumberOfGrids);
int NumTuples = N/3;
for( int i=0; i < NumTuples; ++i )
{
int senderGrid = buffersizes[ i*3 ];
int rcvGrid = buffersizes[ i*3+1 ];
int size = buffersizes[ i*3+2 ];
if( this->IsGridLocal( rcvGrid ) )
{
int neiIndex = this->GetNeighborIndex(rcvGrid,senderGrid);
assert("ERROR: rcver grid is out-of-bounds!" &&
(rcvGrid >= 0) &&
(rcvGrid < static_cast<int>(this->NumberOfGrids)));
assert("ERROR: neighbor index is out-of-bounds!" &&
(neiIndex >= 0) &&
(neiIndex < static_cast<int>(this->RcvBufferSizes[rcvGrid].size())));
this->RcvBufferSizes[ rcvGrid ][ neiIndex ] = size;
} // END if the grid is local
} // END for all tuples
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::ExchangeBufferSizes()
{
vtkIdType N = 0;
int *sizesbuffer = NULL;
this->SerializeBufferSizes( sizesbuffer, N );
assert("ERROR: sizesbuffer is NULL!" && (sizesbuffer != NULL));
assert("ERROR: N > 0" && (N > 0));
// STEP 1: Get the number of ints each process will send with an all gather
vtkIdType numRanks = this->Controller->GetNumberOfProcesses();
vtkIdType *rcvcounts = new vtkIdType[ numRanks ];
this->Controller->AllGather( &N, rcvcounts, 1);
// STEP 2: Calculate the receive buffer size & Allocate
vtkIdType rcvBufferSize = rcvcounts[0];
for( int i=1; i < numRanks; ++i )
{
rcvBufferSize += rcvcounts[i];
}
int *rcvbuffer = new int[ rcvBufferSize ];
assert( "pre: receive buffer should not be NULL" && (rcvbuffer != NULL) );
// STEP 3: Calculate offset to the rcvbuffer for each rank
vtkIdType *offSet = new vtkIdType[ numRanks ];
offSet[0] = 0;
for( int i=1; i < numRanks; ++i )
{
offSet[ i ] = offSet[ i-1 ] + rcvcounts[ i-1 ];
}
// STEP 4: AllGatherv of all the remote buffer size information
this->Controller->AllGatherV( sizesbuffer, rcvbuffer, N, rcvcounts, offSet );
// STEP 5: Deserialize Grid Extent(s) for each remote process
for( int i=0; i < numRanks; ++i )
{
if( i != this->Rank )
{
this->DeserializeBufferSizesForProcess(
rcvbuffer+offSet[i], rcvcounts[i], i);
}// END if remote rank
} // END for all ranks
// STEP 6: Deallocate
delete [] sizesbuffer;
delete [] rcvcounts;
delete [] rcvbuffer;
delete [] offSet;
// STEP 7: Synch processes
this->Controller->Barrier();
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::UnpackGhostData()
{
assert("pre: RcvBuffers is not properly allocated!" &&
this->RcvBuffers.size()==this->NumberOfGrids );
for( unsigned int idx=0; idx < this->GridIds.size(); ++idx )
{
int gridIdx = this->GridIds[ idx ];
assert("ERROR: grid index is out-of-bounds!" &&
(gridIdx >= 0) &&
(gridIdx < static_cast<int>(this->NumberOfGrids)));
int NumNeis = this->GetNumberOfNeighbors( gridIdx );
assert("ERROR: rcv buffers for grid are not properly allocated" &&
(static_cast<int>(this->RcvBuffers[gridIdx].size())==NumNeis));
for( int nei=0; nei < NumNeis; ++nei )
{
// Get the global grid index of the neighboring grid
int neiGridIdx = this->Neighbors[ gridIdx ][ nei ].NeighborID;
assert( "ERROR: neighbor grid index is out-of-bounds" &&
(neiGridIdx >= 0) &&
(neiGridIdx < static_cast<int>(this->NumberOfGrids) ) );
// int neiListIndex = this->GetNeighborIndex( gridIdx, neiGridIdx );
// assert("ERROR: neiListIndex mismatch!" && (neiListIndex==nei) );
if( this->IsGridRemote(neiGridIdx) )
{
this->DeserializeGhostData(
gridIdx,nei,neiGridIdx,this->Neighbors[ gridIdx ][ nei ].RcvExtent,
this->RcvBuffers[ gridIdx ][ nei ],
this->RcvBufferSizes[ gridIdx ][ nei ] );
} // END if the grid is remote
} // END for all neighbors
} // END for all local grids
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::ExchangeGhostDataInit()
{
// STEP 0: Pack ghost data
this->PackGhostData();
// STEP 1: Exchange buffer size
this->ExchangeBufferSizes();
// STEP 2: Synchronize
this->Controller->Barrier();
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::PostReceives()
{
// STEP 0: Acquire MPI controller from supplied Multi-Process controller
vtkMPIController *myMPIController =
vtkMPIController::SafeDownCast(this->Controller);
assert("pre: Cannot acquire MPI controller" && (myMPIController != NULL) );
// STEP 1: Loop through all local grids and post receives
int rqstIdx = 0;
for( unsigned int idx=0; idx < this->GridIds.size(); ++idx )
{
int gridIdx = this->GridIds[ idx ];
assert("ERROR: grid index is out-of-bounds" &&
(gridIdx >= 0) &&
(gridIdx < static_cast<int>(this->NumberOfGrids) ) );
assert("ERROR: grid must be local" && this->IsGridLocal( gridIdx ) );
assert("ERROR: grid rcv buffers must be 1-to-1 with the grid neighbors" &&
this->Neighbors[gridIdx].size()==this->RcvBuffers[gridIdx].size() );
assert("ERROR: grid rcv buffers must be 1-to-1 with the rcv buffer sizes" &&
this->RcvBufferSizes[gridIdx].size()==this->RcvBuffers[gridIdx].size() );
size_t NumNeis = this->Neighbors[ gridIdx ].size();
for( size_t nei=0; nei < NumNeis; ++nei )
{
int neiGridIdx = this->Neighbors[gridIdx][nei].NeighborID;
assert("ERROR: Neighbor grid index is out-of-bounds!" &&
(neiGridIdx >= 0) &&
(neiGridIdx < static_cast<int>(this->NumberOfGrids) ) );
if( this->IsGridLocal( neiGridIdx ) )
{
// The neighboring grid is local, thus, the ghost data are transfered
// directly using vtkStructuredGrid::TransferLocalNeighborData().
// Consequently, there is no need for any communication.
continue;
}
int NeighborRank = this->GetGridRank( neiGridIdx );
assert("pre: RcvBuffer must be NULL!" &&
(this->RcvBuffers[gridIdx][nei] == NULL));
this->RcvBuffers[gridIdx][nei] =
new unsigned char[ this->RcvBufferSizes[gridIdx][nei] ];
assert("ERROR: Could not allocate RcvBuffer!" &&
this->RcvBuffers[gridIdx][nei] );
assert("pre: RequestIndex is out-of-bounds!" &&
(rqstIdx >= 0) && (rqstIdx < this->TotalNumberOfMsgs) );
unsigned char *bufferPtr = this->RcvBuffers[gridIdx][nei];
int length = this->RcvBufferSizes[ gridIdx][nei];
myMPIController->NoBlockReceive(
bufferPtr,length,NeighborRank,neiGridIdx,this->MPIRequests[rqstIdx]);
++rqstIdx;
} // END for all neis
} // END for all local grids
assert(this->TotalNumberOfRcvs == rqstIdx);
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::PostSends()
{
// STEP 0: Acquire MPI controller from supplied Multi-Process controller
vtkMPIController *myMPIController =
vtkMPIController::SafeDownCast(this->Controller);
assert("pre: Cannot acquire MPI controller" && (myMPIController != NULL) );
// STEP 1: Loop through all local grids and post receives
int rqstIdx = this->TotalNumberOfRcvs;
for( unsigned int idx=0; idx < this->GridIds.size(); ++idx )
{
int gridIdx = this->GridIds[ idx ];
assert("ERROR: grid index is out-of-bounds" &&
(gridIdx >= 0) &&
(gridIdx < static_cast<int>(this->NumberOfGrids) ) );
assert("ERROR: grid must be local" && this->IsGridLocal(gridIdx) );
assert("ERROR: grid rcv buffers must be 1-to-1 with the grid neighbors" &&
this->Neighbors[gridIdx].size()==this->SendBuffers[gridIdx].size() );
assert("ERROR: grid rcv buffers must be 1-to-1 with the rcv buffer sizes" &&
this->SendBufferSizes[gridIdx].size()==this->SendBuffers[gridIdx].size() );
int NumNeis = static_cast<int>(this->Neighbors[gridIdx].size());
for( int nei=0; nei < NumNeis; ++nei )
{
int neiGridIdx = this->Neighbors[gridIdx][nei].NeighborID;
assert("ERROR: Neighbor grid index is out-of-bounds!" &&
(neiGridIdx >= 0) &&
(neiGridIdx < static_cast<int>(this->NumberOfGrids) ) );
if( this->IsGridLocal( neiGridIdx ) )
{
// The neighboring grid is local, thus, the ghost data are transfered
// directly using vtkStructuredGrid::TransferLocalNeighborData().
// Consequently, there is no need for any communication.
continue;
}
int NeighborRank = this->GetGridRank( neiGridIdx );
assert("pre: RcvBuffer must be NULL!" &&
(this->SendBuffers[gridIdx][nei] != NULL));
assert("pre: RequestIndex is out-of-bounds!" &&
(rqstIdx >= 0) && (rqstIdx < this->TotalNumberOfMsgs) );
unsigned char *bufferPtr = this->SendBuffers[gridIdx][nei];
int length = this->SendBufferSizes[gridIdx][nei];
myMPIController->NoBlockSend(
bufferPtr,length,NeighborRank,gridIdx,this->MPIRequests[rqstIdx]);
++rqstIdx;
} // END for all neis
} // END for all local grids
assert(rqstIdx == this->TotalNumberOfMsgs);
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::CommunicateGhostData()
{
// STEP 0: Sanity Checks!
assert("pre: Instance has not been initialized!" && this->Initialized );
assert("pre: RcvBuffers is not properly allocated" &&
(this->RcvBuffers.size() == this->NumberOfGrids) );
assert("pre: RcvBufferSizes is not properly allocated" &&
(this->RcvBufferSizes.size() == this->NumberOfGrids ) );
assert("pre: Neighbors have not been computed!" &&
this->Neighbors.size() == this->NumberOfGrids );
assert("pre: MPI requests array must be NULL!" &&
(this->MPIRequests==NULL));
// STEP 1: Allocate the MPI requests array
this->MPIRequests = new vtkMPICommunicator::Request[this->TotalNumberOfMsgs];
assert("pre:Could not alloc MPI requests array" && (this->MPIRequests!=NULL));
// STEP 2: Allocate receive buffers and post receives
this->PostReceives();
this->Controller->Barrier();
// STEP 3: Post sends
this->PostSends();
this->Controller->Barrier();
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::ExchangeGhostDataPost()
{
vtkMPIController *myMPIController =
vtkMPIController::SafeDownCast(this->Controller);
assert("pre: Cannot acquire MPI controller" && (myMPIController != NULL) );
// STEP 0: Block until all communication is completed
myMPIController->WaitAll(this->TotalNumberOfMsgs,this->MPIRequests);
// STEP 1: Process receive buffers
this->UnpackGhostData();
// STEP 2: De-allocate receive buffers
this->ClearRawBuffers();
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::ExchangeGhostData()
{
assert( "pre: Instance has not been intialized!" && this->Initialized );
// STEP 0:
this->InitializeMessageCounters();
// STEP 1: Allocate internal data-structures
this->RemotePoints.resize( this->NumberOfGrids);
this->RemotePointData.resize( this->NumberOfGrids);
this->RemoteCellData.resize( this->NumberOfGrids);
for( unsigned int i=0; i < this->NumberOfGrids; ++i )
{
this->RemotePoints[ i ].resize( this->GetNumberOfNeighbors(i), NULL );
this->RemotePointData[ i ].resize( this->GetNumberOfNeighbors(i), NULL );
this->RemoteCellData[ i ].resize( this->GetNumberOfNeighbors(i), NULL );
}
this->SendBuffers.resize(this->NumberOfGrids);
this->RcvBuffers.resize(this->NumberOfGrids);
this->SendBufferSizes.resize(this->NumberOfGrids);
this->RcvBufferSizes.resize(this->NumberOfGrids);
// STEP 2: Serialize the ghost data and exchange buffer sizes
this->ExchangeGhostDataInit();
// STEP 3: Allocate rcv buffers and perform non-blocking communication
this->CommunicateGhostData();
// STEP 4: Block until communication is complete and raw rcv buffers are
// de-serialized into the VTK data-structures.
this->ExchangeGhostDataPost();
// STEP 5: Synchronize with all processes
this->Controller->Barrier();
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::SerializeGhostPoints(
const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream )
{
assert("pre: gridIdx is out-of-bounds" &&
(gridIdx >= 0) && (gridIdx < static_cast<int>(this->NumberOfGrids)));
assert("pre: GridPoints is not properly allocated" &&
this->GridPoints.size() == this->NumberOfGrids);
// STEP 0: Check if the user has registered points for this grid instance
if( this->GridPoints[ gridIdx ] == NULL )
{
// If not points are registered put a 0 in the bytestream and return!
bytestream << 0;
return;
}
// STEP 1: Otherwise, put a "1" in the bytestream to indicate that the are
// points included in the bytestream
bytestream << 1;
// STEP 2: Get the grid extent of the send grid
int GridExtent[6];
this->GetGridExtent( gridIdx, GridExtent );
// STEP 3: Compute the number of nodes in the send extent
int dataDescription = vtkStructuredData::GetDataDescriptionFromExtent( ext );
int N = vtkStructuredData::GetNumberOfPoints(ext, dataDescription );
// bytestream << N;
// STEP 4: Allocate and store points in a temporary array
double *pnts = new double[ 3*N ];
assert( "Cannot allocate temporary pnts array" && (pnts != NULL) );
int ijk[3];
double x[3];
for( int i=ext[0]; i <= ext[1]; ++i )
{
for( int j=ext[2]; j <= ext[3]; ++j )
{
for( int k=ext[4]; k <= ext[5]; ++k )
{
assert("pre: IJK must be within grid extent!" &&
this->IsNodeWithinExtent(i,j,k,GridExtent) );
ijk[0]=i; ijk[1]=j; ijk[2]=k;
// Compute the source index
vtkIdType sourceIdx =
vtkStructuredData::ComputePointIdForExtent(GridExtent,ijk);
assert("pre: sourceIdx is out-of-bounds" &&
(sourceIdx >= 0) &&
(sourceIdx < this->GridPoints[gridIdx]->GetNumberOfPoints()) );
this->GridPoints[ gridIdx ]->GetPoint(sourceIdx,x);
// Compute the target index
vtkIdType targetIdx =
vtkStructuredData::ComputePointIdForExtent(ext,ijk,dataDescription);
assert("pre: targetIdx is out-of-bounds" &&
(targetIdx >= 0) && (targetIdx < N) );
// Store the point
pnts[ targetIdx*3 ] = x[ 0 ];
pnts[ targetIdx*3+1 ] = x[ 1 ];
pnts[ targetIdx*3+2 ] = x[ 2 ];
} // END for all k
} // END for all j
} // END for all i
// STEP 5: Push the points on the bytestream
bytestream.Push( pnts, 3*N );
delete [] pnts;
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::DeserializeGhostPoints(
const int gridIdx, const int nei,
int ext[6], vtkMultiProcessStream& bytestream )
{
assert("pre:Cannot deserialize an empty bytestream" && !bytestream.Empty());
assert("pre:Grid index is out-of-bounds" &&
(gridIdx >= 0 ) &&
(gridIdx < static_cast<int>(this->GetNumberOfGrids())));
assert("pre:Neighbor list index is out-of-bounds" &&
(nei >=0) && (nei < this->GetNumberOfNeighbors(gridIdx)));
assert("pre:Remote points is not properly allocated!" &&
this->RemotePoints.size() == this->NumberOfGrids );
// STEP 0: Check if there are serialized points in the bytestream
int hasPoints;
bytestream >> hasPoints;
if( hasPoints == 0 )
{
return;
}
assert("pre:Remote points for grid is not properly allocated!" &&
(this->GetNumberOfNeighbors(gridIdx)==
static_cast<int>(this->RemotePoints[gridIdx].size())));
// STEP 1: If there are points, deserialize them
int dataDescription = vtkStructuredData::GetDataDescriptionFromExtent( ext );
int N = vtkStructuredData::GetNumberOfPoints(ext,dataDescription);
// STEP 2: Pop the points from the bytestream
double *pnts = NULL;
unsigned int size = 0;
bytestream.Pop( pnts, size );
assert("pre: deserialize ghosts points array is NULL" &&
(pnts != NULL) );
assert("pre: points array is not of the expected size!" &&
(static_cast<int>(size) == 3*N) );
this->RemotePoints[ gridIdx ][ nei ] = vtkPoints::New();
this->RemotePoints[ gridIdx ][ nei ]->SetDataTypeToDouble();
this->RemotePoints[ gridIdx ][ nei ]->SetNumberOfPoints( N );
for( int i=0; i < N ; ++i )
{
this->RemotePoints[ gridIdx ][ nei ]->SetPoint(
i, pnts[i*3], pnts[i*3+1], pnts[i*3+2] );
}
delete [] pnts;
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::SerializeDataArray(
vtkDataArray *dataArray, vtkMultiProcessStream& bytestream )
{
assert("pre: Cannot serialize a NULL data array" && (dataArray != NULL) );
// STEP 0: Compute number of elements in flat array
int k = dataArray->GetNumberOfComponents();
assert("pre: number of components must be at least 1" && (k>=1) );
vtkIdType N = dataArray->GetNumberOfTuples();
assert("pre: number of elements must be at least 1" && (N>=1) );
unsigned int size = N*k;
// STEP 1: Push the raw data into the bytestream
// TODO: Add more cases here
switch( dataArray->GetDataType( ) )
{
case VTK_FLOAT:
bytestream.Push(
static_cast<float*>(dataArray->GetVoidPointer(0)), size );
break;
case VTK_DOUBLE:
bytestream.Push(
static_cast<double*>(dataArray->GetVoidPointer(0)), size );
break;
case VTK_INT:
bytestream.Push(
static_cast<int*>(dataArray->GetVoidPointer(0)), size );
break;
default:
vtkErrorMacro("Cannot serialize data array of type"<<dataArray->GetDataType());
}
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::DeserializeDataArray(
vtkDataArray *&dataArray,
const int dataType,
const int numberOfTuples,
const int numberOfComponents,
vtkMultiProcessStream& bytestream )
{
assert("pre: Cannot deserialize an empty bytestream" && !bytestream.Empty());
switch( dataType )
{
case VTK_FLOAT:
{
// STEP 0: Get the raw data
unsigned int size = 0;
float *data = NULL;
bytestream.Pop( data, size );
assert( "pre: de-serialized data array is not of the expected size" &&
(size == static_cast<unsigned int>((numberOfTuples*numberOfComponents)) ) );
// STEP 1: Allocate vtkdata array
dataArray = vtkDataArray::CreateDataArray( dataType );
dataArray->SetNumberOfComponents( numberOfComponents );
dataArray->SetNumberOfTuples( numberOfTuples );
// STEP 2: Copy the data
float *dataArrayPtr = static_cast<float*>(dataArray->GetVoidPointer(0));
for( int i=0; i < numberOfTuples; ++i )
{
for( int j=0; j < numberOfComponents; ++j )
{
dataArrayPtr[ i*numberOfComponents+j ] =
data[ i*numberOfComponents+j ];
} // END for all components
}// END for all tuples
delete [] data;
} // END VTK_FLOAT case
break;
case VTK_DOUBLE:
{
// STEP 0: Get the raw data
unsigned int size = 0;
double *data = NULL;
bytestream.Pop(data,size);
assert( "pre: de-serialized data array is not of the expected size" &&
(size == static_cast<unsigned int>((numberOfTuples*numberOfComponents)) ) );
// STEP 1: Allocate vtkdata array
dataArray = vtkDataArray::CreateDataArray( dataType );
dataArray->SetNumberOfComponents(numberOfComponents);
dataArray->SetNumberOfTuples(numberOfTuples);
// STEP 2: Copy the data
double *dataArrayPtr = static_cast<double*>(dataArray->GetVoidPointer(0));
for( int i=0; i < numberOfTuples; ++i )
{
for( int j=0; j < numberOfComponents; ++j )
{
dataArrayPtr[ i*numberOfComponents+j ] =
data[ i*numberOfComponents+j ];
} // END for all components
} // END for all tuples
delete [] data;
} // END VTK_DOUBLE case
break;
case VTK_INT:
{
// STEP 0: Get the raw data
unsigned int size = 0;
int *data = NULL;
bytestream.Pop(data,size);
assert( "pre: de-serialized data array is not of the expected size" &&
(size == static_cast<unsigned int>((numberOfTuples*numberOfComponents)) ) );
// STEP 1: Allocate vtkdata array
dataArray = vtkDataArray::CreateDataArray( dataType );
dataArray->SetNumberOfComponents( numberOfComponents );
dataArray->SetNumberOfTuples( numberOfTuples );
// STEP 2: Copy the data
int *dataArrayPtr = static_cast<int*>(dataArray->GetVoidPointer(0));
for( int i=0; i < numberOfTuples; ++i )
{
for( int j=0; j < numberOfComponents; ++j )
{
dataArrayPtr[ i*numberOfComponents+j ] =
data[ i*numberOfComponents+j ];
} // END for all components
} // END for all tuples
delete [] data;
} // END VTK_INT case
break;
default:
vtkErrorMacro("Cannot de-serialize data array of this type");
assert(false);
}
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::SerializeFieldData(
int GridExtent[6], int ext[6], vtkFieldData *fieldData,
vtkMultiProcessStream& bytestream )
{
assert("pre: FieldData is NULL" && (fieldData != NULL) );
// STEP 0: Write the number of arrays
bytestream << fieldData->GetNumberOfArrays();
// For each array:
for( int array=0; array < fieldData->GetNumberOfArrays(); ++array )
{
vtkDataArray *myArray = fieldData->GetArray( array );
assert( "pre: attempting to serialize a NULL array!" && (myArray!=NULL) );
int dataType = myArray->GetDataType();
int numComp = myArray->GetNumberOfComponents();
int numTuples = vtkStructuredData::GetNumberOfPoints(ext);
// STEP 1: Write the datatype and number of components
bytestream << dataType << numTuples << numComp;
bytestream << std::string( myArray->GetName() );
// STEP 2: Extract the ghost data within the given ext
// Allocate the ghost array where the data will be extracted
vtkDataArray *ghostArray =
vtkDataArray::CreateDataArray( myArray->GetDataType() );
ghostArray->SetName( myArray->GetName() );
ghostArray->SetNumberOfComponents( numComp );
ghostArray->SetNumberOfTuples( numTuples );
int ijk[3];
for( int i=ext[0]; i <= ext[1]; ++i )
{
for( int j=ext[2]; j <= ext[3]; ++j )
{
for( int k=ext[4]; k <= ext[5]; ++k )
{
ijk[0]=i; ijk[1]=j; ijk[2]=k;
assert("pre: IJK is outside the grid extent!" &&
this->IsNodeWithinExtent(i,j,k,GridExtent) );
// Compute the source index from the grid extent. Note, this could
// be a cell index if the incoming GridExtent and ext are cell extents.
vtkIdType sourceIdx =
vtkStructuredData::ComputePointIdForExtent(GridExtent,ijk);
assert("pre: source index is out-of-bounds!" &&
(sourceIdx >= 0) &&
(sourceIdx < myArray->GetNumberOfTuples() ) );
// Compute the target index from the grid extent. Note, this could
// be a cell index if the incoming GridExtent and ext are cell extents.
vtkIdType targetIdx =
vtkStructuredData::ComputePointIdForExtent(ext,ijk);
assert("pre: target index is out-of-bounds!" &&
(targetIdx >= 0) &&
(targetIdx < ghostArray->GetNumberOfTuples() ) );
ghostArray->SetTuple( targetIdx, sourceIdx, myArray );
} // END for k
} // END for j
} // END for i
// STEP 3: Serialize the ghost array
this->SerializeDataArray( ghostArray, bytestream );
ghostArray->Delete();
} // END for all arrays
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::DeserializeFieldData(
int* vtkNotUsed(ext), vtkFieldData* fieldData, vtkMultiProcessStream& bytestream )
{
assert("pre: Cannot deserialize an empty bytestream" && !bytestream.Empty());
assert("pre: field data should not be NULL!" && (fieldData != NULL) );
int NumberOfArrays;
bytestream >> NumberOfArrays;
assert("ERROR: number of arrays must be greater or equal to 1" &&
(NumberOfArrays >= 1) );
vtkDataArray *dataArray;
int dataType;
int numTuples;
int numComponents;
std::string arrayName;
for( int i=0; i < NumberOfArrays; ++i )
{
dataArray = NULL;
bytestream >> dataType >> numTuples >> numComponents;
bytestream >> arrayName;
this->DeserializeDataArray(
dataArray, dataType, numTuples, numComponents, bytestream );
assert("ERROR: data array should not be NULL!" && (dataArray != NULL));
dataArray->SetName( arrayName.c_str() );
fieldData->AddArray( dataArray );
dataArray->Delete();
} // END for all arrays
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::SerializeGhostPointData(
const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream )
{
assert("pre: Grid to be serialized must be local" &&
this->IsGridLocal( gridIdx ) );
assert("pre: gridIdx is out-of-bounds" &&
(gridIdx >= 0) && (gridIdx < static_cast<int>(this->NumberOfGrids)));
assert("pre: GridPointData is not properly allocated" &&
(this->GridPointData.size()==this->NumberOfGrids));
if( (this->GridPointData[gridIdx] == NULL) ||
(this->GridPointData[gridIdx]->GetNumberOfArrays() == 0) )
{
bytestream << 0;
return;
}
// STEP 0: Get the grid's node extent
int GridExtent[6];
this->GetGridExtent( gridIdx, GridExtent );
// STEP 1: Serialize the node data
bytestream << 1;
this->SerializeFieldData(
GridExtent, ext, this->GridPointData[gridIdx], bytestream );
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::DeserializeGhostPointData(
const int gridIdx, const int nei,int ext[6],
vtkMultiProcessStream& bytestream )
{
assert("pre: Cannot deserialize an empty bytestream" && !bytestream.Empty());
assert("pre:Grid index is out-of-bounds" &&
(gridIdx >= 0 ) &&
(gridIdx < static_cast<int>(this->GetNumberOfGrids())));
assert("pre: Grid to be serialized must be local" &&
this->IsGridLocal(gridIdx) );
assert("pre:Neighbor list index is out-of-bounds" &&
(nei >=0) && (nei < this->GetNumberOfNeighbors(gridIdx)));
assert("pre:Remote point data is not properly allocated!" &&
this->RemotePointData.size() == this->NumberOfGrids );
// STEP 0: Check if there are point data in the byte-stream
int hasPointData;
bytestream >> hasPointData;
if( hasPointData == 0 )
{
return;
}
assert("pre:Remote point data for grid is not properly allocated!" &&
(this->GetNumberOfNeighbors(gridIdx)==
static_cast<int>(this->RemotePointData[gridIdx].size())));
// STEP 1: De-serialize the point data
this->RemotePointData[gridIdx][nei] = vtkPointData::New();
this->DeserializeFieldData(ext,this->RemotePointData[gridIdx][nei],bytestream);
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::SerializeGhostCellData(
const int gridIdx, int ext[6], vtkMultiProcessStream& bytestream )
{
assert("pre: Grid to be serialized must be local" &&
this->IsGridLocal( gridIdx ) );
assert("pre: gridIdx is out-of-bounds" &&
(gridIdx >= 0) && (gridIdx < static_cast<int>(this->NumberOfGrids)));
assert("pre: GridCellData is not properly allocated" &&
(this->GridCellData.size()==this->NumberOfGrids));
if( this->GridCellData[gridIdx] == NULL ||
this->GridCellData[gridIdx]->GetNumberOfArrays() == 0 )
{
bytestream << 0;
return;
}
// STEP 0: Get the grid node/cell extent
int GridExtent[6];
this->GetGridExtent( gridIdx, GridExtent );
int GridCellExtent[6];
vtkStructuredData::GetCellExtentFromPointExtent(GridExtent,GridCellExtent);
// STEP 1: Get the cell extent of the sub-extent
int cellExtent[6];
vtkStructuredData::GetCellExtentFromPointExtent( ext, cellExtent );
// STEP 2: Serialize the cell data
bytestream << 1;
this->SerializeFieldData(
GridCellExtent, cellExtent,
this->GridCellData[gridIdx], bytestream );
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::DeserializeGhostCellData(
const int gridIdx, const int nei, int ext[6],
vtkMultiProcessStream& bytestream )
{
assert("pre: Cannot deserialize an empty bytestream" && !bytestream.Empty());
assert("pre:Grid index is out-of-bounds" &&
(gridIdx >= 0 ) &&
(gridIdx < static_cast<int>(this->GetNumberOfGrids())));
assert("pre: Grid to be serialized must be local" &&
this->IsGridLocal(gridIdx) );
assert("pre:Neighbor list index is out-of-bounds" &&
(nei >=0) && (nei < this->GetNumberOfNeighbors(gridIdx)));
assert("pre:Remote point data is not properly allocated!" &&
this->RemoteCellData.size() == this->NumberOfGrids );
// STEP 0: Check if there are cell data in the byte-stream
int hasCellData;
bytestream >> hasCellData;
if( hasCellData == 0)
{
return;
}
assert("pre:Remote cell data for grid is not properly allocated!" &&
(this->GetNumberOfNeighbors(gridIdx)==
static_cast<int>(this->RemoteCellData[gridIdx].size())));
// STEP 1: De-serialize the cell data
this->RemoteCellData[gridIdx][nei] = vtkCellData::New();
int cellext[6];
vtkStructuredData::GetCellExtentFromPointExtent( ext, cellext );
this->DeserializeFieldData(
cellext,this->RemoteCellData[gridIdx][nei],bytestream);
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::SerializeGhostData(
const int sendGridID, const int rcvGrid, int sndext[6],
unsigned char*& buffer, unsigned int& size)
{
// Pre-conditions
assert("pre: Grid to be serialized must be local" &&
this->IsGridLocal( sendGridID ) );
assert("pre: Receive grid should not be local" &&
!this->IsGridLocal(rcvGrid) );
assert("pre: sendGridID out-of-bounds!" &&
(sendGridID >= 0 && sendGridID < static_cast<int>(this->NumberOfGrids)));
assert("pre: rcvGridID is out-of-bounds!" &&
(rcvGrid >= 0) && rcvGrid < static_cast<int>(this->NumberOfGrids) );
vtkMultiProcessStream bytestream;
// STEP 0: Write the header
bytestream << sendGridID << rcvGrid;
bytestream.Push( sndext, 6 );
// STEP 1: Serialize the points
this->SerializeGhostPoints( sendGridID, sndext, bytestream );
// STEP 2: Serialize point data (if any)
this->SerializeGhostPointData(sendGridID, sndext, bytestream );
// STEP 3: Serialize cell data (if any)
this->SerializeGhostCellData( sendGridID, sndext, bytestream );
// STEP 4: Get the raw data buffer
bytestream.GetRawData( buffer, size );
// BEGIN DEBUG
// std::ostringstream oss;
// oss << "SendLog_" << this->Rank << ".txt";
// std::ofstream ofs;
// ofs.open( oss.str().c_str() );
// ofs << "SEND MESSAGE SIZE: " << size << "\n";
// ofs << sendGridID << " " << rcvGrid << std::endl;
// for( int i=0; i < 6; ++i )
// {
// ofs << sndext[ i ] << " ";
// }
// ofs << std::endl;
// ofs.close();
// END DEBUG
// Post-conditions
assert("post: buffer should not be NULL!" && (buffer != NULL) );
assert("post: size > 0" && (size > 0) );
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::DeserializeGhostData(
const int gridID, const int neiGridID, const int vtkNotUsed(neiGridIdx),
int rcvext[6],unsigned char *buffer, unsigned int size )
{
assert("pre: raw buffer is NULL!" && (buffer != NULL) );
assert("pre: raw buffer size > 0" && (size > 0) );
// STEP 0: Constructs the byte-stream object with raw data
vtkMultiProcessStream bytestream;
bytestream.SetRawData( buffer, size );
// STEP 1: Extract the header
int remoteGrid;
int rcvGrid;
bytestream >> remoteGrid >> rcvGrid;
// assert("pre: Remote grid must match the Neighbor grid index" &&
// (neiGridIdx==remoteGrid) );
assert("pre: Serialized receiver grid must match this grid instance" &&
(rcvGrid == gridID) );
// STEP 2: Extract the rcv extent
int *ext = NULL;
unsigned int s = 0;
bytestream.Pop(ext,s);
// BEGIN DEBUG
// std::ostringstream oss;
// oss << "RcvLog_" << this->Rank << ".txt";
// std::ofstream ofs;
// ofs.open( oss.str().c_str() );
// ofs << "RCV MESSAGE SIZE: " << size << "\n";
// ofs << remoteGrid << " " << rcvGrid << std::endl;
// for( int i=0; i < 6; ++i )
// {
// ofs << ext[ i ] << " ";
// }
// ofs << std::endl;
// ofs.close();
// END DEBUG
assert("ERROR: parsed extent is not of expected size" && (s==6));
assert("ERROR: parsed extent does not matched expected receive extent" &&
this->GridExtentsAreEqual(ext,rcvext) );
// std::ostringstream oss;
// oss << "Extents-" << this->Rank << "-" << neiGridIdx << ".txt";
// std::ofstream ofs;
// ofs.open( oss.str().c_str() );
// assert( ofs.is_open() );
//
// ofs << "GRID " << gridID << std::endl;
// bool error = false;
// for( int i=0; i < 6; ++i )
// {
// ofs << i << " ";
// if( ext[i] == rcvext[i] )
// {
// ofs << ext[i] << "==" << rcvext[i] << "!\n";
// }
// else
// {
// ofs << ext[i] << "!=" << rcvext[i] << "!!!\n";
// error = true;
// }
// }
//
// ofs.close();
// assert("ERROR: Serialize send extent must match receive extent" && !error );
delete [] ext;
// STEP 2: De-serialize the grid points
this->DeserializeGhostPoints(gridID, neiGridID, rcvext, bytestream);
// STEP 3: De-serialize the ghost point data
this->DeserializeGhostPointData(gridID, neiGridID, rcvext, bytestream);
// STEP 4: De-serialize the ghost cell data
this->DeserializeGhostCellData(gridID, neiGridID, rcvext, bytestream);
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::ExchangeGridExtents()
{
assert( "pre: Instance has not been intialized!" && this->Initialized );
assert( "pre: Controller is NULL!" && (this->Controller != NULL) );
// STEP 0: Serialize the data buffer
int *buffer = NULL;
vtkIdType N = 0;
this->SerializeGridExtents( buffer, N );
assert( "pre: buffer != NULL" && (buffer != NULL) );
assert( "pre: N > 0" && (N > 0) );
// STEP 1: Get the number of ints each process will send with an all gather
vtkIdType numRanks = this->Controller->GetNumberOfProcesses();
vtkIdType *rcvcounts = new vtkIdType[ numRanks ];
this->Controller->AllGather( &N, rcvcounts, 1);
// STEP 2: Calculate the receive buffer size & Allocate
vtkIdType rcvBufferSize = rcvcounts[0];
for( int i=1; i < numRanks; ++i )
{
rcvBufferSize += rcvcounts[i];
}
int *rcvbuffer = new int[ rcvBufferSize ];
assert( "pre: receive buffer should not be NULL" && (rcvbuffer != NULL) );
// STEP 3: Calculate offset to the rcvbuffer for each rank
vtkIdType *offSet = new vtkIdType[ numRanks ];
offSet[0] = 0;
for( int i=1; i < numRanks; ++i )
{
offSet[ i ] = offSet[ i-1 ] + rcvcounts[ i-1 ];
}
// STEP 4: AllGatherv of all the extent information
this->Controller->AllGatherV( buffer, rcvbuffer, N, rcvcounts, offSet );
// STEP 5: Deserialize Grid Extent(s) for each remote process
for( int i=0; i < numRanks; ++i )
{
if( i != this->Rank )
{
this->DeserializeGridExtentForProcess(rcvbuffer+offSet[i],rcvcounts[i],i);
}// END if remote rank
} // END for all ranks
// STEP 6: Deallocate
delete [] buffer;
delete [] rcvcounts;
delete [] rcvbuffer;
delete [] offSet;
// STEP 7: Synch processes
this->Controller->Barrier();
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::SerializeGridExtents(
int *&sndbuffer, vtkIdType &N )
{
assert( "pre: Instance has not been intialized!" && this->Initialized );
assert( "pre: send buffer is expected ot be NULL" && sndbuffer == NULL );
//Each local extent is serialized with 7 ints:ID imin imax jmin jmax kmin kmax
N = this->GetNumberOfLocalGrids()*7;
sndbuffer = new int[ N ];
for( int i=0; i < this->GetNumberOfLocalGrids(); ++i )
{
int gridID = this->GridIds[ i ];
int ext[6];
this->GetGridExtent( gridID, ext );
sndbuffer[ i*7 ] = gridID;
for( int j=0; j < 6; ++j )
{
sndbuffer[ (i*7)+(j+1) ] = ext[ j ];
}
} // END for all local grids
}
//------------------------------------------------------------------------------
void vtkPStructuredGridConnectivity::DeserializeGridExtentForProcess(
int *rcvbuffer, vtkIdType &N, const int processId )
{
// Sanity checks
assert( "pre: Instance has not been intialized!" && this->Initialized );
assert( "pre: Process controller should not be NULL!" &&
(this->Controller != NULL) );
assert( "pre: rcvbuffer should not be NULL" && (rcvbuffer != NULL) );
assert( "pre: must be called for a remote process" &&
(processId != this->Rank) );
assert( "pre: processId out-of-bounds!" && (processId >= 0) &&
(processId < this->Controller->GetNumberOfProcesses()) );
assert( "pre: extents must be a multiple of 7" && ( (N%7) == 0) );
int numGrids = N/7;
for( int i=0; i < numGrids; ++i )
{
int gridID = rcvbuffer[i*7];
int ext[6];
for( int j=0; j < 6; ++j )
{
ext[ j ] = rcvbuffer[ (i*7)+(j+1) ];
}
this->RegisterRemoteGrid( gridID, ext, processId );
}
}
|