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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkPartitionedDataSetSource.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkLogger.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkParametricFunctionSource.h"
#include "vtkParametricKlein.h"
#include "vtkPartitionedDataSet.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkStringArray.h"
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <vector>
VTK_ABI_NAMESPACE_BEGIN
namespace
{
enum NUM_PARTITIONS : signed int
{
MULTIPLE_PARTITIONS = -1,
NO_PARTITIONS = 0
};
/*
* Generate allocations for the given ranks taking in consideration that:
*
* - Some ranks might not accept any partitions
* - Some ranks might accept a finite amount of partitions.
* - Some ranks might accept a any multiplicity of partitions.
*
*/
std::vector<int> GenerateAllocations(const std::vector<int>& allocs, const int numPartitions)
{
std::vector<int> partsPerRank(allocs);
const int partsAllocated = std::accumulate(allocs.begin(), allocs.end(), 0,
[](int a, int b) { return (b == NUM_PARTITIONS::MULTIPLE_PARTITIONS) ? a : a + b; });
const int partsToAlloc = std::max(numPartitions - partsAllocated, 0);
if (partsToAlloc > 0)
{
// Make a vector with iters of partitions to be alloc
std::vector<std::vector<int>::iterator> ranksToAllocIters;
auto it = partsPerRank.begin();
while (partsPerRank.end() !=
(it = find(partsPerRank.begin(), partsPerRank.end(), NUM_PARTITIONS::MULTIPLE_PARTITIONS)))
{
*it = 0; // Initialize to 0
ranksToAllocIters.push_back(it);
}
// Schedule blocks in a round-robin fashion
const size_t ranksToAllocSize = ranksToAllocIters.size();
for (size_t i = 0; i < static_cast<size_t>(partsToAlloc); ++i)
{
++(*ranksToAllocIters[i % ranksToAllocSize]);
}
}
if (std::accumulate(partsPerRank.begin(), partsPerRank.end(), 0) != numPartitions)
{
vtkLogF(ERROR, "GenerateAllocations generated partitions != given numPartitions");
}
return partsPerRank;
}
// returns [start, end].
std::pair<int, int> GetRange(const int rank, const std::vector<int>& parts)
{
std::pair<int, int> result(0, parts[0]);
if (rank == 0)
{
return result;
}
for (int cc = 1; cc <= rank; ++cc)
{
result.first = result.second;
result.second += parts[cc];
}
return result;
}
}
vtkStandardNewMacro(vtkPartitionedDataSetSource);
vtkCxxSetObjectMacro(vtkPartitionedDataSetSource, ParametricFunction, vtkParametricFunction);
//----------------------------------------------------------------------------
vtkPartitionedDataSetSource::vtkPartitionedDataSetSource()
{
this->SetNumberOfInputPorts(0);
this->SetNumberOfOutputPorts(1);
// Default Parametric Function
vtkNew<vtkParametricKlein> pklein;
this->SetParametricFunction(pklein.Get());
}
//----------------------------------------------------------------------------
vtkPartitionedDataSetSource::~vtkPartitionedDataSetSource()
{
this->SetParametricFunction(nullptr);
}
//----------------------------------------------------------------------------
void vtkPartitionedDataSetSource::EnableRank(int rank)
{
auto it = this->Allocations.find(rank);
if (it == this->Allocations.end() || it->second != NUM_PARTITIONS::MULTIPLE_PARTITIONS)
{
this->Allocations[rank] = NUM_PARTITIONS::MULTIPLE_PARTITIONS;
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkPartitionedDataSetSource::DisableRank(int rank)
{
auto it = this->Allocations.find(rank);
if (it == this->Allocations.end() || it->second != NUM_PARTITIONS::NO_PARTITIONS)
{
this->Allocations[rank] = NUM_PARTITIONS::NO_PARTITIONS;
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkPartitionedDataSetSource::EnableAllRanks()
{
if (!this->RanksEnabledByDefault)
{
this->RanksEnabledByDefault = true;
this->Modified();
}
if (!this->Allocations.empty())
{
this->Allocations.clear();
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkPartitionedDataSetSource::DisableAllRanks()
{
if (this->RanksEnabledByDefault)
{
this->RanksEnabledByDefault = false;
this->Modified();
}
if (!this->Allocations.empty())
{
this->Allocations.clear();
this->Modified();
}
}
//----------------------------------------------------------------------------
bool vtkPartitionedDataSetSource::IsEnabledRank(int rank)
{
auto it = this->Allocations.find(rank);
// By default return the default partitioning policy
if (it == this->Allocations.end())
{
return RanksEnabledByDefault;
}
return it->second == NUM_PARTITIONS::MULTIPLE_PARTITIONS;
}
//----------------------------------------------------------------------------
void vtkPartitionedDataSetSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "ParametricFunction: "
<< (this->ParametricFunction ? this->ParametricFunction->GetClassName() : "(nullptr)") << endl;
}
//------------------------------------------------------------------------------
int vtkPartitionedDataSetSource::RequestInformation(vtkInformation* vtkNotUsed(request),
vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* outputVector)
{
auto outInfo = outputVector->GetInformationObject(0);
outInfo->Set(CAN_HANDLE_PIECE_REQUEST(), 1);
return 1;
}
//----------------------------------------------------------------------------
int vtkPartitionedDataSetSource::RequestData(
vtkInformation*, vtkInformationVector**, vtkInformationVector* outputVector)
{
// We must meet these preconditions to continue this method
if (!this->GetParametricFunction())
{
vtkLogF(WARNING, "RequestData aborted since ParametricFunction is missing");
return 1;
}
if (!this->RanksEnabledByDefault && this->Allocations.empty())
{
return 1;
}
auto outInfo = outputVector->GetInformationObject(0);
auto pds = vtkPartitionedDataSet::GetData(outInfo);
const int rank = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
const int numRanks = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
auto* function = this->GetParametricFunction();
function->JoinVOff();
function->JoinUOff();
vtkNew<vtkParametricFunctionSource> source;
source->SetOutputPointsPrecision(vtkAlgorithm::DOUBLE_PRECISION);
source->SetParametricFunction(function);
source->SetScalarModeToV();
// We set our default policy here
auto defaultAlloc = (this->RanksEnabledByDefault) ? NUM_PARTITIONS::MULTIPLE_PARTITIONS
: NUM_PARTITIONS::NO_PARTITIONS;
std::vector<int> allocs(numRanks, defaultAlloc);
for (auto& kv : this->Allocations)
{
if (kv.first < static_cast<int>(allocs.size()))
{
allocs[kv.first] = kv.second;
}
}
const int numberOfPartitions = (this->NumberOfPartitions > 0)
? this->NumberOfPartitions
: std::count(allocs.begin(), allocs.end(), NUM_PARTITIONS::MULTIPLE_PARTITIONS);
if (numberOfPartitions <= 0)
{
return 1;
}
const auto partsPerRank = ::GenerateAllocations(allocs, numberOfPartitions);
const auto range = ::GetRange(rank, partsPerRank);
const double deltaV = function->GetMaximumV() / numberOfPartitions;
for (int idx = 0, partition = range.first; partition < range.second; ++partition, ++idx)
{
function->SetMinimumV(partition * deltaV);
function->SetMaximumV((partition + 1) * deltaV);
vtkLogF(TRACE, "min=%f max=%f", function->GetMinimumV(), function->GetMaximumV());
source->Update();
vtkNew<vtkPolyData> clone;
clone->ShallowCopy(source->GetOutputDataObject(0));
vtkNew<vtkIntArray> partId;
partId->SetName("PartitionId");
partId->SetNumberOfTuples(clone->GetNumberOfPoints());
partId->FillValue(partition);
clone->GetPointData()->AddArray(partId);
pds->SetPartition(idx, clone);
}
return 1;
}
VTK_ABI_NAMESPACE_END
|