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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-FileCopyrightText: Copyright (C) 2008 The Trustees of Indiana University.
// SPDX-License-Identifier: BSD-3-Clause AND BSL-1.0
/**
* @class vtkBlockDistribution
* @brief A helper class that manages a block distribution of N elements of data.
*
*
*
*/
#ifndef vtkBlockDistribution_h
#define vtkBlockDistribution_h
#include "vtkABINamespace.h"
VTK_ABI_NAMESPACE_BEGIN
class vtkBlockDistribution
{
public:
/**
* Create a block distribution with N elements on P processors.
*/
vtkBlockDistribution(vtkIdType N, vtkIdType P);
/**
* Retrieves the number of elements for which this block distribution was
* built.
*/
vtkIdType GetNumElements() { return this->NumElements; }
/**
* Retrieves the number of processors for which this block
* distribution was built.
*/
vtkIdType GetNumProcessors() { return this->NumProcessors; }
/**
* Get the block size for the processor with the given rank. This is the
* number of elements that the processor will store.
*/
vtkIdType GetBlockSize(vtkIdType rank);
/**
* Retrieve the process number in [0, GetNumProcessors()) where the element
* with the given global index will be located.
*/
vtkIdType GetProcessorOfElement(vtkIdType globalIndex);
/**
* Retrieve the local index (offset) on the processor determined by
* GetProcessorOfElement that refers to the given global index.
*/
vtkIdType GetLocalIndexOfElement(vtkIdType globalIndex);
/**
* Retrieve the first global index stored on the processor with the given
* rank.
*/
vtkIdType GetFirstGlobalIndexOnProcessor(vtkIdType rank);
/**
* Retrieve the global index associated with the given local index on the
* processor with the given rank.
*/
vtkIdType GetGlobalIndex(vtkIdType localIndex, vtkIdType rank);
private:
vtkIdType NumElements;
vtkIdType NumProcessors;
};
// ----------------------------------------------------------------------
inline vtkBlockDistribution::vtkBlockDistribution(vtkIdType N, vtkIdType P)
: NumElements(N)
, NumProcessors(P)
{
}
// ----------------------------------------------------------------------
inline vtkIdType vtkBlockDistribution::GetBlockSize(vtkIdType rank)
{
return (this->NumElements / this->NumProcessors) +
(rank < this->NumElements % this->NumProcessors ? 1 : 0);
}
// ----------------------------------------------------------------------
inline vtkIdType vtkBlockDistribution::GetProcessorOfElement(vtkIdType globalIndex)
{
vtkIdType smallBlockSize = this->NumElements / this->NumProcessors;
vtkIdType cutoffProcessor = this->NumElements % this->NumProcessors;
vtkIdType cutoffIndex = cutoffProcessor * (smallBlockSize + 1);
if (globalIndex < cutoffIndex)
{
return globalIndex / (smallBlockSize + 1);
}
else
{
return cutoffProcessor + (globalIndex - cutoffIndex) / smallBlockSize;
}
}
// ----------------------------------------------------------------------
inline vtkIdType vtkBlockDistribution::GetLocalIndexOfElement(vtkIdType globalIndex)
{
vtkIdType rank = this->GetProcessorOfElement(globalIndex);
return globalIndex - this->GetFirstGlobalIndexOnProcessor(rank);
}
// ----------------------------------------------------------------------
inline vtkIdType vtkBlockDistribution::GetFirstGlobalIndexOnProcessor(vtkIdType rank)
{
vtkIdType estimate = rank * (this->NumElements / this->NumProcessors + 1);
vtkIdType cutoffProcessor = this->NumElements % this->NumProcessors;
if (rank < cutoffProcessor)
{
return estimate;
}
else
{
return estimate - (rank - cutoffProcessor);
}
}
// ----------------------------------------------------------------------
inline vtkIdType vtkBlockDistribution::GetGlobalIndex(vtkIdType localIndex, vtkIdType rank)
{
return this->GetFirstGlobalIndexOnProcessor(rank) + localIndex;
}
VTK_ABI_NAMESPACE_END
#endif
// VTK-HeaderTest-Exclude: vtkBlockDistribution.h
|