Skip to content
Snippets Groups Projects
Commit 94c12fed authored by Jonathan Lambrechts's avatar Jonathan Lambrechts
Browse files

petsc : clean backward compatibility

parent a395cede
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,9 @@ ...@@ -9,6 +9,9 @@
#if defined(HAVE_SLEPC) #if defined(HAVE_SLEPC)
#include <slepceps.h> #include <slepceps.h>
#if SLEPC_VERSION_RELEASE != 0 && (SLEPC_VERSION_MAJOR < 3 || (SLEPC_VERSION_MAJOR == 3 && SLEPC_VERSION_MINOR < 2))
#define EPSDestroy(e) EPSDestroy(*(e))
#endif
void eigenSolver::_try(int ierr) const void eigenSolver::_try(int ierr) const
{ {
...@@ -80,7 +83,11 @@ bool eigenSolver::solve(int numEigenValues, std::string which) ...@@ -80,7 +83,11 @@ bool eigenSolver::solve(int numEigenValues, std::string which)
_try(EPSSetWhichEigenpairs(eps, EPS_LARGEST_MAGNITUDE)); _try(EPSSetWhichEigenpairs(eps, EPS_LARGEST_MAGNITUDE));
// print info // print info
#if (SLEPC_VERSION_RELEASE == 0 || (SLEPC_VERSION_MAJOR > 3 || (SLEPC_VERSION_MAJOR == 3 && SLEPC_VERSION_MINOR >= 4)))
EPSType type;
#else
const EPSType type; const EPSType type;
#endif
_try(EPSGetType(eps, &type)); _try(EPSGetType(eps, &type));
Msg::Debug("SLEPc solution method: %s", type); Msg::Debug("SLEPc solution method: %s", type);
...@@ -110,7 +117,7 @@ bool eigenSolver::solve(int numEigenValues, std::string which) ...@@ -110,7 +117,7 @@ bool eigenSolver::solve(int numEigenValues, std::string which)
Msg::Error("SLEPc diverged after %d iterations", its); Msg::Error("SLEPc diverged after %d iterations", its);
else if(reason == EPS_DIVERGED_BREAKDOWN) else if(reason == EPS_DIVERGED_BREAKDOWN)
Msg::Error("SLEPc generic breakdown in method"); Msg::Error("SLEPc generic breakdown in method");
#if !(PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2))) #if (SLEPC_VERSION_MAJOR < 3 || (SLEPC_VERSION_MAJOR == 3 && SLEPC_VERSION_MINOR < 2))
else if(reason == EPS_DIVERGED_NONSYMMETRIC) else if(reason == EPS_DIVERGED_NONSYMMETRIC)
Msg::Error("The operator is nonsymmetric"); Msg::Error("The operator is nonsymmetric");
#endif #endif
...@@ -159,20 +166,11 @@ bool eigenSolver::solve(int numEigenValues, std::string which) ...@@ -159,20 +166,11 @@ bool eigenSolver::solve(int numEigenValues, std::string which)
} }
_eigenVectors.push_back(ev); _eigenVectors.push_back(ev);
} }
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
_try(VecDestroy(&xr)); _try(VecDestroy(&xr));
_try(VecDestroy(&xi)); _try(VecDestroy(&xi));
#else
_try(VecDestroy(xr));
_try(VecDestroy(xi));
#endif
} }
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
_try(EPSDestroy(&eps)); _try(EPSDestroy(&eps));
#else
_try(EPSDestroy(eps));
#endif
if(reason == EPS_CONVERGED_TOL){ if(reason == EPS_CONVERGED_TOL){
Msg::Debug("SLEPc done"); Msg::Debug("SLEPc done");
......
...@@ -29,7 +29,6 @@ void linearSystemPETScBlockDouble::_kspCreate() ...@@ -29,7 +29,6 @@ void linearSystemPETScBlockDouble::_kspCreate()
linearSystemPETScBlockDouble::~linearSystemPETScBlockDouble() linearSystemPETScBlockDouble::~linearSystemPETScBlockDouble()
{ {
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
if (_isAllocated) { if (_isAllocated) {
MatDestroy(&_a); MatDestroy(&_a);
VecDestroy(&_b); VecDestroy(&_b);
...@@ -38,16 +37,6 @@ linearSystemPETScBlockDouble::~linearSystemPETScBlockDouble() ...@@ -38,16 +37,6 @@ linearSystemPETScBlockDouble::~linearSystemPETScBlockDouble()
if (_kspAllocated) { if (_kspAllocated) {
KSPDestroy(&_ksp); KSPDestroy(&_ksp);
} }
#else
if (_isAllocated) {
MatDestroy(_a);
VecDestroy(_b);
VecDestroy(_x);
}
if (_kspAllocated) {
KSPDestroy(_ksp);
}
#endif
} }
void linearSystemPETScBlockDouble::addToMatrix(int row, int col, void linearSystemPETScBlockDouble::addToMatrix(int row, int col,
...@@ -202,15 +191,9 @@ bool linearSystemPETScBlockDouble::isAllocated() const ...@@ -202,15 +191,9 @@ bool linearSystemPETScBlockDouble::isAllocated() const
void linearSystemPETScBlockDouble::clear() void linearSystemPETScBlockDouble::clear()
{ {
if(_isAllocated){ if(_isAllocated){
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
MatDestroy(&_a); MatDestroy(&_a);
VecDestroy(&_x); VecDestroy(&_x);
VecDestroy(&_b); VecDestroy(&_b);
#else
MatDestroy(_a);
VecDestroy(_x);
VecDestroy(_b);
#endif
} }
_isAllocated = false; _isAllocated = false;
} }
...@@ -265,7 +248,7 @@ void linearSystemPETScBlockDouble::preAllocateEntries() ...@@ -265,7 +248,7 @@ void linearSystemPETScBlockDouble::preAllocateEntries()
if (!_isAllocated) Msg::Fatal("system must be allocated first"); if (!_isAllocated) Msg::Fatal("system must be allocated first");
if (_sparsity.getNbRows() == 0) { if (_sparsity.getNbRows() == 0) {
PetscInt prealloc = 300; PetscInt prealloc = 300;
PetscTruth set; PetscBool set;
PetscOptionsGetInt(PETSC_NULL, "-petsc_prealloc", &prealloc, &set); PetscOptionsGetInt(PETSC_NULL, "-petsc_prealloc", &prealloc, &set);
if (_blockSize == 0) { if (_blockSize == 0) {
MatSeqAIJSetPreallocation(_a, prealloc, PETSC_NULL); MatSeqAIJSetPreallocation(_a, prealloc, PETSC_NULL);
...@@ -351,13 +334,8 @@ void linearSystemPETScBlockDouble::printMatlab(const char *filename) const ...@@ -351,13 +334,8 @@ void linearSystemPETScBlockDouble::printMatlab(const char *filename) const
PetscViewer viewer; PetscViewer viewer;
PetscViewerASCIIOpen(PETSC_COMM_WORLD, filename, &viewer); PetscViewerASCIIOpen(PETSC_COMM_WORLD, filename, &viewer);
PetscViewerSetFormat(viewer,PETSC_VIEWER_ASCII_MATLAB); PetscViewerSetFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
printf("export mat to %s\n", filename);
MatView(_a, viewer); MatView(_a, viewer);
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
PetscViewerDestroy(&viewer); PetscViewerDestroy(&viewer);
#else
PetscViewerDestroy(viewer);
#endif
return; return;
} }
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
#include "fullMatrix.h" #include "fullMatrix.h"
#include <vector> #include <vector>
#if defined(HAVE_PETSC) #if defined(HAVE_PETSC)
#ifndef SWIG #ifndef SWIG
#include "petsc.h" #include "petsc.h"
#include "petscksp.h" #include "petscksp.h"
...@@ -50,6 +51,17 @@ typedef struct _p_Vec* Vec; ...@@ -50,6 +51,17 @@ typedef struct _p_Vec* Vec;
typedef struct _p_KSP* KSP; typedef struct _p_KSP* KSP;
#endif #endif
//support old PETSc version, try to avoid using PETSC_VERSION somewhere else
#if PETSC_VERSION_RELEASE != 0 && (PETSC_VERSION_MAJOR < 3 || (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2))
#define KSPDestroy(k) KSPDestroy(*(k))
#define MatDestroy(m) MatDestroy(*(m))
#define VecDestroy(v) VecDestroy(*(v))
#define PetscViewerDestroy(v) PetscViewerDestroy(*(v))
#define PetscBool PetscTruth
#define PetscOptionsGetBool PetscOptionsGetTruth
#endif
template <class scalar> template <class scalar>
class linearSystemPETSc : public linearSystem<scalar> { class linearSystemPETSc : public linearSystem<scalar> {
protected: protected:
......
...@@ -3,10 +3,6 @@ ...@@ -3,10 +3,6 @@
#include <petscksp.h> #include <petscksp.h>
#include "linearSystemPETSc.h" #include "linearSystemPETSc.h"
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
#define PetscTruth PetscBool
#define PetscOptionsGetTruth PetscOptionsGetBool
#endif
static void _try(int ierr) static void _try(int ierr)
{ {
...@@ -58,11 +54,7 @@ linearSystemPETSc<scalar>::~linearSystemPETSc() ...@@ -58,11 +54,7 @@ linearSystemPETSc<scalar>::~linearSystemPETSc()
{ {
clear(); clear();
if(_kspAllocated) if(_kspAllocated)
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
_try(KSPDestroy(&_ksp)); _try(KSPDestroy(&_ksp));
#else
_try(KSPDestroy(_ksp));
#endif
} }
template <class scalar> template <class scalar>
...@@ -80,7 +72,7 @@ void linearSystemPETSc<scalar>::preAllocateEntries() ...@@ -80,7 +72,7 @@ void linearSystemPETSc<scalar>::preAllocateEntries()
if (!_isAllocated) Msg::Fatal("system must be allocated first"); if (!_isAllocated) Msg::Fatal("system must be allocated first");
if (_sparsity.getNbRows() == 0) { if (_sparsity.getNbRows() == 0) {
PetscInt prealloc = 300; PetscInt prealloc = 300;
PetscTruth set; PetscBool set;
PetscOptionsGetInt(PETSC_NULL, "-petsc_prealloc", &prealloc, &set); PetscOptionsGetInt(PETSC_NULL, "-petsc_prealloc", &prealloc, &set);
if (_blockSize == 0) { if (_blockSize == 0) {
_try(MatSeqAIJSetPreallocation(_a, prealloc, PETSC_NULL)); _try(MatSeqAIJSetPreallocation(_a, prealloc, PETSC_NULL));
...@@ -195,15 +187,9 @@ template <class scalar> ...@@ -195,15 +187,9 @@ template <class scalar>
void linearSystemPETSc<scalar>::clear() void linearSystemPETSc<scalar>::clear()
{ {
if(_isAllocated){ if(_isAllocated){
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
_try(MatDestroy(&_a)); _try(MatDestroy(&_a));
_try(VecDestroy(&_x)); _try(VecDestroy(&_x));
_try(VecDestroy(&_b)); _try(VecDestroy(&_b));
#else
_try(MatDestroy(_a));
_try(VecDestroy(_x));
_try(VecDestroy(_b));
#endif
} }
_isAllocated = false; _isAllocated = false;
} }
...@@ -379,7 +365,7 @@ std::vector<int> linearSystemPETSc<scalar>::getRowPointers() ...@@ -379,7 +365,7 @@ std::vector<int> linearSystemPETSc<scalar>::getRowPointers()
const PetscInt *rows; const PetscInt *rows;
const PetscInt *columns; const PetscInt *columns;
PetscInt n; PetscInt n;
PetscTruth done; PetscBool done;
_try(MatGetRowIJ(_a,0,PETSC_FALSE,PETSC_FALSE,&n,&rows,&columns,&done)); //case done == PETSC_FALSE should be handled _try(MatGetRowIJ(_a,0,PETSC_FALSE,PETSC_FALSE,&n,&rows,&columns,&done)); //case done == PETSC_FALSE should be handled
std::vector<int> rowPointers; // Maybe I should reserve or resize (SAM) std::vector<int> rowPointers; // Maybe I should reserve or resize (SAM)
for (int i = 0; i <= n; i++) for (int i = 0; i <= n; i++)
...@@ -396,7 +382,7 @@ std::vector<int> linearSystemPETSc<scalar>::getColumnsIndices() ...@@ -396,7 +382,7 @@ std::vector<int> linearSystemPETSc<scalar>::getColumnsIndices()
const PetscInt *rows; const PetscInt *rows;
const PetscInt *columns; const PetscInt *columns;
PetscInt n; PetscInt n;
PetscTruth done; PetscBool done;
_try(MatGetRowIJ(_a,0,PETSC_FALSE,PETSC_FALSE,&n,&rows,&columns,&done)); //case done == PETSC_FALSE should be handled _try(MatGetRowIJ(_a,0,PETSC_FALSE,PETSC_FALSE,&n,&rows,&columns,&done)); //case done == PETSC_FALSE should be handled
MatInfo info; MatInfo info;
_try(MatGetInfo(_a,MAT_LOCAL,&info)); _try(MatGetInfo(_a,MAT_LOCAL,&info));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment