| 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 |
/* ******************************************************************** */ |
| 11 |
/* See the file COPYRIGHT for a complete copyright notice, contact */ |
| 12 |
/* person and disclaimer. */ |
| 13 |
/* ******************************************************************** */ |
| 14 |
/*! |
| 15 |
* \file ml_nox_preconditioner_utils.cpp |
| 16 |
* |
| 17 |
* \brief ML nonlinear preconditioner and solver |
| 18 |
* |
| 19 |
* \date Last update do Doxygen: 31-Mar-05 |
| 20 |
* |
| 21 |
*/ |
| 22 |
// ML-headers |
| 23 |
#include "ml_common.h" |
| 24 |
#include "TrilinosCouplings_config.h" |
| 25 |
|
| 26 |
#if defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA) && defined(HAVE_ML_AZTECOO) && defined(HAVE_ML_TEUCHOS) && defined(HAVE_ML_IFPACK) && defined(HAVE_ML_AMESOS) && defined(HAVE_ML_EPETRAEXT) |
| 27 |
|
| 28 |
#include <ctime> |
| 29 |
#include <cstdlib> |
| 30 |
#include <iostream> |
| 31 |
#include <string> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
#include "Epetra_Comm.h" |
| 35 |
|
| 36 |
#ifdef ML_MPI // FIXME: do we need this here? |
| 37 |
#include <mpi.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#include "ml_nox_preconditioner_utils.H" |
| 41 |
|
| 42 |
/*----------------------------------------------------------------------* |
| 43 |
| make a deep copy of a graph m.gee 01/05| |
| 44 |
| allocate the new graph | |
| 45 |
*----------------------------------------------------------------------*/ |
| 46 |
Epetra_CrsGraph* ML_NOX::deepcopy_graph(const Epetra_CrsGraph* oldgraph) |
| 47 |
{ |
| 48 |
int i,ierr; |
| 49 |
int nrows = oldgraph->NumMyRows(); |
| 50 |
int* nIndicesperRow = new int[nrows]; |
| 51 |
|
| 52 |
for (i=0; i<nrows; i++) |
| 53 |
nIndicesperRow[i] = oldgraph->NumMyIndices(i); |
| 54 |
Epetra_CrsGraph* graph = new Epetra_CrsGraph(Copy,oldgraph->RowMap(),oldgraph->ColMap(), |
| 55 |
&(nIndicesperRow[0])); |
| 56 |
delete [] nIndicesperRow; |
| 57 |
nIndicesperRow = 0; |
| 58 |
|
| 59 |
for (i=0; i<nrows; i++) |
| 60 |
{ |
| 61 |
int numIndices; |
| 62 |
int* Indices=0; |
| 63 |
ierr = oldgraph->ExtractMyRowView(i,numIndices,Indices); |
| 64 |
ierr = graph->InsertMyIndices(i,numIndices,Indices); |
| 65 |
} |
| 66 |
|
| 67 |
graph->FillComplete(); |
| 68 |
return graph; |
| 69 |
} |
| 70 |
|
| 71 |
/*----------------------------------------------------------------------* |
| 72 |
| color a graph in a collapsed way m.gee 05/05| |
| 73 |
| note: | |
| 74 |
| this routine makes 2 assumptions: | |
| 75 |
| - the blocksize is constant everywhere | |
| 76 |
| - degrees of freedom (rows) on a node(block) a contigous | |
| 77 |
*----------------------------------------------------------------------*/ |
| 78 |
Epetra_MapColoring* ML_NOX::ML_Nox_collapsedcoloring(Epetra_CrsGraph* cgraph, |
| 79 |
const int bsize, |
| 80 |
bool diagonalonly, |
| 81 |
int printlevel) |
| 82 |
{ |
| 83 |
// create a new rangemap for the amalgamated graph |
| 84 |
int new_nummyrows = cgraph->NumMyRows(); |
| 85 |
int new_numglobalrows = cgraph->NumGlobalRows(); |
| 86 |
int lok=1; |
| 87 |
int gok=1; |
| 88 |
if (new_nummyrows % bsize != 0 || new_numglobalrows % bsize != 0) |
| 89 |
{ |
| 90 |
lok = 0; |
| 91 |
if (printlevel>5) |
| 92 |
cout << "**WRN**: ML_NOX::ML_Nox_collapsedcoloring:\n" |
| 93 |
<< "**WRN**: cannot amalgamate cgraph\n" |
| 94 |
<< "**WRN**: numlocalrows%bsize= " << (new_nummyrows % bsize) << " numglobalrows%bsize= " << (new_numglobalrows % bsize) << endl; |
| 95 |
} |
| 96 |
cgraph->Comm().MinAll(&lok,&gok,1); |
| 97 |
if (!gok) |
| 98 |
return NULL; |
| 99 |
|
| 100 |
lok=1; |
| 101 |
gok=1; |
| 102 |
// number of local and global rows |
| 103 |
new_nummyrows /= bsize; |
| 104 |
new_numglobalrows /= bsize; |
| 105 |
int* myRows = new int [new_nummyrows]; |
| 106 |
int counter=0; |
| 107 |
// calculate which global rows are mine |
| 108 |
lok=1; |
| 109 |
gok=1; |
| 110 |
for (int i=0; i<cgraph->RowMap().NumMyElements(); ++i) |
| 111 |
{ |
| 112 |
int old_grow = cgraph->RowMap().GID(i); |
| 113 |
if (old_grow<0) |
| 114 |
{ |
| 115 |
if (printlevel>5) |
| 116 |
cout << "**WRN**: ML_NOX::ML_Nox_collapsedcoloring:\n" |
| 117 |
<< "**WRN**: cgraph->RowMap().GID() returned " << old_grow << endl |
| 118 |
<< "**WRN**: switching to none-collapsed coloring\n" |
| 119 |
<< "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; |
| 120 |
lok=0; |
| 121 |
} |
| 122 |
if (lok==0) |
| 123 |
break; |
| 124 |
if (old_grow % bsize != 0) |
| 125 |
continue; |
| 126 |
int new_grow = old_grow/bsize; |
| 127 |
myRows[counter] = new_grow; |
| 128 |
++counter; |
| 129 |
} |
| 130 |
cgraph->Comm().MinAll(&lok,&gok,1); |
| 131 |
if (!gok) |
| 132 |
{ |
| 133 |
if (myRows) delete[] myRows; |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
if (counter != new_nummyrows) |
| 138 |
{ |
| 139 |
if (printlevel>5) |
| 140 |
cout << "**WRN**: ML_NOX::ML_Nox_collapsedcoloring:\n" |
| 141 |
<< "**WRN**: collapsed coloring problem, switching to standard coloring\n" |
| 142 |
<< "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; |
| 143 |
lok=0; |
| 144 |
} |
| 145 |
cgraph->Comm().MinAll(&lok,&gok,1); |
| 146 |
if (!gok) |
| 147 |
{ |
| 148 |
if (myRows) delete [] myRows; |
| 149 |
return NULL; |
| 150 |
} |
| 151 |
|
| 152 |
// create the BlockMap |
| 153 |
Epetra_BlockMap newrowmap(new_numglobalrows,new_nummyrows,myRows,1,0,cgraph->Comm()); |
| 154 |
delete [] myRows; myRows = NULL; |
| 155 |
|
| 156 |
// create the nodal cgraph |
| 157 |
Epetra_CrsGraph nodegraph(Copy,newrowmap,27); |
| 158 |
|
| 159 |
// loop over NumMyElements in old graph and insert every bsize row in |
| 160 |
// new graph |
| 161 |
int new_length=200; |
| 162 |
int* new_gindices = new int[new_length]; |
| 163 |
|
| 164 |
lok=1; |
| 165 |
gok=1; |
| 166 |
for (int i=0; i<cgraph->RowMap().NumMyElements(); ++i) |
| 167 |
{ |
| 168 |
int old_grow = cgraph->RowMap().GID(i); |
| 169 |
if (old_grow<0) |
| 170 |
{ |
| 171 |
if (printlevel>5) |
| 172 |
cout << "**WRN**: ML_NOX::ML_Nox_collapsedcoloring:\n" |
| 173 |
<< "**WRN**: cgraph->RowMap().GID() returned " << old_grow << endl |
| 174 |
<< "**WRN**: switching to none-collapsed coloring\n" |
| 175 |
<< "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; |
| 176 |
lok=0; |
| 177 |
} |
| 178 |
if (old_grow % bsize != 0) |
| 179 |
continue; |
| 180 |
if (lok==0) |
| 181 |
break; |
| 182 |
int new_grow = old_grow/bsize; |
| 183 |
|
| 184 |
// extract the row from the old cgraph |
| 185 |
int old_numindices; |
| 186 |
int* old_colindices; |
| 187 |
int err = cgraph->ExtractMyRowView(i,old_numindices,old_colindices); |
| 188 |
if (err) |
| 189 |
{ |
| 190 |
if (printlevel>5) |
| 191 |
cout << "**WRN**: ML_NOX::ML_Nox_collapsedcoloring:\n" |
| 192 |
<< "**WRN**: cgraph->ExtractMyRowView returned " << err << endl |
| 193 |
<< "**WRN**: switching to none-collapsed coloring\n" |
| 194 |
<< "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; |
| 195 |
lok=0; |
| 196 |
} |
| 197 |
if (lok==0) |
| 198 |
break; |
| 199 |
|
| 200 |
// check whether the allocated row for the new cgraph is long enough |
| 201 |
if (old_numindices>new_length) |
| 202 |
{ |
| 203 |
new_length = old_numindices; |
| 204 |
delete [] new_gindices; |
| 205 |
new_gindices = new int[new_length]; |
| 206 |
} |
| 207 |
|
| 208 |
// calculate the row in global numbering for the new cgraph |
| 209 |
int new_numindices=0; |
| 210 |
for (int j=0; j<old_numindices; ++j) |
| 211 |
{ |
| 212 |
int old_gcol = cgraph->ColMap().GID(old_colindices[j]); |
| 213 |
if (old_gcol<0) |
| 214 |
{ |
| 215 |
if (printlevel>5) |
| 216 |
cout << "**WRN**: ML_NOX::ML_Nox_collapsedcoloring:\n" |
| 217 |
<< "**WRN**: cgraph->ColMap().GID(old_colindices[j]) returned " << old_gcol << endl |
| 218 |
<< "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; |
| 219 |
} |
| 220 |
if (lok==0) |
| 221 |
break; |
| 222 |
if (old_gcol % bsize != 0) |
| 223 |
continue; |
| 224 |
|
| 225 |
int new_gcol = old_gcol/bsize; |
| 226 |
new_gindices[new_numindices] = new_gcol; |
| 227 |
++new_numindices; |
| 228 |
} |
| 229 |
|
| 230 |
// insert the row in the new graph |
| 231 |
err = nodegraph.InsertGlobalIndices(new_grow,new_numindices,new_gindices); |
| 232 |
if (err!=0 && err !=1) |
| 233 |
{ |
| 234 |
if (printlevel>5) |
| 235 |
cout << "**WRN**: ML_NOX::ML_Nox_collapsedcoloring:\n" |
| 236 |
<< "**WRN**: nodegraph.InsertGlobalIndices returned " << err << endl |
| 237 |
<< "**WRN**: switching to none-collapsed coloring\n" |
| 238 |
<< "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; |
| 239 |
lok=0; |
| 240 |
} |
| 241 |
if (lok==0) |
| 242 |
break; |
| 243 |
} |
| 244 |
// catch all errors that may have occurred in this loop |
| 245 |
cgraph->Comm().MinAll(&lok,&gok,1); |
| 246 |
if (!gok) |
| 247 |
{ |
| 248 |
if (myRows) delete [] myRows; |
| 249 |
if (new_gindices) delete [] new_gindices; new_gindices=NULL; |
| 250 |
if (new_gindices) delete [] new_gindices; new_gindices=NULL; |
| 251 |
return NULL; |
| 252 |
} |
| 253 |
|
| 254 |
nodegraph.FillComplete(); |
| 255 |
nodegraph.OptimizeStorage(); |
| 256 |
|
| 257 |
// tidy up |
| 258 |
if (new_gindices) delete [] new_gindices; new_gindices=NULL; |
| 259 |
if (new_gindices) delete [] new_gindices; new_gindices=NULL; |
| 260 |
|
| 261 |
EpetraExt::CrsGraph_MapColoring::ColoringAlgorithm algType = |
| 262 |
EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN; |
| 263 |
|
| 264 |
EpetraExt::CrsGraph_MapColoring* node_MapColoring = |
| 265 |
new EpetraExt::CrsGraph_MapColoring(algType,0,diagonalonly,0); |
| 266 |
|
| 267 |
Epetra_MapColoring* node_colorMap = &(*node_MapColoring)(nodegraph); |
| 268 |
|
| 269 |
// now get the colors out of the node_colorMap and create a colorMap for the original system |
| 270 |
int node_ncolors = node_colorMap->NumColors(); |
| 271 |
int* node_colors = node_colorMap->ElementColors(); |
| 272 |
int* node_loc = node_colorMap->ListOfColors(); |
| 273 |
int* col_colors = new int[cgraph->ColMap().NumMyElements()]; |
| 274 |
|
| 275 |
// node_ncolors is a local value, we need the global highest color number here |
| 276 |
int lmax = 0; |
| 277 |
int gmax = 0; |
| 278 |
for (int i=0; i<node_ncolors; ++i) |
| 279 |
if (lmax<node_loc[i]) lmax = node_loc[i]; |
| 280 |
cgraph->Comm().MaxAll(&lmax,&gmax,1); |
| 281 |
|
| 282 |
#if 0 |
| 283 |
for (int i=0; i<nodegraph.ColMap().NumMyElements(); ++i) |
| 284 |
cout << "node_colors[ " << i << "] " << node_colors[i] << endl; |
| 285 |
|
| 286 |
cout << "nodegraph.ColMap().NumMyElements() " << nodegraph.ColMap().NumMyElements() << endl; |
| 287 |
cout << "cgraph->ColMap().NumMyElements() " << cgraph->ColMap().NumMyElements() << endl; |
| 288 |
#endif |
| 289 |
|
| 290 |
// expand the colors to point wise columns |
| 291 |
int j=0; |
| 292 |
for (int i=0; i<nodegraph.ColMap().NumMyElements(); ++i) |
| 293 |
for (int k=0; k<bsize; ++k) |
| 294 |
{ |
| 295 |
col_colors[j] = node_colors[i]+k*gmax; |
| 296 |
#if 0 |
| 297 |
cout << "col_colors[ " << j << "] " << col_colors[j] << endl; |
| 298 |
#endif |
| 299 |
++j; |
| 300 |
} |
| 301 |
lok=1; |
| 302 |
gok=1; |
| 303 |
if (j!=cgraph->ColMap().NumMyElements()) |
| 304 |
{ |
| 305 |
if (printlevel>5) |
| 306 |
cout << "**ERR**: ML_NOX::ML_Nox_collapsedcoloring:\n" |
| 307 |
<< "**ERR**: error expanding node colors to column colors\n" |
| 308 |
<< "**WRN**: switching to none-collapsed coloring\n" |
| 309 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; |
| 310 |
lok=0; |
| 311 |
} |
| 312 |
cgraph->Comm().MinAll(&lok,&gok,1); |
| 313 |
if (!gok) |
| 314 |
{ |
| 315 |
if (myRows) delete [] myRows; |
| 316 |
if (new_gindices) delete [] new_gindices; new_gindices=NULL; |
| 317 |
if (new_gindices) delete [] new_gindices; new_gindices=NULL; |
| 318 |
if (node_MapColoring) delete node_MapColoring; |
| 319 |
if (col_colors) delete [] col_colors; |
| 320 |
return NULL; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
Epetra_MapColoring* colorMap = new Epetra_MapColoring(cgraph->ColMap(),col_colors); |
| 325 |
|
| 326 |
if (myRows) delete [] myRows; |
| 327 |
if (new_gindices) delete [] new_gindices; |
| 328 |
if (new_gindices) delete [] new_gindices; |
| 329 |
if (node_MapColoring) delete node_MapColoring; |
| 330 |
if (col_colors) delete [] col_colors; |
| 331 |
|
| 332 |
return colorMap; |
| 333 |
} |
| 334 |
|
| 335 |
/*----------------------------------------------------------------------* |
| 336 |
| color a graph in a standard way m.gee 05/05| |
| 337 |
*----------------------------------------------------------------------*/ |
| 338 |
Epetra_MapColoring* ML_NOX::ML_Nox_standardcoloring(Epetra_CrsGraph* graph, |
| 339 |
bool diagonalonly) |
| 340 |
{ |
| 341 |
EpetraExt::CrsGraph_MapColoring::ColoringAlgorithm algType = |
| 342 |
EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN; |
| 343 |
|
| 344 |
EpetraExt::CrsGraph_MapColoring* MapColoring = |
| 345 |
new EpetraExt::CrsGraph_MapColoring(algType,0,diagonalonly,0); |
| 346 |
|
| 347 |
Epetra_MapColoring* colorMap = &(*MapColoring)(*graph); |
| 348 |
|
| 349 |
delete MapColoring; |
| 350 |
|
| 351 |
return colorMap; |
| 352 |
} |
| 353 |
|
| 354 |
/*----------------------------------------------------------------------* |
| 355 |
| m.gee 11/05| |
| 356 |
*----------------------------------------------------------------------*/ |
| 357 |
bool ML_NOX::Print_Epetra_CrsMatrix(Epetra_CrsMatrix& matrix) |
| 358 |
{ |
| 359 |
for (int i=0; i<matrix.NumMyRows(); ++i) |
| 360 |
{ |
| 361 |
printf("Lrow %5d: ",i); fflush(stdout); |
| 362 |
int numentries; |
| 363 |
int* indices; |
| 364 |
double* values; |
| 365 |
int err = matrix.ExtractMyRowView(i,numentries,values,indices); |
| 366 |
for (int j=0; j<numentries; ++j) |
| 367 |
printf("%5d %10.3e ",indices[j],values[j]); |
| 368 |
printf("\n"); fflush(stdout); |
| 369 |
} |
| 370 |
return true; |
| 371 |
} |
| 372 |
|
| 373 |
/*----------------------------------------------------------------------* |
| 374 |
| m.gee 11/05| |
| 375 |
*----------------------------------------------------------------------*/ |
| 376 |
Epetra_CrsMatrix* ML_NOX::StripZeros(Epetra_CrsMatrix& in, double eps) |
| 377 |
{ |
| 378 |
Epetra_CrsMatrix* out = new Epetra_CrsMatrix(Copy,in.RowMap(),200); |
| 379 |
for (int lrow=0; lrow<in.NumMyRows(); ++lrow) |
| 380 |
{ |
| 381 |
int grow = in.GRID(lrow); |
| 382 |
if (grow<0) { cout << "ERROR: grow<0 \n"; exit(0); } |
| 383 |
int numentries; |
| 384 |
int* lindices; |
| 385 |
double* values; |
| 386 |
int err = in.ExtractMyRowView(lrow,numentries,values,lindices); |
| 387 |
if (err) { cout << "ExtractMyRowView returned " << err << endl; exit(0);} |
| 388 |
for (int j=0; j<numentries; ++j) |
| 389 |
{ |
| 390 |
int lcol = lindices[j]; |
| 391 |
int gcol = in.GCID(lcol); |
| 392 |
if (gcol<0) { cout << "ERROR: gcol<0 \n"; exit(0); } |
| 393 |
if (abs(values[j])<eps && gcol != grow) |
| 394 |
continue; |
| 395 |
int err = out->InsertGlobalValues(grow,1,&values[j],&gcol); |
| 396 |
if (err) { cout << "InsertGlobalValues returned " << err << endl; exit(0);} |
| 397 |
} |
| 398 |
} |
| 399 |
out->FillComplete(); |
| 400 |
out->OptimizeStorage(); |
| 401 |
return out; |
| 402 |
} |
| 403 |
|
| 404 |
#endif // defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA) |