VTK
|
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkBlockDistribution.h 00005 00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00007 All rights reserved. 00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00015 /* 00016 * Copyright (C) 2008 The Trustees of Indiana University. 00017 * Use, modification and distribution is subject to the Boost Software 00018 * License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt) 00019 */ 00027 #ifndef __vtkBlockDistribution_h 00028 #define __vtkBlockDistribution_h 00029 00030 class vtkBlockDistribution 00031 { 00032 public: 00034 vtkBlockDistribution(vtkIdType N, vtkIdType P); 00035 00038 vtkIdType GetNumElements() { return this->NumElements; } 00039 00042 vtkIdType GetNumProcessors() { return this->NumProcessors; } 00043 00046 vtkIdType GetBlockSize(vtkIdType rank); 00047 00050 vtkIdType GetProcessorOfElement(vtkIdType globalIndex); 00051 00054 vtkIdType GetLocalIndexOfElement(vtkIdType globalIndex); 00055 00058 vtkIdType GetFirstGlobalIndexOnProcessor(vtkIdType rank); 00059 00062 vtkIdType GetGlobalIndex(vtkIdType localIndex, vtkIdType rank); 00063 00064 private: 00065 vtkIdType NumElements; 00066 vtkIdType NumProcessors; 00067 }; 00068 00069 // ---------------------------------------------------------------------- 00070 00071 inline vtkBlockDistribution::vtkBlockDistribution(vtkIdType N, vtkIdType P) 00072 : NumElements(N), NumProcessors(P) 00073 { 00074 } 00075 00076 // ---------------------------------------------------------------------- 00077 00078 inline vtkIdType vtkBlockDistribution::GetBlockSize(vtkIdType rank) 00079 { 00080 return (this->NumElements / this->NumProcessors) 00081 + (rank < this->NumElements % this->NumProcessors? 1 : 0); 00082 } 00083 00084 // ---------------------------------------------------------------------- 00085 00086 inline vtkIdType 00087 vtkBlockDistribution::GetProcessorOfElement(vtkIdType globalIndex) 00088 { 00089 vtkIdType smallBlockSize = this->NumElements / this->NumProcessors; 00090 vtkIdType cutoffProcessor = this->NumElements % this->NumProcessors; 00091 vtkIdType cutoffIndex = cutoffProcessor * (smallBlockSize + 1); 00092 00093 if (globalIndex < cutoffIndex) 00094 { 00095 return globalIndex / (smallBlockSize + 1); 00096 } 00097 else 00098 { 00099 return cutoffProcessor + (globalIndex - cutoffIndex) / smallBlockSize; 00100 } 00101 } 00102 00103 // ---------------------------------------------------------------------- 00104 00105 inline vtkIdType 00106 vtkBlockDistribution::GetLocalIndexOfElement(vtkIdType globalIndex) 00107 { 00108 vtkIdType rank = this->GetProcessorOfElement(globalIndex); 00109 return globalIndex - this->GetFirstGlobalIndexOnProcessor(rank); 00110 } 00111 00112 // ---------------------------------------------------------------------- 00113 00114 inline vtkIdType 00115 vtkBlockDistribution::GetFirstGlobalIndexOnProcessor(vtkIdType rank) 00116 { 00117 vtkIdType estimate = rank * (this->NumElements / this->NumProcessors + 1); 00118 vtkIdType cutoffProcessor = this->NumElements % this->NumProcessors; 00119 if (rank < cutoffProcessor) 00120 { 00121 return estimate; 00122 } 00123 else 00124 { 00125 return estimate - (rank - cutoffProcessor); 00126 } 00127 } 00128 00129 // ---------------------------------------------------------------------- 00130 00131 inline vtkIdType 00132 vtkBlockDistribution::GetGlobalIndex(vtkIdType localIndex, vtkIdType rank) 00133 { 00134 return this->GetFirstGlobalIndexOnProcessor(rank) + localIndex; 00135 } 00136 00137 #endif 00138 // VTK-HeaderTest-Exclude: vtkBlockDistribution.h