]> https://gitweb.dealii.org/ - dealii.git/commitdiff
PETScWrappers::MatrixBase remove improper usage of WORLD
authorStefano Zampini <stefano.zampini@gmail.com>
Thu, 10 Nov 2022 16:54:47 +0000 (17:54 +0100)
committerLuca Heltai <luca.heltai@sissa.it>
Tue, 20 Dec 2022 09:53:09 +0000 (10:53 +0100)
support printing a matrix that does not implement MatGetRow

source/lac/petsc_matrix_base.cc

index c8a26155b8af19f8bb83938b566f596b989e61d3..6bcede476fd0e8da70fb62668fb530ce3228dc5a 100644 (file)
@@ -639,20 +639,33 @@ namespace PETScWrappers
   MatrixBase::write_ascii(const PetscViewerFormat format)
   {
     assert_is_compressed();
+    MPI_Comm comm = PetscObjectComm((PetscObject)matrix);
 
     // Set options
     PetscErrorCode ierr =
-      PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD, format);
+      PetscViewerSetFormat(PETSC_VIEWER_STDOUT_(comm), format);
     AssertThrow(ierr == 0, ExcPETScError(ierr));
 
     // Write to screen
-    ierr = MatView(matrix, PETSC_VIEWER_STDOUT_WORLD);
+    ierr = MatView(matrix, PETSC_VIEWER_STDOUT_(comm));
     AssertThrow(ierr == 0, ExcPETScError(ierr));
   }
 
   void
   MatrixBase::print(std::ostream &out, const bool /*alternative_output*/) const
   {
+    PetscBool has;
+
+    PetscErrorCode ierr = MatHasOperation(matrix, MATOP_GET_ROW, &has);
+    AssertThrow(ierr == 0, ExcPETScError(ierr));
+
+    Mat vmatrix = matrix;
+    if (!has)
+      {
+        ierr = MatConvert(matrix, MATAIJ, MAT_INITIAL_MATRIX, &vmatrix);
+        AssertThrow(ierr == 0, ExcPETScError(ierr));
+      }
+
     std::pair<MatrixBase::size_type, MatrixBase::size_type> loc_range =
       local_range();
 
@@ -663,7 +676,7 @@ namespace PETScWrappers
     MatrixBase::size_type row;
     for (row = loc_range.first; row < loc_range.second; ++row)
       {
-        PetscErrorCode ierr = MatGetRow(*this, row, &ncols, &colnums, &values);
+        ierr = MatGetRow(vmatrix, row, &ncols, &colnums, &values);
         AssertThrow(ierr == 0, ExcPETScError(ierr));
 
         for (PetscInt col = 0; col < ncols; ++col)
@@ -672,10 +685,14 @@ namespace PETScWrappers
                 << std::endl;
           }
 
-        ierr = MatRestoreRow(*this, row, &ncols, &colnums, &values);
+        ierr = MatRestoreRow(vmatrix, row, &ncols, &colnums, &values);
+        AssertThrow(ierr == 0, ExcPETScError(ierr));
+      }
+    if (vmatrix != matrix)
+      {
+        ierr = PETScWrappers::destroy_matrix(vmatrix);
         AssertThrow(ierr == 0, ExcPETScError(ierr));
       }
-
     AssertThrow(out.fail() == false, ExcIO());
   }
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.