From 9f98cf8fe459c088760ab0b0cf7375829f8d0c8e Mon Sep 17 00:00:00 2001 From: Wenqing Wang <wenqing.wang@ufz.de> Date: Wed, 7 Jan 2015 13:49:32 +0100 Subject: [PATCH] [MaL] Small preparations for PETSc matrix/vector. Added linear solver time info. Matrix preallocation type. Change type of MatSetValues(). Collect finilize assembly calls in one place. Make PETScVector::viewer() a const method. --- MathLib/LinAlg/PETSc/PETScLinearSolver.h | 5 +++++ MathLib/LinAlg/PETSc/PETScMatrix.cpp | 3 +++ MathLib/LinAlg/PETSc/PETScMatrix.h | 4 +++- MathLib/LinAlg/PETSc/PETScTools.cpp | 4 ++++ MathLib/LinAlg/PETSc/PETScVector.cpp | 4 +--- MathLib/LinAlg/PETSc/PETScVector.h | 2 +- 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/MathLib/LinAlg/PETSc/PETScLinearSolver.h b/MathLib/LinAlg/PETSc/PETScLinearSolver.h index 1496df53a59..710194095eb 100644 --- a/MathLib/LinAlg/PETSc/PETScLinearSolver.h +++ b/MathLib/LinAlg/PETSc/PETScLinearSolver.h @@ -19,6 +19,8 @@ #include<string> +#include "logog/include/logog.hpp" + #include "petscksp.h" #include "PETScMatrix.h" @@ -47,6 +49,9 @@ class PETScLinearSolver ~PETScLinearSolver() { + INFO("info: Time elapsed in PETSc ksp solver for equation: %g s.\n", + _elapsed_ctime); + KSPDestroy(&_solver); } diff --git a/MathLib/LinAlg/PETSc/PETScMatrix.cpp b/MathLib/LinAlg/PETSc/PETScMatrix.cpp index 9b7d1dec0c4..5fa3c8cca8a 100644 --- a/MathLib/LinAlg/PETSc/PETScMatrix.cpp +++ b/MathLib/LinAlg/PETSc/PETScMatrix.cpp @@ -91,8 +91,11 @@ void PETScMatrix::create(const PetscInt d_nz, const PetscInt o_nz) MatSetFromOptions(_A); + MatSetType(_A, MATMPIAIJ); MatSeqAIJSetPreallocation(_A, d_nz, PETSC_NULL); MatMPIAIJSetPreallocation(_A, d_nz, PETSC_NULL, o_nz, PETSC_NULL); + // If pre-allocation does not work one can use MatSetUp(_A), which is much + // slower. MatGetOwnershipRange(_A, &_start_rank, &_end_rank); MatGetSize(_A, &_nrows, &_ncols); diff --git a/MathLib/LinAlg/PETSc/PETScMatrix.h b/MathLib/LinAlg/PETSc/PETScMatrix.h index b341a19c0ef..ee77b4e106d 100644 --- a/MathLib/LinAlg/PETSc/PETScMatrix.h +++ b/MathLib/LinAlg/PETSc/PETScMatrix.h @@ -21,6 +21,8 @@ #include "PETScMatrixOption.h" #include "PETScVector.h" +#include "MathLib/LinAlg/RowColumnIndices.h" + typedef Mat PETSc_Mat; namespace MathLib @@ -242,7 +244,7 @@ void PETScMatrix::add(std::vector<PetscInt> const& row_pos, const PetscInt nrows = static_cast<PetscInt> (row_pos.size()); const PetscInt ncols = static_cast<PetscInt> (col_pos.size()); - MatSetValues(_A, nrows, &row_pos[0], ncols, &col_pos[0], sub_mat.getEntries(), ADD_VALUES); + MatSetValues(_A, nrows, &row_pos[0], ncols, &col_pos[0], &sub_mat(0,0), ADD_VALUES); }; /*! diff --git a/MathLib/LinAlg/PETSc/PETScTools.cpp b/MathLib/LinAlg/PETSc/PETScTools.cpp index 00d5b775562..09beeb20daf 100644 --- a/MathLib/LinAlg/PETSc/PETScTools.cpp +++ b/MathLib/LinAlg/PETSc/PETScTools.cpp @@ -23,9 +23,13 @@ void applyKnownSolution(PETScMatrix &A, PETScVector &b, PETScVector &x, const std::vector<PetscInt> &vec_knownX_id, const std::vector<PetscScalar> &vec_knownX_x) { + A.finalizeAssembly(); + A.setRowsColumnsZero(vec_knownX_id); A.finalizeAssembly(); + x.finalizeAssembly(); + b.finalizeAssembly(); if(vec_knownX_id.size() > 0) { x.set(vec_knownX_id, vec_knownX_x); diff --git a/MathLib/LinAlg/PETSc/PETScVector.cpp b/MathLib/LinAlg/PETSc/PETScVector.cpp index 3f426550086..7669745f118 100644 --- a/MathLib/LinAlg/PETSc/PETScVector.cpp +++ b/MathLib/LinAlg/PETSc/PETScVector.cpp @@ -135,14 +135,12 @@ PetscScalar PETScVector::getNorm(MathLib::VecNormType nmtype) const return norm; } -void PETScVector::viewer(const std::string &file_name, const PetscViewerFormat vw_format) +void PETScVector::viewer(const std::string &file_name, const PetscViewerFormat vw_format) const { PetscViewer viewer; PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &viewer); PetscViewerPushFormat(viewer, vw_format); - finalizeAssembly(); - PetscObjectSetName((PetscObject)_v, file_name.c_str()); VecView(_v, viewer); diff --git a/MathLib/LinAlg/PETSc/PETScVector.h b/MathLib/LinAlg/PETSc/PETScVector.h index 351a77f51b5..33f6667961d 100644 --- a/MathLib/LinAlg/PETSc/PETScVector.h +++ b/MathLib/LinAlg/PETSc/PETScVector.h @@ -231,7 +231,7 @@ class PETScVector PETSC_VIEWER_DRAW_CONTOUR Views the vector with a contour plot */ void viewer(const std::string &file_name, - const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB ); + const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB ) const; private: PETSc_Vec _v; -- GitLab