00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "Ifpack_ConfigDefs.h"
00031 #include "Ifpack_Partitioner.h"
00032 #include "Ifpack_OverlappingPartitioner.h"
00033 #include "Ifpack_GreedyPartitioner.h"
00034 #include "Ifpack_Graph.h"
00035
00036 #include "Epetra_Comm.h"
00037 #include "Epetra_BlockMap.h"
00038 #include "Epetra_Map.h"
00039 #include "Epetra_CrsGraph.h"
00040 #include "Teuchos_ParameterList.hpp"
00041
00042
00043 int Ifpack_GreedyPartitioner::ComputePartitions()
00044 {
00045 vector<int> ElementsPerPart(NumLocalParts());
00046 vector<int> Count(NumLocalParts());
00047 for (int i = 0 ; i < NumLocalParts() ; ++i)
00048 Count[i] = 0;
00049
00050
00051 int div = NumMyRows() / NumLocalParts();
00052 int mod = NumMyRows() % NumLocalParts();
00053
00054 for (int i = 0 ; i < NumLocalParts() ; ++i) {
00055 Count[i] = 0;
00056 ElementsPerPart[i] = div;
00057 if (i < mod) ElementsPerPart[i]++;
00058 }
00059
00060 for( int i=0 ; i<NumMyRows() ; ++i ) {
00061 Partition_[i] = -1;
00062 }
00063
00064 int NumEntries;
00065 vector<int> Indices(MaxNumEntries());
00066
00067
00068 int CurrentPartition = 0;
00069 int TotalCount = 0;
00070
00071
00072 for (int i = 0 ; i < NumMyRows() ; ++i) {
00073 NumEntries = 0;
00074 int ierr = Graph_->ExtractMyRowCopy(i, MaxNumEntries(),
00075 NumEntries, &Indices[0]);
00076 IFPACK_CHK_ERR(ierr);
00077 if (NumEntries <= 1) {
00078 Partition_[i] = 0;
00079 TotalCount++;
00080 }
00081 }
00082
00083 if (TotalCount)
00084 CurrentPartition = 1;
00085
00086 vector<int> ThisLevel(1);
00087 ThisLevel[0] = RootNode_;
00088
00089
00090 if (Partition_[RootNode_] != -1) {
00091
00092 for (int i = 0 ; i < NumMyRows() ; ++i)
00093 if (Partition_[i] == -1) {
00094 ThisLevel[0] = i;
00095 break;
00096 }
00097 }
00098 else {
00099 Partition_[RootNode_] = CurrentPartition;
00100 }
00101
00102
00103 while (ThisLevel.size()) {
00104
00105 vector<int> NextLevel;
00106
00107 for (unsigned int i = 0 ; i < ThisLevel.size() ; ++i) {
00108
00109 int CurrentNode = ThisLevel[i];
00110 int ierr = Graph_->ExtractMyRowCopy(CurrentNode, MaxNumEntries(),
00111 NumEntries, &Indices[0]);
00112 IFPACK_CHK_ERR(ierr);
00113
00114 if (NumEntries <= 1)
00115 continue;
00116
00117 for (int j = 0 ; j < NumEntries ; ++j) {
00118
00119 int NextNode = Indices[j];
00120 if (NextNode >= NumMyRows()) continue;
00121
00122 if (Partition_[NextNode] == -1) {
00123
00124 NumLocalParts_ = CurrentPartition + 1;
00125 Partition_[NextNode] = CurrentPartition;
00126 ++Count[CurrentPartition];
00127 ++TotalCount;
00128 NextLevel.push_back(NextNode);
00129 }
00130 }
00131 }
00132
00133
00134 if (Count[CurrentPartition] >= ElementsPerPart[CurrentPartition])
00135 ++CurrentPartition;
00136
00137
00138 ThisLevel.resize(0);
00139 for (unsigned int i = 0 ; i < NextLevel.size() ; ++i)
00140 ThisLevel.push_back(NextLevel[i]);
00141
00142 if (ThisLevel.size() == 0 && (TotalCount != NumMyRows())) {
00143
00144 for (int i = 0 ; i < NumMyRows() ; i++) {
00145 if (Partition_[i] == -1)
00146 ThisLevel.push_back(i);
00147 break;
00148 }
00149 }
00150
00151 }
00152
00153 return(0);
00154 }
00155