| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Trilinos: An Object-Oriented Solver Framework |
| 4 |
// |
| 5 |
// Copyright 2001-2024 NTESS and the Trilinos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include <EpetraExt_AmesosBTFGlobal_LinearProblem.h> |
| 11 |
|
| 12 |
#include <Epetra_Import.h> |
| 13 |
#include <Epetra_Export.h> |
| 14 |
#include <Epetra_CrsMatrix.h> |
| 15 |
#include <Epetra_Vector.h> |
| 16 |
#include <Epetra_MultiVector.h> |
| 17 |
#include <Epetra_CrsGraph.h> |
| 18 |
#include <Epetra_Map.h> |
| 19 |
#include <Epetra_Comm.h> |
| 20 |
#include <Epetra_LinearProblem.h> |
| 21 |
#include <Epetra_Util.h> |
| 22 |
|
| 23 |
#include <EpetraExt_AmesosBTF_CrsMatrix.h> |
| 24 |
#include <EpetraExt_Reindex_CrsMatrix.h> |
| 25 |
#include <EpetraExt_BlockAdjacencyGraph.h> |
| 26 |
|
| 27 |
#include <Teuchos_ParameterList.hpp> |
| 28 |
#include <Isorropia_Epetra.hpp> |
| 29 |
#include <Isorropia_EpetraCostDescriber.hpp> |
| 30 |
#include <Isorropia_EpetraRedistributor.hpp> |
| 31 |
#include <Isorropia_EpetraPartitioner.hpp> |
| 32 |
|
| 33 |
using std::vector; |
| 34 |
|
| 35 |
namespace EpetraExt { |
| 36 |
|
| 37 |
AmesosBTFGlobal_LinearProblem:: |
| 38 |
~AmesosBTFGlobal_LinearProblem() |
| 39 |
{ |
| 40 |
if ( newObj_ ) delete newObj_; |
| 41 |
} |
| 42 |
|
| 43 |
AmesosBTFGlobal_LinearProblem::NewTypeRef |
| 44 |
AmesosBTFGlobal_LinearProblem:: |
| 45 |
operator()( OriginalTypeRef orig ) |
| 46 |
{ |
| 47 |
origObj_ = &orig; |
| 48 |
|
| 49 |
// Extract the matrix and vectors from the linear problem |
| 50 |
OldRHS_ = Teuchos::rcp( orig.GetRHS(), false ); |
| 51 |
OldLHS_ = Teuchos::rcp( orig.GetLHS(), false ); |
| 52 |
OldMatrix_ = Teuchos::rcp( dynamic_cast<Epetra_CrsMatrix *>( orig.GetMatrix() ), false ); |
| 53 |
|
| 54 |
int nGlobal = OldMatrix_->NumGlobalRows(); |
| 55 |
int n = OldMatrix_->NumMyRows(); |
| 56 |
|
| 57 |
// Check if the matrix is on one processor. |
| 58 |
int myMatProc = -1, matProc = -1; |
| 59 |
int myPID = OldMatrix_->Comm().MyPID(); |
| 60 |
int numProcs = OldMatrix_->Comm().NumProc(); |
| 61 |
|
| 62 |
const Epetra_BlockMap& oldRowMap = OldMatrix_->RowMap(); |
| 63 |
|
| 64 |
// Get some information about the parallel distribution. |
| 65 |
int maxMyRows = 0; |
| 66 |
std::vector<int> numGlobalElem( numProcs ); |
| 67 |
OldMatrix_->Comm().GatherAll(&n, &numGlobalElem[0], 1); |
| 68 |
OldMatrix_->Comm().MaxAll(&n, &maxMyRows, 1); |
| 69 |
|
| 70 |
for (int proc=0; proc<numProcs; proc++) |
| 71 |
{ |
| 72 |
if (OldMatrix_->NumGlobalNonzeros() == OldMatrix_->NumMyNonzeros()) |
| 73 |
myMatProc = myPID; |
| 74 |
} |
| 75 |
|
| 76 |
OldMatrix_->Comm().MaxAll( &myMatProc, &matProc, 1 ); |
| 77 |
|
| 78 |
Teuchos::RCP<Epetra_CrsMatrix> serialMatrix; |
| 79 |
Teuchos::RCP<Epetra_Map> serialMap; |
| 80 |
if( oldRowMap.DistributedGlobal() && matProc == -1) |
| 81 |
{ |
| 82 |
// The matrix is distributed and needs to be moved to processor zero. |
| 83 |
// Set the zero processor as the master. |
| 84 |
matProc = 0; |
| 85 |
serialMap = Teuchos::rcp( new Epetra_Map( Epetra_Util::Create_Root_Map( OldMatrix_->RowMap(), matProc ) ) ); |
| 86 |
|
| 87 |
Epetra_Import serialImporter( *serialMap, OldMatrix_->RowMap() ); |
| 88 |
serialMatrix = Teuchos::rcp( new Epetra_CrsMatrix( Copy, *serialMap, 0 ) ); |
| 89 |
serialMatrix->Import( *OldMatrix_, serialImporter, Insert ); |
| 90 |
serialMatrix->FillComplete(); |
| 91 |
} |
| 92 |
else { |
| 93 |
// The old matrix has already been moved to one processor (matProc). |
| 94 |
serialMatrix = OldMatrix_; |
| 95 |
} |
| 96 |
|
| 97 |
if( debug_ ) |
| 98 |
{ |
| 99 |
std::cout << "Original (serial) Matrix:\n"; |
| 100 |
std::cout << *serialMatrix << std::endl; |
| 101 |
} |
| 102 |
|
| 103 |
// Obtain the current row and column orderings |
| 104 |
std::vector<int> origGlobalRows(nGlobal), origGlobalCols(nGlobal); |
| 105 |
serialMatrix->RowMap().MyGlobalElements( &origGlobalRows[0] ); |
| 106 |
serialMatrix->ColMap().MyGlobalElements( &origGlobalCols[0] ); |
| 107 |
|
| 108 |
// Perform reindexing on the full serial matrix (needed for BTF). |
| 109 |
Epetra_Map reIdxMap( serialMatrix->RowMap().NumGlobalElements(), serialMatrix->RowMap().NumMyElements(), 0, serialMatrix->Comm() ); |
| 110 |
Teuchos::RCP<EpetraExt::ViewTransform<Epetra_CrsMatrix> > reIdxTrans = |
| 111 |
Teuchos::rcp( new EpetraExt::CrsMatrix_Reindex( reIdxMap ) ); |
| 112 |
Epetra_CrsMatrix newSerialMatrix = (*reIdxTrans)( *serialMatrix ); |
| 113 |
reIdxTrans->fwd(); |
| 114 |
|
| 115 |
// Compute and apply BTF to the serial CrsMatrix and has been filtered by the threshold |
| 116 |
EpetraExt::AmesosBTF_CrsMatrix BTFTrans( threshold_, upperTri_, verbose_, debug_ ); |
| 117 |
Epetra_CrsMatrix newSerialMatrixBTF = BTFTrans( newSerialMatrix ); |
| 118 |
|
| 119 |
rowPerm_ = BTFTrans.RowPerm(); |
| 120 |
colPerm_ = BTFTrans.ColPerm(); |
| 121 |
blockPtr_ = BTFTrans.BlockPtr(); |
| 122 |
numBlocks_ = BTFTrans.NumBlocks(); |
| 123 |
|
| 124 |
if ( (myPID == matProc) && verbose_) { |
| 125 |
bool isSym = true; |
| 126 |
for (int i=0; i<nGlobal; ++i) { |
| 127 |
if (rowPerm_[i] != colPerm_[i]) { |
| 128 |
isSym = false; |
| 129 |
break; |
| 130 |
} |
| 131 |
} |
| 132 |
std::cout << "The BTF permutation symmetry (0=false,1=true) is : " << isSym << std::endl; |
| 133 |
} |
| 134 |
|
| 135 |
// Compute the permutation w.r.t. the original row and column GIDs. |
| 136 |
std::vector<int> origGlobalRowsPerm(nGlobal), origGlobalColsPerm(nGlobal); |
| 137 |
if (myPID == matProc) { |
| 138 |
for (int i=0; i<nGlobal; ++i) { |
| 139 |
origGlobalRowsPerm[i] = origGlobalRows[ rowPerm_[i] ]; |
| 140 |
origGlobalColsPerm[i] = origGlobalCols[ colPerm_[i] ]; |
| 141 |
} |
| 142 |
} |
| 143 |
OldMatrix_->Comm().Broadcast( &origGlobalRowsPerm[0], nGlobal, matProc ); |
| 144 |
OldMatrix_->Comm().Broadcast( &origGlobalColsPerm[0], nGlobal, matProc ); |
| 145 |
|
| 146 |
// Generate the full serial matrix that imports according to the previously computed BTF. |
| 147 |
Epetra_CrsMatrix newSerialMatrixT( Copy, newSerialMatrixBTF.RowMap(), 0 ); |
| 148 |
newSerialMatrixT.Import( newSerialMatrix, *(BTFTrans.Importer()), Insert ); |
| 149 |
newSerialMatrixT.FillComplete(); |
| 150 |
|
| 151 |
if( debug_ ) |
| 152 |
{ |
| 153 |
std::cout << "Original (serial) Matrix permuted via BTF:\n"; |
| 154 |
std::cout << newSerialMatrixT << std::endl; |
| 155 |
} |
| 156 |
|
| 157 |
// Perform reindexing on the full serial matrix (needed for balancing). |
| 158 |
Epetra_Map reIdxMap2( newSerialMatrixT.RowMap().NumGlobalElements(), newSerialMatrixT.RowMap().NumMyElements(), 0, newSerialMatrixT.Comm() ); |
| 159 |
Teuchos::RCP<EpetraExt::ViewTransform<Epetra_CrsMatrix> > reIdxTrans2 = |
| 160 |
Teuchos::rcp( new EpetraExt::CrsMatrix_Reindex( reIdxMap2 ) ); |
| 161 |
Epetra_CrsMatrix tNewSerialMatrixT = (*reIdxTrans2)( newSerialMatrixT ); |
| 162 |
reIdxTrans2->fwd(); |
| 163 |
|
| 164 |
Teuchos::RCP<Epetra_Map> balancedMap; |
| 165 |
|
| 166 |
if (balance_ == "linear") |
| 167 |
{ |
| 168 |
// Distribute block somewhat evenly across processors |
| 169 |
std::vector<int> rowDist(numProcs+1,0); |
| 170 |
int balRows = nGlobal / numProcs + 1; |
| 171 |
int numRows = balRows, currProc = 1; |
| 172 |
for ( int i=0; i<numBlocks_ || currProc < numProcs; ++i ) { |
| 173 |
if (blockPtr_[i] > numRows) { |
| 174 |
rowDist[currProc++] = blockPtr_[i-1]; |
| 175 |
numRows = blockPtr_[i-1] + balRows; |
| 176 |
} |
| 177 |
} |
| 178 |
rowDist[numProcs] = nGlobal; |
| 179 |
|
| 180 |
// Create new Map based on this linear distribution. |
| 181 |
int numMyBalancedRows = rowDist[myPID+1]-rowDist[myPID]; |
| 182 |
|
| 183 |
NewRowMap_ = Teuchos::rcp( new Epetra_Map( nGlobal, numMyBalancedRows, &origGlobalRowsPerm[ rowDist[myPID] ], 0, OldMatrix_->Comm() ) ); |
| 184 |
// Right now we do not explicitly build the column map and assume the BTF permutation is symmetric! |
| 185 |
//NewColMap_ = Teuchos::rcp( new Epetra_Map( nGlobal, nGlobal, &colPerm_[0], 0, OldMatrix_->Comm() ) ); |
| 186 |
|
| 187 |
if ( verbose_ ) |
| 188 |
{ |
| 189 |
std::cout << "Processor " << myPID << " has " << numMyBalancedRows << " rows." << std::endl; |
| 190 |
} |
| 191 |
} |
| 192 |
else if (balance_ == "isorropia") |
| 193 |
{ |
| 194 |
// Compute block adjacency graph for partitioning. |
| 195 |
std::vector<double> weight; |
| 196 |
EpetraExt::BlockAdjacencyGraph adjGraph; |
| 197 |
Teuchos::RCP<const Epetra_CrsGraph> blkGraph = |
| 198 |
adjGraph.compute( const_cast<Epetra_CrsGraph&>(tNewSerialMatrixT.Graph()), |
| 199 |
numBlocks_, blockPtr_, weight, verbose_); |
| 200 |
|
| 201 |
// Get row weights for rebalancing the graph. |
| 202 |
Teuchos::RCP<const Epetra_Vector> rowWeights = Teuchos::rcp( new Epetra_Vector( View, blkGraph->Map(), &weight[0] ) ); |
| 203 |
|
| 204 |
// Create the cost describer object for Isorropia, pass in row weights. |
| 205 |
Teuchos::RCP<Isorropia::Epetra::CostDescriber> costPtr = |
| 206 |
Teuchos::rcp( new Isorropia::Epetra::CostDescriber() ); |
| 207 |
costPtr->setVertexWeights(rowWeights); |
| 208 |
|
| 209 |
// Create the partitioner object, pass in original graph, cost describer, and empty parameter list. |
| 210 |
Teuchos::RCP<Isorropia::Epetra::Partitioner> partitioner = |
| 211 |
Teuchos::rcp(new Isorropia::Epetra::Partitioner(blkGraph, costPtr)); |
| 212 |
|
| 213 |
// Create the redistributor object. |
| 214 |
Isorropia::Epetra::Redistributor rd(partitioner); |
| 215 |
|
| 216 |
// Call Isorropia to rebalance this graph. |
| 217 |
Teuchos::RCP<Epetra_CrsGraph> balancedGraph = rd.redistribute( *blkGraph ); |
| 218 |
|
| 219 |
int myNumBlkRows = balancedGraph->NumMyRows(); |
| 220 |
|
| 221 |
//std::vector<int> myGlobalElements(nGlobal); |
| 222 |
std::vector<int> newRangeElements(nGlobal), newDomainElements(nGlobal); |
| 223 |
int grid = 0, myElements = 0; |
| 224 |
for (int i=0; i<myNumBlkRows; ++i) { |
| 225 |
grid = balancedGraph->GRID( i ); |
| 226 |
for (int j=blockPtr_[grid]; j<blockPtr_[grid+1]; ++j) { |
| 227 |
newRangeElements[myElements++] = origGlobalRowsPerm[j]; |
| 228 |
//myGlobalElements[myElements++] = j; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
NewRowMap_ = Teuchos::rcp( new Epetra_Map( nGlobal, myElements, &newRangeElements[0], 0, OldMatrix_->Comm() ) ); |
| 233 |
// Right now we do not explicitly build the column map and assume the BTF permutation is symmetric! |
| 234 |
//NewColMap_ = Teuchos::rcp( new Epetra_Map( nGlobal, nGlobal, &colPerm_[0], 0, OldMatrix_->Comm() ) ); |
| 235 |
//balancedMap = Teuchos::rcp( new Epetra_Map( nGlobal, myElements, &myGlobalElements[0], 0, serialMatrix->Comm() ) ); |
| 236 |
|
| 237 |
if ( verbose_ ) |
| 238 |
{ |
| 239 |
std::cout << "Processor " << myPID << " has " << myElements << " rows." << std::endl; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
// Use New Domain and Range Maps to Generate Importer |
| 244 |
//for now, assume they start out as identical |
| 245 |
Epetra_Map OldRowMap = OldMatrix_->RowMap(); |
| 246 |
Epetra_Map OldColMap = OldMatrix_->ColMap(); |
| 247 |
|
| 248 |
if( debug_ ) |
| 249 |
{ |
| 250 |
std::cout << "New Row Map\n"; |
| 251 |
std::cout << *NewRowMap_ << std::endl; |
| 252 |
//std::cout << "New Col Map\n"; |
| 253 |
//std::cout << *NewColMap_ << std::endl; |
| 254 |
} |
| 255 |
|
| 256 |
// Generate New Graph |
| 257 |
// NOTE: Right now we are creating the graph, assuming that the permutation is symmetric! |
| 258 |
// NewGraph_ = Teuchos::rcp( new Epetra_CrsGraph( Copy, *NewRowMap_, *NewColMap_, 0 ) ); |
| 259 |
NewGraph_ = Teuchos::rcp( new Epetra_CrsGraph( Copy, *NewRowMap_, 0 ) ); |
| 260 |
Importer_ = Teuchos::rcp( new Epetra_Import( *NewRowMap_, OldRowMap ) ); |
| 261 |
Importer2_ = Teuchos::rcp( new Epetra_Import( OldRowMap, *NewRowMap_ ) ); |
| 262 |
NewGraph_->Import( OldMatrix_->Graph(), *Importer_, Insert ); |
| 263 |
NewGraph_->FillComplete(); |
| 264 |
|
| 265 |
if( debug_ ) |
| 266 |
{ |
| 267 |
std::cout << "NewGraph\n"; |
| 268 |
std::cout << *NewGraph_; |
| 269 |
} |
| 270 |
|
| 271 |
// Create new linear problem and import information from old linear problem |
| 272 |
NewMatrix_ = Teuchos::rcp( new Epetra_CrsMatrix( Copy, *NewGraph_ ) ); |
| 273 |
NewMatrix_->Import( *OldMatrix_, *Importer_, Insert ); |
| 274 |
NewMatrix_->FillComplete(); |
| 275 |
|
| 276 |
NewLHS_ = Teuchos::rcp( new Epetra_MultiVector( *NewRowMap_, OldLHS_->NumVectors() ) ); |
| 277 |
NewLHS_->Import( *OldLHS_, *Importer_, Insert ); |
| 278 |
|
| 279 |
NewRHS_ = Teuchos::rcp( new Epetra_MultiVector( *NewRowMap_, OldRHS_->NumVectors() ) ); |
| 280 |
NewRHS_->Import( *OldRHS_, *Importer_, Insert ); |
| 281 |
|
| 282 |
if( debug_ ) |
| 283 |
{ |
| 284 |
std::cout << "New Matrix\n"; |
| 285 |
std::cout << *NewMatrix_ << std::endl; |
| 286 |
} |
| 287 |
|
| 288 |
newObj_ = new Epetra_LinearProblem( &*NewMatrix_, &*NewLHS_, &*NewRHS_ ); |
| 289 |
|
| 290 |
return *newObj_; |
| 291 |
} |
| 292 |
|
| 293 |
bool |
| 294 |
AmesosBTFGlobal_LinearProblem:: |
| 295 |
fwd() |
| 296 |
{ |
| 297 |
NewLHS_->Import( *OldLHS_, *Importer_, Insert ); |
| 298 |
NewRHS_->Import( *OldRHS_, *Importer_, Insert ); |
| 299 |
NewMatrix_->Import( *OldMatrix_, *Importer_, Insert ); |
| 300 |
|
| 301 |
return true; |
| 302 |
} |
| 303 |
|
| 304 |
bool |
| 305 |
AmesosBTFGlobal_LinearProblem:: |
| 306 |
rvs() |
| 307 |
{ |
| 308 |
// std::cout << "AmesosBTFGlobal_LinearProblem: NewLHS_" << std::endl; |
| 309 |
// std::cout << *NewLHS_ << std::endl; |
| 310 |
OldLHS_->Import( *NewLHS_, *Importer2_, Insert ); |
| 311 |
//std::cout << "AmesosBTFGlobal_LinearProblem: OldLHS_" << std::endl; |
| 312 |
//std::cout << *OldLHS_ << std::endl; |
| 313 |
|
| 314 |
return true; |
| 315 |
} |
| 316 |
|
| 317 |
} //namespace EpetraExt |