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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkAbstractArray.h"
#include "vtkMultiProcessController.h"
#include "vtkMultiProcessStream.h"
#include <cassert>
// Silence warnings that these functions are unused.
// Because the functions are in an anonymous namespace, unused-function
// function warnings are generated. But not every file that includes
// this header needs to use every function.
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#elif defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
#pragma clang diagnostic ignored "-Wunused-template"
#endif
VTK_ABI_NAMESPACE_BEGIN
namespace
{
template <typename T>
bool Synchronize(vtkMultiProcessController* controller, T& data, T& result)
{
if (controller == nullptr || controller->GetNumberOfProcesses() <= 1)
{
return true;
}
vtkMultiProcessStream stream;
stream << data;
std::vector<vtkMultiProcessStream> all_streams;
if (controller->AllGather(stream, all_streams))
{
for (auto& s : all_streams)
{
s >> result;
}
return true;
}
return false;
}
template <typename T>
bool Broadcast(vtkMultiProcessController* controller, T& data, int root)
{
if (controller == nullptr || controller->GetNumberOfProcesses() <= 1)
{
return true;
}
if (controller->GetLocalProcessId() == root)
{
vtkMultiProcessStream stream;
stream << data;
return controller->Broadcast(stream, root) != 0;
}
else
{
data = T();
vtkMultiProcessStream stream;
if (controller->Broadcast(stream, root))
{
stream >> data;
return true;
}
return false;
}
}
vtkSmartPointer<vtkAbstractArray> JoinArrays(
const std::vector<vtkSmartPointer<vtkAbstractArray>>& arrays)
{
if (arrays.empty())
{
return nullptr;
}
else if (arrays.size() == 1)
{
return arrays[0];
}
vtkIdType numTuples = 0;
for (auto& array : arrays)
{
numTuples += array->GetNumberOfTuples();
}
vtkSmartPointer<vtkAbstractArray> result;
result.TakeReference(arrays[0]->NewInstance());
result->CopyInformation(arrays[0]->GetInformation());
result->SetName(arrays[0]->GetName());
result->SetNumberOfComponents(arrays[0]->GetNumberOfComponents());
result->SetNumberOfTuples(numTuples);
vtkIdType offset = 0;
for (auto& array : arrays)
{
const auto count = array->GetNumberOfTuples();
result->InsertTuples(offset, count, 0, array);
offset += count;
}
result->Modified();
assert(offset == numTuples);
return result;
}
} // end of namespace {}
VTK_ABI_NAMESPACE_END
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#elif defined(__clang__)
#pragma clang diagnostic pop
#endif
|