| 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 |
#include "shylu.h" |
| 11 |
#include "shylu_util.h" |
| 12 |
//#include "EpetraExt_Transpose_RowMatrix.h" |
| 13 |
#include "Epetra_Vector.h" |
| 14 |
#include "shylu_probing_operator.h" |
| 15 |
#include "shylu_local_schur_operator.h" |
| 16 |
#include <EpetraExt_Reindex_CrsMatrix.h> |
| 17 |
|
| 18 |
/* Apply an identity matrix to the Schur complement operator. Drop the entries |
| 19 |
entries using a relative threshold. Assemble the result in a Crs Matrix |
| 20 |
which will be our approximate Schur complement. |
| 21 |
*/ |
| 22 |
Teuchos::RCP<Epetra_CrsMatrix> computeApproxSchur(shylu_config *config, |
| 23 |
shylu_symbolic *sym, |
| 24 |
Epetra_CrsMatrix *G, Epetra_CrsMatrix *R, |
| 25 |
Epetra_LinearProblem *LP, Amesos_BaseSolver *solver, |
| 26 |
Ifpack_Preconditioner *ifSolver, Epetra_CrsMatrix *C, |
| 27 |
Epetra_Map *localDRowMap) |
| 28 |
{ |
| 29 |
double relative_thres = config->relative_threshold; |
| 30 |
int nvectors = 16; |
| 31 |
|
| 32 |
ShyLU_Probing_Operator probeop(config, sym, G, R, LP, solver, ifSolver, C, |
| 33 |
localDRowMap, nvectors); |
| 34 |
|
| 35 |
// Get row map |
| 36 |
Epetra_Map rMap = G->RowMap(); |
| 37 |
int *rows = rMap.MyGlobalElements(); |
| 38 |
int totalElems = rMap.NumGlobalElements(); |
| 39 |
int localElems = rMap.NumMyElements(); |
| 40 |
//std::cout << " totalElems in Schur Complement" << totalElems << std::endl; |
| 41 |
//std::cout << myPID << " localElems" << localElems << std::endl; |
| 42 |
|
| 43 |
// **************** Two collectives here ********************* |
| 44 |
#ifdef TIMING_OUTPUT |
| 45 |
Teuchos::Time ftime("setup time"); |
| 46 |
ftime.start(); |
| 47 |
#endif |
| 48 |
int prefixSum; |
| 49 |
G->Comm().ScanSum(&localElems, &prefixSum, 1); |
| 50 |
//std::cout << " prefixSum" << prefixSum << std::endl; |
| 51 |
// Start the index in prefixSum-localElems |
| 52 |
int *mySGID = new int[totalElems]; // vector of size Schur complement ! |
| 53 |
int *allSGID = new int[totalElems]; // vector of size Schur complement ! |
| 54 |
int i, j; |
| 55 |
for (i = 0, j = 0; i < totalElems ; i++) |
| 56 |
{ |
| 57 |
if (i >= prefixSum - localElems && i < prefixSum) |
| 58 |
{ |
| 59 |
mySGID[i] = rows[j]; |
| 60 |
j++; |
| 61 |
} |
| 62 |
else |
| 63 |
{ |
| 64 |
mySGID[i] = 0; |
| 65 |
} |
| 66 |
allSGID[i] = 0; |
| 67 |
} |
| 68 |
|
| 69 |
C->Comm().SumAll(mySGID, allSGID, totalElems); |
| 70 |
|
| 71 |
#ifdef TIMING_OUTPUT |
| 72 |
ftime.stop(); |
| 73 |
std::cout << "Time to Compute RowIDS" << ftime.totalElapsedTime() << std::endl; |
| 74 |
ftime.reset(); |
| 75 |
#endif |
| 76 |
// Now everyone knows the GIDs in the Schur complement |
| 77 |
|
| 78 |
//std::cout << rMap << std::endl; |
| 79 |
j = 0; |
| 80 |
Teuchos::RCP<Epetra_CrsMatrix> Sbar = Teuchos::rcp(new Epetra_CrsMatrix( |
| 81 |
Copy, rMap, localElems)); |
| 82 |
int nentries; |
| 83 |
double *values = new double[localElems]; // Need to adjust this for more |
| 84 |
int *indices = new int[localElems]; // than one vector |
| 85 |
double *vecvalues; |
| 86 |
int dropped = 0; |
| 87 |
double *maxvalue = new double[nvectors]; |
| 88 |
#ifdef TIMING_OUTPUT |
| 89 |
ftime.start(); |
| 90 |
#endif |
| 91 |
#ifdef TIMING_OUTPUT |
| 92 |
Teuchos::Time app_time("Apply time"); |
| 93 |
#endif |
| 94 |
int findex = totalElems / nvectors ; |
| 95 |
for (i = 0 ; i < findex*nvectors ; i+=nvectors) |
| 96 |
{ |
| 97 |
Epetra_MultiVector probevec(rMap, nvectors); |
| 98 |
Epetra_MultiVector Scol(rMap, nvectors); |
| 99 |
|
| 100 |
probevec.PutScalar(0.0); |
| 101 |
int cindex; |
| 102 |
for (int k = 0; k < nvectors; k++) |
| 103 |
{ |
| 104 |
cindex = k+i; |
| 105 |
if (cindex >= prefixSum - localElems && cindex < prefixSum) |
| 106 |
{ |
| 107 |
probevec.ReplaceGlobalValue(allSGID[cindex], k, 1.0); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
#ifdef TIMING_OUTPUT |
| 112 |
app_time.start(); |
| 113 |
#endif |
| 114 |
probeop.Apply(probevec, Scol); |
| 115 |
#ifdef TIMING_OUTPUT |
| 116 |
app_time.stop(); |
| 117 |
#endif |
| 118 |
Scol.MaxValue(maxvalue); |
| 119 |
for (int k = 0; k < nvectors; k++) //TODO:Need to switch these loops |
| 120 |
{ |
| 121 |
cindex = k+i; |
| 122 |
vecvalues = Scol[k]; |
| 123 |
//std::cout << "MAX" << maxvalue << std::endl; |
| 124 |
for (j = 0 ; j < localElems ; j++) |
| 125 |
{ |
| 126 |
nentries = 0; // inserting one entry in each row for now |
| 127 |
if (allSGID[cindex] == rows[j]) // diagonal entry |
| 128 |
{ |
| 129 |
values[nentries] = vecvalues[j]; |
| 130 |
indices[nentries] = allSGID[cindex]; |
| 131 |
nentries++; |
| 132 |
Sbar->InsertGlobalValues(rows[j], nentries, values, indices); |
| 133 |
} |
| 134 |
else if (abs(vecvalues[j]/maxvalue[k]) > relative_thres) |
| 135 |
{ |
| 136 |
values[nentries] = vecvalues[j]; |
| 137 |
indices[nentries] = allSGID[cindex]; |
| 138 |
nentries++; |
| 139 |
Sbar->InsertGlobalValues(rows[j], nentries, values, indices); |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
if (vecvalues[j] != 0.0) dropped++; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
probeop.ResetTempVectors(1); |
| 150 |
|
| 151 |
for ( ; i < totalElems ; i++) |
| 152 |
{ |
| 153 |
Epetra_MultiVector probevec(rMap, 1); // TODO: Try doing more than one |
| 154 |
Epetra_MultiVector Scol(rMap, 1); // vector at a time |
| 155 |
|
| 156 |
probevec.PutScalar(0.0); |
| 157 |
if (i >= prefixSum - localElems && i < prefixSum) |
| 158 |
{ |
| 159 |
probevec.ReplaceGlobalValue(allSGID[i], 0, 1.0); |
| 160 |
} |
| 161 |
|
| 162 |
#ifdef TIMING_OUTPUT |
| 163 |
app_time.start(); |
| 164 |
#endif |
| 165 |
probeop.Apply(probevec, Scol); |
| 166 |
#ifdef TIMING_OUTPUT |
| 167 |
app_time.stop(); |
| 168 |
#endif |
| 169 |
vecvalues = Scol[0]; |
| 170 |
Scol.MaxValue(maxvalue); |
| 171 |
//std::cout << "MAX" << maxvalue << std::endl; |
| 172 |
for (j = 0 ; j < localElems ; j++) |
| 173 |
{ |
| 174 |
nentries = 0; // inserting one entry in each row for now |
| 175 |
if (allSGID[i] == rows[j]) // diagonal entry |
| 176 |
{ |
| 177 |
values[nentries] = vecvalues[j]; |
| 178 |
indices[nentries] = allSGID[i]; |
| 179 |
nentries++; |
| 180 |
Sbar->InsertGlobalValues(rows[j], nentries, values, indices); |
| 181 |
} |
| 182 |
else if (abs(vecvalues[j]/maxvalue[0]) > relative_thres) |
| 183 |
{ |
| 184 |
values[nentries] = vecvalues[j]; |
| 185 |
indices[nentries] = allSGID[i]; |
| 186 |
nentries++; |
| 187 |
Sbar->InsertGlobalValues(rows[j], nentries, values, indices); |
| 188 |
} |
| 189 |
else |
| 190 |
{ |
| 191 |
if (vecvalues[j] != 0.0) dropped++; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
#ifdef TIMING_OUTPUT |
| 196 |
ftime.stop(); |
| 197 |
std::cout << "Time in finding and dropping entries" << ftime.totalElapsedTime() << std::endl; |
| 198 |
ftime.reset(); |
| 199 |
#endif |
| 200 |
#ifdef TIMING_OUTPUT |
| 201 |
std::cout << "Time in Apply of probing" << app_time.totalElapsedTime() << std::endl; |
| 202 |
#endif |
| 203 |
Sbar->FillComplete(); |
| 204 |
std::cout << "#dropped entries" << dropped << std::endl; |
| 205 |
delete[] allSGID; |
| 206 |
delete[] mySGID; |
| 207 |
delete[] values; |
| 208 |
delete[] indices; |
| 209 |
delete[] maxvalue; |
| 210 |
|
| 211 |
return Sbar; |
| 212 |
} |
| 213 |
|
| 214 |
/* Computes the approximate Schur complement for the wide separator */ |
| 215 |
Teuchos::RCP<Epetra_CrsMatrix> computeApproxWideSchur(shylu_config *config, |
| 216 |
shylu_symbolic *ssym, // symbolic structure |
| 217 |
Epetra_CrsMatrix *G, Epetra_CrsMatrix *R, |
| 218 |
Epetra_LinearProblem *LP, Amesos_BaseSolver *solver, |
| 219 |
Ifpack_Preconditioner *ifSolver, Epetra_CrsMatrix *C, |
| 220 |
Epetra_Map *localDRowMap) |
| 221 |
{ |
| 222 |
int i; |
| 223 |
double relative_thres = config->relative_threshold; |
| 224 |
|
| 225 |
// Need to create local G (block diagonal portion) , R, C |
| 226 |
|
| 227 |
// Get row map of G |
| 228 |
//Epetra_Map CrMap = C->RowMap(); |
| 229 |
//int *c_rows = CrMap.MyGlobalElements(); |
| 230 |
//int *c_cols = (C->ColMap()).MyGlobalElements(); |
| 231 |
//int c_totalElems = CrMap.NumGlobalElements(); |
| 232 |
//int c_localElems = CrMap.NumMyElements(); |
| 233 |
//int c_localcolElems = (C->ColMap()).NumMyElements(); |
| 234 |
|
| 235 |
Epetra_Map GrMap = G->RowMap(); |
| 236 |
int *g_rows = GrMap.MyGlobalElements(); |
| 237 |
//int g_totalElems = GrMap.NumGlobalElements(); |
| 238 |
int g_localElems = GrMap.NumMyElements(); |
| 239 |
|
| 240 |
//Epetra_Map RrMap = R->RowMap(); |
| 241 |
//int *r_rows = RrMap.MyGlobalElements(); |
| 242 |
//int *r_cols = (R->ColMap()).MyGlobalElements(); |
| 243 |
//int r_totalElems = RrMap.NumGlobalElements(); |
| 244 |
//int r_localElems = RrMap.NumMyElements(); |
| 245 |
//int r_localcolElems = (R->ColMap()).NumMyElements(); |
| 246 |
|
| 247 |
Epetra_SerialComm LComm; |
| 248 |
Epetra_Map G_localRMap (-1, g_localElems, g_rows, 0, LComm); |
| 249 |
|
| 250 |
int nentries1, gid; |
| 251 |
// maxentries is the maximum of all three possible matrices as the arrays |
| 252 |
// are reused between the three |
| 253 |
int maxentries = std::max(C->MaxNumEntries(), R->MaxNumEntries()); |
| 254 |
maxentries = std::max(maxentries, G->MaxNumEntries()); |
| 255 |
|
| 256 |
double *values1 = new double[maxentries]; |
| 257 |
double *values2 = new double[maxentries]; |
| 258 |
double *values3 = new double[maxentries]; |
| 259 |
int *indices1 = new int[maxentries]; |
| 260 |
int *indices2 = new int[maxentries]; |
| 261 |
int *indices3 = new int[maxentries]; |
| 262 |
|
| 263 |
// Sbar - Approximate Schur complement |
| 264 |
Teuchos::RCP<Epetra_CrsMatrix> Sbar = Teuchos::rcp(new Epetra_CrsMatrix( |
| 265 |
Copy, GrMap, g_localElems)); |
| 266 |
|
| 267 |
// Include only the block diagonal elements of G in localG |
| 268 |
Epetra_CrsMatrix localG(Copy, G_localRMap, G->MaxNumEntries(), false); |
| 269 |
int cnt, scnt; |
| 270 |
for (i = 0; i < g_localElems ; i++) |
| 271 |
{ |
| 272 |
gid = g_rows[i]; |
| 273 |
G->ExtractGlobalRowCopy(gid, maxentries, nentries1, values1, indices1); |
| 274 |
|
| 275 |
cnt = 0; |
| 276 |
scnt = 0; |
| 277 |
for (int j = 0 ; j < nentries1 ; j++) |
| 278 |
{ |
| 279 |
if (G->LRID(indices1[j]) != -1) |
| 280 |
{ |
| 281 |
values2[cnt] = values1[j]; |
| 282 |
indices2[cnt++] = indices1[j]; |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
// Add it to Sbar immediately |
| 287 |
values3[scnt] = values1[j]; |
| 288 |
indices3[scnt++] = indices1[j]; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
localG.InsertGlobalValues(gid, cnt, values2, indices2); |
| 293 |
Sbar->InsertGlobalValues(gid, scnt, values3, indices3); |
| 294 |
} |
| 295 |
localG.FillComplete(); |
| 296 |
//std::cout << "Created local G matrix" << std::endl; |
| 297 |
|
| 298 |
int nvectors = 16; |
| 299 |
/*ShyLU_Probing_Operator probeop(&localG, &localR, LP, solver, &localC, |
| 300 |
localDRowMap, nvectors);*/ |
| 301 |
ShyLU_Local_Schur_Operator probeop(config, ssym, &localG, R, LP, solver, |
| 302 |
ifSolver, C, localDRowMap, nvectors); |
| 303 |
|
| 304 |
#ifdef DUMP_MATRICES |
| 305 |
//ostringstream fnamestr; |
| 306 |
//fnamestr << "localC" << C->Comm().MyPID() << ".mat"; |
| 307 |
//string Cfname = fnamestr.str(); |
| 308 |
//EpetraExt::RowMatrixToMatlabFile(Cfname.c_str(), localC); |
| 309 |
|
| 310 |
//Epetra_Map defMapg(-1, g_localElems, 0, localG.Comm()); |
| 311 |
//EpetraExt::ViewTransform<Epetra_CrsMatrix> * ReIdx_MatTransg = |
| 312 |
//new EpetraExt::CrsMatrix_Reindex( defMapg ); |
| 313 |
//Epetra_CrsMatrix t2G = (*ReIdx_MatTransg)( localG ); |
| 314 |
//ReIdx_MatTransg->fwd(); |
| 315 |
//EpetraExt::RowMatrixToMatlabFile("localG.mat", t2G); |
| 316 |
#endif |
| 317 |
|
| 318 |
//std::cout << " totalElems in Schur Complement" << totalElems << std::endl; |
| 319 |
//std::cout << myPID << " localElems" << localElems << std::endl; |
| 320 |
|
| 321 |
// **************** Two collectives here ********************* |
| 322 |
#ifdef TIMING_OUTPUT |
| 323 |
Teuchos::Time ftime("setup time"); |
| 324 |
ftime.start(); |
| 325 |
#endif |
| 326 |
#ifdef TIMING_OUTPUT |
| 327 |
Teuchos::Time app_time("setup time"); |
| 328 |
#endif |
| 329 |
|
| 330 |
int nentries; |
| 331 |
// size > maxentries as there could be fill |
| 332 |
// TODO: Currently the size of the two arrays can be one, Even if we switch |
| 333 |
// the loop below the size of the array required is nvectors. Fix it |
| 334 |
double *values = new double[nvectors]; |
| 335 |
int *indices = new int[nvectors]; |
| 336 |
double *vecvalues; |
| 337 |
#ifdef SHYLU_DEBUG |
| 338 |
// mfh 25 May 2015: Don't declare this variable if it's not used. |
| 339 |
// It's only used if SHYLU_DEBUG is defined. |
| 340 |
int dropped = 0; |
| 341 |
#endif // SHYLU_DEBUG |
| 342 |
double *maxvalue = new double[nvectors]; |
| 343 |
#ifdef TIMING_OUTPUT |
| 344 |
ftime.start(); |
| 345 |
#endif |
| 346 |
int findex = g_localElems / nvectors ; |
| 347 |
|
| 348 |
int cindex; |
| 349 |
// int mypid = C->Comm().MyPID(); // unused |
| 350 |
Epetra_MultiVector probevec (G_localRMap, nvectors); |
| 351 |
Epetra_MultiVector Scol (G_localRMap, nvectors); |
| 352 |
probevec.PutScalar(0.0); |
| 353 |
for (i = 0 ; i < findex*nvectors ; i+=nvectors) |
| 354 |
{ |
| 355 |
// Set the probevec to find block columns of S. |
| 356 |
for (int k = 0; k < nvectors; k++) |
| 357 |
{ |
| 358 |
cindex = k+i; |
| 359 |
// TODO: Can do better than this, just need to go to the column map |
| 360 |
// of C, there might be null columns in C |
| 361 |
probevec.ReplaceGlobalValue(g_rows[cindex], k, 1.0); |
| 362 |
//if (mypid == 0) |
| 363 |
//std::cout << "Changing row to 1.0 " << g_rows[cindex] << std::endl; |
| 364 |
} |
| 365 |
|
| 366 |
#ifdef TIMING_OUTPUT |
| 367 |
app_time.start(); |
| 368 |
#endif |
| 369 |
probeop.Apply(probevec, Scol); |
| 370 |
#ifdef TIMING_OUTPUT |
| 371 |
app_time.stop(); |
| 372 |
#endif |
| 373 |
|
| 374 |
// Reset the probevec to all zeros. |
| 375 |
for (int k = 0; k < nvectors; k++) |
| 376 |
{ |
| 377 |
cindex = k+i; |
| 378 |
probevec.ReplaceGlobalValue(g_rows[cindex], k, 0.0); |
| 379 |
} |
| 380 |
|
| 381 |
Scol.MaxValue(maxvalue); |
| 382 |
nentries = 0; |
| 383 |
for (int j = 0 ; j < g_localElems ; j++) |
| 384 |
{ |
| 385 |
for (int k = 0; k < nvectors; k++) |
| 386 |
{ |
| 387 |
cindex = k+i; |
| 388 |
vecvalues = Scol[k]; |
| 389 |
if ((g_rows[cindex] == g_rows[j]) || |
| 390 |
(abs(vecvalues[j]/maxvalue[k]) > relative_thres)) |
| 391 |
// diagonal entry or large entry. |
| 392 |
{ |
| 393 |
values[nentries] = vecvalues[j]; |
| 394 |
indices[nentries++] = g_rows[cindex]; |
| 395 |
} |
| 396 |
#ifdef SHYLU_DEBUG |
| 397 |
else if (vecvalues[j] != 0.0) |
| 398 |
{ |
| 399 |
dropped++; |
| 400 |
} |
| 401 |
#endif // SHYLU_DEBUG |
| 402 |
} |
| 403 |
Sbar->InsertGlobalValues(g_rows[j], nentries, values, |
| 404 |
indices); |
| 405 |
nentries = 0; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
if (i < g_localElems) |
| 410 |
{ |
| 411 |
nvectors = g_localElems - i; |
| 412 |
probeop.ResetTempVectors(nvectors); |
| 413 |
Epetra_MultiVector probevec1 (G_localRMap, nvectors); |
| 414 |
Epetra_MultiVector Scol1 (G_localRMap, nvectors); |
| 415 |
|
| 416 |
probevec1.PutScalar(0.0); |
| 417 |
for (int k = 0; k < nvectors; k++) |
| 418 |
{ |
| 419 |
cindex = k+i; |
| 420 |
// TODO: Can do better than this, just need to go to the column map |
| 421 |
// of C, there might be null columns in C |
| 422 |
probevec1.ReplaceGlobalValue(g_rows[cindex], k, 1.0); |
| 423 |
} |
| 424 |
|
| 425 |
#ifdef TIMING_OUTPUT |
| 426 |
app_time.start(); |
| 427 |
#endif |
| 428 |
probeop.Apply(probevec1, Scol1); |
| 429 |
#ifdef TIMING_OUTPUT |
| 430 |
app_time.stop(); |
| 431 |
#endif |
| 432 |
Scol1.MaxValue(maxvalue); |
| 433 |
nentries = 0; |
| 434 |
for (int j = 0 ; j < g_localElems ; j++) |
| 435 |
{ |
| 436 |
//std::cout << "MAX" << maxvalue << std::endl; |
| 437 |
for (int k = 0; k < nvectors; k++) |
| 438 |
{ |
| 439 |
cindex = k+i; |
| 440 |
vecvalues = Scol1[k]; |
| 441 |
//nentries = 0; // inserting one entry in each row for now |
| 442 |
if ((g_rows[cindex] == g_rows[j]) || |
| 443 |
(abs(vecvalues[j]/maxvalue[k]) > relative_thres)) |
| 444 |
// diagonal entry or large entry. |
| 445 |
{ |
| 446 |
values[nentries] = vecvalues[j]; |
| 447 |
indices[nentries++] = g_rows[cindex]; |
| 448 |
} |
| 449 |
#ifdef SHYLU_DEBUG |
| 450 |
else if (vecvalues[j] != 0.0) |
| 451 |
{ |
| 452 |
dropped++; |
| 453 |
} |
| 454 |
#endif // SHYLU_DEBUG |
| 455 |
} |
| 456 |
Sbar->InsertGlobalValues(g_rows[j], nentries, values, |
| 457 |
indices); |
| 458 |
nentries = 0; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
#ifdef TIMING_OUTPUT |
| 463 |
ftime.stop(); |
| 464 |
std::cout << "Time in finding and dropping entries" << ftime.totalElapsedTime() |
| 465 |
<< std::endl; |
| 466 |
ftime.reset(); |
| 467 |
std::cout << "Time in Apply of probing" << app_time.totalElapsedTime() << std::endl; |
| 468 |
probeop.PrintTimingInfo(); |
| 469 |
#endif |
| 470 |
Sbar->FillComplete(); |
| 471 |
|
| 472 |
#ifdef DUMP_MATRICES |
| 473 |
Epetra_Map defMap2(-1, g_localElems, 0, C->Comm()); |
| 474 |
EpetraExt::ViewTransform<Epetra_CrsMatrix> * ReIdx_MatTrans2 = |
| 475 |
new EpetraExt::CrsMatrix_Reindex( defMap2 ); |
| 476 |
Epetra_CrsMatrix t2S = (*ReIdx_MatTrans2)( *Sbar ); |
| 477 |
ReIdx_MatTrans2->fwd(); |
| 478 |
EpetraExt::RowMatrixToMatlabFile("Schur.mat", t2S); |
| 479 |
#endif |
| 480 |
|
| 481 |
#ifdef SHYLU_DEBUG |
| 482 |
std::cout << "#dropped entries" << dropped << std::endl; |
| 483 |
#endif |
| 484 |
delete[] values; |
| 485 |
delete[] indices; |
| 486 |
delete[] values1; |
| 487 |
delete[] indices1; |
| 488 |
delete[] values2; |
| 489 |
delete[] indices2; |
| 490 |
delete[] values3; |
| 491 |
delete[] indices3; |
| 492 |
delete[] maxvalue; |
| 493 |
|
| 494 |
return Sbar; |
| 495 |
} |
| 496 |
|
| 497 |
/* Computes the approximate Schur complement for the wide separator |
| 498 |
using guided probing*/ |
| 499 |
Teuchos::RCP<Epetra_CrsMatrix> computeSchur_GuidedProbing |
| 500 |
( |
| 501 |
shylu_config *config, |
| 502 |
shylu_symbolic *ssym, // symbolic structure |
| 503 |
shylu_data *data, // numeric structure |
| 504 |
Epetra_Map *localDRowMap |
| 505 |
) |
| 506 |
{ |
| 507 |
int i; |
| 508 |
double relative_thres = config->relative_threshold; |
| 509 |
|
| 510 |
Epetra_CrsMatrix *G = ssym->G.getRawPtr(); |
| 511 |
Epetra_CrsMatrix *R = ssym->R.getRawPtr(); |
| 512 |
Epetra_LinearProblem *LP = ssym->LP.getRawPtr(); |
| 513 |
Amesos_BaseSolver *solver = ssym->Solver.getRawPtr(); |
| 514 |
Ifpack_Preconditioner *ifSolver = ssym->ifSolver.getRawPtr(); |
| 515 |
Epetra_CrsMatrix *C = ssym->C.getRawPtr(); |
| 516 |
|
| 517 |
// Need to create local G (block diagonal portion) , R, C |
| 518 |
|
| 519 |
// Get row map of G |
| 520 |
Epetra_Map CrMap = C->RowMap(); |
| 521 |
int *c_rows = CrMap.MyGlobalElements(); |
| 522 |
int *c_cols = (C->ColMap()).MyGlobalElements(); |
| 523 |
//int c_totalElems = CrMap.NumGlobalElements(); |
| 524 |
int c_localElems = CrMap.NumMyElements(); |
| 525 |
int c_localcolElems = (C->ColMap()).NumMyElements(); |
| 526 |
|
| 527 |
Epetra_Map GrMap = G->RowMap(); |
| 528 |
int *g_rows = GrMap.MyGlobalElements(); |
| 529 |
//int g_totalElems = GrMap.NumGlobalElements(); |
| 530 |
int g_localElems = GrMap.NumMyElements(); |
| 531 |
|
| 532 |
Epetra_Map RrMap = R->RowMap(); |
| 533 |
int *r_rows = RrMap.MyGlobalElements(); |
| 534 |
int *r_cols = (R->ColMap()).MyGlobalElements(); |
| 535 |
//int r_totalElems = RrMap.NumGlobalElements(); |
| 536 |
int r_localElems = RrMap.NumMyElements(); |
| 537 |
int r_localcolElems = (R->ColMap()).NumMyElements(); |
| 538 |
|
| 539 |
Epetra_SerialComm LComm; |
| 540 |
Epetra_Map C_localRMap (-1, c_localElems, c_rows, 0, LComm); |
| 541 |
Epetra_Map C_localCMap (-1, c_localcolElems, c_cols, 0, LComm); |
| 542 |
Epetra_Map G_localRMap (-1, g_localElems, g_rows, 0, LComm); |
| 543 |
Epetra_Map R_localRMap (-1, r_localElems, r_rows, 0, LComm); |
| 544 |
Epetra_Map R_localCMap (-1, r_localcolElems, r_cols, 0, LComm); |
| 545 |
|
| 546 |
//std::cout << "#local rows" << g_localElems << "#non zero local cols" << c_localcolElems << std::endl; |
| 547 |
|
| 548 |
#ifdef DEBUG |
| 549 |
std::cout << "DEBUG MODE" << std::endl; |
| 550 |
int nrows = C->RowMap().NumMyElements(); |
| 551 |
assert(nrows == localDRowMap->NumGlobalElements()); |
| 552 |
|
| 553 |
int gids[nrows], gids1[nrows]; |
| 554 |
C_localRMap.MyGlobalElements(gids); |
| 555 |
localDRowMap->MyGlobalElements(gids1); |
| 556 |
std::cout << "Comparing R's domain map with D's row map" << std::endl; |
| 557 |
|
| 558 |
for (int i = 0; i < nrows; i++) |
| 559 |
{ |
| 560 |
assert(gids[i] == gids1[i]); |
| 561 |
} |
| 562 |
#endif |
| 563 |
|
| 564 |
int nentries1, gid; |
| 565 |
// maxentries is the maximum of all three possible matrices as the arrays |
| 566 |
// are reused between the three |
| 567 |
int maxentries = std::max(C->MaxNumEntries(), R->MaxNumEntries()); |
| 568 |
maxentries = std::max(maxentries, G->MaxNumEntries()); |
| 569 |
|
| 570 |
double *values1 = new double[maxentries]; |
| 571 |
double *values2 = new double[maxentries]; |
| 572 |
double *values3 = new double[maxentries]; |
| 573 |
int *indices1 = new int[maxentries]; |
| 574 |
int *indices2 = new int[maxentries]; |
| 575 |
int *indices3 = new int[maxentries]; |
| 576 |
|
| 577 |
//std::cout << "Creating local matrices" << std::endl; |
| 578 |
int err = 0; |
| 579 |
Epetra_CrsMatrix localC(Copy, C_localRMap, C->MaxNumEntries(), false); |
| 580 |
for (i = 0; i < c_localElems ; i++) |
| 581 |
{ |
| 582 |
gid = c_rows[i]; |
| 583 |
err = C->ExtractGlobalRowCopy(gid, maxentries, nentries1, values1, |
| 584 |
indices1); |
| 585 |
assert (err == 0); |
| 586 |
//if (nentries1 > 0) // TODO: Later |
| 587 |
//{ |
| 588 |
err = localC.InsertGlobalValues(gid, nentries1, values1, indices1); |
| 589 |
assert (err == 0); |
| 590 |
//} |
| 591 |
} |
| 592 |
localC.FillComplete(G_localRMap, C_localRMap); |
| 593 |
|
| 594 |
//std::cout << "Created local C matrix" << std::endl; |
| 595 |
|
| 596 |
Epetra_CrsMatrix localR(Copy, R_localRMap, R->MaxNumEntries(), false); |
| 597 |
for (i = 0; i < r_localElems ; i++) |
| 598 |
{ |
| 599 |
gid = r_rows[i]; |
| 600 |
R->ExtractGlobalRowCopy(gid, maxentries, nentries1, values1, indices1); |
| 601 |
localR.InsertGlobalValues(gid, nentries1, values1, indices1); |
| 602 |
} |
| 603 |
localR.FillComplete(*localDRowMap, R_localRMap); |
| 604 |
//std::cout << "Created local R matrix" << std::endl; |
| 605 |
|
| 606 |
// Sbar - Approximate Schur complement |
| 607 |
Teuchos::RCP<Epetra_CrsMatrix> Sbar = Teuchos::rcp(new Epetra_CrsMatrix( |
| 608 |
Copy, GrMap, g_localElems)); |
| 609 |
|
| 610 |
// Include only the block diagonal elements of G in localG |
| 611 |
Epetra_CrsMatrix localG(Copy, G_localRMap, G->MaxNumEntries(), false); |
| 612 |
int cnt, scnt; |
| 613 |
for (i = 0; i < g_localElems ; i++) |
| 614 |
{ |
| 615 |
gid = g_rows[i]; |
| 616 |
G->ExtractGlobalRowCopy(gid, maxentries, nentries1, values1, indices1); |
| 617 |
|
| 618 |
cnt = 0; |
| 619 |
scnt = 0; |
| 620 |
for (int j = 0 ; j < nentries1 ; j++) |
| 621 |
{ |
| 622 |
if (G->LRID(indices1[j]) != -1) |
| 623 |
{ |
| 624 |
values2[cnt] = values1[j]; |
| 625 |
indices2[cnt++] = indices1[j]; |
| 626 |
} |
| 627 |
else |
| 628 |
{ |
| 629 |
// Add it to Sbar immediately |
| 630 |
values3[scnt] = values1[j]; |
| 631 |
indices3[scnt++] = indices1[j]; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
localG.InsertGlobalValues(gid, cnt, values2, indices2); |
| 636 |
Sbar->InsertGlobalValues(gid, scnt, values3, indices3); |
| 637 |
} |
| 638 |
localG.FillComplete(); |
| 639 |
std::cout << "Created local G matrix" << std::endl; |
| 640 |
|
| 641 |
int nvectors = 16; |
| 642 |
ShyLU_Probing_Operator probeop(config, ssym, &localG, &localR, LP, solver, |
| 643 |
ifSolver, &localC, localDRowMap, nvectors); |
| 644 |
|
| 645 |
#ifdef DUMP_MATRICES |
| 646 |
//ostringstream fnamestr; |
| 647 |
//fnamestr << "localC" << C->Comm().MyPID() << ".mat"; |
| 648 |
//string Cfname = fnamestr.str(); |
| 649 |
//EpetraExt::RowMatrixToMatlabFile(Cfname.c_str(), localC); |
| 650 |
|
| 651 |
//Epetra_Map defMapg(-1, g_localElems, 0, localG.Comm()); |
| 652 |
//EpetraExt::ViewTransform<Epetra_CrsMatrix> * ReIdx_MatTransg = |
| 653 |
//new EpetraExt::CrsMatrix_Reindex( defMapg ); |
| 654 |
//Epetra_CrsMatrix t2G = (*ReIdx_MatTransg)( localG ); |
| 655 |
//ReIdx_MatTransg->fwd(); |
| 656 |
//EpetraExt::RowMatrixToMatlabFile("localG.mat", t2G); |
| 657 |
#endif |
| 658 |
|
| 659 |
//std::cout << " totalElems in Schur Complement" << totalElems << std::endl; |
| 660 |
//std::cout << myPID << " localElems" << localElems << std::endl; |
| 661 |
|
| 662 |
// **************** Two collectives here ********************* |
| 663 |
#ifdef TIMING_OUTPUT |
| 664 |
Teuchos::Time ftime("setup time"); |
| 665 |
#endif |
| 666 |
#ifdef TIMING_OUTPUT |
| 667 |
Teuchos::Time app_time("setup time"); |
| 668 |
#endif |
| 669 |
|
| 670 |
Teuchos::RCP<Epetra_CrsGraph> lSGraph = Teuchos::RCP<Epetra_CrsGraph> ( |
| 671 |
new Epetra_CrsGraph(Copy, G_localRMap, maxentries)); |
| 672 |
|
| 673 |
if (data->num_compute % config->reset_iter == 0) |
| 674 |
{ |
| 675 |
int nentries; |
| 676 |
// size > maxentries as there could be fill |
| 677 |
// TODO: Currently the size of the two arrays can be one, Even if we switch |
| 678 |
// the loop below the size of the array required is nvectors. Fix it |
| 679 |
double *values = new double[g_localElems]; |
| 680 |
int *indices = new int[g_localElems]; |
| 681 |
double *vecvalues; |
| 682 |
int dropped = 0; |
| 683 |
double *maxvalue = new double[nvectors]; |
| 684 |
#ifdef TIMING_OUTPUT |
| 685 |
ftime.start(); |
| 686 |
#endif |
| 687 |
int findex = g_localElems / nvectors ; |
| 688 |
|
| 689 |
int cindex; |
| 690 |
// int mypid = C->Comm().MyPID(); // unused |
| 691 |
Epetra_MultiVector ProbeVec(G_localRMap, nvectors); |
| 692 |
Epetra_MultiVector Scol(G_localRMap, nvectors); |
| 693 |
for (i = 0 ; i < findex*nvectors ; i+=nvectors) |
| 694 |
{ |
| 695 |
ProbeVec.PutScalar(0.0); // TODO: Move it out |
| 696 |
for (int k = 0; k < nvectors; k++) |
| 697 |
{ |
| 698 |
cindex = k+i; |
| 699 |
// TODO: Can do better than this, just need to go to the column map |
| 700 |
// of C, there might be null columns in C |
| 701 |
// Not much of use for Shasta 2x2 .. Later. |
| 702 |
ProbeVec.ReplaceGlobalValue(g_rows[cindex], k, 1.0); |
| 703 |
//if (mypid == 0) |
| 704 |
//std::cout << "Changing row to 1.0 " << g_rows[cindex] << std::endl; |
| 705 |
} |
| 706 |
|
| 707 |
#ifdef TIMING_OUTPUT |
| 708 |
app_time.start(); |
| 709 |
#endif |
| 710 |
probeop.Apply(ProbeVec, Scol); |
| 711 |
#ifdef TIMING_OUTPUT |
| 712 |
app_time.stop(); |
| 713 |
#endif |
| 714 |
|
| 715 |
Scol.MaxValue(maxvalue); |
| 716 |
for (int k = 0; k < nvectors; k++) //TODO:Need to switch these loops |
| 717 |
{ |
| 718 |
cindex = k+i; |
| 719 |
vecvalues = Scol[k]; |
| 720 |
//std::cout << "MAX" << maxvalue << std::endl; |
| 721 |
for (int j = 0 ; j < g_localElems ; j++) |
| 722 |
{ |
| 723 |
nentries = 0; // inserting one entry in each row for now |
| 724 |
if (g_rows[cindex] == g_rows[j]) // diagonal entry |
| 725 |
{ |
| 726 |
values[nentries] = vecvalues[j]; |
| 727 |
indices[nentries] = g_rows[cindex]; |
| 728 |
nentries++; |
| 729 |
err = Sbar->InsertGlobalValues(g_rows[j], nentries, values, |
| 730 |
indices); |
| 731 |
assert(err >= 0); |
| 732 |
err = lSGraph->InsertGlobalIndices(g_rows[j], nentries, |
| 733 |
indices); |
| 734 |
assert(err >= 0); |
| 735 |
} |
| 736 |
else if (abs(vecvalues[j]/maxvalue[k]) > relative_thres) |
| 737 |
{ |
| 738 |
values[nentries] = vecvalues[j]; |
| 739 |
indices[nentries] = g_rows[cindex]; |
| 740 |
nentries++; |
| 741 |
err = Sbar->InsertGlobalValues(g_rows[j], nentries, values, |
| 742 |
indices); |
| 743 |
assert(err >= 0); |
| 744 |
err = lSGraph->InsertGlobalIndices(g_rows[j], nentries, |
| 745 |
indices); |
| 746 |
assert(err >= 0); |
| 747 |
} |
| 748 |
else |
| 749 |
{ |
| 750 |
if (vecvalues[j] != 0.0) |
| 751 |
{ |
| 752 |
dropped++; |
| 753 |
//std::cout << "vecvalues[j]" << vecvalues[j] << |
| 754 |
// " max" << maxvalue[k] << std::endl; |
| 755 |
} |
| 756 |
} |
| 757 |
} |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
probeop.ResetTempVectors(1); |
| 762 |
|
| 763 |
Epetra_MultiVector probevec(G_localRMap, 1); |
| 764 |
Epetra_MultiVector scol(G_localRMap, 1); |
| 765 |
for ( ; i < g_localElems ; i++) |
| 766 |
{ |
| 767 |
|
| 768 |
probevec.PutScalar(0.0); |
| 769 |
// TODO: Can do better than this, just need to go to the column map |
| 770 |
// of C, there might be null columns in C |
| 771 |
probevec.ReplaceGlobalValue(g_rows[i], 0, 1.0); |
| 772 |
|
| 773 |
#ifdef TIMING_OUTPUT |
| 774 |
app_time.start(); |
| 775 |
#endif |
| 776 |
probeop.Apply(probevec, scol); |
| 777 |
#ifdef TIMING_OUTPUT |
| 778 |
app_time.stop(); |
| 779 |
#endif |
| 780 |
vecvalues = scol[0]; |
| 781 |
scol.MaxValue(maxvalue); |
| 782 |
//std::cout << "MAX" << maxvalue << std::endl; |
| 783 |
for (int j = 0 ; j < g_localElems ; j++) |
| 784 |
{ |
| 785 |
nentries = 0; // inserting one entry in each row for now |
| 786 |
if (g_rows[i] == g_rows[j]) // diagonal entry |
| 787 |
{ |
| 788 |
values[nentries] = vecvalues[j]; |
| 789 |
indices[nentries] = g_rows[i]; |
| 790 |
nentries++; |
| 791 |
err = Sbar->InsertGlobalValues(g_rows[j], nentries, values, indices); |
| 792 |
assert(err >= 0); |
| 793 |
err = lSGraph->InsertGlobalIndices(g_rows[j], nentries, indices); |
| 794 |
assert(err >= 0); |
| 795 |
} |
| 796 |
else if (abs(vecvalues[j]/maxvalue[0]) > relative_thres) |
| 797 |
{ |
| 798 |
values[nentries] = vecvalues[j]; |
| 799 |
indices[nentries] = g_rows[i]; |
| 800 |
nentries++; |
| 801 |
err = Sbar->InsertGlobalValues(g_rows[j], nentries, values, indices); |
| 802 |
assert(err >= 0); |
| 803 |
err = lSGraph->InsertGlobalIndices(g_rows[j], nentries, indices); |
| 804 |
assert(err >= 0); |
| 805 |
} |
| 806 |
else |
| 807 |
{ |
| 808 |
if (vecvalues[j] != 0.0) dropped++; |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
#ifdef TIMING_OUTPUT |
| 814 |
ftime.stop(); |
| 815 |
std::cout << "Time in finding and dropping entries" << ftime.totalElapsedTime() << std::endl; |
| 816 |
ftime.reset(); |
| 817 |
#endif |
| 818 |
#ifdef TIMING_OUTPUT |
| 819 |
std::cout << "Time in Apply of probing" << app_time.totalElapsedTime() << std::endl; |
| 820 |
#endif |
| 821 |
probeop.PrintTimingInfo(); |
| 822 |
Sbar->FillComplete(); |
| 823 |
lSGraph->FillComplete(); |
| 824 |
|
| 825 |
data->localSbargraph = lSGraph; |
| 826 |
|
| 827 |
#ifdef DUMP_MATRICES |
| 828 |
Epetra_Map defMap2(-1, g_localElems, 0, C->Comm()); |
| 829 |
EpetraExt::ViewTransform<Epetra_CrsMatrix> * ReIdx_MatTrans2 = |
| 830 |
new EpetraExt::CrsMatrix_Reindex( defMap2 ); |
| 831 |
Epetra_CrsMatrix t2S = (*ReIdx_MatTrans2)( *Sbar ); |
| 832 |
ReIdx_MatTrans2->fwd(); |
| 833 |
EpetraExt::RowMatrixToMatlabFile("Schur.mat", t2S); |
| 834 |
#endif |
| 835 |
|
| 836 |
std::cout << "#dropped entries" << dropped << std::endl; |
| 837 |
delete[] values; |
| 838 |
delete[] indices; |
| 839 |
delete[] maxvalue; |
| 840 |
} |
| 841 |
else |
| 842 |
{ |
| 843 |
if (((data->num_compute-1) % config->reset_iter) == 0) |
| 844 |
{ |
| 845 |
// We recomputed the Schur complement with dropping for the last |
| 846 |
// compute. Reset the prober with the new orthogonal vectors for |
| 847 |
// the Sbar from the previous iteration. |
| 848 |
Teuchos::ParameterList pList; |
| 849 |
Teuchos::RCP<Isorropia::Epetra::Prober> gprober = |
| 850 |
Teuchos::RCP<Isorropia::Epetra::Prober> (new |
| 851 |
Isorropia::Epetra::Prober( |
| 852 |
data->localSbargraph.getRawPtr(), pList, false)); |
| 853 |
gprober->color(); |
| 854 |
data->guided_prober = gprober; |
| 855 |
|
| 856 |
} |
| 857 |
// Use the prober to probe the probeop for the sparsity pattern |
| 858 |
// add that to Sbar and call Fill complete |
| 859 |
nvectors = data->guided_prober->getNumOrthogonalVectors(); |
| 860 |
std::cout << "Number of Orthogonal Vectors for guided probing" << nvectors |
| 861 |
<< std::endl; |
| 862 |
|
| 863 |
probeop.ResetTempVectors(nvectors); |
| 864 |
Teuchos::RCP<Epetra_CrsMatrix> blockdiag_Sbar = |
| 865 |
data->guided_prober->probe(probeop); |
| 866 |
maxentries = blockdiag_Sbar->GlobalMaxNumEntries(); |
| 867 |
int *indices = new int[maxentries]; |
| 868 |
double *values = new double[maxentries]; |
| 869 |
|
| 870 |
int numentries; |
| 871 |
for (i = 0; i < blockdiag_Sbar->NumGlobalRows() ; i++) |
| 872 |
{ |
| 873 |
gid = blockdiag_Sbar->GRID(i); |
| 874 |
blockdiag_Sbar->ExtractGlobalRowCopy(gid, maxentries, numentries, |
| 875 |
values, indices); |
| 876 |
Sbar->InsertGlobalValues(gid, numentries, values, indices); |
| 877 |
} |
| 878 |
|
| 879 |
Sbar->FillComplete(); |
| 880 |
delete[] indices; |
| 881 |
delete[] values; |
| 882 |
} |
| 883 |
|
| 884 |
(void)err; |
| 885 |
delete[] values1; |
| 886 |
delete[] indices1; |
| 887 |
delete[] values2; |
| 888 |
delete[] indices2; |
| 889 |
delete[] values3; |
| 890 |
delete[] indices3; |
| 891 |
return Sbar; |
| 892 |
} |