| 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_AmesosBTF_CrsMatrix.h> |
| 11 |
|
| 12 |
#include <Epetra_Import.h> |
| 13 |
#include <Epetra_CrsMatrix.h> |
| 14 |
#include <Epetra_CrsGraph.h> |
| 15 |
#include <Epetra_Map.h> |
| 16 |
#include <Epetra_Comm.h> |
| 17 |
|
| 18 |
#include <trilinos_btf_decl.h> |
| 19 |
|
| 20 |
using std::vector; |
| 21 |
using std::cout; |
| 22 |
using std::endl; |
| 23 |
|
| 24 |
namespace EpetraExt { |
| 25 |
|
| 26 |
AmesosBTF_CrsMatrix:: |
| 27 |
~AmesosBTF_CrsMatrix() |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
AmesosBTF_CrsMatrix::NewTypeRef |
| 32 |
AmesosBTF_CrsMatrix:: |
| 33 |
operator()( OriginalTypeRef orig ) |
| 34 |
{ |
| 35 |
origObj_ = &orig; |
| 36 |
const Epetra_BlockMap & OldRowMap = orig.RowMap(); |
| 37 |
const Epetra_BlockMap & OldColMap = orig.ColMap(); |
| 38 |
|
| 39 |
// Check if the matrix is on one processor. |
| 40 |
int myMatProc = -1, matProc = -1; |
| 41 |
int myPID = orig.Comm().MyPID(); |
| 42 |
for (int proc=0; proc<orig.Comm().NumProc(); proc++) |
| 43 |
{ |
| 44 |
if (orig.NumGlobalNonzeros() == orig.NumMyNonzeros()) |
| 45 |
myMatProc = myPID; |
| 46 |
} |
| 47 |
orig.Comm().MaxAll( &myMatProc, &matProc, 1 ); |
| 48 |
|
| 49 |
if( orig.RowMap().DistributedGlobal() && matProc == -1) |
| 50 |
{ cout << "FAIL for Global!\n"; abort(); } |
| 51 |
if( orig.IndicesAreGlobal() && matProc == -1) |
| 52 |
{ cout << "FAIL for Global Indices!\n"; abort(); } |
| 53 |
|
| 54 |
int nGlobal = orig.NumGlobalRows(); |
| 55 |
int n = orig.NumMyRows(); |
| 56 |
int nnz = orig.NumMyNonzeros(); |
| 57 |
|
| 58 |
if( debug_ ) |
| 59 |
{ |
| 60 |
cout << "Orig Matrix:\n"; |
| 61 |
cout << orig << endl; |
| 62 |
} |
| 63 |
|
| 64 |
// Create std CRS format (without elements above the threshold) |
| 65 |
vector<int> ia(n+1,0); |
| 66 |
int maxEntries = orig.MaxNumEntries(); |
| 67 |
vector<int> ja(nnz), ja_tmp(nnz); |
| 68 |
vector<double> jva_tmp(maxEntries); |
| 69 |
int cnt; |
| 70 |
|
| 71 |
Epetra_CrsGraph strippedGraph( Copy, OldRowMap, OldColMap, 0 ); |
| 72 |
|
| 73 |
for( int i = 0; i < n; ++i ) |
| 74 |
{ |
| 75 |
orig.ExtractMyRowCopy( i, maxEntries, cnt, &jva_tmp[0], &ja_tmp[0] ); |
| 76 |
ia[i+1] = ia[i]; |
| 77 |
for( int j = 0; j < cnt; ++j ) |
| 78 |
if( fabs(jva_tmp[j]) > threshold_ ) |
| 79 |
ja[ ia[i+1]++ ] = ja_tmp[j]; |
| 80 |
|
| 81 |
int new_cnt = ia[i+1] - ia[i]; |
| 82 |
strippedGraph.InsertMyIndices( i, new_cnt, &ja[ ia[i] ] ); |
| 83 |
} |
| 84 |
nnz = ia[n]; |
| 85 |
strippedGraph.FillComplete(); |
| 86 |
|
| 87 |
if( debug_ ) |
| 88 |
{ |
| 89 |
cout << "Stripped Graph\n"; |
| 90 |
cout << strippedGraph; |
| 91 |
} |
| 92 |
|
| 93 |
// Compute the BTF permutation only on the processor that has the graph. |
| 94 |
if ( matProc == myPID ) { |
| 95 |
|
| 96 |
if( debug_ ) |
| 97 |
{ |
| 98 |
cout << "-----------------------------------------\n"; |
| 99 |
cout << "CRS Format Graph (stripped) \n"; |
| 100 |
cout << "-----------------------------------------\n"; |
| 101 |
for( int i = 0; i < n; ++i ) |
| 102 |
{ |
| 103 |
cout << ia[i] << " - " << ia[i+1] << " : "; |
| 104 |
for( int j = ia[i]; j<ia[i+1]; ++j ) |
| 105 |
cout << " " << ja[j]; |
| 106 |
cout << endl; |
| 107 |
} |
| 108 |
cout << "-----------------------------------------\n"; |
| 109 |
} |
| 110 |
|
| 111 |
// Transpose the graph, not the values |
| 112 |
int j=0, next=0; |
| 113 |
vector<int> ia_tmp(n+1,0); |
| 114 |
|
| 115 |
// Compute row lengths |
| 116 |
for (int i = 0; i < n; i++) |
| 117 |
for (int k = ia[i]; k < ia[i+1]; k++) |
| 118 |
++ia_tmp[ ja[k]+1 ]; |
| 119 |
|
| 120 |
// Compute pointers from row lengths |
| 121 |
ia_tmp[0] = 0; |
| 122 |
for (int i = 0; i < n; i++) |
| 123 |
ia_tmp[i+1] += ia_tmp[i]; |
| 124 |
|
| 125 |
// Copy over indices |
| 126 |
for (int i = 0; i < n; i++) { |
| 127 |
for (int k = ia[i]; k < ia[i+1]; k++) { |
| 128 |
j = ja[k]; |
| 129 |
next = ia_tmp[j]; |
| 130 |
ja_tmp[next] = i; |
| 131 |
ia_tmp[j] = next + 1; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// Reshift ia_tmp |
| 136 |
for (int i=n-1; i >= 0; i--) ia_tmp[i+1] = ia_tmp[i]; |
| 137 |
ia_tmp[0] = 0; |
| 138 |
|
| 139 |
// Transformation information |
| 140 |
int numMatch = 0; // number of nonzeros on diagonal after permutation. |
| 141 |
double maxWork = 0.0; // no limit on how much work to perform in max-trans. |
| 142 |
double workPerf = 0.0; // how much work was performed in max-trans. |
| 143 |
|
| 144 |
// Create a work vector for the BTF code. |
| 145 |
vector<int> work(5*n); |
| 146 |
|
| 147 |
// Storage for the row and column permutations. |
| 148 |
vector<int> rowperm(n); |
| 149 |
vector<int> colperm(n); |
| 150 |
vector<int> blockptr(n+1); |
| 151 |
|
| 152 |
// NOTE: The permutations are sent in backwards since the matrix is transposed. |
| 153 |
// On output, rowperm and colperm are the row and column permutations of A, where |
| 154 |
// i = TRILINOS_BTF_UNFLIP(rowperm[k]) if row i of A is the kth row of P*A*Q, and j = colperm[k] |
| 155 |
// if column j of A is the kth column of P*A*Q. If rowperm[k] < 0, then the |
| 156 |
// (k,k)th entry in P*A*Q is structurally zero. |
| 157 |
|
| 158 |
numBlocks_ = trilinos_btf_order( n, &ia_tmp[0], &ja_tmp[0], maxWork, &workPerf, |
| 159 |
&rowperm[0], &colperm[0], &blockptr[0], |
| 160 |
&numMatch, &work[0] ); |
| 161 |
|
| 162 |
// Reverse ordering of permutation to get upper triangular form, if necessary. |
| 163 |
rowPerm_.resize( n ); |
| 164 |
colPerm_.resize( n ); |
| 165 |
blockptr.resize( numBlocks_+1 ); |
| 166 |
blockPtr_.resize( numBlocks_+1 ); |
| 167 |
if (!upperTri_) { |
| 168 |
for( int i = 0; i < n; ++i ) |
| 169 |
{ |
| 170 |
rowPerm_[i] = TRILINOS_BTF_UNFLIP(rowperm[(n-1)-i]); |
| 171 |
colPerm_[i] = colperm[(n-1)-i]; |
| 172 |
} |
| 173 |
for( int i = 0; i < numBlocks_+1; ++i ) |
| 174 |
{ |
| 175 |
blockPtr_[i] = n - blockptr[numBlocks_-i]; |
| 176 |
} |
| 177 |
} |
| 178 |
else { |
| 179 |
colPerm_ = colperm; |
| 180 |
blockPtr_ = blockptr; |
| 181 |
for( int i = 0; i < n; ++i ) |
| 182 |
{ |
| 183 |
rowPerm_[i] = TRILINOS_BTF_UNFLIP(rowperm[i]); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if( debug_ ) { |
| 188 |
cout << "-----------------------------------------\n"; |
| 189 |
cout << "BTF Output (n = " << n << ")\n"; |
| 190 |
cout << "-----------------------------------------\n"; |
| 191 |
cout << "Num Blocks: " << numBlocks_ << endl; |
| 192 |
cout << "Num NNZ Diags: " << numMatch << endl; |
| 193 |
cout << "RowPerm and ColPerm \n"; |
| 194 |
for( int i = 0; i<n; ++i ) |
| 195 |
cout << rowPerm_[i] << "\t" << colPerm_[i] << endl; |
| 196 |
cout << "-----------------------------------------\n"; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// Broadcast the BTF permutation information to all processors. |
| 201 |
rowPerm_.resize( nGlobal ); |
| 202 |
colPerm_.resize( nGlobal ); |
| 203 |
|
| 204 |
orig.Comm().Broadcast(&rowPerm_[0], nGlobal, matProc); |
| 205 |
orig.Comm().Broadcast(&colPerm_[0], nGlobal, matProc); |
| 206 |
orig.Comm().Broadcast(&numBlocks_, 1, matProc); |
| 207 |
|
| 208 |
blockPtr_.resize( numBlocks_+1 ); |
| 209 |
orig.Comm().Broadcast(&blockPtr_[0], numBlocks_+1, matProc); |
| 210 |
|
| 211 |
//Generate New Domain and Range Maps |
| 212 |
//for now, assume they start out as identical |
| 213 |
vector<int> myElements( n ); |
| 214 |
OldRowMap.MyGlobalElements( &myElements[0] ); |
| 215 |
|
| 216 |
vector<int> newDomainElements( n ); |
| 217 |
vector<int> newRangeElements( n ); |
| 218 |
for( int i = 0; i < n; ++i ) |
| 219 |
{ |
| 220 |
newRangeElements[ i ] = myElements[ rowPerm_[i] ]; |
| 221 |
newDomainElements[ i ] = myElements[ colPerm_[i] ]; |
| 222 |
} |
| 223 |
|
| 224 |
NewRowMap_ = Teuchos::rcp( new Epetra_Map( nGlobal, n, &newRangeElements[0], OldRowMap.IndexBase(), OldRowMap.Comm() ) ); |
| 225 |
NewColMap_ = Teuchos::rcp( new Epetra_Map( nGlobal, n, &newDomainElements[0], OldColMap.IndexBase(), OldColMap.Comm() ) ); |
| 226 |
|
| 227 |
if( debug_ ) |
| 228 |
{ |
| 229 |
cout << "New Row Map\n"; |
| 230 |
cout << *NewRowMap_ << endl; |
| 231 |
cout << "New Col Map\n"; |
| 232 |
cout << *NewColMap_ << endl; |
| 233 |
} |
| 234 |
|
| 235 |
//Generate New Graph |
| 236 |
NewGraph_ = Teuchos::rcp( new Epetra_CrsGraph( Copy, *NewRowMap_, *NewColMap_, 0 ) ); |
| 237 |
Importer_ = Teuchos::rcp( new Epetra_Import( *NewRowMap_, OldRowMap ) ); |
| 238 |
NewGraph_->Import( strippedGraph, *Importer_, Insert ); |
| 239 |
NewGraph_->FillComplete(); |
| 240 |
|
| 241 |
if( debug_ ) |
| 242 |
{ |
| 243 |
cout << "NewGraph\n"; |
| 244 |
cout << *NewGraph_; |
| 245 |
} |
| 246 |
|
| 247 |
NewMatrix_ = Teuchos::rcp( new Epetra_CrsMatrix( Copy, *NewGraph_ ) ); |
| 248 |
NewMatrix_->Import( orig, *Importer_, Insert ); |
| 249 |
NewMatrix_->FillComplete(); |
| 250 |
|
| 251 |
if( debug_ ) |
| 252 |
{ |
| 253 |
cout << "New CrsMatrix\n"; |
| 254 |
cout << *NewMatrix_ << endl; |
| 255 |
} |
| 256 |
|
| 257 |
newObj_ = &*NewMatrix_; |
| 258 |
|
| 259 |
return *NewMatrix_; |
| 260 |
} |
| 261 |
|
| 262 |
bool |
| 263 |
AmesosBTF_CrsMatrix:: |
| 264 |
fwd() |
| 265 |
{ |
| 266 |
NewMatrix_->Import( *origObj_, *Importer_, Insert ); |
| 267 |
return true; |
| 268 |
} |
| 269 |
|
| 270 |
bool |
| 271 |
AmesosBTF_CrsMatrix:: |
| 272 |
rvs() |
| 273 |
{ |
| 274 |
origObj_->Export( *NewMatrix_, *Importer_, Insert ); |
| 275 |
return true; |
| 276 |
} |
| 277 |
|
| 278 |
} //namespace EpetraExt |