43 #if defined(HAVE_HYPRE) && defined(HAVE_MPI)
47 #include "Epetra_MpiComm.h"
48 #include "Epetra_IntVector.h"
49 #include "Epetra_Import.h"
50 #include "Teuchos_ParameterList.hpp"
51 #include "Teuchos_RCP.hpp"
52 #include "HYPRE_IJ_mv.h"
53 #include "HYPRE_parcsr_ls.h"
55 #include "_hypre_parcsr_mv.h"
56 #include "_hypre_parcsr_ls.h"
57 #include "_hypre_IJ_mv.h"
58 #include "HYPRE_parcsr_mv.h"
63 using Teuchos::rcpFromRef;
66 typedef int (*int_func)(HYPRE_Solver, int);
67 typedef int (*double_func)(HYPRE_Solver, double);
68 typedef int (*double_int_func)(HYPRE_Solver, double, int);
69 typedef int (*int_double_func)(HYPRE_Solver, int, double);
70 typedef int (*int_int_func)(HYPRE_Solver, int, int);
71 typedef int (*int_star_func)(HYPRE_Solver,
int*);
72 typedef int (*int_star_star_func)(HYPRE_Solver,
int**);
73 typedef int (*double_star_func)(HYPRE_Solver,
double*);
74 typedef int (*int_int_double_double_func)(HYPRE_Solver, int, int, double, double);
75 typedef int (*int_int_int_double_int_int_func)(HYPRE_Solver, int, int, int, double, int, int);
76 typedef int (*char_star_func)(HYPRE_Solver,
char*);
79 class FunctionParameter{
82 FunctionParameter(
Hypre_Chooser chooser, int_func funct,
int param1) :
86 int_param1_(param1) {}
88 FunctionParameter(
Hypre_Chooser chooser, std::string funct_name,
int param1) :
91 int_func_(hypreMapIntFunc_.at(funct_name)),
92 int_param1_(param1) {}
95 FunctionParameter(
Hypre_Chooser chooser, double_func funct,
double param1):
99 double_param1_(param1) {}
101 FunctionParameter(
Hypre_Chooser chooser, std::string funct_name,
double param1):
104 double_func_(hypreMapDoubleFunc_.at(funct_name)),
105 double_param1_(param1) {}
108 FunctionParameter(
Hypre_Chooser chooser, double_int_func funct,
double param1,
int param2):
111 double_int_func_(funct),
113 double_param1_(param1) {}
116 FunctionParameter(
Hypre_Chooser chooser, int_double_func funct,
int param1,
double param2):
119 int_double_func_(funct),
121 double_param1_(param2) {}
123 FunctionParameter(
Hypre_Chooser chooser, std::string funct_name,
double param1,
int param2):
126 double_int_func_(hypreMapDoubleIntFunc_.at(funct_name)),
128 double_param1_(param1) {}
131 FunctionParameter(
Hypre_Chooser chooser, int_int_func funct,
int param1,
int param2):
134 int_int_func_(funct),
136 int_param2_(param2) {}
138 FunctionParameter(
Hypre_Chooser chooser, std::string funct_name,
int param1,
int param2):
141 int_int_func_(hypreMapIntIntFunc_.at(funct_name)),
143 int_param2_(param2) {}
146 FunctionParameter(
Hypre_Chooser chooser, int_star_func funct,
int *param1):
149 int_star_func_(funct),
150 int_star_param_(param1) {}
152 FunctionParameter(
Hypre_Chooser chooser, std::string funct_name,
int *param1):
155 int_star_func_(hypreMapIntStarFunc_.at(funct_name)),
156 int_star_param_(param1) {}
159 FunctionParameter(
Hypre_Chooser chooser, double_star_func funct,
double* param1):
162 double_star_func_(funct),
163 double_star_param_(param1) {}
165 FunctionParameter(
Hypre_Chooser chooser, std::string funct_name,
double* param1):
168 double_star_func_(hypreMapDoubleStarFunc_.at(funct_name)),
169 double_star_param_(param1) {}
172 FunctionParameter(
Hypre_Chooser chooser, int_int_double_double_func funct,
int param1,
int param2,
double param3,
double param4):
175 int_int_double_double_func_(funct),
178 double_param1_(param3),
179 double_param2_(param4) {}
181 FunctionParameter(
Hypre_Chooser chooser, std::string funct_name,
int param1,
int param2,
double param3,
double param4):
184 int_int_double_double_func_(hypreMapIntIntDoubleDoubleFunc_.at(funct_name)),
187 double_param1_(param3),
188 double_param2_(param4) {}
191 FunctionParameter(
Hypre_Chooser chooser, int_star_star_func funct,
int ** param1):
194 int_star_star_func_(funct),
195 int_star_star_param_(param1) {}
197 FunctionParameter(
Hypre_Chooser chooser, std::string funct_name,
int** param1):
200 int_star_star_func_(hypreMapIntStarStarFunc_.at(funct_name)),
201 int_star_star_param_(param1) {}
204 FunctionParameter(
Hypre_Chooser chooser, int_int_int_double_int_int_func funct,
int param1,
int param2,
int param3,
double param4,
int param5,
int param6):
207 int_int_int_double_int_int_func_(funct),
213 double_param1_(param4) {}
215 FunctionParameter(
Hypre_Chooser chooser, std::string funct_name,
int param1,
int param2,
int param3,
double param4,
int param5,
int param6):
218 int_int_int_double_int_int_func_(hypreMapIntIntIntDoubleIntIntFunc_.at(funct_name)),
224 double_param1_(param4) {}
227 FunctionParameter(
Hypre_Chooser chooser, char_star_func funct,
char *param1):
230 char_star_func_(funct),
231 char_star_param_(param1) {}
233 FunctionParameter(
Hypre_Chooser chooser, std::string funct_name,
char *param1):
236 char_star_func_(hypreMapCharStarFunc_.at(funct_name)),
237 char_star_param_(param1) {}
240 int CallFunction(HYPRE_Solver solver, HYPRE_Solver precond) {
243 return int_func_(solver, int_param1_);
244 }
else if(option_ == 1){
245 return double_func_(solver, double_param1_);
246 }
else if(option_ == 2){
247 return double_int_func_(solver, double_param1_, int_param1_);
248 }
else if (option_ == 3){
249 return int_int_func_(solver, int_param1_, int_param2_);
250 }
else if (option_ == 4){
251 return int_star_func_(solver, int_star_param_);
252 }
else if (option_ == 5){
253 return double_star_func_(solver, double_star_param_);
254 }
else if (option_ == 6) {
255 return int_int_double_double_func_(solver, int_param1_, int_param2_, double_param1_, double_param2_);
256 }
else if (option_ == 7) {
257 return int_star_star_func_(solver, int_star_star_param_);
258 }
else if (option_ == 8) {
259 return int_int_int_double_int_int_func_(solver, int_param1_, int_param2_, int_param3_, double_param1_, int_param4_, int_param5_);
260 }
else if (option_ == 9) {
261 return char_star_func_(solver, char_star_param_);
262 }
else if (option_ == 10) {
263 return int_double_func_(solver, int_param1_, double_param1_);
269 return int_func_(precond, int_param1_);
270 }
else if(option_ == 1){
271 return double_func_(precond, double_param1_);
272 }
else if(option_ == 2){
273 return double_int_func_(precond, double_param1_, int_param1_);
274 }
else if(option_ == 3) {
275 return int_int_func_(precond, int_param1_, int_param2_);
276 }
else if(option_ == 4) {
277 return int_star_func_(precond, int_star_param_);
278 }
else if(option_ == 5) {
279 return double_star_func_(precond, double_star_param_);
280 }
else if (option_ == 6) {
281 return int_int_double_double_func_(precond, int_param1_, int_param2_, double_param1_, double_param2_);
282 }
else if (option_ == 7) {
283 return int_star_star_func_(precond, int_star_star_param_);
284 }
else if (option_ == 8) {
285 return int_int_int_double_int_int_func_(precond, int_param1_, int_param2_, int_param3_, double_param1_, int_param4_, int_param5_);
286 }
else if (option_ == 9) {
287 return char_star_func_(solver, char_star_param_);
288 }
else if (option_ == 10) {
289 return int_double_func_(precond, int_param1_, double_param1_);
296 static bool isFuncIntInt(std::string funct_name) {
297 return (hypreMapIntIntFunc_.find(funct_name) != hypreMapIntIntFunc_.end());
300 static bool isFuncIntIntDoubleDouble(std::string funct_name) {
301 return (hypreMapIntIntDoubleDoubleFunc_.find(funct_name) != hypreMapIntIntDoubleDoubleFunc_.end());
304 static bool isFuncIntDouble(std::string funct_name) {
305 return (hypreMapIntDoubleFunc_.find(funct_name) != hypreMapIntDoubleFunc_.end());
308 static bool isFuncIntIntIntDoubleIntInt(std::string funct_name) {
309 return (hypreMapIntIntIntDoubleIntIntFunc_.find(funct_name) != hypreMapIntIntIntDoubleIntIntFunc_.end());
312 static bool isFuncIntStarStar(std::string funct_name) {
313 return (hypreMapIntStarStarFunc_.find(funct_name) != hypreMapIntStarStarFunc_.end());
320 double_func double_func_;
321 double_int_func double_int_func_;
322 int_double_func int_double_func_;
323 int_int_func int_int_func_;
324 int_star_func int_star_func_;
325 double_star_func double_star_func_;
326 int_int_double_double_func int_int_double_double_func_;
327 int_int_int_double_int_int_func int_int_int_double_int_int_func_;
328 int_star_star_func int_star_star_func_;
329 char_star_func char_star_func_;
335 double double_param1_;
336 double double_param2_;
337 int *int_star_param_;
338 int **int_star_star_param_;
339 double *double_star_param_;
340 char *char_star_param_;
342 static const std::map<std::string, int_func> hypreMapIntFunc_;
343 static const std::map<std::string, double_func> hypreMapDoubleFunc_;
344 static const std::map<std::string, double_int_func> hypreMapDoubleIntFunc_;
345 static const std::map<std::string, int_double_func> hypreMapIntDoubleFunc_;
346 static const std::map<std::string, int_int_func> hypreMapIntIntFunc_;
347 static const std::map<std::string, int_star_func> hypreMapIntStarFunc_;
348 static const std::map<std::string, double_star_func> hypreMapDoubleStarFunc_;
349 static const std::map<std::string, int_int_double_double_func> hypreMapIntIntDoubleDoubleFunc_;
350 static const std::map<std::string, int_int_int_double_int_int_func> hypreMapIntIntIntDoubleIntIntFunc_;
351 static const std::map<std::string, int_star_star_func> hypreMapIntStarStarFunc_;
352 static const std::map<std::string, char_star_func> hypreMapCharStarFunc_;
357 #include "Ifpack_HypreParameterMap.h"
361 UseTranspose_(
false),
362 IsInitialized_(
false),
368 InitializeTime_(0.0),
370 ApplyInverseTime_(0.0),
372 ApplyInverseFlops_(0.0),
379 IsSolverCreated_(
false),
380 IsPrecondCreated_(
false),
385 UsePreconditioner_(
false),
388 MPI_Comm comm = GetMpiComm();
392 if (!A_->RowMatrixRowMap().SameAs(A_->OperatorRangeMap())) {
396 if (A_->RowMatrixRowMap().LinearMap()) {
398 GloballyContiguousRowMap_ = rcpFromRef(A_->RowMatrixRowMap());
399 GloballyContiguousColMap_ = rcpFromRef(A_->RowMatrixColMap());
402 if(A_->OperatorDomainMap().SameAs(A_->RowMatrixRowMap())) {
404 GloballyContiguousColMap_ = MakeContiguousColumnMap(Aconst);
405 GloballyContiguousRowMap_ =
rcp(
new Epetra_Map(A_->RowMatrixRowMap().NumGlobalElements(),
406 A_->RowMatrixRowMap().NumMyElements(), 0, Comm()));
409 throw std::runtime_error(
"Ifpack_Hypre: Unsupported map configuration: Row/Domain maps do not match");
413 int ilower = GloballyContiguousRowMap_->MinMyGID();
414 int iupper = GloballyContiguousRowMap_->MaxMyGID();
421 XVec_ =
Teuchos::rcp((hypre_ParVector *) hypre_IJVectorObject(((hypre_IJVector *) XHypre_)),
false);
429 YVec_ =
Teuchos::rcp((hypre_ParVector *) hypre_IJVectorObject(((hypre_IJVector *) YHypre_)),
false);
436 void Ifpack_Hypre::Destroy(){
442 if(IsSolverCreated_){
445 if(IsPrecondCreated_){
465 int Ifpack_Hypre::Initialize(){
466 Time_.ResetStartTime();
467 if(IsInitialized_)
return 0;
470 NumInitialize_ = NumInitialize_ + 1;
471 InitializeTime_ = InitializeTime_ + Time_.ElapsedTime();
478 std::map<std::string, Hypre_Solver> solverMap;
481 solverMap[
"Euclid"] =
Euclid;
482 solverMap[
"AMS"] =
AMS;
483 solverMap[
"Hybrid"] =
Hybrid;
484 solverMap[
"PCG"] =
PCG;
485 solverMap[
"GMRES"] =
GMRES;
487 solverMap[
"LGMRES"] =
LGMRES;
490 std::map<std::string, Hypre_Chooser> chooserMap;
491 chooserMap[
"Solver"] =
Solver;
496 if (list.
isType<std::string>(
"hypre: Solver"))
497 solType = solverMap[list.
get<std::string>(
"hypre: Solver")];
499 solType = list.
get(
"hypre: Solver", PCG);
500 SolverType_ = solType;
502 if (list.
isType<std::string>(
"hypre: Preconditioner"))
503 precType = solverMap[list.
get<std::string>(
"hypre: Preconditioner")];
505 precType = list.
get(
"hypre: Preconditioner", Euclid);
506 PrecondType_ = precType;
508 if (list.
isType<std::string>(
"hypre: SolveOrPrecondition"))
509 chooser = chooserMap[list.
get<std::string>(
"hypre: SolveOrPrecondition")];
511 chooser = list.
get(
"hypre: SolveOrPrecondition",
Solver);
512 SolveOrPrec_ = chooser;
513 bool SetPrecond = list.
get(
"hypre: SetPreconditioner",
false);
515 int NumFunctions = list.
get(
"hypre: NumFunctions", 0);
518 if(NumFunctions > 0){
519 RCP<FunctionParameter>* params = list.
get<RCP<FunctionParameter>*>(
"hypre: Functions");
520 for(
int i = 0; i < NumFunctions; i++){
525 if (list.
isSublist(
"hypre: Solver functions")) {
527 for (
auto it = solverList.
begin(); it != solverList.
end(); ++it) {
528 std::string funct_name = it->first;
529 if (it->second.isType<
int>()) {
530 IFPACK_CHK_ERR(AddFunToList(
rcp(
new FunctionParameter(
Solver, funct_name , Teuchos::getValue<int>(it->second)))));
531 }
else if (it->second.isType<
double>()) {
532 IFPACK_CHK_ERR(AddFunToList(
rcp(
new FunctionParameter(
Solver, funct_name , Teuchos::getValue<double>(it->second)))));
539 if (list.
isSublist(
"hypre: Preconditioner functions")) {
541 for (
auto it = precList.
begin(); it != precList.
end(); ++it) {
542 std::string funct_name = it->first;
543 if (it->second.isType<
int>()) {
544 IFPACK_CHK_ERR(AddFunToList(
rcp(
new FunctionParameter(Preconditioner, funct_name , Teuchos::getValue<int>(it->second)))));
545 }
else if (it->second.isType<
double>()) {
546 IFPACK_CHK_ERR(AddFunToList(
rcp(
new FunctionParameter(Preconditioner, funct_name , Teuchos::getValue<double>(it->second)))));
547 }
else if (it->second.isList()) {
549 if (FunctionParameter::isFuncIntInt(funct_name)) {
550 int arg0 = pl.
get<
int>(
"arg 0");
551 int arg1 = pl.
get<
int>(
"arg 1");
552 IFPACK_CHK_ERR(AddFunToList(
rcp(
new FunctionParameter(Preconditioner, funct_name , arg0, arg1))));
553 }
else if (FunctionParameter::isFuncIntDouble(funct_name)) {
554 int arg0 = pl.
get<
int>(
"arg 0");
555 double arg1 = pl.
get<
double>(
"arg 1");
556 IFPACK_CHK_ERR(AddFunToList(
rcp(
new FunctionParameter(Preconditioner, funct_name , arg0, arg1))));
557 }
else if (FunctionParameter::isFuncIntIntDoubleDouble(funct_name)) {
558 int arg0 = pl.
get<
int>(
"arg 0");
559 int arg1 = pl.
get<
int>(
"arg 1");
560 double arg2 = pl.
get<
double>(
"arg 2");
561 double arg3 = pl.
get<
double>(
"arg 3");
562 IFPACK_CHK_ERR(AddFunToList(
rcp(
new FunctionParameter(Preconditioner, funct_name , arg0, arg1, arg2, arg3))));
563 }
else if (FunctionParameter::isFuncIntIntIntDoubleIntInt(funct_name)) {
564 int arg0 = pl.
get<
int>(
"arg 0");
565 int arg1 = pl.
get<
int>(
"arg 1");
566 int arg2 = pl.
get<
int>(
"arg 2");
567 double arg3 = pl.
get<
double>(
"arg 3");
568 int arg4 = pl.
get<
int>(
"arg 4");
569 int arg5 = pl.
get<
int>(
"arg 5");
570 IFPACK_CHK_ERR(AddFunToList(
rcp(
new FunctionParameter(Preconditioner, funct_name , arg0, arg1, arg2, arg3, arg4, arg5))));
583 Dump_ = list.
get(
"hypre: Dump",
false);
589 int Ifpack_Hypre::AddFunToList(RCP<FunctionParameter> NewFun){
590 NumFunsToCall_ = NumFunsToCall_+1;
591 FunsToCall_.resize(NumFunsToCall_);
592 FunsToCall_[NumFunsToCall_-1] = NewFun;
597 int Ifpack_Hypre::SetParameter(
Hypre_Chooser chooser,
int (*pt2Func)(HYPRE_Solver,
int),
int parameter){
598 RCP<FunctionParameter> temp =
rcp(
new FunctionParameter(chooser, pt2Func, parameter));
604 int Ifpack_Hypre::SetParameter(
Hypre_Chooser chooser,
int (*pt2Func)(HYPRE_Solver,
double),
double parameter){
605 RCP<FunctionParameter> temp =
rcp(
new FunctionParameter(chooser, pt2Func, parameter));
611 int Ifpack_Hypre::SetParameter(
Hypre_Chooser chooser,
int (*pt2Func)(HYPRE_Solver,
double,
int),
double parameter1,
int parameter2){
612 RCP<FunctionParameter> temp =
rcp(
new FunctionParameter(chooser, pt2Func, parameter1, parameter2));
618 int Ifpack_Hypre::SetParameter(
Hypre_Chooser chooser,
int (*pt2Func)(HYPRE_Solver,
int,
double),
int parameter1,
double parameter2){
619 RCP<FunctionParameter> temp =
rcp(
new FunctionParameter(chooser, pt2Func, parameter1, parameter2));
625 int Ifpack_Hypre::SetParameter(
Hypre_Chooser chooser,
int (*pt2Func)(HYPRE_Solver,
int,
int),
int parameter1,
int parameter2){
626 RCP<FunctionParameter> temp =
rcp(
new FunctionParameter(chooser, pt2Func, parameter1, parameter2));
632 int Ifpack_Hypre::SetParameter(
Hypre_Chooser chooser,
int (*pt2Func)(HYPRE_Solver,
double*),
double* parameter){
633 RCP<FunctionParameter> temp =
rcp(
new FunctionParameter(chooser, pt2Func, parameter));
639 int Ifpack_Hypre::SetParameter(
Hypre_Chooser chooser,
int (*pt2Func)(HYPRE_Solver,
int*),
int* parameter){
640 RCP<FunctionParameter> temp =
rcp(
new FunctionParameter(chooser, pt2Func, parameter));
646 int Ifpack_Hypre::SetParameter(
Hypre_Chooser chooser,
int (*pt2Func)(HYPRE_Solver,
int**),
int** parameter){
647 RCP<FunctionParameter> temp =
rcp(
new FunctionParameter(chooser, pt2Func, parameter));
655 SolverType_ = solver;
657 PrecondType_ = solver;
666 if(!A_->RowMatrixRowMap().SameAs(G->
RowMap()))
667 throw std::runtime_error(
"Ifpack_Hypre: Edge map mismatch: A and discrete gradient");
673 GloballyContiguousNodeColMap_ = MakeContiguousColumnMap(Grow);
676 MPI_Comm comm = GetMpiComm();
677 int ilower = GloballyContiguousRowMap_->MinMyGID();
678 int iupper = GloballyContiguousRowMap_->MaxMyGID();
679 int jlower = GloballyContiguousNodeRowMap_->MinMyGID();
680 int jupper = GloballyContiguousNodeRowMap_->MaxMyGID();
681 IFPACK_CHK_ERR(HYPRE_IJMatrixCreate(comm, ilower, iupper, jlower, jupper, &HypreG_));
682 IFPACK_CHK_ERR(HYPRE_IJMatrixSetObjectType(HypreG_, HYPRE_PARCSR));
691 for(
int j = 0; j < numEntries; j++){
692 new_indices[j] = GloballyContiguousNodeColMap_->GID(indices[j]);
695 GlobalRow[0] = GloballyContiguousRowMap_->GID(i);
696 IFPACK_CHK_ERR(HYPRE_IJMatrixSetValues(HypreG_, 1, &numEntries, GlobalRow, new_indices.data(), values));
699 IFPACK_CHK_ERR(HYPRE_IJMatrixGetObject(HypreG_, (
void**)&ParMatrixG_));
702 HYPRE_ParCSRMatrixPrint(ParMatrixG_,
"G.mat");
704 if(SolverType_ == AMS)
705 HYPRE_AMSSetDiscreteGradient(Solver_, ParMatrixG_);
706 if(PrecondType_ == AMS)
707 HYPRE_AMSSetDiscreteGradient(Preconditioner_, ParMatrixG_);
715 if(!G_.is_null() && !G_->DomainMap().SameAs(coords->
Map()))
716 throw std::runtime_error(
"Ifpack_Hypre: Node map mismatch: G->DomainMap() and coords");
718 if(SolverType_ != AMS && PrecondType_ != AMS)
729 MPI_Comm comm = GetMpiComm();
730 int NumEntries = coords->MyLength();
731 int * indices = GloballyContiguousNodeRowMap_->MyGlobalElements();
733 int ilower = GloballyContiguousNodeRowMap_->MinMyGID();
734 int iupper = GloballyContiguousNodeRowMap_->MaxMyGID();
736 if( NumEntries != iupper-ilower+1) {
737 std::cout<<
"Ifpack_Hypre::SetCoordinates(): Error on rank "<<Comm().MyPID()<<
": MyLength = "<<coords->MyLength()<<
" GID range = ["<<ilower<<
","<<iupper<<
"]"<<std::endl;
738 throw std::runtime_error(
"Ifpack_Hypre: SetCoordinates: Length mismatch");
741 IFPACK_CHK_ERR(HYPRE_IJVectorCreate(comm, ilower, iupper, &xHypre_));
742 IFPACK_CHK_ERR(HYPRE_IJVectorSetObjectType(xHypre_, HYPRE_PARCSR));
744 IFPACK_CHK_ERR(HYPRE_IJVectorSetValues(xHypre_,NumEntries,indices,xPtr));
746 IFPACK_CHK_ERR(HYPRE_IJVectorGetObject(xHypre_, (
void**) &xPar_));
748 IFPACK_CHK_ERR(HYPRE_IJVectorCreate(comm, ilower, iupper, &yHypre_));
749 IFPACK_CHK_ERR(HYPRE_IJVectorSetObjectType(yHypre_, HYPRE_PARCSR));
751 IFPACK_CHK_ERR(HYPRE_IJVectorSetValues(yHypre_,NumEntries,indices,yPtr));
753 IFPACK_CHK_ERR(HYPRE_IJVectorGetObject(yHypre_, (
void**) &yPar_));
755 IFPACK_CHK_ERR(HYPRE_IJVectorCreate(comm, ilower, iupper, &zHypre_));
756 IFPACK_CHK_ERR(HYPRE_IJVectorSetObjectType(zHypre_, HYPRE_PARCSR));
758 IFPACK_CHK_ERR(HYPRE_IJVectorSetValues(zHypre_,NumEntries,indices,zPtr));
760 IFPACK_CHK_ERR(HYPRE_IJVectorGetObject(zHypre_, (
void**) &zPar_));
763 HYPRE_ParVectorPrint(xPar_,
"coordX.dat");
764 HYPRE_ParVectorPrint(yPar_,
"coordY.dat");
765 HYPRE_ParVectorPrint(zPar_,
"coordZ.dat");
768 if(SolverType_ == AMS)
769 HYPRE_AMSSetCoordinateVectors(Solver_, xPar_, yPar_, zPar_);
770 if(PrecondType_ == AMS)
771 HYPRE_AMSSetCoordinateVectors(Preconditioner_, xPar_, yPar_, zPar_);
778 int Ifpack_Hypre::Compute(){
779 if(IsInitialized() ==
false){
782 Time_.ResetStartTime();
789 MPI_Comm comm = GetMpiComm();
790 int ilower = GloballyContiguousRowMap_->MinMyGID();
791 int iupper = GloballyContiguousRowMap_->MaxMyGID();
792 IFPACK_CHK_ERR(HYPRE_IJMatrixCreate(comm, ilower, iupper, ilower, iupper, &HypreA_));
793 IFPACK_CHK_ERR(HYPRE_IJMatrixSetObjectType(HypreA_, HYPRE_PARCSR));
796 if(SolveOrPrec_ ==
Solver) {
798 if (SolverPrecondPtr_ != NULL && UsePreconditioner_) {
802 IFPACK_CHK_ERR(SolverPrecondPtr_(Solver_, PrecondSolvePtr_, PrecondSetupPtr_, Preconditioner_));
812 SetDiscreteGradient(G_);
815 if (!Coords_.is_null()) {
816 SetCoordinates(Coords_);
820 if(SolveOrPrec_ ==
Solver){
821 IFPACK_CHK_ERR(SolverSetupPtr_(Solver_, ParMatrix_, ParX_, ParY_));
823 IFPACK_CHK_ERR(PrecondSetupPtr_(Preconditioner_, ParMatrix_, ParX_, ParY_));
827 if(Dump_ && PrecondSolvePtr_ == &HYPRE_BoomerAMGSolve) {
828 hypre_ParAMGData *amg_data = (hypre_ParAMGData*) Preconditioner_;
829 hypre_ParCSRMatrix **A_array = hypre_ParAMGDataAArray(amg_data);
830 hypre_ParCSRMatrix **P_array = hypre_ParAMGDataPArray(amg_data);
832 HYPRE_Int **CF_marker_array = hypre_ParAMGDataCFMarkerArray(amg_data);
834 HYPRE_Int num_levels = hypre_ParAMGDataNumLevels(amg_data);
837 for(
int k=0; k<num_levels; k++) {
839 sprintf(ofs,
"A_matrix.bmg.%d.dat",k);
840 HYPRE_ParCSRMatrixPrint(A_array[k], ofs);
841 if(k!=num_levels-1) {
843 sprintf(ofs,
"P_matrix.bmg.%d.dat",k);
844 HYPRE_ParCSRMatrixPrint(P_array[k], ofs);
849 HYPRE_Int local_size = hypre_CSRMatrixNumRows(hypre_ParCSRMatrixDiag(A_array[k]));
850 sprintf(ofs,
"cf_marker.bmg.%d.dat.%d",k,Comm().MyPID());
851 FILE *
f = fopen(ofs,
"w");
852 fprintf(f,
"%%%%MatrixMarket matrix array real general\n");
853 fprintf(f,
"%d 1\n",local_size);
854 for(
int i=0; i<local_size; i++)
855 fprintf(f,
"%d\n",(
int)CF_marker_array[k][i]);
865 NumCompute_ = NumCompute_ + 1;
866 ComputeTime_ = ComputeTime_ + Time_.ElapsedTime();
871 int Ifpack_Hypre::CallFunctions()
const{
872 for(
int i = 0; i < NumFunsToCall_; i++){
873 IFPACK_CHK_ERR(FunsToCall_[i]->CallFunction(Solver_, Preconditioner_));
880 if(IsComputed() ==
false){
883 Time_.ResetStartTime();
884 hypre_Vector *XLocal_ = hypre_ParVectorLocalVector(XVec_);
885 hypre_Vector *YLocal_ = hypre_ParVectorLocalVector(YVec_);
887 bool SameVectors =
false;
890 if(X.Pointers() == Y.Pointers() || (NumVectors == 1 && X[0] == Y[0])){
898 for(
int VecNum = 0; VecNum <
NumVectors; VecNum++) {
901 double * XValues =
const_cast<double*
>(X[VecNum]);
904 YValues =
const_cast<double*
>(Y[VecNum]);
906 YValues = VectorCache_.getRawPtr();
909 double *XTemp = XLocal_->data;
911 XLocal_->data = XValues;
912 double *YTemp = YLocal_->data;
913 YLocal_->data = YValues;
916 if(SolveOrPrec_ ==
Solver){
918 IFPACK_CHK_ERR(SolverSolvePtr_(Solver_, ParMatrix_, ParX_, ParY_));
921 IFPACK_CHK_ERR(PrecondSolvePtr_(Preconditioner_, ParMatrix_, ParX_, ParY_));
924 int NumEntries = Y.MyLength();
925 for(
int i = 0; i < NumEntries; i++)
926 Y[VecNum][i] = YValues[i];
928 XLocal_->data = XTemp;
929 YLocal_->data = YTemp;
931 NumApplyInverse_ = NumApplyInverse_ + 1;
932 ApplyInverseTime_ = ApplyInverseTime_ + Time_.ElapsedTime();
938 if(IsInitialized() ==
false){
941 hypre_Vector *XLocal_ = hypre_ParVectorLocalVector(XVec_);
942 hypre_Vector *YLocal_ = hypre_ParVectorLocalVector(YVec_);
943 bool SameVectors =
false;
944 int NumVectors = X.NumVectors();
946 if(X.Pointers() == Y.Pointers() || (NumVectors == 1 && X[0] == Y[0])){
954 for(
int VecNum = 0; VecNum <
NumVectors; VecNum++) {
956 double * XValues=
const_cast<double*
>(X[VecNum]);
958 double *XTemp = XLocal_->data;
959 double *YTemp = YLocal_->data;
961 YValues =
const_cast<double*
>(Y[VecNum]);
963 YValues = VectorCache_.getRawPtr();
965 YLocal_->data = YValues;
969 XLocal_->data = XValues;
973 IFPACK_CHK_ERR(HYPRE_ParCSRMatrixMatvecT(1.0, ParMatrix_, ParX_, 1.0, ParY_));
975 IFPACK_CHK_ERR(HYPRE_ParCSRMatrixMatvec(1.0, ParMatrix_, ParX_, 1.0, ParY_));
978 int NumEntries = Y.MyLength();
979 for(
int i = 0; i < NumEntries; i++)
980 Y[VecNum][i] = YValues[i];
982 XLocal_->data = XTemp;
983 YLocal_->data = YTemp;
989 std::ostream& Ifpack_Hypre::Print(std::ostream& os)
const{
991 if (!Comm().MyPID()) {
993 os <<
"================================================================================" << endl;
994 os <<
"Ifpack_Hypre: " << Label() << endl << endl;
995 os <<
"Using " << Comm().NumProc() <<
" processors." << endl;
996 os <<
"Global number of rows = " << A_->NumGlobalRows() << endl;
997 os <<
"Global number of nonzeros = " << A_->NumGlobalNonzeros() << endl;
998 os <<
"Condition number estimate = " << Condest() << endl;
1000 os <<
"Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
1001 os <<
"----- ------- -------------- ------------ --------" << endl;
1002 os <<
"Initialize() " << std::setw(5) << NumInitialize_
1003 <<
" " << std::setw(15) << InitializeTime_
1004 <<
" 0.0 0.0" << endl;
1005 os <<
"Compute() " << std::setw(5) << NumCompute_
1006 <<
" " << std::setw(15) << ComputeTime_
1007 <<
" " << std::setw(15) << 1.0e-6 * ComputeFlops_;
1008 if (ComputeTime_ != 0.0)
1009 os <<
" " << std::setw(15) << 1.0e-6 * ComputeFlops_ / ComputeTime_ << endl;
1011 os <<
" " << std::setw(15) << 0.0 << endl;
1012 os <<
"ApplyInverse() " << std::setw(5) << NumApplyInverse_
1013 <<
" " << std::setw(15) << ApplyInverseTime_
1014 <<
" " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_;
1015 if (ApplyInverseTime_ != 0.0)
1016 os <<
" " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_ / ApplyInverseTime_ << endl;
1018 os <<
" " << std::setw(15) << 0.0 << endl;
1019 os <<
"================================================================================" << endl;
1040 if(IsSolverCreated_){
1041 SolverDestroyPtr_(Solver_);
1042 IsSolverCreated_ =
false;
1044 SolverCreatePtr_ = &Ifpack_Hypre::Hypre_BoomerAMGCreate;
1045 SolverDestroyPtr_ = &HYPRE_BoomerAMGDestroy;
1046 SolverSetupPtr_ = &HYPRE_BoomerAMGSetup;
1047 SolverPrecondPtr_ = NULL;
1048 SolverSolvePtr_ = &HYPRE_BoomerAMGSolve;
1051 if(IsSolverCreated_){
1052 SolverDestroyPtr_(Solver_);
1053 IsSolverCreated_ =
false;
1055 SolverCreatePtr_ = &Ifpack_Hypre::Hypre_AMSCreate;
1056 SolverDestroyPtr_ = &HYPRE_AMSDestroy;
1057 SolverSetupPtr_ = &HYPRE_AMSSetup;
1058 SolverSolvePtr_ = &HYPRE_AMSSolve;
1059 SolverPrecondPtr_ = NULL;
1062 if(IsSolverCreated_){
1063 SolverDestroyPtr_(Solver_);
1064 IsSolverCreated_ =
false;
1066 SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRHybridCreate;
1067 SolverDestroyPtr_ = &HYPRE_ParCSRHybridDestroy;
1068 SolverSetupPtr_ = &HYPRE_ParCSRHybridSetup;
1069 SolverSolvePtr_ = &HYPRE_ParCSRHybridSolve;
1070 SolverPrecondPtr_ = &HYPRE_ParCSRHybridSetPrecond;
1073 if(IsSolverCreated_){
1074 SolverDestroyPtr_(Solver_);
1075 IsSolverCreated_ =
false;
1077 SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRPCGCreate;
1078 SolverDestroyPtr_ = &HYPRE_ParCSRPCGDestroy;
1079 SolverSetupPtr_ = &HYPRE_ParCSRPCGSetup;
1080 SolverSolvePtr_ = &HYPRE_ParCSRPCGSolve;
1081 SolverPrecondPtr_ = &HYPRE_ParCSRPCGSetPrecond;
1084 if(IsSolverCreated_){
1085 SolverDestroyPtr_(Solver_);
1086 IsSolverCreated_ =
false;
1088 SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRGMRESCreate;
1089 SolverDestroyPtr_ = &HYPRE_ParCSRGMRESDestroy;
1090 SolverSetupPtr_ = &HYPRE_ParCSRGMRESSetup;
1091 SolverPrecondPtr_ = &HYPRE_ParCSRGMRESSetPrecond;
1094 if(IsSolverCreated_){
1095 SolverDestroyPtr_(Solver_);
1096 IsSolverCreated_ =
false;
1098 SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRFlexGMRESCreate;
1099 SolverDestroyPtr_ = &HYPRE_ParCSRFlexGMRESDestroy;
1100 SolverSetupPtr_ = &HYPRE_ParCSRFlexGMRESSetup;
1101 SolverSolvePtr_ = &HYPRE_ParCSRFlexGMRESSolve;
1102 SolverPrecondPtr_ = &HYPRE_ParCSRFlexGMRESSetPrecond;
1105 if(IsSolverCreated_){
1106 SolverDestroyPtr_(Solver_);
1107 IsSolverCreated_ =
false;
1109 SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRLGMRESCreate;
1110 SolverDestroyPtr_ = &HYPRE_ParCSRLGMRESDestroy;
1111 SolverSetupPtr_ = &HYPRE_ParCSRLGMRESSetup;
1112 SolverSolvePtr_ = &HYPRE_ParCSRLGMRESSolve;
1113 SolverPrecondPtr_ = &HYPRE_ParCSRLGMRESSetPrecond;
1116 if(IsSolverCreated_){
1117 SolverDestroyPtr_(Solver_);
1118 IsSolverCreated_ =
false;
1120 SolverCreatePtr_ = &Ifpack_Hypre::Hypre_ParCSRBiCGSTABCreate;
1121 SolverDestroyPtr_ = &HYPRE_ParCSRBiCGSTABDestroy;
1122 SolverSetupPtr_ = &HYPRE_ParCSRBiCGSTABSetup;
1123 SolverSolvePtr_ = &HYPRE_ParCSRBiCGSTABSolve;
1124 SolverPrecondPtr_ = &HYPRE_ParCSRBiCGSTABSetPrecond;
1134 int Ifpack_Hypre::SetPrecondType(
Hypre_Solver Precond){
1137 if(IsPrecondCreated_){
1138 PrecondDestroyPtr_(Preconditioner_);
1139 IsPrecondCreated_ =
false;
1141 PrecondCreatePtr_ = &Ifpack_Hypre::Hypre_BoomerAMGCreate;
1142 PrecondDestroyPtr_ = &HYPRE_BoomerAMGDestroy;
1143 PrecondSetupPtr_ = &HYPRE_BoomerAMGSetup;
1144 PrecondSolvePtr_ = &HYPRE_BoomerAMGSolve;
1147 if(IsPrecondCreated_){
1148 PrecondDestroyPtr_(Preconditioner_);
1149 IsPrecondCreated_ =
false;
1151 PrecondCreatePtr_ = &Ifpack_Hypre::Hypre_ParaSailsCreate;
1152 PrecondDestroyPtr_ = &HYPRE_ParaSailsDestroy;
1153 PrecondSetupPtr_ = &HYPRE_ParaSailsSetup;
1154 PrecondSolvePtr_ = &HYPRE_ParaSailsSolve;
1157 if(IsPrecondCreated_){
1158 PrecondDestroyPtr_(Preconditioner_);
1159 IsPrecondCreated_ =
false;
1161 PrecondCreatePtr_ = &Ifpack_Hypre::Hypre_EuclidCreate;
1162 PrecondDestroyPtr_ = &HYPRE_EuclidDestroy;
1163 PrecondSetupPtr_ = &HYPRE_EuclidSetup;
1164 PrecondSolvePtr_ = &HYPRE_EuclidSolve;
1167 if(IsPrecondCreated_){
1168 PrecondDestroyPtr_(Preconditioner_);
1169 IsPrecondCreated_ =
false;
1171 PrecondCreatePtr_ = &Ifpack_Hypre::Hypre_AMSCreate;
1172 PrecondDestroyPtr_ = &HYPRE_AMSDestroy;
1173 PrecondSetupPtr_ = &HYPRE_AMSSetup;
1174 PrecondSolvePtr_ = &HYPRE_AMSSolve;
1185 int Ifpack_Hypre::CreateSolver(){
1187 HYPRE_ParCSRMatrixGetComm(ParMatrix_, &comm);
1188 int ierr = (this->*SolverCreatePtr_)(comm, &Solver_);
1189 IsSolverCreated_ =
true;
1194 int Ifpack_Hypre::CreatePrecond(){
1196 HYPRE_ParCSRMatrixGetComm(ParMatrix_, &comm);
1197 int ierr = (this->*PrecondCreatePtr_)(comm, &Preconditioner_);
1198 IsPrecondCreated_ =
true;
1204 int Ifpack_Hypre::CopyEpetraToHypre(){
1207 throw std::runtime_error(
"Ifpack_Hypre: Unsupported matrix configuration: Epetra_CrsMatrix required");
1210 for(
int i = 0; i < Matrix->
NumMyRows(); i++){
1215 for(
int j = 0; j < numEntries; j++){
1216 new_indices[j] = GloballyContiguousColMap_->GID(indices[j]);
1219 GlobalRow[0] = GloballyContiguousRowMap_->GID(i);
1220 IFPACK_CHK_ERR(HYPRE_IJMatrixSetValues(HypreA_, 1, &numEntries, GlobalRow, new_indices.data(), values));
1223 IFPACK_CHK_ERR(HYPRE_IJMatrixGetObject(HypreA_, (
void**)&ParMatrix_));
1225 HYPRE_ParCSRMatrixPrint(ParMatrix_,
"A.mat");
1230 int Ifpack_Hypre::Hypre_BoomerAMGCreate(MPI_Comm , HYPRE_Solver *solver)
1231 {
return HYPRE_BoomerAMGCreate(solver);}
1234 int Ifpack_Hypre::Hypre_ParaSailsCreate(MPI_Comm comm, HYPRE_Solver *solver)
1235 {
return HYPRE_ParaSailsCreate(comm, solver);}
1238 int Ifpack_Hypre::Hypre_EuclidCreate(MPI_Comm comm, HYPRE_Solver *solver)
1239 {
return HYPRE_EuclidCreate(comm, solver);}
1242 int Ifpack_Hypre::Hypre_AMSCreate(MPI_Comm , HYPRE_Solver *solver)
1243 {
return HYPRE_AMSCreate(solver);}
1246 int Ifpack_Hypre::Hypre_ParCSRHybridCreate(MPI_Comm , HYPRE_Solver *solver)
1247 {
return HYPRE_ParCSRHybridCreate(solver);}
1250 int Ifpack_Hypre::Hypre_ParCSRPCGCreate(MPI_Comm comm, HYPRE_Solver *solver)
1251 {
return HYPRE_ParCSRPCGCreate(comm, solver);}
1254 int Ifpack_Hypre::Hypre_ParCSRGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver)
1255 {
return HYPRE_ParCSRGMRESCreate(comm, solver);}
1258 int Ifpack_Hypre::Hypre_ParCSRFlexGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver)
1259 {
return HYPRE_ParCSRFlexGMRESCreate(comm, solver);}
1262 int Ifpack_Hypre::Hypre_ParCSRLGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver)
1263 {
return HYPRE_ParCSRLGMRESCreate(comm, solver);}
1266 int Ifpack_Hypre::Hypre_ParCSRBiCGSTABCreate(MPI_Comm comm, HYPRE_Solver *solver)
1267 {
return HYPRE_ParCSRBiCGSTABCreate(comm, solver);}
1277 throw std::runtime_error(
"Ifpack_Hypre: Unsupported matrix configuration: Epetra_CrsMatrix required");
1284 return rcpFromRef(ColumnMap);
1296 ColGIDsHYPRE.Import(MyGIDsHYPRE, *importer, Insert);
1310 #endif // HAVE_HYPRE && HAVE_MPI
int NumGlobalElements() const
ConstIterator end() const
virtual const Epetra_Map & RowMatrixRowMap() const =0
int MyGlobalElements(int *MyGlobalElementList) const
T & get(ParameterList &l, const std::string &name)
RCP< ParameterList > sublist(const RCP< ParameterList > ¶mList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
const Epetra_Map & ColMap() const
int ExtractMyRowView(int MyRow, int &NumEntries, double *&Values, int *&Indices) const
Ifpack_CondestType
Ifpack_CondestType: enum to define the type of condition number estimate.
const Epetra_Map & RowMap() const
int NumMyElements() const
#define IFPACK_CHK_ERRV(ifpack_err)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
const Epetra_Import * Importer() const
bool is_null(const RCP< T > &p)
bool isSublist(const std::string &name) const
int MaxNumEntries() const
virtual const Epetra_BlockMap & Map() const =0
ConstIterator begin() const
void BiCGSTAB(Epetra_CrsMatrix &A, Epetra_Vector &x, Epetra_Vector &b, Ifpack_CrsRiluk *M, int Maxiter, double Tolerance, double *residual, bool verbose)
double Ifpack_Condest(const Ifpack_Preconditioner &IFP, const Ifpack_CondestType CT, const int MaxIters, const double Tol, Epetra_RowMatrix *Matrix)
const Epetra_Map & DomainMap() const
bool isType(const std::string &name) const
#define IFPACK_CHK_ERR(ifpack_err)