From a71d9fd00e2ea340bfb057b5af9ecbb17eb19ad6 Mon Sep 17 00:00:00 2001 From: Stefano Zampini Date: Thu, 10 Nov 2022 17:54:47 +0100 Subject: [PATCH] PETScWrappers::MatrixBase remove improper usage of WORLD support printing a matrix that does not implement MatGetRow --- source/lac/petsc_matrix_base.cc | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) 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()); } -- 2.39.5