From: Stefano Zampini Date: Thu, 10 Nov 2022 16:54:47 +0000 (+0100) Subject: PETScWrappers::MatrixBase remove improper usage of WORLD X-Git-Tag: v9.5.0-rc1~717^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a71d9fd00e2ea340bfb057b5af9ecbb17eb19ad6;p=dealii.git PETScWrappers::MatrixBase remove improper usage of WORLD support printing a matrix that does not implement MatGetRow --- diff --git a/source/lac/petsc_matrix_base.cc b/source/lac/petsc_matrix_base.cc index c8a26155b8..6bcede476f 100644 --- a/source/lac/petsc_matrix_base.cc +++ b/source/lac/petsc_matrix_base.cc @@ -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 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()); }