IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_Utils.cpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #include "Ifpack_ConfigDefs.h"
44 #include "Ifpack_Preconditioner.h"
45 #include "Ifpack_Utils.h"
46 #include "Epetra_Comm.h"
47 #include "Epetra_CrsMatrix.h"
48 #include "Epetra_CrsGraph.h"
49 #include "Epetra_Map.h"
50 #include "Epetra_BlockMap.h"
51 #include "Epetra_Import.h"
52 #include "Epetra_MultiVector.h"
53 #include "Epetra_Vector.h"
54 
56 {
57  cout << "================================================================================" << endl;
58 }
59 
60 //============================================================================
61 void Ifpack_BreakForDebugger(Epetra_Comm& Comm)
62 {
63  char hostname[80];
64  char buf[80];
65  if (Comm.MyPID() == 0) cout << "Host and Process Ids for tasks" << endl;
66  for (int i = 0; i <Comm.NumProc() ; i++) {
67  if (i == Comm.MyPID() ) {
68 #if defined(TFLOP) || defined(JANUS_STLPORT)
69  sprintf(buf, "Host: %s PID: %d", "janus", getpid());
70 #elif defined(_WIN32)
71  sprintf(buf,"Windows compiler, unknown hostname and PID!");
72 #else
73  gethostname(hostname, sizeof(hostname));
74  sprintf(buf, "Host: %s\tComm.MyPID(): %d\tPID: %d",
75  hostname, Comm.MyPID(), getpid());
76 #endif
77  printf("%s\n",buf);
78  fflush(stdout);
79 #if !( defined(_WIN32) )
80  sleep(1);
81 #endif
82  }
83  }
84  if(Comm.MyPID() == 0) {
85  printf("\n");
86  printf("** Pausing to attach debugger...\n");
87  printf("** You may now attach debugger to the processes listed above.\n");
88  printf( "**\n");
89  printf( "** Enter a character to continue > "); fflush(stdout);
90  char go;
91  scanf("%c",&go);
92  }
93 
94  Comm.Barrier();
95 
96 }
97 
98 //============================================================================
99 Epetra_CrsMatrix* Ifpack_CreateOverlappingCrsMatrix(const Epetra_RowMatrix* Matrix,
100  const int OverlappingLevel)
101 {
102 
103  if (OverlappingLevel == 0)
104  return(0); // All done
105  if (Matrix->Comm().NumProc() == 1)
106  return(0); // All done
107 
108  Epetra_CrsMatrix* OverlappingMatrix;
109  OverlappingMatrix = 0;
110  Epetra_Map* OverlappingMap;
111  OverlappingMap = (Epetra_Map*)&(Matrix->RowMatrixRowMap());
112 
113  const Epetra_RowMatrix* OldMatrix;
114  const Epetra_Map* DomainMap = &(Matrix->OperatorDomainMap());
115  const Epetra_Map* RangeMap = &(Matrix->OperatorRangeMap());
116 
117  for (int level = 1; level <= OverlappingLevel ; ++level) {
118 
119  if (OverlappingMatrix)
120  OldMatrix = OverlappingMatrix;
121  else
122  OldMatrix = Matrix;
123 
124  Epetra_Import* OverlappingImporter;
125  OverlappingImporter = (Epetra_Import*)OldMatrix->RowMatrixImporter();
126  int NumMyElements = OverlappingImporter->TargetMap().NumMyElements();
127  int* MyGlobalElements = OverlappingImporter->TargetMap().MyGlobalElements();
128 
129  // need to build an Epetra_Map in this way because Epetra_CrsMatrix
130  // requires Epetra_Map and not Epetra_BlockMap
131 
132  OverlappingMap = new Epetra_Map(-1,NumMyElements,MyGlobalElements,
133  0, Matrix->Comm());
134 
135  if (level < OverlappingLevel)
136  OverlappingMatrix = new Epetra_CrsMatrix(Copy, *OverlappingMap, 0);
137  else
138  // On last iteration, we want to filter out all columns except
139  // those that correspond
140  // to rows in the graph. This assures that our matrix is square
141  OverlappingMatrix = new Epetra_CrsMatrix(Copy, *OverlappingMap,
142  *OverlappingMap, 0);
143 
144  OverlappingMatrix->Import(*OldMatrix, *OverlappingImporter, Insert);
145  if (level < OverlappingLevel) {
146  OverlappingMatrix->FillComplete(*DomainMap, *RangeMap);
147  }
148  else {
149  OverlappingMatrix->FillComplete(*DomainMap, *RangeMap);
150  }
151 
152  delete OverlappingMap;
153 
154  if (level > 1) {
155  delete OldMatrix;
156  }
157  OverlappingMatrix->FillComplete();
158 
159  }
160 
161  return(OverlappingMatrix);
162 }
163 
164 //============================================================================
165 Epetra_CrsGraph* Ifpack_CreateOverlappingCrsMatrix(const Epetra_CrsGraph* Graph,
166  const int OverlappingLevel)
167 {
168 
169  if (OverlappingLevel == 0)
170  return(0); // All done
171  if (Graph->Comm().NumProc() == 1)
172  return(0); // All done
173 
174  Epetra_CrsGraph* OverlappingGraph;
175  Epetra_BlockMap* OverlappingMap;
176  OverlappingGraph = const_cast<Epetra_CrsGraph*>(Graph);
177  OverlappingMap = const_cast<Epetra_BlockMap*>(&(Graph->RowMap()));
178 
179  Epetra_CrsGraph* OldGraph;
180  Epetra_BlockMap* OldMap;
181  const Epetra_BlockMap* DomainMap = &(Graph->DomainMap());
182  const Epetra_BlockMap* RangeMap = &(Graph->RangeMap());
183 
184  for (int level = 1; level <= OverlappingLevel ; ++level) {
185 
186  OldGraph = OverlappingGraph;
187  OldMap = OverlappingMap;
188 
189  Epetra_Import* OverlappingImporter;
190  OverlappingImporter = const_cast<Epetra_Import*>(OldGraph->Importer());
191  OverlappingMap = new Epetra_BlockMap(OverlappingImporter->TargetMap());
192 
193  if (level < OverlappingLevel)
194  OverlappingGraph = new Epetra_CrsGraph(Copy, *OverlappingMap, 0);
195  else
196  // On last iteration, we want to filter out all columns except
197  // those that correspond
198  // to rows in the graph. This assures that our matrix is square
199  OverlappingGraph = new Epetra_CrsGraph(Copy, *OverlappingMap,
200  *OverlappingMap, 0);
201 
202  OverlappingGraph->Import(*OldGraph, *OverlappingImporter, Insert);
203  if (level < OverlappingLevel)
204  OverlappingGraph->FillComplete(*DomainMap, *RangeMap);
205  else {
206  // Copy last OverlapImporter because we will use it later
207  OverlappingImporter = new Epetra_Import(*OverlappingMap, *DomainMap);
208  OverlappingGraph->FillComplete(*DomainMap, *RangeMap);
209  }
210 
211  if (level > 1) {
212  delete OldGraph;
213  delete OldMap;
214  }
215 
216  delete OverlappingMap;
217  OverlappingGraph->FillComplete();
218 
219  }
220 
221  return(OverlappingGraph);
222 }
223 
224 //============================================================================
225 string Ifpack_toString(const int& x)
226 {
227  char s[100];
228  sprintf(s, "%d", x);
229  return string(s);
230 }
231 
232 //============================================================================
233 string Ifpack_toString(const double& x)
234 {
235  char s[100];
236  sprintf(s, "%g", x);
237  return string(s);
238 }
239 
240 //============================================================================
241 int Ifpack_PrintResidual(char* Label, const Epetra_RowMatrix& A,
242  const Epetra_MultiVector& X, const Epetra_MultiVector&Y)
243 {
244  if (X.Comm().MyPID() == 0) {
245  cout << "***** " << Label << endl;
246  }
247  Ifpack_PrintResidual(0,A,X,Y);
248 
249  return(0);
250 }
251 
252 //============================================================================
253 int Ifpack_PrintResidual(const int iter, const Epetra_RowMatrix& A,
254  const Epetra_MultiVector& X, const Epetra_MultiVector&Y)
255 {
256  Epetra_MultiVector RHS(X);
257  std::vector<double> Norm2;
258  Norm2.resize(X.NumVectors());
259 
260  IFPACK_CHK_ERR(A.Multiply(false,X,RHS));
261  RHS.Update(1.0, Y, -1.0);
262 
263  RHS.Norm2(&Norm2[0]);
264 
265  if (X.Comm().MyPID() == 0) {
266  cout << "***** iter: " << iter << ": ||Ax - b||_2 = "
267  << Norm2[0] << endl;
268  }
269 
270  return(0);
271 }
272 
273 //============================================================================
274 // very simple debugging function that prints on screen the structure
275 // of the input matrix.
276 void Ifpack_PrintSparsity_Simple(const Epetra_RowMatrix& A)
277 {
278  int MaxEntries = A.MaxNumEntries();
279  vector<int> Indices(MaxEntries);
280  vector<double> Values(MaxEntries);
281  vector<bool> FullRow(A.NumMyRows());
282 
283  cout << "+-";
284  for (int j = 0 ; j < A.NumMyRows() ; ++j)
285  cout << '-';
286  cout << "-+" << endl;
287 
288  for (int i = 0 ; i < A.NumMyRows() ; ++i) {
289 
290  int Length;
291  A.ExtractMyRowCopy(i,MaxEntries,Length,
292  &Values[0], &Indices[0]);
293 
294  for (int j = 0 ; j < A.NumMyRows() ; ++j)
295  FullRow[j] = false;
296 
297  for (int j = 0 ; j < Length ; ++j) {
298  FullRow[Indices[j]] = true;
299  }
300 
301  cout << "| ";
302  for (int j = 0 ; j < A.NumMyRows() ; ++j) {
303  if (FullRow[j])
304  cout << '*';
305  else
306  cout << ' ';
307  }
308  cout << " |" << endl;
309  }
310 
311  cout << "+-";
312  for (int j = 0 ; j < A.NumMyRows() ; ++j)
313  cout << '-';
314  cout << "-+" << endl << endl;
315 
316 }
317 
318 //============================================================================
319 
320 double Ifpack_FrobeniusNorm(const Epetra_RowMatrix& A)
321 {
322  double MyNorm = 0.0, GlobalNorm;
323 
324  vector<int> colInd(A.MaxNumEntries());
325  vector<double> colVal(A.MaxNumEntries());
326 
327  for (int i = 0 ; i < A.NumMyRows() ; ++i) {
328 
329  int Nnz;
330  IFPACK_CHK_ERR(A.ExtractMyRowCopy(i,A.MaxNumEntries(),Nnz,
331  &colVal[0],&colInd[0]));
332 
333  for (int j = 0 ; j < Nnz ; ++j) {
334  MyNorm += colVal[j] * colVal[j];
335  }
336  }
337 
338  A.Comm().SumAll(&MyNorm,&GlobalNorm,1);
339 
340  return(sqrt(GlobalNorm));
341 }
342 
343 static void print()
344 {
345  printf("\n");
346 }
347 
348 #include <iomanip>
349 template<class T>
350 static void print(const char str[], T val)
351 {
352  cout.width(30); cout.setf(ios::left);
353  cout << str;
354  cout << " = " << val << endl;
355 }
356 
357 template<class T>
358 static void print(const char str[], T val, double percentage)
359 {
360  cout.width(30); cout.setf(ios::left);
361  cout << str;
362  cout << " = ";
363  cout.width(20); cout.setf(ios::left);
364  cout << val;
365  cout << " ( " << percentage << " %)" << endl;
366 }
367 template<class T>
368 static void print(const char str[], T one, T two, T three, bool equal = true)
369 {
370  cout.width(30); cout.setf(ios::left);
371  cout << str;
372  if (equal)
373  cout << " = ";
374  else
375  cout << " ";
376  cout.width(15); cout.setf(ios::left);
377  cout << one;
378  cout.width(15); cout.setf(ios::left);
379  cout << two;
380  cout.width(15); cout.setf(ios::left);
381  cout << three;
382  cout << endl;
383 }
384 
385 //============================================================================
386 #include "limits.h"
387 #include "float.h"
388 #include "Epetra_FECrsMatrix.h"
389 
390 int Ifpack_Analyze(const Epetra_RowMatrix& A, const bool Cheap,
391  const int NumPDEEqns)
392 {
393 
394  int NumMyRows = A.NumMyRows();
395  int NumGlobalRows = A.NumGlobalRows();
396  int NumGlobalCols = A.NumGlobalCols();
397  int MyBandwidth = 0, GlobalBandwidth;
398  int MyLowerNonzeros = 0, MyUpperNonzeros = 0;
399  int GlobalLowerNonzeros, GlobalUpperNonzeros;
400  int MyDiagonallyDominant = 0, GlobalDiagonallyDominant;
401  int MyWeaklyDiagonallyDominant = 0, GlobalWeaklyDiagonallyDominant;
402  double MyMin, MyAvg, MyMax;
403  double GlobalMin, GlobalAvg, GlobalMax;
404  int GlobalStorage;
405 
406  bool verbose = (A.Comm().MyPID() == 0);
407 
408  GlobalStorage = sizeof(int*) * NumGlobalRows +
409  sizeof(int) * A.NumGlobalNonzeros() +
410  sizeof(double) * A.NumGlobalNonzeros();
411 
412  if (verbose) {
413  print();
415  print<const char*>("Label", A.Label());
416  print<int>("Global rows", NumGlobalRows);
417  print<int>("Global columns", NumGlobalCols);
418  print<int>("Stored nonzeros", A.NumGlobalNonzeros());
419  print<int>("Nonzeros / row", A.NumGlobalNonzeros() / NumGlobalRows);
420  print<double>("Estimated storage (Mbytes)", 1.0e-6 * GlobalStorage);
421  }
422 
423  int NumMyActualNonzeros = 0, NumGlobalActualNonzeros;
424  int NumMyEmptyRows = 0, NumGlobalEmptyRows;
425  int NumMyDirichletRows = 0, NumGlobalDirichletRows;
426 
427  vector<int> colInd(A.MaxNumEntries());
428  vector<double> colVal(A.MaxNumEntries());
429 
430  Epetra_Vector Diag(A.RowMatrixRowMap());
431  Epetra_Vector RowSum(A.RowMatrixRowMap());
432  Diag.PutScalar(0.0);
433  RowSum.PutScalar(0.0);
434 
435  for (int i = 0 ; i < NumMyRows ; ++i) {
436 
437  int GRID = A.RowMatrixRowMap().GID(i);
438  int Nnz;
439  IFPACK_CHK_ERR(A.ExtractMyRowCopy(i,A.MaxNumEntries(),Nnz,
440  &colVal[0],&colInd[0]));
441 
442  if (Nnz == 0)
443  NumMyEmptyRows++;
444 
445  if (Nnz == 1)
446  NumMyDirichletRows++;
447 
448  for (int j = 0 ; j < Nnz ; ++j) {
449 
450  double v = colVal[j];
451  if (v < 0) v = -v;
452  if (colVal[j] != 0.0)
453  NumMyActualNonzeros++;
454 
455  int GCID = A.RowMatrixColMap().GID(colInd[j]);
456 
457  if (GCID != GRID)
458  RowSum[i] += v;
459  else
460  Diag[i] = v;
461 
462  if (GCID < GRID)
463  MyLowerNonzeros++;
464  else if (GCID > GRID)
465  MyUpperNonzeros++;
466  int b = GCID - GRID;
467  if (b < 0) b = -b;
468  if (b > MyBandwidth)
469  MyBandwidth = b;
470  }
471 
472  if (Diag[i] > RowSum[i])
473  MyDiagonallyDominant++;
474 
475  if (Diag[i] >= RowSum[i])
476  MyWeaklyDiagonallyDominant++;
477 
478  RowSum[i] += Diag[i];
479  }
480 
481  // ======================== //
482  // summing up global values //
483  // ======================== //
484 
485  A.Comm().SumAll(&MyDiagonallyDominant,&GlobalDiagonallyDominant,1);
486  A.Comm().SumAll(&MyWeaklyDiagonallyDominant,&GlobalWeaklyDiagonallyDominant,1);
487  A.Comm().SumAll(&NumMyActualNonzeros, &NumGlobalActualNonzeros, 1);
488  A.Comm().SumAll(&NumMyEmptyRows, &NumGlobalEmptyRows, 1);
489  A.Comm().SumAll(&NumMyDirichletRows, &NumGlobalDirichletRows, 1);
490  A.Comm().SumAll(&MyBandwidth, &GlobalBandwidth, 1);
491  A.Comm().SumAll(&MyLowerNonzeros, &GlobalLowerNonzeros, 1);
492  A.Comm().SumAll(&MyUpperNonzeros, &GlobalUpperNonzeros, 1);
493  A.Comm().SumAll(&MyDiagonallyDominant, &GlobalDiagonallyDominant, 1);
494  A.Comm().SumAll(&MyWeaklyDiagonallyDominant, &GlobalWeaklyDiagonallyDominant, 1);
495 
496  double NormOne = A.NormOne();
497  double NormInf = A.NormInf();
498  double NormF = Ifpack_FrobeniusNorm(A);
499 
500  if (verbose) {
501  print();
502  print<int>("Actual nonzeros", NumGlobalActualNonzeros);
503  print<int>("Nonzeros in strict lower part", GlobalLowerNonzeros);
504  print<int>("Nonzeros in strict upper part", GlobalUpperNonzeros);
505  print();
506  print<int>("Empty rows", NumGlobalEmptyRows,
507  100.0 * NumGlobalEmptyRows / NumGlobalRows);
508  print<int>("Dirichlet rows", NumGlobalDirichletRows,
509  100.0 * NumGlobalDirichletRows / NumGlobalRows);
510  print<int>("Diagonally dominant rows", GlobalDiagonallyDominant,
511  100.0 * GlobalDiagonallyDominant / NumGlobalRows);
512  print<int>("Weakly diag. dominant rows",
513  GlobalWeaklyDiagonallyDominant,
514  100.0 * GlobalWeaklyDiagonallyDominant / NumGlobalRows);
515  print();
516  print<int>("Maximum bandwidth", GlobalBandwidth);
517 
518  print();
519  print("", "one-norm", "inf-norm", "Frobenius", false);
520  print("", "========", "========", "=========", false);
521  print();
522 
523  print<double>("A", NormOne, NormInf, NormF);
524  }
525 
526  if (Cheap == false) {
527 
528  // create A + A^T and A - A^T
529 
530  Epetra_FECrsMatrix AplusAT(Copy, A.RowMatrixRowMap(), 0);
531  Epetra_FECrsMatrix AminusAT(Copy, A.RowMatrixRowMap(), 0);
532 
533  for (int i = 0 ; i < NumMyRows ; ++i) {
534 
535  int GRID = A.RowMatrixRowMap().GID(i);
536  assert (GRID != -1);
537 
538  int Nnz;
539  IFPACK_CHK_ERR(A.ExtractMyRowCopy(i,A.MaxNumEntries(),Nnz,
540  &colVal[0],&colInd[0]));
541 
542  for (int j = 0 ; j < Nnz ; ++j) {
543 
544  int GCID = A.RowMatrixColMap().GID(colInd[j]);
545  assert (GCID != -1);
546 
547  double plus_val = colVal[j];
548  double minus_val = -colVal[j];
549 
550  if (AplusAT.SumIntoGlobalValues(1,&GRID,1,&GCID,&plus_val) != 0) {
551  IFPACK_CHK_ERR(AplusAT.InsertGlobalValues(1,&GRID,1,&GCID,&plus_val));
552  }
553 
554  if (AplusAT.SumIntoGlobalValues(1,&GCID,1,&GRID,&plus_val) != 0) {
555  IFPACK_CHK_ERR(AplusAT.InsertGlobalValues(1,&GCID,1,&GRID,&plus_val));
556  }
557 
558  if (AminusAT.SumIntoGlobalValues(1,&GRID,1,&GCID,&plus_val) != 0) {
559  IFPACK_CHK_ERR(AminusAT.InsertGlobalValues(1,&GRID,1,&GCID,&plus_val));
560  }
561 
562  if (AminusAT.SumIntoGlobalValues(1,&GCID,1,&GRID,&minus_val) != 0) {
563  IFPACK_CHK_ERR(AminusAT.InsertGlobalValues(1,&GCID,1,&GRID,&minus_val));
564  }
565 
566  }
567  }
568 
569  AplusAT.FillComplete();
570  AminusAT.FillComplete();
571 
572  AplusAT.Scale(0.5);
573  AminusAT.Scale(0.5);
574 
575  NormOne = AplusAT.NormOne();
576  NormInf = AplusAT.NormInf();
577  NormF = Ifpack_FrobeniusNorm(AplusAT);
578 
579  if (verbose) {
580  print<double>("A + A^T", NormOne, NormInf, NormF);
581  }
582 
583  NormOne = AminusAT.NormOne();
584  NormInf = AminusAT.NormInf();
585  NormF = Ifpack_FrobeniusNorm(AminusAT);
586 
587  if (verbose) {
588  print<double>("A - A^T", NormOne, NormInf, NormF);
589  }
590  }
591 
592  if (verbose) {
593  print();
594  print<const char*>("", "min", "avg", "max", false);
595  print<const char*>("", "===", "===", "===", false);
596  }
597 
598  MyMax = -DBL_MAX;
599  MyMin = DBL_MAX;
600  MyAvg = 0.0;
601 
602  for (int i = 0 ; i < NumMyRows ; ++i) {
603 
604  int Nnz;
605  IFPACK_CHK_ERR(A.ExtractMyRowCopy(i,A.MaxNumEntries(),Nnz,
606  &colVal[0],&colInd[0]));
607 
608  for (int j = 0 ; j < Nnz ; ++j) {
609  MyAvg += colVal[j];
610  if (colVal[j] > MyMax) MyMax = colVal[j];
611  if (colVal[j] < MyMin) MyMin = colVal[j];
612  }
613  }
614 
615  A.Comm().MaxAll(&MyMax, &GlobalMax, 1);
616  A.Comm().MinAll(&MyMin, &GlobalMin, 1);
617  A.Comm().SumAll(&MyAvg, &GlobalAvg, 1);
618  GlobalAvg /= A.NumGlobalNonzeros();
619 
620  if (verbose) {
621  print();
622  print<double>(" A(i,j)", GlobalMin, GlobalAvg, GlobalMax);
623  }
624 
625  MyMax = 0.0;
626  MyMin = DBL_MAX;
627  MyAvg = 0.0;
628 
629  for (int i = 0 ; i < NumMyRows ; ++i) {
630 
631  int Nnz;
632  IFPACK_CHK_ERR(A.ExtractMyRowCopy(i,A.MaxNumEntries(),Nnz,
633  &colVal[0],&colInd[0]));
634 
635  for (int j = 0 ; j < Nnz ; ++j) {
636  double v = colVal[j];
637  if (v < 0) v = -v;
638  MyAvg += v;
639  if (colVal[j] > MyMax) MyMax = v;
640  if (colVal[j] < MyMin) MyMin = v;
641  }
642  }
643 
644  A.Comm().MaxAll(&MyMax, &GlobalMax, 1);
645  A.Comm().MinAll(&MyMin, &GlobalMin, 1);
646  A.Comm().SumAll(&MyAvg, &GlobalAvg, 1);
647  GlobalAvg /= A.NumGlobalNonzeros();
648 
649  if (verbose) {
650  print<double>("|A(i,j)|", GlobalMin, GlobalAvg, GlobalMax);
651  }
652 
653  // ================= //
654  // diagonal elements //
655  // ================= //
656 
657  Diag.MinValue(&GlobalMin);
658  Diag.MaxValue(&GlobalMax);
659  Diag.MeanValue(&GlobalAvg);
660 
661  if (verbose) {
662  print();
663  print<double>(" A(k,k)", GlobalMin, GlobalAvg, GlobalMax);
664  }
665 
666  Diag.Abs(Diag);
667  Diag.MinValue(&GlobalMin);
668  Diag.MaxValue(&GlobalMax);
669  Diag.MeanValue(&GlobalAvg);
670  if (verbose) {
671  print<double>("|A(k,k)|", GlobalMin, GlobalAvg, GlobalMax);
672  }
673 
674  // ============================================== //
675  // cycle over all equations for diagonal elements //
676  // ============================================== //
677 
678  if (NumPDEEqns > 1 ) {
679 
680  if (verbose) print();
681 
682  for (int ie = 0 ; ie < NumPDEEqns ; ie++) {
683 
684  MyMin = DBL_MAX;
685  MyMax = -DBL_MAX;
686  MyAvg = 0.0;
687 
688  for (int i = ie ; i < Diag.MyLength() ; i += NumPDEEqns) {
689  double d = Diag[i];
690  MyAvg += d;
691  if (d < MyMin)
692  MyMin = d;
693  if (d > MyMax)
694  MyMax = d;
695  }
696  A.Comm().MinAll(&MyMin, &GlobalMin, 1);
697  A.Comm().MaxAll(&MyMax, &GlobalMax, 1);
698  A.Comm().SumAll(&MyAvg, &GlobalAvg, 1);
699  // does not really work fine if the number of global
700  // elements is not a multiple of NumPDEEqns
701  GlobalAvg /= (Diag.GlobalLength() / NumPDEEqns);
702 
703  if (verbose) {
704  char str[80];
705  sprintf(str, " A(k,k), eq %d", ie);
706  print<double>(str, GlobalMin, GlobalAvg, GlobalMax);
707  }
708  }
709  }
710 
711  // ======== //
712  // row sums //
713  // ======== //
714 
715  RowSum.MinValue(&GlobalMin);
716  RowSum.MaxValue(&GlobalMax);
717  RowSum.MeanValue(&GlobalAvg);
718 
719  if (verbose) {
720  print();
721  print<double>(" sum_j A(k,j)", GlobalMin, GlobalAvg, GlobalMax);
722  }
723 
724  // ===================================== //
725  // cycle over all equations for row sums //
726  // ===================================== //
727 
728  if (NumPDEEqns > 1 ) {
729 
730  if (verbose) print();
731 
732  for (int ie = 0 ; ie < NumPDEEqns ; ie++) {
733 
734  MyMin = DBL_MAX;
735  MyMax = -DBL_MAX;
736  MyAvg = 0.0;
737 
738  for (int i = ie ; i < Diag.MyLength() ; i += NumPDEEqns) {
739  double d = RowSum[i];
740  MyAvg += d;
741  if (d < MyMin)
742  MyMin = d;
743  if (d > MyMax)
744  MyMax = d;
745  }
746  A.Comm().MinAll(&MyMin, &GlobalMin, 1);
747  A.Comm().MaxAll(&MyMax, &GlobalMax, 1);
748  A.Comm().SumAll(&MyAvg, &GlobalAvg, 1);
749  // does not really work fine if the number of global
750  // elements is not a multiple of NumPDEEqns
751  GlobalAvg /= (Diag.GlobalLength() / NumPDEEqns);
752 
753  if (verbose) {
754  char str[80];
755  sprintf(str, " sum_j A(k,j), eq %d", ie);
756  print<double>(str, GlobalMin, GlobalAvg, GlobalMax);
757  }
758  }
759  }
760 
761  if (verbose)
763 
764  return(0);
765 }
766 
767 int Ifpack_AnalyzeVectorElements(const Epetra_Vector& Diagonal,
768  const bool abs, const int steps)
769 {
770 
771  bool verbose = (Diagonal.Comm().MyPID() == 0);
772  double min_val = DBL_MAX;
773  double max_val = -DBL_MAX;
774 
775  for (int i = 0 ; i < Diagonal.MyLength() ; ++i) {
776  double v = Diagonal[i];
777  if (abs)
778  if (v < 0) v = -v;
779  if (v > max_val)
780  max_val = v;
781  if (v < min_val)
782  min_val = v;
783  }
784 
785  if (verbose) {
786  cout << endl;
788  cout << "Vector label = " << Diagonal.Label() << endl;
789  cout << endl;
790  }
791 
792  double delta = (max_val - min_val) / steps;
793  for (int k = 0 ; k < steps ; ++k) {
794 
795  double below = delta * k + min_val;
796  double above = below + delta;
797  int MyBelow = 0, GlobalBelow;
798 
799  for (int i = 0 ; i < Diagonal.MyLength() ; ++i) {
800  double v = Diagonal[i];
801  if (v < 0) v = -v;
802  if (v >= below && v < above) MyBelow++;
803  }
804 
805  Diagonal.Comm().SumAll(&MyBelow, &GlobalBelow, 1);
806 
807  if (verbose) {
808  printf("Elements in [%+7e, %+7e) = %10d ( = %5.2f %%)\n",
809  below, above, GlobalBelow,
810  100.0 * GlobalBelow / Diagonal.GlobalLength());
811  }
812  }
813 
814  if (verbose) {
816  cout << endl;
817  }
818 
819  return(0);
820 }
821 
822 // ======================================================================
823 
824 int Ifpack_AnalyzeMatrixElements(const Epetra_RowMatrix& A,
825  const bool abs, const int steps)
826 {
827 
828  bool verbose = (A.Comm().MyPID() == 0);
829  double min_val = DBL_MAX;
830  double max_val = -DBL_MAX;
831 
832  vector<int> colInd(A.MaxNumEntries());
833  vector<double> colVal(A.MaxNumEntries());
834 
835  for (int i = 0 ; i < A.NumMyRows() ; ++i) {
836 
837  int Nnz;
838  IFPACK_CHK_ERR(A.ExtractMyRowCopy(i,A.MaxNumEntries(),Nnz,
839  &colVal[0],&colInd[0]));
840 
841  for (int j = 0 ; j < Nnz ; ++j) {
842  double v = colVal[j];
843  if (abs)
844  if (v < 0) v = -v;
845  if (v < min_val)
846  min_val = v;
847  if (v > max_val)
848  max_val = v;
849  }
850  }
851 
852  if (verbose) {
853  cout << endl;
855  cout << "Label of matrix = " << A.Label() << endl;
856  cout << endl;
857  }
858 
859  double delta = (max_val - min_val) / steps;
860  for (int k = 0 ; k < steps ; ++k) {
861 
862  double below = delta * k + min_val;
863  double above = below + delta;
864  int MyBelow = 0, GlobalBelow;
865 
866  for (int i = 0 ; i < A.NumMyRows() ; ++i) {
867 
868  int Nnz;
869  IFPACK_CHK_ERR(A.ExtractMyRowCopy(i,A.MaxNumEntries(),Nnz,
870  &colVal[0],&colInd[0]));
871 
872  for (int j = 0 ; j < Nnz ; ++j) {
873  double v = colVal[j];
874  if (abs)
875  if (v < 0) v = -v;
876  if (v >= below && v < above) MyBelow++;
877  }
878 
879  }
880  A.Comm().SumAll(&MyBelow, &GlobalBelow, 1);
881  if (verbose) {
882  printf("Elements in [%+7e, %+7e) = %10d ( = %5.2f %%)\n",
883  below, above, GlobalBelow,
884  100.0 * GlobalBelow / A.NumGlobalNonzeros());
885  }
886  }
887 
888  if (verbose) {
890  cout << endl;
891  }
892 
893  return(0);
894 }
895 
896 // ======================================================================
897 int Ifpack_PrintSparsity(const Epetra_RowMatrix& A, const char* InputFileName,
898  const int NumPDEEqns)
899 {
900 
901  int m,nc,nr,maxdim,ltit;
902  double lrmrgn,botmrgn,xtit,ytit,ytitof,fnstit,siz = 0.0;
903  double xl,xr, yb,yt, scfct,u2dot,frlw,delt,paperx;
904  bool square = false;
905  /*change square to .true. if you prefer a square frame around
906  a rectangular matrix */
907  double conv = 2.54;
908  char munt = 'E'; /* put 'E' for centimeters, 'U' for inches */
909  int ptitle = 0; /* position of the title, 0 under the drawing,
910  else above */
911  FILE* fp = NULL;
912  int NumMyRows;
913  //int NumMyCols;
914  int NumGlobalRows;
915  int NumGlobalCols;
916  int MyPID;
917  int NumProc;
918  char FileName[1024];
919  char title[1024];
920 
921  const Epetra_Comm& Comm = A.Comm();
922 
923  /* --------------------- execution begins ---------------------- */
924 
925  if (strlen(A.Label()) != 0)
926  strcpy(title, A.Label());
927  else
928  sprintf(title, "%s", "matrix");
929 
930  if (InputFileName == 0)
931  sprintf(FileName, "%s.ps", title);
932  else
933  strcpy(FileName, InputFileName);
934 
935  MyPID = Comm.MyPID();
936  NumProc = Comm.NumProc();
937 
938  NumMyRows = A.NumMyRows();
939  //NumMyCols = A.NumMyCols();
940 
941  NumGlobalRows = A.NumGlobalRows();
942  NumGlobalCols = A.NumGlobalCols();
943 
944  if (NumGlobalRows != NumGlobalCols)
945  IFPACK_CHK_ERR(-1); // never tested
946 
947  /* to be changed for rect matrices */
948  maxdim = (NumGlobalRows>NumGlobalCols)?NumGlobalRows:NumGlobalCols;
949  maxdim /= NumPDEEqns;
950 
951  m = 1 + maxdim;
952  nr = NumGlobalRows / NumPDEEqns + 1;
953  nc = NumGlobalCols / NumPDEEqns + 1;
954 
955  if (munt == 'E') {
956  u2dot = 72.0/conv;
957  paperx = 21.0;
958  siz = 10.0;
959  }
960  else {
961  u2dot = 72.0;
962  paperx = 8.5*conv;
963  siz = siz*conv;
964  }
965 
966  /* left and right margins (drawing is centered) */
967 
968  lrmrgn = (paperx-siz)/2.0;
969 
970  /* bottom margin : 2 cm */
971 
972  botmrgn = 2.0;
973  /* c scaling factor */
974  scfct = siz*u2dot/m;
975  /* matrix frame line witdh */
976  frlw = 0.25;
977  /* font size for title (cm) */
978  fnstit = 0.5;
979  /* mfh 23 Jan 2013: title is always nonnull, since it's an array of
980  fixed nonzero length. The 'if' test thus results in a compiler
981  warning. */
982  /*if (title) ltit = strlen(title);*/
983  /*else ltit = 0;*/
984  ltit = strlen(title);
985 
986  /* position of title : centered horizontally */
987  /* at 1.0 cm vertically over the drawing */
988  ytitof = 1.0;
989  xtit = paperx/2.0;
990  ytit = botmrgn+siz*nr/m + ytitof;
991  /* almost exact bounding box */
992  xl = lrmrgn*u2dot - scfct*frlw/2;
993  xr = (lrmrgn+siz)*u2dot + scfct*frlw/2;
994  yb = botmrgn*u2dot - scfct*frlw/2;
995  yt = (botmrgn+siz*nr/m)*u2dot + scfct*frlw/2;
996  if (ltit == 0) {
997  yt = yt + (ytitof+fnstit*0.70)*u2dot;
998  }
999  /* add some room to bounding box */
1000  delt = 10.0;
1001  xl = xl-delt;
1002  xr = xr+delt;
1003  yb = yb-delt;
1004  yt = yt+delt;
1005 
1006  /* correction for title under the drawing */
1007  if ((ptitle == 0) && (ltit == 0)) {
1008  ytit = botmrgn + fnstit*0.3;
1009  botmrgn = botmrgn + ytitof + fnstit*0.7;
1010  }
1011 
1012  /* begin of output */
1013 
1014  if (MyPID == 0) {
1015 
1016  fp = fopen(FileName,"w");
1017 
1018  fprintf(fp,"%s","%%!PS-Adobe-2.0\n");
1019  fprintf(fp,"%s","%%Creator: IFPACK\n");
1020  fprintf(fp,"%%%%BoundingBox: %f %f %f %f\n",
1021  xl,yb,xr,yt);
1022  fprintf(fp,"%s","%%EndComments\n");
1023  fprintf(fp,"%s","/cm {72 mul 2.54 div} def\n");
1024  fprintf(fp,"%s","/mc {72 div 2.54 mul} def\n");
1025  fprintf(fp,"%s","/pnum { 72 div 2.54 mul 20 string ");
1026  fprintf(fp,"%s","cvs print ( ) print} def\n");
1027  fprintf(fp,"%s","/Cshow {dup stringwidth pop -2 div 0 rmoveto show} def\n");
1028 
1029  /* we leave margins etc. in cm so it is easy to modify them if
1030  needed by editing the output file */
1031  fprintf(fp,"%s","gsave\n");
1032  if (ltit != 0) {
1033  fprintf(fp,"/Helvetica findfont %e cm scalefont setfont\n",
1034  fnstit);
1035  fprintf(fp,"%f cm %f cm moveto\n",
1036  xtit,ytit);
1037  fprintf(fp,"(%s) Cshow\n", title);
1038  fprintf(fp,"%f cm %f cm translate\n",
1039  lrmrgn,botmrgn);
1040  }
1041  fprintf(fp,"%f cm %d div dup scale \n",
1042  siz,m);
1043  /* draw a frame around the matrix */
1044 
1045  fprintf(fp,"%f setlinewidth\n",
1046  frlw);
1047  fprintf(fp,"%s","newpath\n");
1048  fprintf(fp,"%s","0 0 moveto ");
1049  if (square) {
1050  printf("------------------- %d\n",m);
1051  fprintf(fp,"%d %d lineto\n",
1052  m,0);
1053  fprintf(fp,"%d %d lineto\n",
1054  m, m);
1055  fprintf(fp,"%d %d lineto\n",
1056  0, m);
1057  }
1058  else {
1059  fprintf(fp,"%d %d lineto\n",
1060  nc, 0);
1061  fprintf(fp,"%d %d lineto\n",
1062  nc, nr);
1063  fprintf(fp,"%d %d lineto\n",
1064  0, nr);
1065  }
1066  fprintf(fp,"%s","closepath stroke\n");
1067 
1068  /* plotting loop */
1069 
1070  fprintf(fp,"%s","1 1 translate\n");
1071  fprintf(fp,"%s","0.8 setlinewidth\n");
1072  fprintf(fp,"%s","/p {moveto 0 -.40 rmoveto \n");
1073  fprintf(fp,"%s"," 0 .80 rlineto stroke} def\n");
1074 
1075  fclose(fp);
1076  }
1077 
1078  int MaxEntries = A.MaxNumEntries();
1079  vector<int> Indices(MaxEntries);
1080  vector<double> Values(MaxEntries);
1081 
1082  for (int pid = 0 ; pid < NumProc ; ++pid) {
1083 
1084  if (pid == MyPID) {
1085 
1086  fp = fopen(FileName,"a");
1087  if( fp == NULL ) {
1088  fprintf(stderr,"%s","ERROR\n");
1089  exit(EXIT_FAILURE);
1090  }
1091 
1092  for (int i = 0 ; i < NumMyRows ; ++i) {
1093 
1094  if (i % NumPDEEqns) continue;
1095 
1096  int Nnz;
1097  A.ExtractMyRowCopy(i,MaxEntries,Nnz,&Values[0],&Indices[0]);
1098 
1099  int grow = A.RowMatrixRowMap().GID(i);
1100 
1101  for (int j = 0 ; j < Nnz ; ++j) {
1102  int col = Indices[j];
1103  if (col % NumPDEEqns == 0) {
1104  int gcol = A.RowMatrixColMap().GID(Indices[j]);
1105  grow /= NumPDEEqns;
1106  gcol /= NumPDEEqns;
1107  fprintf(fp,"%d %d p\n",
1108  gcol, NumGlobalRows - grow - 1);
1109  }
1110  }
1111  }
1112 
1113  fprintf(fp,"%s","%end of data for this process\n");
1114 
1115  if( pid == NumProc - 1 )
1116  fprintf(fp,"%s","showpage\n");
1117 
1118  fclose(fp);
1119  }
1120  Comm.Barrier();
1121  }
1122 
1123  return(0);
1124 }
int Ifpack_Analyze(const Epetra_RowMatrix &A, const bool Cheap=false, const int NumPDEEqns=1)
Analyzes the basic properties of the input matrix A; see Usage of Ifpack_Analyze()..
string Ifpack_toString(const int &x)
Convertes an integer to string.
int Ifpack_AnalyzeVectorElements(const Epetra_Vector &Diagonal, const bool abs=false, const int steps=10)
Analyzes the distribution of values of the input vector Diagonal.
int Ifpack_AnalyzeMatrixElements(const Epetra_RowMatrix &A, const bool abs=false, const int steps=10)
Analyzes the distribution of values of the input matrix A.
void Ifpack_PrintLine()
Prints a line of `=&#39; on cout.
Epetra_CrsMatrix * Ifpack_CreateOverlappingCrsMatrix(const Epetra_RowMatrix *Matrix, const int OverlappingLevel)
Creates an overlapping Epetra_CrsMatrix. Returns 0 if OverlappingLevel is 0.
void Ifpack_BreakForDebugger(Epetra_Comm &Comm)
Stops the execution of code, so that a debugger can be attached.
int Ifpack_PrintResidual(char *Label, const Epetra_RowMatrix &A, const Epetra_MultiVector &X, const Epetra_MultiVector &Y)
Prints on cout the true residual.