| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// ShyLU: Scalable Hybrid LU Preconditioner and Solver |
| 4 |
// |
| 5 |
// Copyright 2011 NTESS and the ShyLU contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/** \file shylu_util.cpp |
| 11 |
|
| 12 |
\brief Utilities for ShyLU |
| 13 |
|
| 14 |
\author Siva Rajamanickam |
| 15 |
|
| 16 |
*/ |
| 17 |
|
| 18 |
#include <assert.h> |
| 19 |
#include <fstream> |
| 20 |
|
| 21 |
#include "shylu_util.h" |
| 22 |
|
| 23 |
#ifdef HAVE_SHYLU_DDCORE_MPI |
| 24 |
#include "Epetra_MpiComm.h" |
| 25 |
#else |
| 26 |
#include "Epetra_SerialComm.h" |
| 27 |
#endif |
| 28 |
#include "Epetra_CrsMatrix.h" |
| 29 |
|
| 30 |
// EpetraExt includes |
| 31 |
#include "EpetraExt_RowMatrixOut.h" |
| 32 |
|
| 33 |
//Teuchos includes |
| 34 |
#include "Teuchos_XMLParameterListHelpers.hpp" |
| 35 |
|
| 36 |
//Isorropia includes |
| 37 |
#include "Isorropia_Epetra.hpp" |
| 38 |
#include "Isorropia_EpetraRedistributor.hpp" |
| 39 |
#include "Isorropia_EpetraPartitioner.hpp" |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
// Currently takes onle MpiComm |
| 46 |
Epetra_CrsMatrix *balanceAndRedistribute(Epetra_CrsMatrix *A, |
| 47 |
Teuchos::ParameterList isoList) |
| 48 |
{ |
| 49 |
// int myPID = A->Comm().MyPID(); // unused |
| 50 |
|
| 51 |
// Debug [ |
| 52 |
Epetra_Map ARowMap = A->RowMap(); |
| 53 |
// int nrows = ARowMap.NumMyElements(); // unused |
| 54 |
// int *rows = ARowMap.MyGlobalElements(); // unused |
| 55 |
// ] |
| 56 |
|
| 57 |
// ==================== Symbolic factorization ========================= |
| 58 |
// 1. Partition and redistribute [ |
| 59 |
Isorropia::Epetra::Partitioner *partitioner = new |
| 60 |
Isorropia::Epetra::Partitioner(A, isoList, false); |
| 61 |
partitioner->partition(); |
| 62 |
|
| 63 |
Isorropia::Epetra::Redistributor rd(partitioner); |
| 64 |
Epetra_CrsMatrix *newA; |
| 65 |
rd.redistribute(*A, newA); |
| 66 |
// ] |
| 67 |
EpetraExt::RowMatrixToMatlabFile("A.mat", *newA); |
| 68 |
|
| 69 |
delete partitioner; |
| 70 |
return newA; |
| 71 |
} |
| 72 |
|
| 73 |
/* TODO : Do this only for Debug ? */ |
| 74 |
void checkMaps(Epetra_CrsMatrix *A) |
| 75 |
{ |
| 76 |
// Get column map |
| 77 |
Epetra_Map AColMap = A->ColMap(); |
| 78 |
// int ncols = AColMap.NumMyElements(); // unused |
| 79 |
// int *cols = AColMap.MyGlobalElements(); // unused |
| 80 |
|
| 81 |
// Get domain map |
| 82 |
Epetra_Map ADomainMap = A->DomainMap(); |
| 83 |
|
| 84 |
int nelems = ADomainMap.NumMyElements(); |
| 85 |
#ifndef NDEBUG |
| 86 |
// mfh 25 May 2015: Only used in an assert() below. |
| 87 |
// assert() is defined to nothing in a release build. |
| 88 |
int *dom_cols = ADomainMap.MyGlobalElements(); |
| 89 |
#endif // NDEBUG |
| 90 |
|
| 91 |
// Get range map |
| 92 |
Epetra_Map ARangeMap = A->RangeMap(); |
| 93 |
// int npts = ARangeMap.NumMyElements(); // unused |
| 94 |
#ifndef NDEBUG |
| 95 |
// mfh 25 May 2015: Only used in an assert() below. |
| 96 |
// assert() is defined to nothing in a release build. |
| 97 |
int *ran_cols = ARangeMap.MyGlobalElements(); |
| 98 |
#endif // NDEBUG |
| 99 |
|
| 100 |
// Get row map |
| 101 |
Epetra_Map ARowMap = A->RowMap(); |
| 102 |
// int nrows = ARowMap.NumMyElements(); // unused |
| 103 |
#ifndef NDEBUG |
| 104 |
// mfh 25 May 2015: Only used in an assert() below. |
| 105 |
// assert() is defined to nothing in a release build. |
| 106 |
int *rows = ARowMap.MyGlobalElements(); |
| 107 |
#endif // NDEBUG |
| 108 |
|
| 109 |
//cout <<"In PID ="<< A->Comm().MyPID() <<" #cols="<< ncols << " #rows="<< |
| 110 |
//nrows <<" #domain elems="<< nelems <<" #range elems="<< npts << endl; |
| 111 |
// See if domain map == range map == row map |
| 112 |
for (int i = 0; i < nelems ; i++) |
| 113 |
{ |
| 114 |
// Will this always be the case ? We will find out if assertion fails ! |
| 115 |
assert(dom_cols[i] == ran_cols[i]); |
| 116 |
assert(rows[i] == ran_cols[i]); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// TODO: SNumGlobalCols never used |
| 121 |
void findLocalColumns(Epetra_CrsMatrix *A, int *gvals, int &SNumGlobalCols) |
| 122 |
{ |
| 123 |
|
| 124 |
int n = A->NumGlobalRows(); |
| 125 |
// Get column map |
| 126 |
Epetra_Map AColMap = A->ColMap(); |
| 127 |
int ncols = AColMap.NumMyElements(); |
| 128 |
int *cols = AColMap.MyGlobalElements(); // TODO : Indexing using GID ! |
| 129 |
|
| 130 |
// 2. Find column permutation [ |
| 131 |
// Find all columns in this proc |
| 132 |
int *vals = new int[n]; // vector of size n, not ncols |
| 133 |
for (int i = 0; i < n ; i++) |
| 134 |
{ |
| 135 |
vals[i] = 0; |
| 136 |
gvals[i] = 0; |
| 137 |
} |
| 138 |
|
| 139 |
// Null columns in A are not part of any proc |
| 140 |
for (int i = 0; i < ncols ; i++) |
| 141 |
{ |
| 142 |
vals[cols[i]] = 1; // Set to 1 for locally owned columns |
| 143 |
} |
| 144 |
|
| 145 |
// Bottleneck?: Compute the column permutation |
| 146 |
A->Comm().SumAll(vals, gvals, n); |
| 147 |
|
| 148 |
SNumGlobalCols = 0; |
| 149 |
for (int i = 0; i < n ; i++) |
| 150 |
{ |
| 151 |
//cout << gvals[i] ; |
| 152 |
if (gvals[i] > 1) |
| 153 |
SNumGlobalCols++; |
| 154 |
} |
| 155 |
//cout << endl; |
| 156 |
//cout << "Snum Global cols=" << SNumGlobalCols << endl; |
| 157 |
|
| 158 |
delete[] vals; |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
// This function uses a very simple tie-breaking heuristic to find a |
| 163 |
// "narrow" separator from a wide separator. The vertices in the proc with |
| 164 |
// smaller procID will become part of the separator |
| 165 |
// This is not a true narrow separator, which needs a vertex cover algorithm. |
| 166 |
// This is like a medium separator ! |
| 167 |
// TODO : This assumes symmetry I guess, Check |
| 168 |
void findNarrowSeparator(Epetra_CrsMatrix *A, int *gvals) |
| 169 |
{ |
| 170 |
int nentries; |
| 171 |
double *values; |
| 172 |
int *indices; |
| 173 |
int n = A->NumGlobalRows(); |
| 174 |
|
| 175 |
int myPID = A->Comm().MyPID(); |
| 176 |
|
| 177 |
// Get row map |
| 178 |
Epetra_Map rMap = A->RowMap(); |
| 179 |
Epetra_Map cMap = A->ColMap(); |
| 180 |
int *rows = rMap.MyGlobalElements(); |
| 181 |
int relems = rMap.NumMyElements(); |
| 182 |
|
| 183 |
int *vals = new int[n]; // vector of size n, not ncols |
| 184 |
int *allGIDs = new int[n]; // vector of size n, not ncols |
| 185 |
for (int i = 0; i < n ; i++) // initialize to zero |
| 186 |
{ |
| 187 |
vals[i] = 0; |
| 188 |
} |
| 189 |
|
| 190 |
// Rows are uniquely owned, so this will work |
| 191 |
for (int i = 0; i < relems ; i++) |
| 192 |
{ |
| 193 |
vals[rows[i]] = myPID; // I own relems[i] |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
// **************** Collective communication ************** |
| 198 |
// This will not scale well for very large number of nodes |
| 199 |
// But on the node this should be fine |
| 200 |
A->Comm().SumAll(vals, allGIDs, n); |
| 201 |
|
| 202 |
// At this point all procs know who owns what rows |
| 203 |
for (int i = 0; i < n ; i++) // initialize to zero |
| 204 |
vals[i] = 0; |
| 205 |
|
| 206 |
int gid, cgid; |
| 207 |
for (int i = 0; i < relems; i++) |
| 208 |
{ |
| 209 |
gid = rows[i]; |
| 210 |
//cout << "PID=" << myPID << " " << "rowid=" << gid ; |
| 211 |
if (gvals[gid] != 1) |
| 212 |
{ |
| 213 |
//cout << " in the sep "; |
| 214 |
bool movetoBlockDiagonal = false; |
| 215 |
// mfh 25 May 2015: This call used to assign its (int) |
| 216 |
// return value to 'err'. I got rid of this, because |
| 217 |
// 'err' was unused. This resulted in a "set but unused |
| 218 |
// variable" warning. |
| 219 |
(void) A->ExtractMyRowView(i, nentries, values, indices); |
| 220 |
//cout << " with nentries= "<< nentries; |
| 221 |
|
| 222 |
assert(nentries != 0); |
| 223 |
for (int j = 0; j < nentries; j++) |
| 224 |
{ |
| 225 |
cgid = cMap.GID(indices[j]); |
| 226 |
assert(cgid != -1); |
| 227 |
if (gvals[cgid] == 1 || allGIDs[cgid] == myPID) |
| 228 |
continue; // simplify the rest |
| 229 |
|
| 230 |
/*if (numProcs == 2) |
| 231 |
{*/ |
| 232 |
if (allGIDs[cgid] < myPID) |
| 233 |
{ |
| 234 |
// row cgid is owned by a proc with smaller PID |
| 235 |
movetoBlockDiagonal = true; |
| 236 |
//cout << "\t mving to diag because of column" << cgid; |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
// There is at least one edge from this vertex to a |
| 241 |
// vertex in a proc with PID > myPID, cannot move |
| 242 |
// to diagonal. This is too restrictive, but |
| 243 |
// important for correctness, until we can use a |
| 244 |
// vertex cover algorithm. |
| 245 |
movetoBlockDiagonal = false; |
| 246 |
break; |
| 247 |
//cout << "\tNo problem with cgid=" << cgid << "in sep"; |
| 248 |
} |
| 249 |
/*} |
| 250 |
else |
| 251 |
{ |
| 252 |
if (myPID == 0 && allGIDs[cgid] == numProcs-1) |
| 253 |
{ |
| 254 |
// row cgid is owned by a proc with smaller PID |
| 255 |
movetoBlockDiagonal = true; |
| 256 |
cout << "\t I AM HERE mving to diag because of column" << cgid; |
| 257 |
} |
| 258 |
else if (myPID == numProcs-1 && allGIDs[cgid] == 0) |
| 259 |
{ |
| 260 |
cout << "\t I AM HERE to continue " << cgid; |
| 261 |
continue; |
| 262 |
} |
| 263 |
else if (allGIDs[cgid] < myPID) |
| 264 |
{ |
| 265 |
// row cgid is owned by a proc with smaller PID |
| 266 |
movetoBlockDiagonal = true; |
| 267 |
cout << "\t mving to diag because of column" << cgid; |
| 268 |
} |
| 269 |
else |
| 270 |
{ |
| 271 |
//cout << "\tNo problem with cgid=" << cgid << "in sep"; |
| 272 |
} |
| 273 |
}*/ |
| 274 |
} |
| 275 |
if (movetoBlockDiagonal) |
| 276 |
{ |
| 277 |
//cout << "Moving to Diagonal"; |
| 278 |
vals[gid] = 1; |
| 279 |
gvals[gid] = 1; // The smaller PIDs have to know about this |
| 280 |
// change. Send the change using gvals. |
| 281 |
} |
| 282 |
} |
| 283 |
else |
| 284 |
{ |
| 285 |
// do nothing, in the diagonal block already |
| 286 |
//cout << "In the diagonal block"; |
| 287 |
} |
| 288 |
//cout << endl; |
| 289 |
} |
| 290 |
|
| 291 |
// Reuse allGIDs to propagate the result of moving to diagonal |
| 292 |
for (int i = 0; i < n ; i++) // initialize to zero |
| 293 |
allGIDs[i] = 0; |
| 294 |
|
| 295 |
A->Comm().SumAll(vals, allGIDs, n); |
| 296 |
for (int i = 0; i < n ; i++) |
| 297 |
{ |
| 298 |
if (allGIDs[i] == 1) |
| 299 |
{ |
| 300 |
// Some interface columns will have gvals[1] after this |
| 301 |
// as the separator is narrow now. |
| 302 |
gvals[i] = 1; // GIDs as indices assumption |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
delete[] vals; |
| 307 |
delete[] allGIDs; |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
void findBlockElems(Epetra_CrsMatrix *A, int nrows, int *rows, int *gvals, |
| 312 |
int Lnr, int *LeftElems, |
| 313 |
int Rnr, int *RightElems, std::string s1, std::string s2, bool cols) |
| 314 |
{ |
| 315 |
|
| 316 |
int gid; |
| 317 |
int rcnt = 0; int lcnt = 0; |
| 318 |
// Assemble ids in two arrays |
| 319 |
std::ostringstream ssmsg1; |
| 320 |
std::ostringstream ssmsg2; |
| 321 |
|
| 322 |
#ifdef DUMP_MATRICES |
| 323 |
std::ostringstream fnamestr; |
| 324 |
fnamestr << s1 << ".mat"; |
| 325 |
std::string fname = fnamestr.str(); |
| 326 |
ofstream outD(fname.c_str()); |
| 327 |
|
| 328 |
std::ostringstream fnamestrR; |
| 329 |
fnamestrR << s2 << ".mat"; |
| 330 |
std::string fnameR = fnamestrR.str(); |
| 331 |
ofstream outR(fnameR.c_str()); |
| 332 |
#endif |
| 333 |
|
| 334 |
ssmsg1 << s1; |
| 335 |
ssmsg2 << s2; |
| 336 |
for (int i = 0; i < nrows; i++) |
| 337 |
{ |
| 338 |
gid = rows[i]; |
| 339 |
assert (gvals[gid] >= 1); |
| 340 |
// If the row is local & row/column is not shared then row/column |
| 341 |
// belongs to D (this is not true for R, which can have more columns |
| 342 |
// than D) |
| 343 |
//if (A->LRID(gid) != -1 && gvals[gid] == 1) |
| 344 |
if (gvals[gid] == 1) |
| 345 |
{ |
| 346 |
if (cols && A->LRID(gid) == -1) continue; |
| 347 |
assert(lcnt < Lnr); |
| 348 |
LeftElems[lcnt++] = gid; |
| 349 |
ssmsg1 << gid << " "; |
| 350 |
#ifdef DUMP_MATRICES |
| 351 |
outD << gid << endl; |
| 352 |
#endif |
| 353 |
} |
| 354 |
else |
| 355 |
{ |
| 356 |
assert(rcnt < Rnr); |
| 357 |
RightElems[rcnt++] = gid; |
| 358 |
ssmsg2 << gid << " "; |
| 359 |
#ifdef DUMP_MATRICES |
| 360 |
outR << gid << endl; |
| 361 |
#endif |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
#ifdef DUMP_MATRICES |
| 366 |
outD.close(); |
| 367 |
outR.close(); |
| 368 |
#endif |
| 369 |
|
| 370 |
#ifdef DEBUG |
| 371 |
cout << ssmsg1.str() << endl; |
| 372 |
cout << ssmsg2.str() << endl; |
| 373 |
#endif |
| 374 |
ssmsg1.clear(); ssmsg1.str(""); |
| 375 |
ssmsg2.clear(); ssmsg2.str(""); |
| 376 |
|
| 377 |
assert(lcnt == Lnr); |
| 378 |
assert(rcnt == Rnr); |
| 379 |
} |