16#ifndef Solv_Petsc_included
17#define Solv_Petsc_included
19#include <petsc_for_kernel.h>
23#include <Solv_Externe.h>
29#include <PCShell_base.h>
30#include <TRUST_Deriv.h>
36enum solveur_direct_ { no, mumps, superlu_dist, petsc, umfpack, pastix, cholmod, strumpack, cli };
37extern bool gmres_right_unpreconditionned;
49 Declare_instanciable_sans_constructeur_ni_destructeur(
Solv_Petsc);
64 if (SolveurPetsc_==
nullptr)
Process::exit(
"Can't call to Solv_Petsc::solveur_direct() yet.");
76 void reset_solver(
const Nom& name)
80 Cout <<
"Setting PETSc solver: " << name << finl;
90 inline void initialize();
91 inline bool gpu()
const
95 inline PCstruct& get_precond_user()
100 inline bool amgx()
const
104 inline bool amgx_initialized()
108#if PETSC_VERSION_GE(3,24,0)
109 PetscErrorCode set_convergence_test(PetscErrorCode (*converge)(KSP,PetscInt,PetscReal,KSPConvergedReason*,
void*),
void *cctx,PetscErrorCode (*destroy)(
void**))
111 PetscErrorCode set_convergence_test(PetscErrorCode (*converge)(KSP,PetscInt,PetscReal,KSPConvergedReason*,
void*),
void *cctx,PetscErrorCode (*destroy)(
void*))
115 return KSPSetConvergenceTest(SolveurPetsc_, converge, cctx, destroy);
118 static PetscLogStage KSPSolve_Stage_;
119 static PetscLogStage Create_Stage_;
120 void set_rtol(
const double& rtol)
122 seuil_relatif_ = rtol;
123 if (SolveurPetsc_!=
nullptr) KSPSetTolerances(SolveurPetsc_, seuil_relatif_, seuil_, (divtol_==0 ? PETSC_DEFAULT : divtol_), nb_it_max_);
129 virtual void Update_matrix(Mat& MatricePetsc,
const Matrice_Morse& mat_morse);
139 bool isViennaCLVector();
140 bool isKokkosVector();
142 void Create_DM(
const DoubleVect& );
144 virtual void Create_vectors(
const DoubleVect&);
145 virtual void Update_vectors(
const DoubleVect& secmem, DoubleVect& solution);
148 virtual int solve(ArrOfDouble& residual);
149 virtual void finalize() {};
151 bool nouveau_stencil()
153 return nouveau_stencil_;
155 bool enable_ksp_view();
156 bool has_option(
const Nom& option, Nom& current_value);
157 int add_option(
const Nom& option,
const double& value,
int cli = 0);
158 int add_option(
const Nom& option,
const Nom& value,
int cli = 0);
159 void add_amgx_option(
const Nom& key,
const Nom& value,
const std::string& comment=
"");
160 void add_amgx_option(
const Nom& key_value);
161 void SaveObjectsToFile(
const DoubleVect& b, DoubleVect& x);
162 void RestoreMatrixFromFile();
163 PetscInt compute_nb_rows_petsc(PetscInt);
167 double seuil_relatif_;
169 bool nouveau_stencil_ =
true;
173 Vec SecondMembrePetsc_;
176 PC PreconditionneurPetsc_;
179 int preconditionnement_non_symetrique_;
181 int convergence_with_nb_it_max_;
182 int ignore_nb_it_max_;
183 int controle_residu_;
185 Nom factored_matrix_;
193 int petsc_cpus_selection_;
194 int different_partition_;
196 Vec LocalSolutionPetsc_;
197 VecScatter VecScatter_;
218 IS rowperm =
nullptr, colperm =
nullptr;
219 IS inv_rowperm =
nullptr, inv_colperm =
nullptr;
223#define NB_IT_MAX_DEFINED 10000
243 if (SolveurPetsc_!=
nullptr)
245 KSPDestroy(&SolveurPetsc_);
248 if (MatricePetsc_!=
nullptr)
251 VecDestroy(&SecondMembrePetsc_);
252 VecDestroy(&SolutionPetsc_);
253 if (LocalSolutionPetsc_!=
nullptr)
255 VecDestroy(&LocalSolutionPetsc_);
256 VecScatterDestroy(&VecScatter_);
259 MatDestroy(&MatricePetsc_);
264 if (rowperm!=
nullptr)
265 ISDestroy(&rowperm), ISDestroy(&inv_rowperm);
266 if (colperm!=
nullptr)
267 ISDestroy(&colperm), ISDestroy(&inv_colperm);
273#define _RTOL_MIN_ 1.e-24
274inline void Solv_Petsc::initialize()
277 preconditionnement_non_symetrique_=0;
279 seuil_relatif_ = _RTOL_MIN_;
281 nouveau_stencil_ =
true;
282 petsc_cpus_selection_ = 0;
284 different_partition_ = 0;
286 convergence_with_nb_it_max_ = 0;
287 ignore_nb_it_max_ = 0;
288 nb_it_max_ = NB_IT_MAX_DEFINED;
299 MatricePetsc_ =
nullptr;
300 SecondMembrePetsc_ =
nullptr;
301 SolutionPetsc_ =
nullptr;
302 SolveurPetsc_ =
nullptr;
303 LocalSolutionPetsc_ =
nullptr;
304 VecScatter_ =
nullptr;
317 PetscGetVersion(version,256);
318 Cerr <<
"******************************************************************************************" << finl;
319 Cerr <<
"Commands lines possible for " << version <<
":" << finl;
320 Cerr <<
"-ksp_view : to have some informations on the solver/preconditioner used by PETSc." << finl;
321 Cerr <<
"-info : to have among other informations on storage of matrices of PETSc." << finl;
322 Cerr <<
"-log_summary : to have at the end of the calculation, informations about performances and memory." << finl;
323 Cerr <<
"-log_all : trace all PETSc calls." << finl;
324 Cerr <<
"-malloc_dump : to have at the end of the calculation the memory not deallocated by PETSc." << finl;
325 Cerr <<
"-help : to know all the commands lines of PETSc including those related to the solver/preconditioner selected." << finl;
326 Cerr <<
"******************************************************************************************" << finl;
328 Cerr <<
"NB: if you want to disable the wrinting of the *_petsc.TU file then specify the disable_TU flag in your datafile before reading the block of schema in time." << finl;
330 Cerr <<
"Reading of disable_TU flag => Disable the writing of the *_petsc.TU file."<< finl;
331 PetscLogStageRegister(
"CreateStage",&Create_Stage_);
332 PetscLogStageRegister(
"KSPSolve",&KSPSolve_Stage_);
344 option_prefix_=org.option_prefix_;
352 Cerr<<
"Solv_Petsc::operator=(const Solv_Petsc&) is not coded."<<finl;
Une entree dont la source est une chaine de caracteres.
Class defining operators and methods for all reading operation in an input flow (file,...
Classe Matrice_Base Classe de base de la hierarchie des matrices.
Classe Matrice_Morse_Sym Represente une matrice M (creuse) symetrique stockee au format Morse.
Classe Matrice_Morse Represente une matrice M (creuse), non necessairement carree.
class Nom Une chaine de caractere pour nommer les objets de TRUST
const Objet_U & operator=(const Objet_U &)
Operateur= : ne fait rien (on conserve le numero et l'identifiant).
static bool disable_TU
Flag to disable or not the writing of the .TU files.
virtual Entree & readOn(Entree &)
Lecture d'un Objet_U sur un flot d'entree Methode a surcharger.
static int nproc()
renvoie le nombre de processeurs dans le groupe courant Voir Comm_Group::nproc() et PE_Groups::curren...
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
void Update_solution(DoubleVect &x)
bool mat_ignore_zero_entries_
int solveur_direct() const override
int resoudre_systeme(const Matrice_Base &M, const DoubleVect &A, DoubleVect &B, int niter_max) override
static public_for_cuda int instance
int reuse_preconditioner_nb_it_max_
static int numero_solveur
int resoudre_systeme(const Matrice_Base &, const DoubleVect &, DoubleVect &) override
const Nom & get_chaine_lue() const
void set_read_matrix(bool flag)
Represents a an array of int/int64/double/... values.
option_double(Nom n, double v)
option_string(Nom n, Nom v)
OWN_PTR(PCShell_base) pc_shell