Debian Codegraph @

packages/trilinoscouplings/src/ml/NonlinML/ml_nox_matrixfreelevel.cpp

raw
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 // ML-headers
15 #include "ml_common.h"
16 #include "TrilinosCouplings_config.h"
17
18 #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)
19
20 #include <ctime>
21 #include <cstdlib>
22 #include <iostream>
23 #include <string>
24 #include <vector>
25
26 #ifdef ML_MPI // FIXME: do I need this?
27 #include <mpi.h>
28 #endif
29
30 // this class
31 #include "ml_nox_matrixfreelevel.H"
32
33 /*----------------------------------------------------------------------*
34 | the class defining a matrix-free coarse level m.gee 12/04|
35 *----------------------------------------------------------------------*/
36 /*----------------------------------------------------------------------*
37 | Constructor (public) m.gee 12/04|
38 | IMPORTANT: |
39 | No matter on which level we are here, the vector xfine is ALWAYS |
40 | a fine grid vector here! |
41 *----------------------------------------------------------------------*/
42 ML_NOX::ML_Nox_MatrixfreeLevel::ML_Nox_MatrixfreeLevel(int level, int nlevel, int plevel,
43 ML* ml, ML_Aggregate* ag, Epetra_CrsMatrix** P,
44 ML_NOX::Ml_Nox_Fineinterface& interface, const Epetra_Comm& comm,
45 const Epetra_Vector& xfine, double fd_alpha, double fd_beta,
46 bool fd_centered, bool isDiagonalOnly, int bsize)
47 : fineinterface_(interface),
48 comm_(comm)
49 {
50 level_ = level;
51 nlevel_ = nlevel;
52 ml_printlevel_ = plevel;
53 ml_ = ml;
54 ag_ = ag;
55 fd_alpha_ = fd_alpha;
56 fd_beta_ = fd_beta;
57 fd_centered_ = fd_centered;
58 isDiagonalOnly_ = isDiagonalOnly;
59 A_ = 0;
60 coarseinterface_= 0;
61 bsize_ = bsize;
62
63
64 // we need the graph of the operator on this level. On the fine grid we can just ask the
65 // fineinterface for it, on the coarser levels it has to be extracted from the ML-hierachy
66 if (level_==0)
67 {
68 // the Epetra_CrsGraph-copy-constructor shares data with the original one.
69 // We want a really deep copy here so we cannot use it
70 // graph_ will be given to the FiniteDifferencing class and will be destroyed by it
71 graph_ = ML_NOX::deepcopy_graph(interface.getGraph());
72 }
73 else
74 {
75 // Note that ML has no understanding of global indices, so it makes up new GIDs
76 // (This also holds for the Prolongators P)
77 Epetra_CrsMatrix* tmpMat = 0;
78 int maxnnz = 0;
79 double cputime = 0.0;
80 ML_Operator2EpetraCrsMatrix(&(ml_->Amat[level_]), tmpMat, maxnnz, false, cputime);
81 // copy the graph
82 double t0 = GetClock();
83 graph_ = ML_NOX::deepcopy_graph(&(tmpMat->Graph()));
84 // delete the copy of the Epetra_CrsMatrix
85 if (tmpMat) delete tmpMat; tmpMat = 0;
86 double t1 = GetClock();
87 if (ml_printlevel_ > 0 && 0 == comm_.MyPID())
88 cout << "matrixfreeML (level " << level_ << "): extraction/copy of Graph in " << cputime+t1-t0 << " sec\n"
89 << " max-nonzeros in Graph: " << maxnnz << "\n";
90 }
91
92 // create this levels coarse interface
93 coarseinterface_ = new ML_NOX::Nox_CoarseProblem_Interface(fineinterface_,level_,ml_printlevel_,
94 P,&(graph_->RowMap()),nlevel_);
95
96 // restrict the xfine-vector to this level
97 Epetra_Vector* xthis = coarseinterface_->restrict_fine_to_this(xfine);
98 if (!xthis)
99 {
100 cout << "**ERR**: ML_Epetra::ML_Nox_MatrixfreeLevel::ML_Nox_MatrixfreeLevel:\n"
101 << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::restrict_fine_to_this returned NULL on level "
102 << level_ << "\n"
103 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
104 }
105
106 Epetra_Vector* xc = new Epetra_Vector(graph_->RowMap(),false);
107 // FIXME: after intesive testing, this test might be obsolet
108 #if 0
109 bool samemap = xc->Map().PointSameAs(xthis->Map());
110 if (samemap)
111 {
112 #endif
113 xc->Update(1.0,*xthis,0.0);
114 #if 0
115 }
116 else
117 {
118 cout << "**WRN** Maps are not equal in\n"
119 << "**WRN** file/line: " << __FILE__ << "/" << __LINE__ << "\n";
120 // import the xthis vector in the Map that ML produced for graph_
121 Epetra_Import* importer = new Epetra_Import(graph_->RowMap(),xthis->Map());
122 int ierr = xc->Import(*xthis,*importer,Insert);
123 if (ierr)
124 {
125 cout << "**ERR**: ML_Epetra::ML_Nox_MatrixfreeLevel::ML_Nox_MatrixfreeLevel:\n"
126 << "**ERR**: export from xthis to xc returned err=" << ierr <<"\n"
127 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
128 }
129 if (importer) delete importer; importer = 0;
130 }
131 #endif
132 if (xthis) delete xthis; xthis = 0;
133
134 // create the coloring of the graph
135 if (ml_printlevel_>0 && comm_.MyPID()==0)
136 {
137 cout << "matrixfreeML (level " << level_ << "): Entering Coloring on level " << level_ << "\n";
138 fflush(stdout);
139 }
140 double t0 = GetClock();
141 colorMap_ = ML_NOX::ML_Nox_collapsedcoloring(graph_,bsize_,isDiagonalOnly,ml_printlevel_);
142 if (!colorMap_) colorMap_ = ML_NOX::ML_Nox_standardcoloring(graph_,isDiagonalOnly);
143 colorMapIndex_ = new EpetraExt::CrsGraph_MapColoringIndex(*colorMap_);
144 colorcolumns_ = &(*colorMapIndex_)(*graph_);
145 double t1 = GetClock();
146 if (ml_printlevel_>0 && comm_.MyPID()==0)
147 {
148 cout << "matrixfreeML (level " << level_ << "): Proc " << comm_.MyPID() <<" Coloring time is " << (t1-t0) << " sec\n";
149 fflush(stdout);
150 }
151
152 // construct the FiniteDifferenceColoring-Matrix
153 if (ml_printlevel_>0 && comm_.MyPID()==0)
154 {
155 cout << "matrixfreeML (level " << level_ << "): Entering Construction FD-Operator on level " << level_ << "\n";
156 fflush(stdout);
157 }
158
159 t0 = GetClock();
160
161 #if 1 // FD-operator with coloring
162 FD_ = new NOX::EpetraNew::FiniteDifferenceColoring(*coarseinterface_,
163 *xc,
164 *graph_,
165 *colorMap_,
166 *colorcolumns_,
167 true,
168 isDiagonalOnly_,
169 fd_beta_,fd_alpha_);
170 #else // FD-operator without coloring
171 FD_ = new NOX::EpetraNew::FiniteDifference(*coarseinterface_,
172 *xc,
173 *graph_,
174 fd_beta_,fd_alpha_);
175 #endif
176 // set differencing method
177 if (fd_centered_)
178 FD_->setDifferenceMethod(NOX::EpetraNew::FiniteDifferenceColoring::Centered);
179
180 bool err = FD_->computeJacobian(*xc);
181 if (err==false)
182 {
183 cout << "**ERR**: ML_NOX::ML_Nox_MatrixfreeLevel::ML_Nox_MatrixfreeLevel:\n"
184 << "**ERR**: NOX::Epetra::FiniteDifferenceColoring returned an error on level "
185 << level_ << "\n"
186 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
187 }
188
189 // print number of calls to the coarse interface
190 if (ml_printlevel_>0 && comm_.MyPID()==0)
191 cout << "matrixfreeML (level " << level_ << "): Calls to coarse-computeF in FD-Operator: "
192 << coarseinterface_->numcallscomputeF() << "\n";
193
194 t1 = GetClock();
195 if (ml_printlevel_>0 && comm_.MyPID()==0)
196 {
197 cout << "matrixfreeML (level " << level_ << "): Proc " << comm_.MyPID() <<" colored Finite Differencing time is " << (t1-t0) << " sec\n";
198 fflush(stdout);
199 }
200
201 // get ref to computed Epetra_CrsMatrix
202 A_ = dynamic_cast<Epetra_CrsMatrix*>(&(FD_->getUnderlyingMatrix()));
203
204 // set counter for number of calls to the coarseinterface_->computeF back to zero
205 coarseinterface_->resetnumcallscomputeF();
206
207 // tidy up
208 if (xc) delete xc; xc = 0;
209
210 return;
211 }
212
213 /*----------------------------------------------------------------------*
214 | recreate this level (public) m.gee 01/05|
215 | this function assumes, that the graph of the fine level problem has |
216 | not changed since call to the constructor and therefore |
217 | the graph and it's coloring do not have to be recomputed |
218 | IMPORTANT: |
219 | No matter on which level we are here, the vector xfine is ALWAYS |
220 | a fine grid vector here! |
221 *----------------------------------------------------------------------*/
222 bool ML_NOX::ML_Nox_MatrixfreeLevel::recreateLevel(int level, int nlevel, int plevel,
223 ML* ml, ML_Aggregate* ag, Epetra_CrsMatrix** P,
224 ML_NOX::Ml_Nox_Fineinterface& interface,
225 const Epetra_Comm& comm, const Epetra_Vector& xfine)
226 {
227 // make some tests
228 if (level != level_)
229 {
230 cout << "**ERR**: ML_Epetra::ML_Nox_MatrixfreeLevel::recreateLevel:\n"
231 << "**ERR**: level_ " << level_ << " not equal level " << level << "\n"
232 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
233 }
234 if (nlevel != nlevel_)
235 {
236 cout << "**ERR**: ML_Epetra::ML_Nox_MatrixfreeLevel::recreateLevel:\n"
237 << "**ERR**: nlevel_ " << nlevel_ << " not equal nlevel " << nlevel << "\n"
238 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
239 }
240 // printlevel might have changed
241 ml_printlevel_ = plevel;
242 ml_ = ml;
243 ag_ = ag;
244 destroyP(); // safer to use the new Ps
245 setP(NULL);
246
247 // we need the graph of the operator on this level. On the fine grid we can just ask the
248 // fineinterface for it, on the coarser levels it has to be extracted from the ML-hierachy
249 bool same;
250 if (level_==0)
251 {
252 const Epetra_CrsGraph* graph = interface.getGraph();
253 // check whether the old graph matches the new one
254 same = compare_graphs(graph,graph_);
255 destroyFD(); // we are here to recompute the FD-operator (this destroys graph_)
256 graph_ = ML_NOX::deepcopy_graph(graph);
257 }
258 else
259 {
260 // Note that ML has no understanding of global indices, so it makes up new GIDs
261 // (This also holds for the Prolongators P)
262 Epetra_CrsMatrix* tmpMat = 0;
263 int maxnnz = 0;
264 double cputime = 0.0;
265 ML_Operator2EpetraCrsMatrix(&(ml_->Amat[level_]), tmpMat, maxnnz, false, cputime);
266 // get a view from the graph
267 const Epetra_CrsGraph& graph = tmpMat->Graph();
268 // compare the graph to the existing one
269 same = compare_graphs(&graph,graph_);
270 destroyFD(); // we are here to recompute the FD-operator (this destroys graph_)
271 double t0 = GetClock();
272 graph_ = ML_NOX::deepcopy_graph(&graph);
273 // delete the copy of the Epetra_CrsMatrix
274 if (tmpMat) delete tmpMat; tmpMat = 0;
275 double t1 = GetClock();
276 if (ml_printlevel_ > 0 && 0 == comm_.MyPID())
277 cout << "matrixfreeML (level " << level_ << "): extraction/copy of Graph in " << cputime+t1-t0 << " sec\n"
278 << " max-nonzeros in Graph: " << maxnnz << "\n";
279 }
280
281 // recreate this levels coarse interface
282 if (same)
283 coarseinterface_->recreate(ml_printlevel_,P,&(graph_->RowMap()));
284 else
285 {
286 delete coarseinterface_;
287 coarseinterface_ = new ML_NOX::Nox_CoarseProblem_Interface(fineinterface_,level_,ml_printlevel_,
288 P,&(graph_->RowMap()),nlevel_);
289 }
290
291 // restrict the xfine-vector to this level
292 Epetra_Vector* xthis = coarseinterface_->restrict_fine_to_this(xfine);
293 if (!xthis)
294 {
295 cout << "**ERR**: ML_Epetra::ML_Nox_MatrixfreeLevel::ML_Nox_MatrixfreeLevel:\n"
296 << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::restrict_fine_to_this returned NULL on level "
297 << level_ << "\n"
298 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
299 }
300
301 Epetra_Vector* xc = new Epetra_Vector(graph_->RowMap(),false);
302 // FIXME: after intesive testing, this test might be obsolet
303 #if 0
304 bool samemap = xc->Map().PointSameAs(xthis->Map());
305 if (samemap)
306 {
307 #endif
308 xc->Update(1.0,*xthis,0.0);
309 #if 0
310 }
311 else
312 {
313 cout << "**WRN** Maps are not equal in\n"
314 << "**WRN** file/line: " << __FILE__ << "/" << __LINE__ << "\n";
315 // import the xthis vector in the Map that ML produced for graph_
316 Epetra_Import* importer = new Epetra_Import(graph_->RowMap(),xthis->Map());
317 int ierr = xc->Import(*xthis,*importer,Insert);
318 if (ierr)
319 {
320 cout << "**ERR**: ML_Epetra::ML_Nox_MatrixfreeLevel::ML_Nox_MatrixfreeLevel:\n"
321 << "**ERR**: export from xthis to xc returned err=" << ierr <<"\n"
322 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
323 }
324 if (importer) delete importer; importer = 0;
325 }
326 #endif
327 if (xthis) delete xthis; xthis = 0;
328
329 // create the coloring of the graph
330 if (ml_printlevel_>0 && comm_.MyPID()==0)
331 {
332 cout << "matrixfreeML (level " << level_ << "): Entering Recoloring on level " << level_ << "\n";
333 fflush(stdout);
334 }
335 double t0 = GetClock();
336 if (!same) // te graph has obviously changed, so we need to recolor
337 {
338 if (colorMap_) delete colorMap_; colorMap_ = 0;
339 if (colorMapIndex_) delete colorMapIndex_; colorMapIndex_ = 0;
340 if (colorcolumns_) delete colorcolumns_; colorcolumns_ = 0;
341
342 colorMap_ = ML_NOX::ML_Nox_collapsedcoloring(graph_,bsize_,isDiagonalOnly_,ml_printlevel_);
343 if (!colorMap_) colorMap_ = ML_NOX::ML_Nox_standardcoloring(graph_,isDiagonalOnly_);
344 colorMapIndex_ = new EpetraExt::CrsGraph_MapColoringIndex(*colorMap_);
345 colorcolumns_ = &(*colorMapIndex_)(*graph_);
346 }
347 else if (ml_printlevel_>0 && comm_.MyPID()==0)
348 cout << "matrixfreeML (level " << level_ << "): Reusing existing Coloring on level " << level_ << "\n";
349 double t1 = GetClock();
350 if (ml_printlevel_>5)
351 {
352 cout << "matrixfreeML (level " << level_ << "): Proc " << comm_.MyPID() <<" (Re)Coloring time is " << (t1-t0) << " sec\n";
353 fflush(stdout);
354 }
355
356 #if 0
357 // print the colorMap_
358 if (comm_.MyPID()==0)
359 cout << "colorMap_\n";
360 cout << *colorMap_;
361 for (int i=0; i<colorcolumns_->size(); i++)
362 {
363 if (comm_.MyPID()==0)
364 cout << "the " << i << " th colorcolumn_ - vector\n";
365 cout << colorcolumns_->operator[](i);
366 }
367 #endif
368
369 // construct the FiniteDifferenceColoring-Matrix
370 if (ml_printlevel_>0 && comm_.MyPID()==0)
371 {
372 cout << "matrixfreeML (level " << level_ << "): Entering Construction FD-Operator on level " << level_ << "\n";
373 fflush(stdout);
374 }
375
376 t0 = GetClock();
377 #if 1 // FD-operator with coloring (see the #if 1 in ml_nox_matrixfreelevel.H as well!)
378 FD_ = new NOX::EpetraNew::FiniteDifferenceColoring(*coarseinterface_,
379 *xc,
380 *graph_,
381 *colorMap_,
382 *colorcolumns_,
383 true,
384 isDiagonalOnly_,
385 fd_beta_,fd_alpha_);
386 #else // FD-operator without coloring
387 FD_ = new NOX::EpetraNew::FiniteDifference(*coarseinterface_,
388 *xc,
389 *graph_,
390 fd_beta_,fd_alpha_);
391 #endif
392
393 // set differencing method
394 if (fd_centered_)
395 {
396 FD_->setDifferenceMethod(NOX::EpetraNew::FiniteDifferenceColoring::Centered);
397 }
398
399 bool err = FD_->computeJacobian(*xc);
400 if (err==false)
401 {
402 cout << "**ERR**: ML_Epetra::ML_Nox_MatrixfreeLevel::ML_Nox_MatrixfreeLevel:\n"
403 << "**ERR**: NOX::Epetra::FiniteDifferenceColoring returned an error on level "
404 << level_ << "\n"
405 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
406 }
407
408 t1 = GetClock();
409 if (ml_printlevel_>5)
410 cout << "matrixfreeML (level " << level_ << "): Proc " << comm_.MyPID()
411 <<" Finite Differencing operator constr. in " << (t1-t0) << " sec\n";
412
413 // get ref to computed Epetra_CrsMatrix
414 A_ = dynamic_cast<Epetra_CrsMatrix*>(&(FD_->getUnderlyingMatrix()));
415
416 // print number of calls to the coarse interface
417 if (ml_printlevel_>5 && comm_.MyPID()==0)
418 cout << "matrixfreeML (level " << level_ << "): Calls to coarse-computeF in FD-Operator: "
419 << coarseinterface_->numcallscomputeF() << "\n";
420
421 // set counter for number of calls to the coarseinterface_->computeF back to zero
422 coarseinterface_->resetnumcallscomputeF();
423
424 // tidy up
425 if (xc) delete xc; xc = 0;
426
427 return true;
428 }
429
430 /*----------------------------------------------------------------------*
431 | compare 2 graphs pointwise in indices m.gee 01/05|
432 *----------------------------------------------------------------------*/
433 bool ML_NOX::ML_Nox_MatrixfreeLevel::compare_graphs(const Epetra_CrsGraph* newgraph,
434 const Epetra_CrsGraph* oldgraph)
435 {
436 int i,j;
437 if (newgraph->NumMyRows() != oldgraph->NumMyRows() ||
438 newgraph->NumMyNonzeros() != oldgraph->NumMyNonzeros() ||
439 newgraph->NumGlobalNonzeros() != oldgraph->NumGlobalNonzeros())
440 return false;
441 for (i=0; i<newgraph->NumMyRows(); i++)
442 {
443 if (newgraph->GRID(i) != oldgraph->GRID(i))
444 return false;
445 int new_Numindices;
446 int* new_Indices;
447 newgraph->ExtractMyRowView(i,new_Numindices,new_Indices);
448 int old_Numindices;
449 int* old_Indices;
450 oldgraph->ExtractMyRowView(i,old_Numindices,old_Indices);
451 if (new_Numindices != old_Numindices)
452 return false;
453 for (j=0; j<new_Numindices; j++)
454 if (new_Indices[j] != old_Indices[j])
455 return false;
456 }
457 return true;
458 }
459
460 /*----------------------------------------------------------------------*
461 | Destructor (public) m.gee 12/04|
462 *----------------------------------------------------------------------*/
463 ML_NOX::ML_Nox_MatrixfreeLevel::~ML_Nox_MatrixfreeLevel()
464 {
465
466 // graph_ and A_ are part of FD and are destroyed there
467 if (FD_)
468 destroyFD();
469
470 // if (MapColoring_)
471 // delete MapColoring_;
472 // MapColoring_ = 0;
473
474 if (colorMap_)
475 delete colorMap_;
476 colorMap_ = 0;
477
478 if (colorMapIndex_)
479 delete colorMapIndex_;
480 colorMapIndex_ = 0;
481
482 if (colorcolumns_)
483 delete colorcolumns_;
484 colorcolumns_ = 0;
485
486 if (coarseinterface_)
487 delete coarseinterface_;
488 coarseinterface_ = 0;
489
490 return;
491 }
492
493
494 #endif // defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA) & defined(HAVE_ML_EPETRAEXT)