VTK
vtkBlockDistribution.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkBlockDistribution.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /*
16  * Copyright (C) 2008 The Trustees of Indiana University.
17  * Use, modification and distribution is subject to the Boost Software
18  * License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt)
19  */
28 #ifndef vtkBlockDistribution_h
29 #define vtkBlockDistribution_h
30 
32 {
33 public:
38 
43  vtkIdType GetNumElements() { return this->NumElements; }
44 
49  vtkIdType GetNumProcessors() { return this->NumProcessors; }
50 
56 
62 
68 
74 
79  vtkIdType GetGlobalIndex(vtkIdType localIndex, vtkIdType rank);
80 
81 private:
82  vtkIdType NumElements;
83  vtkIdType NumProcessors;
84 };
85 
86 // ----------------------------------------------------------------------
87 
89  : NumElements(N), NumProcessors(P)
90 {
91 }
92 
93 // ----------------------------------------------------------------------
94 
96 {
97  return (this->NumElements / this->NumProcessors)
98  + (rank < this->NumElements % this->NumProcessors? 1 : 0);
99 }
100 
101 // ----------------------------------------------------------------------
102 
103 inline vtkIdType
105 {
106  vtkIdType smallBlockSize = this->NumElements / this->NumProcessors;
107  vtkIdType cutoffProcessor = this->NumElements % this->NumProcessors;
108  vtkIdType cutoffIndex = cutoffProcessor * (smallBlockSize + 1);
109 
110  if (globalIndex < cutoffIndex)
111  {
112  return globalIndex / (smallBlockSize + 1);
113  }
114  else
115  {
116  return cutoffProcessor + (globalIndex - cutoffIndex) / smallBlockSize;
117  }
118 }
119 
120 // ----------------------------------------------------------------------
121 
122 inline vtkIdType
124 {
125  vtkIdType rank = this->GetProcessorOfElement(globalIndex);
126  return globalIndex - this->GetFirstGlobalIndexOnProcessor(rank);
127 }
128 
129 // ----------------------------------------------------------------------
130 
131 inline vtkIdType
133 {
134  vtkIdType estimate = rank * (this->NumElements / this->NumProcessors + 1);
135  vtkIdType cutoffProcessor = this->NumElements % this->NumProcessors;
136  if (rank < cutoffProcessor)
137  {
138  return estimate;
139  }
140  else
141  {
142  return estimate - (rank - cutoffProcessor);
143  }
144 }
145 
146 // ----------------------------------------------------------------------
147 
148 inline vtkIdType
150 {
151  return this->GetFirstGlobalIndexOnProcessor(rank) + localIndex;
152 }
153 
154 #endif
155 // VTK-HeaderTest-Exclude: vtkBlockDistribution.h
vtkIdType GetNumProcessors()
Retrieves the number of processors for which this block distribution was built.
A helper class that manages a block distribution of N elements of data.
int vtkIdType
Definition: vtkType.h:347
vtkIdType GetFirstGlobalIndexOnProcessor(vtkIdType rank)
Retrieve the first global index stored on the processor with the given rank.
vtkBlockDistribution(vtkIdType N, vtkIdType P)
Create a block distribution with N elements on P processors.
vtkIdType GetLocalIndexOfElement(vtkIdType globalIndex)
Retrieve the local index (offset) on the processor determined by GetProcessorOfElement that refers to...
vtkIdType GetBlockSize(vtkIdType rank)
Get the block size for the processor with the given rank.
vtkIdType GetProcessorOfElement(vtkIdType globalIndex)
Retrieve the process number in [0, GetNumProcessors()) where the element with the given global index ...
vtkIdType GetGlobalIndex(vtkIdType localIndex, vtkIdType rank)
Retrieve the global index associated with the given local index on the processor with the given rank...
vtkIdType GetNumElements()
Retrieves the number of elements for which this block distribution was built.