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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
// This class was developed by Takuya Oshima at Niigata University,
// Japan (oshima@eng.niigata-u.ac.jp).
//
// Mark Olesen (OpenCFD Ltd.) www.openfoam.com
// has provided various bugfixes, improvements, cleanup
//
// ---------------------------------------------------------------------------
//
// Bugs or support questions should be addressed to the discourse forum
// https://discourse.paraview.org/ and/or KitWare
//
// ---------------------------------------------------------------------------
// OpenFOAM decomposed cases have different formats (JAN 2021)
//
// - "Uncollated" with separate directories for each rank
// processor0 ... processorN
//
// - "Collated" with a single directory for all NN ranks
// processorsNN
//
// - "Collated" with directories for (inclusive) ranges of ranks
// processorsNN_first-last, ...
//
// The collated format is not yet supported by the underlying readers
//------------------------------------------------------------------------------
// Support for reading collated format
#define VTK_FOAMFILE_COLLATED_FORMAT 0
//------------------------------------------------------------------------------
// Developer option to debug the reader states
#define VTK_FOAMFILE_DEBUG 0
// Similar to vtkErrorMacro etc.
#if VTK_FOAMFILE_DEBUG
#define vtkFoamDebug(x) \
do \
{ \
std::cerr << "" x; \
} while (false)
#else
#define vtkFoamDebug(x) \
do \
{ \
} while (false)
#endif // VTK_FOAMFILE_DEBUG
//------------------------------------------------------------------------------
#include "vtkPOpenFOAMReader.h"
#include "vtkAppendCompositeDataLeaves.h"
#include "vtkCharArray.h"
#include "vtkCollection.h"
#include "vtkDataArraySelection.h"
#include "vtkDirectory.h"
#include "vtkDoubleArray.h"
#include "vtkDummyController.h"
#include "vtkFieldData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkMultiProcessController.h"
#include "vtkObjectFactory.h"
#include "vtkSortDataArray.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStringArray.h"
#include <cctype>
#include <cstring>
//------------------------------------------------------------------------------
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkPOpenFOAMReader);
vtkCxxSetObjectMacro(vtkPOpenFOAMReader, Controller, vtkMultiProcessController);
//------------------------------------------------------------------------------
// Local Functions
namespace
{
// Create a sub-reader with the current characteristics
vtkSmartPointer<vtkOpenFOAMReader> NewFoamReader(vtkOpenFOAMReader* parent)
{
auto reader = vtkSmartPointer<vtkOpenFOAMReader>::New();
reader->SetFileName(parent->GetFileName());
reader->SetParent(parent);
reader->SetSkipZeroTime(parent->GetSkipZeroTime());
reader->SetUse64BitLabels(parent->GetUse64BitLabels());
reader->SetUse64BitFloats(parent->GetUse64BitFloats());
return reader;
}
// Generate a processor dirname from number or tuple.
// For 1 component:
// procNum -> 'processor<procNum>'
//
// For 3 component tuple (nprocs, first, size)
//
// - processors<nprocs>, when first == size == 0
// - processors<nprocs>_<first>-<last>, where last is inclusive
std::string ProcessorDirName(const vtkIntArray* dirs, int index)
{
if (index < 0 || index >= dirs->GetNumberOfTuples())
{
return std::string();
}
else if (3 == dirs->GetNumberOfComponents())
{
// Collated name
const auto nprocs = dirs->GetTypedComponent(index, 0);
const auto first = dirs->GetTypedComponent(index, 1);
const auto size = dirs->GetTypedComponent(index, 2);
std::string stem("processors" + std::to_string(nprocs));
if (size)
{
const auto last = (first + size - 1); // inclusive range
return (stem + "_" + std::to_string(first) + "-" + std::to_string(last));
}
return stem;
}
// Uncollated name
return std::string("processor" + std::to_string(dirs->GetValue(index)));
}
#if VTK_FOAMFILE_COLLATED_FORMAT
// Number of processor pieces represented by the tuple
inline int ProcessorsNumPieces(const int procTuple[])
{
const auto nprocs = procTuple[0];
// const auto first = procTuple[1];
const auto size = procTuple[2];
return (size ? size : nprocs);
}
#endif
// Search and list processor subdirectories
// Detect collated and uncollated processor directories
// - "processor(\d+)"
// - "processors(\d+)"
// - "processors(\d+)_(\d+)-(\d+)"
//
// string parsing logic as per fileOperation / parseProcsNumRange from OpenFOAM-v2012
//
// Return either collated or uncollated directories, never a mix.
// Use the number of components to distinguish
vtkSmartPointer<vtkIntArray> ScanForProcessorDirs(vtkDirectory* dir)
{
// Uncollated: save processor id
auto uncollated = vtkSmartPointer<vtkIntArray>::New();
uncollated->SetNumberOfComponents(1);
// Collated: save (processor count, first, size) tuple
auto collated = vtkSmartPointer<vtkIntArray>::New();
collated->SetNumberOfComponents(3);
// Sort keys for collated
vtkNew<vtkIntArray> collatedNums;
int procTuple[3] = { 0, 0, 0 };
const vtkIdType nFiles = dir->GetNumberOfFiles();
for (vtkIdType filei = 0; filei < nFiles; ++filei)
{
const char* subdir = dir->GetFile(filei);
if (strncmp(subdir, "processor", 9) != 0 || !dir->FileIsDirectory(subdir))
{
continue;
}
if (isdigit(subdir[9]))
{
// processor<digits>
const char* nptr = (subdir + 9);
char* endptr = nullptr;
errno = 0;
long parsed = std::strtol(nptr, &endptr, 10);
if (errno || nptr == endptr)
{
continue; // bad parse
}
const auto procId = static_cast<int>(parsed);
// Require end of string
if (*endptr == '\0')
{
uncollated->InsertNextValue(procId);
}
}
else if (subdir[9] == 's' && isdigit(subdir[10]))
{
// processors<digits> or processors<digits>_<digits>-<digits>
const char* nptr = (subdir + 10);
char* endptr = nullptr;
// 1. numProcs
errno = 0;
long parsed = std::strtol(nptr, &endptr, 10);
if (errno || nptr == endptr)
{
continue; // bad parse
}
const auto nProcs = static_cast<int>(parsed);
// End of string? Then no range and we are done.
if (*endptr == '\0')
{
procTuple[0] = nProcs;
procTuple[1] = 0;
procTuple[2] = 0;
collated->InsertNextTypedTuple(procTuple);
collatedNums->InsertNextValue(nProcs);
continue;
}
// Parse point at start of range ('_' character)?
if (*endptr != '_')
{
continue;
}
nptr = ++endptr;
// 2. firstProc
errno = 0;
parsed = std::strtol(nptr, &endptr, 10);
if (errno || nptr == endptr)
{
continue; // bad parse
}
const auto firstProc = static_cast<int>(parsed);
// Parse point at range separator ('-' character)?
if (*endptr != '-')
{
continue;
}
nptr = ++endptr;
// 3. lastProc
errno = 0;
parsed = std::strtol(nptr, &endptr, 10);
if (errno || nptr == endptr)
{
continue; // bad parse
}
const auto lastProc = static_cast<int>(parsed);
if (
// Parse point at end of string
(*endptr == '\0')
// Input plausibility - accept nProcs == 0 in case that becomes useful in the future
&& (nProcs >= 0 && firstProc >= 0 && firstProc <= lastProc))
{
// Convert first/last to start/size
procTuple[0] = nProcs;
procTuple[1] = firstProc;
procTuple[2] = (lastProc - firstProc + 1);
collated->InsertNextTypedTuple(procTuple);
collatedNums->InsertNextValue(nProcs);
}
}
}
collatedNums->Squeeze();
collated->Squeeze();
uncollated->Squeeze();
vtkSortDataArray::Sort(uncollated);
vtkSortDataArray::Sort(collatedNums, collated);
#if VTK_FOAMFILE_DEBUG
std::cerr << "processor (";
for (vtkIdType proci = 0; proci < uncollated->GetNumberOfTuples(); ++proci)
{
std::cerr << ' ' << uncollated->GetValue(proci);
}
std::cerr << " )\n";
std::cerr << "processors (";
for (vtkIdType proci = 0; proci < collated->GetNumberOfTuples(); ++proci)
{
collated->GetTypedTuple(proci, procTuple);
std::cerr << ' ' << procTuple[0];
if (procTuple[2])
{
std::cerr << '_' << procTuple[1] << '-' << (procTuple[1] + procTuple[2] - 1);
}
}
std::cerr << " )\n";
#endif // VTK_FOAMFILE_DEBUG
#if VTK_FOAMFILE_COLLATED_FORMAT
const int nCollated = static_cast<int>(collated->GetNumberOfTuples());
if (nCollated)
{
// Sanity checks.
// Same number of processors, check that total number of pieces add up, etc.
if (collatedNums->GetValue(0) != collatedNums->GetValue(nCollated - 1))
{
// Failed
return uncollated;
}
else if (nCollated > 1)
{
// Identical nProcs. Now re-sort based on first-last range
for (int i = 0; i < nCollated; ++i)
{
const int firstProc = collated->GetTypedComponent(i, 1);
collatedNums->SetTValue(i, firstProc);
}
vtkSortDataArray::Sort(collatedNums, collated);
}
// Done
return collated;
}
#endif
return uncollated;
}
} // End anonymous namespace
//------------------------------------------------------------------------------
vtkPOpenFOAMReader::vtkPOpenFOAMReader()
{
this->Controller = nullptr;
this->SetController(vtkMultiProcessController::GetGlobalController());
if (this->Controller == nullptr)
{
this->SetController(vtkDummyController::New());
this->NumProcesses = 1;
this->ProcessId = 0;
}
else
{
this->NumProcesses = this->Controller->GetNumberOfProcesses();
this->ProcessId = this->Controller->GetLocalProcessId();
}
this->CaseType = RECONSTRUCTED_CASE;
this->MTimeOld = 0;
}
//------------------------------------------------------------------------------
vtkPOpenFOAMReader::~vtkPOpenFOAMReader()
{
this->SetController(nullptr);
}
//------------------------------------------------------------------------------
void vtkPOpenFOAMReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Case Type: " << this->CaseType << endl;
os << indent << "MTimeOld: " << this->MTimeOld << endl;
os << indent << "Number of Processes: " << this->NumProcesses << endl;
os << indent << "Process Id: " << this->ProcessId << endl;
os << indent << "Controller: " << this->Controller << endl;
}
//------------------------------------------------------------------------------
void vtkPOpenFOAMReader::SetCaseType(int t)
{
if (this->CaseType != t)
{
this->CaseType = static_cast<caseType>(t);
this->Refresh = true;
this->Modified();
}
}
//------------------------------------------------------------------------------
int vtkPOpenFOAMReader::RequestInformation(
vtkInformation* request, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
const bool isRootProc = (this->ProcessId == 0);
const bool isParallel = (this->NumProcesses > 1);
int returnCode = 1;
if (this->CaseType == RECONSTRUCTED_CASE)
{
if (isRootProc)
{
returnCode = this->Superclass::RequestInformation(request, inputVector, outputVector);
}
if (isParallel)
{
this->Controller->Broadcast(&returnCode, 1, 0);
if (returnCode == 0)
{
// Error encountered in process 0 - abort all processes
vtkErrorMacro(<< "The master process returned an error.");
return 0;
}
vtkDoubleArray* timeValues = nullptr;
if (isRootProc)
{
timeValues = this->Superclass::GetTimeValues();
}
else
{
timeValues = vtkDoubleArray::New();
}
this->Controller->Broadcast(timeValues, 0);
if (!isRootProc)
{
this->Superclass::SetTimeInformation(outputVector, timeValues);
this->Superclass::Refresh = false;
timeValues->Delete();
}
}
this->GatherMetaData();
return returnCode;
}
if (!this->Superclass::FileName || strlen(this->Superclass::FileName) == 0)
{
vtkErrorMacro("FileName has to be specified!");
return 0;
}
// Handle the decomposed case
if (*this->Superclass::FileNameOld != this->Superclass::FileName ||
this->Superclass::ListTimeStepsByControlDict !=
this->Superclass::ListTimeStepsByControlDictOld ||
this->Superclass::SkipZeroTime != this->Superclass::SkipZeroTimeOld ||
this->Superclass::Refresh)
{
// retain selection status when just refreshing a case
if (!this->Superclass::FileNameOld->empty() &&
*this->Superclass::FileNameOld != this->Superclass::FileName)
{
// clear selections
this->Superclass::CellDataArraySelection->RemoveAllArrays();
this->Superclass::PointDataArraySelection->RemoveAllArrays();
this->Superclass::LagrangianDataArraySelection->RemoveAllArrays();
this->Superclass::PatchDataArraySelection->RemoveAllArrays();
}
*this->Superclass::FileNameOld = this->FileName;
this->Superclass::Readers->RemoveAllItems();
this->Superclass::NumberOfReaders = 0;
// Recreate case information
vtkStdString masterCasePath, controlDictPath;
this->Superclass::CreateCasePath(masterCasePath, controlDictPath);
this->Superclass::CreateCharArrayFromString(
this->Superclass::CasePath, "CasePath", masterCasePath);
int nProcessorDirs = 0;
auto processorDirs = vtkSmartPointer<vtkIntArray>::New();
vtkStringArray* timeNames = nullptr;
vtkDoubleArray* timeValues = nullptr;
if (isRootProc)
{
do
{
// Search and list processor subdirectories
vtkNew<vtkDirectory> dir;
if (!dir->Open(masterCasePath.c_str()))
{
vtkErrorMacro(<< "Cannot open " << masterCasePath);
returnCode = 0;
break; // Failed
}
processorDirs = ::ScanForProcessorDirs(dir);
nProcessorDirs = static_cast<int>(processorDirs->GetNumberOfTuples());
if (nProcessorDirs)
{
// Get times from the first processor subdirectory
const std::string procDirName = ::ProcessorDirName(processorDirs, 0);
vtkFoamDebug(<< "First processor dir: " << procDirName << "\n");
auto masterReader = ::NewFoamReader(this);
if (!masterReader->MakeInformationVector(outputVector, procDirName) ||
!masterReader->MakeMetaDataAtTimeStep(true))
{
returnCode = 0;
break; // Failed
}
this->Superclass::Readers->AddItem(masterReader);
timeNames = masterReader->GetTimeNames();
timeValues = masterReader->GetTimeValues();
}
else
{
timeNames = vtkStringArray::New();
timeValues = vtkDoubleArray::New();
this->Superclass::SetTimeInformation(outputVector, timeValues);
}
} while (false); // End of process 0 only execution scope
}
if (isParallel)
{
this->Controller->Broadcast(&returnCode, 1, 0);
if (returnCode == 0)
{
// Error encountered in process 0 - abort all processes
vtkErrorMacro(<< "The master process returned an error.");
return 0;
}
if (!isRootProc)
{
timeNames = vtkStringArray::New();
timeValues = vtkDoubleArray::New();
}
this->Controller->Broadcast(processorDirs, 0);
this->Controller->Broadcast(timeValues, 0);
this->Broadcast(timeNames);
if (!isRootProc)
{
this->Superclass::SetTimeInformation(outputVector, timeValues);
}
nProcessorDirs = static_cast<int>(processorDirs->GetNumberOfTuples());
}
else
{
if (returnCode == 0)
{
// Error encountered (single process). Nothing to cleanup
return 0;
}
}
// Create reader instances for processor subdirectories,
// skip first one since it has already been created above
for (int dirIndex = (this->ProcessId ? this->ProcessId : this->NumProcesses);
dirIndex < nProcessorDirs; dirIndex += this->NumProcesses)
{
const std::string procDirName = ::ProcessorDirName(processorDirs, dirIndex);
vtkFoamDebug(<< "Additional processor dir: " << procDirName << "\n");
auto subReader = ::NewFoamReader(this);
// If getting metadata failed, simply skip the reader instance
if (subReader->MakeInformationVector(nullptr, procDirName, timeNames, timeValues) &&
subReader->MakeMetaDataAtTimeStep(true))
{
this->Superclass::Readers->AddItem(subReader);
}
else
{
vtkWarningMacro(<< "Removing reader for processor subdirectory " << procDirName);
}
}
// Cleanup
if (!isRootProc || (nProcessorDirs == 0))
{
if (timeNames)
{
timeNames->Delete();
}
if (timeValues)
{
timeValues->Delete();
}
}
this->GatherMetaData();
this->Superclass::Refresh = false;
}
outputVector->GetInformationObject(0)->Set(CAN_HANDLE_PIECE_REQUEST(), 1);
return returnCode;
}
//------------------------------------------------------------------------------
int vtkPOpenFOAMReader::RequestData(
vtkInformation* request, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
const bool isRootProc = (this->ProcessId == 0);
const bool isParallel = (this->NumProcesses > 1);
int returnCode = 1;
vtkSmartPointer<vtkMultiProcessController> splitController;
vtkInformation* outInfo = outputVector->GetInformationObject(0);
auto* output = vtkMultiBlockDataSet::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
if (this->CaseType == RECONSTRUCTED_CASE)
{
if (isRootProc)
{
returnCode = this->Superclass::RequestData(request, inputVector, outputVector);
}
this->GatherMetaData();
if (isParallel)
{
this->Controller->Broadcast(&returnCode, 1, 0);
splitController.TakeReference(this->Controller->PartitionController(1, this->ProcessId));
vtkNew<vtkMultiBlockDataSet> mb;
if (isRootProc)
{
mb->CopyStructure(output);
splitController->Broadcast(mb, 0);
}
else
{
splitController->Broadcast(mb, 0);
output->CopyStructure(mb);
}
}
return returnCode;
}
if (this->Superclass::Readers->GetNumberOfItems())
{
int nTimes = 0; // Also used for logic
double requestedTimeValue = 0;
if (outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()))
{
nTimes = outInfo->Length(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
// UPDATE_TIME_STEP is unreliable if there is only one time-step
requestedTimeValue =
(1 == nTimes ? outInfo->Get(vtkStreamingDemandDrivenPipeline::TIME_STEPS(), 0)
: outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP()));
if (nTimes)
{
outInfo->Set(vtkDataObject::DATA_TIME_STEP(), requestedTimeValue);
}
}
// NOTE: do not call SetTimeValue() directly here
vtkAppendCompositeDataLeaves* append = vtkAppendCompositeDataLeaves::New();
// append->AppendFieldDataOn();
vtkOpenFOAMReader* reader;
this->Superclass::CurrentReaderIndex = 0;
this->Superclass::Readers->InitTraversal();
while ((reader = vtkOpenFOAMReader::SafeDownCast(
this->Superclass::Readers->GetNextItemAsObject())) != nullptr)
{
// even if the child readers themselves are not modified, mark
// them as modified if "this" has been modified, since they
// refer to the property of "this"
if ((nTimes && reader->SetTimeValue(requestedTimeValue)) ||
(this->MTimeOld != this->GetMTime()))
{
reader->Modified();
}
if (reader->MakeMetaDataAtTimeStep(false))
{
append->AddInputConnection(reader->GetOutputPort());
}
}
this->GatherMetaData();
if (append->GetNumberOfInputConnections(0) == 0)
{
output->Initialize();
returnCode = 0;
}
else
{
// reader->RequestInformation() and RequestData() are called
// for all reader instances without setting UPDATE_TIME_STEPS
append->Update();
output->CompositeShallowCopy(append->GetOutput());
}
append->Delete();
// known issue: output for process without sub-reader will not have CasePath
output->GetFieldData()->AddArray(this->Superclass::CasePath);
// Processor 0 needs to broadcast the structure of the multiblock
// to the processors that didn't have the chance to load something
// To do so, we split the controller to broadcast only to the interested
// processors (else case below) and avoid useless communication.
splitController.TakeReference(
this->Controller->PartitionController(isRootProc, this->ProcessId));
if (isRootProc)
{
vtkNew<vtkMultiBlockDataSet> mb;
mb->CopyStructure(output);
splitController->Broadcast(mb, 0);
}
}
else
{
this->GatherMetaData();
// This rank did not receive anything so data structure is void.
// Let's receive the empty but structured multiblock from processor 0
splitController.TakeReference(this->Controller->PartitionController(true, this->ProcessId));
vtkNew<vtkMultiBlockDataSet> mb;
splitController->Broadcast(mb, 0);
output->CopyStructure(mb);
}
this->Superclass::UpdateStatus();
this->MTimeOld = this->GetMTime();
return returnCode;
}
//------------------------------------------------------------------------------
// Note: includes guard against non-parallel calls
void vtkPOpenFOAMReader::GatherMetaData()
{
if (this->NumProcesses > 1)
{
this->AllGather(this->Superclass::PatchDataArraySelection);
this->AllGather(this->Superclass::CellDataArraySelection);
this->AllGather(this->Superclass::PointDataArraySelection);
this->AllGather(this->Superclass::LagrangianDataArraySelection);
// omit removing duplicated entries of LagrangianPaths as well
// when the number of processes is 1 assuming there's no duplicate
// entry within a process
this->AllGather(this->Superclass::LagrangianPaths);
}
}
//------------------------------------------------------------------------------
// Broadcast a vtkStringArray in process 0 to all processes
// Low-level routine without guard against non-parallel calls
//
// Broadcasts a set of nul-char delimited strings which are re-parsed
// into individual entries.
void vtkPOpenFOAMReader::Broadcast(vtkStringArray* sa)
{
const bool isRootProc = (this->ProcessId == 0);
vtkIdType lengths[2]; // A tuple of local count, send length
std::vector<char> contents;
if (isRootProc)
{
//- Get the overall send length
lengths[0] = sa->GetNumberOfTuples();
lengths[1] = 0;
for (int i = 0; i < lengths[0]; ++i)
{
const std::string& str = sa->GetValue(i);
lengths[1] += static_cast<vtkIdType>(str.length()) + 1; // Trailing nul-char
}
contents.resize(lengths[1]); // Send buffer
//- Add content to send (nul-delimited)
char* name = contents.data();
for (int i = 0; i < lengths[0]; ++i)
{
const std::string& str = sa->GetValue(i);
const int len = static_cast<int>(str.length()) + 1; // Trailing nul-char
memcpy(name, str.c_str(), len);
name += len;
}
}
this->Controller->Broadcast(lengths, 2, 0);
if (!isRootProc)
{
contents.resize(lengths[1]); // Recv buffer
}
this->Controller->Broadcast(contents.data(), contents.size(), 0);
if (!isRootProc)
{
sa->Initialize();
sa->SetNumberOfTuples(lengths[0]);
// Walk the set of nul-char delimited strings
const char* name = contents.data();
for (int i = 0; i < lengths[0]; ++i)
{
sa->SetValue(i, name);
const int len = static_cast<int>(sa->GetValue(i).length()) + 1; // Trailing nul-char
name += len;
}
}
}
//------------------------------------------------------------------------------
// AllGather vtkStringArray from and to all processes
// Low-level routine without guard against non-parallel calls
//
// Sends/receives a set of nul-char delimited strings which can be re-parsed
// into individual entries.
void vtkPOpenFOAMReader::AllGather(vtkStringArray* sa)
{
// Construct a set of nul-char delimited strings to send
//- Get the overall send length
vtkIdType length = 0;
for (int i = 0; i < sa->GetNumberOfTuples(); ++i)
{
// Include trailing nul-char
length += static_cast<vtkIdType>(sa->GetValue(i).length()) + 1;
}
//- Add content to send (nul-delimited)
std::vector<char> contents(length);
{
char* name = contents.data();
for (int i = 0; i < sa->GetNumberOfTuples(); ++i)
{
const int len = static_cast<int>(sa->GetValue(i).length()) + 1; // Trailing nul-char
memcpy(name, sa->GetValue(i).c_str(), len);
name += len;
}
}
std::vector<vtkIdType> lengths(this->NumProcesses);
std::vector<vtkIdType> offsets(this->NumProcesses);
this->Controller->AllGather(&length, lengths.data(), 1);
vtkIdType totalLength = 0;
for (int proci = 0; proci < this->NumProcesses; ++proci)
{
offsets[proci] = totalLength;
totalLength += lengths[proci];
}
std::vector<char> allContents(totalLength);
this->Controller->AllGatherV(
contents.data(), allContents.data(), length, lengths.data(), offsets.data());
sa->Initialize();
// Walk the set of nul-char delimited strings
{
const char* name = allContents.data();
for (int off = 0; off < totalLength; /*nil*/)
{
const int len = static_cast<int>(strlen(name)) + 1; // Trailing nul-char
if (sa->LookupValue(name) == -1)
{
// Insert only when the same string is not found
sa->InsertNextValue(name);
}
name += len;
off += len;
}
}
sa->Squeeze();
}
//------------------------------------------------------------------------------
// AllGather vtkDataArraySelections from/to all processes
// Low-level routine without non-parallel guard
//
// Sends/receives a set of nul-char delimited strings which can be re-parsed
// into individual entries.
// Each string element is prefixed with an additional char for its enabled/disabled state.
void vtkPOpenFOAMReader::AllGather(vtkDataArraySelection* sa)
{
// Construct a set of nul-char delimited strings, with bool prefix
//- Get the overall send length
vtkIdType length = 0;
for (int i = 0; i < sa->GetNumberOfArrays(); ++i)
{
// Include prefix char (bool) for enabled and trailing nul-char
length += static_cast<vtkIdType>(strlen(sa->GetArrayName(i))) + 2;
}
//- Add content to send (nul-delimited)
std::vector<char> contents(length);
{
char* send = contents.data();
for (int i = 0; i < sa->GetNumberOfArrays(); ++i)
{
const char* arrayName = sa->GetArrayName(i);
const int len = static_cast<int>(strlen(arrayName)) + 1; // With trailing nul-char
*send++ = sa->ArrayIsEnabled(arrayName); // enabled/disabled
memcpy(send, arrayName, len);
send += len;
}
}
std::vector<vtkIdType> lengths(this->NumProcesses);
std::vector<vtkIdType> offsets(this->NumProcesses);
this->Controller->AllGather(&length, lengths.data(), 1);
vtkIdType totalLength = 0;
for (int proci = 0; proci < this->NumProcesses; ++proci)
{
offsets[proci] = totalLength;
totalLength += lengths[proci];
}
std::vector<char> allContents(totalLength);
this->Controller->AllGatherV(
contents.data(), allContents.data(), length, lengths.data(), offsets.data());
// do not RemoveAllArray so that the previous arrays are preserved
// sa->RemoveAllArrays();
// Walk the set of nul-char delimited strings.
// Note that each string is prefixed with a bool.
{
const char* name = allContents.data();
for (int off = 0; off < totalLength; /*nil*/)
{
++off; // leading bool
const bool isEnabled(*name++);
const int len = static_cast<int>(strlen(name)) + 1; // With trailing nul-char
// Add new or update current state
if (!sa->AddArray(name, isEnabled))
{
sa->SetArraySetting(name, isEnabled);
}
name += len;
off += len;
}
}
}
VTK_ABI_NAMESPACE_END
|