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 @@
#if defined(HAVE_SLEPC)
#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
{
......@@ -80,7 +83,11 @@ bool eigenSolver::solve(int numEigenValues, std::string which)
_try(EPSSetWhichEigenpairs(eps, EPS_LARGEST_MAGNITUDE));
// 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;
#endif
_try(EPSGetType(eps, &type));
Msg::Debug("SLEPc solution method: %s", type);
......@@ -110,7 +117,7 @@ bool eigenSolver::solve(int numEigenValues, std::string which)
Msg::Error("SLEPc diverged after %d iterations", its);
else if(reason == EPS_DIVERGED_BREAKDOWN)
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)
Msg::Error("The operator is nonsymmetric");
#endif
......@@ -159,20 +166,11 @@ bool eigenSolver::solve(int numEigenValues, std::string which)
}
_eigenVectors.push_back(ev);
}
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
_try(VecDestroy(&xr));
_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));
#else
_try(EPSDestroy(eps));
#endif
if(reason == EPS_CONVERGED_TOL){
Msg::Debug("SLEPc done");
......
......@@ -29,7 +29,6 @@ void linearSystemPETScBlockDouble::_kspCreate()
linearSystemPETScBlockDouble::~linearSystemPETScBlockDouble()
{
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
if (_isAllocated) {
MatDestroy(&_a);
VecDestroy(&_b);
......@@ -38,16 +37,6 @@ linearSystemPETScBlockDouble::~linearSystemPETScBlockDouble()
if (_kspAllocated) {
KSPDestroy(&_ksp);
}
#else
if (_isAllocated) {
MatDestroy(_a);
VecDestroy(_b);
VecDestroy(_x);
}
if (_kspAllocated) {
KSPDestroy(_ksp);
}
#endif
}
void linearSystemPETScBlockDouble::addToMatrix(int row, int col,
......@@ -202,15 +191,9 @@ bool linearSystemPETScBlockDouble::isAllocated() const
void linearSystemPETScBlockDouble::clear()
{
if(_isAllocated){
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
MatDestroy(&_a);
VecDestroy(&_x);
VecDestroy(&_b);
#else
MatDestroy(_a);
VecDestroy(_x);
VecDestroy(_b);
#endif
}
_isAllocated = false;
}
......@@ -265,7 +248,7 @@ void linearSystemPETScBlockDouble::preAllocateEntries()
if (!_isAllocated) Msg::Fatal("system must be allocated first");
if (_sparsity.getNbRows() == 0) {
PetscInt prealloc = 300;
PetscTruth set;
PetscBool set;
PetscOptionsGetInt(PETSC_NULL, "-petsc_prealloc", &prealloc, &set);
if (_blockSize == 0) {
MatSeqAIJSetPreallocation(_a, prealloc, PETSC_NULL);
......@@ -351,13 +334,8 @@ void linearSystemPETScBlockDouble::printMatlab(const char *filename) const
PetscViewer viewer;
PetscViewerASCIIOpen(PETSC_COMM_WORLD, filename, &viewer);
PetscViewerSetFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
printf("export mat to %s\n", filename);
MatView(_a, viewer);
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
PetscViewerDestroy(&viewer);
#else
PetscViewerDestroy(viewer);
#endif
return;
}
......
......@@ -41,6 +41,7 @@
#include "fullMatrix.h"
#include <vector>
#if defined(HAVE_PETSC)
#ifndef SWIG
#include "petsc.h"
#include "petscksp.h"
......@@ -50,6 +51,17 @@ typedef struct _p_Vec* Vec;
typedef struct _p_KSP* KSP;
#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>
class linearSystemPETSc : public linearSystem<scalar> {
protected:
......
......@@ -3,10 +3,6 @@
#include <petscksp.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)
{
......@@ -58,11 +54,7 @@ linearSystemPETSc<scalar>::~linearSystemPETSc()
{
clear();
if(_kspAllocated)
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
_try(KSPDestroy(&_ksp));
#else
_try(KSPDestroy(_ksp));
#endif
}
template <class scalar>
......@@ -80,7 +72,7 @@ void linearSystemPETSc<scalar>::preAllocateEntries()
if (!_isAllocated) Msg::Fatal("system must be allocated first");
if (_sparsity.getNbRows() == 0) {
PetscInt prealloc = 300;
PetscTruth set;
PetscBool set;
PetscOptionsGetInt(PETSC_NULL, "-petsc_prealloc", &prealloc, &set);
if (_blockSize == 0) {
_try(MatSeqAIJSetPreallocation(_a, prealloc, PETSC_NULL));
......@@ -195,15 +187,9 @@ template <class scalar>
void linearSystemPETSc<scalar>::clear()
{
if(_isAllocated){
#if (PETSC_VERSION_RELEASE == 0 || ((PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR >= 2)))
_try(MatDestroy(&_a));
_try(VecDestroy(&_x));
_try(VecDestroy(&_b));
#else
_try(MatDestroy(_a));
_try(VecDestroy(_x));
_try(VecDestroy(_b));
#endif
}
_isAllocated = false;
}
......@@ -379,7 +365,7 @@ std::vector<int> linearSystemPETSc<scalar>::getRowPointers()
const PetscInt *rows;
const PetscInt *columns;
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
std::vector<int> rowPointers; // Maybe I should reserve or resize (SAM)
for (int i = 0; i <= n; i++)
......@@ -396,7 +382,7 @@ std::vector<int> linearSystemPETSc<scalar>::getColumnsIndices()
const PetscInt *rows;
const PetscInt *columns;
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
MatInfo 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