10 #ifndef MUELU_PGPFACTORY_DEF_HPP
11 #define MUELU_PGPFACTORY_DEF_HPP
16 #include <Xpetra_MultiVectorFactory.hpp>
27 #include "MueLu_PerfUtils.hpp"
29 #include "MueLu_Utilities.hpp"
33 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
37 validParamList->
set<
RCP<const FactoryBase> >(
"A", Teuchos::null,
"Generating factory of the matrix A used during the prolongator smoothing process");
40 validParamList->
set<
bool>(
"ReUseRowBasedOmegas",
false,
"Reuse omegas for prolongator for restrictor");
42 return validParamList;
45 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
50 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
56 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
58 Input(fineLevel,
"A");
64 if (initialPFact == Teuchos::null) {
84 bool bReUseRowBasedOmegas = pL.
get<
bool>(
"ReUseRowBasedOmegas");
85 if (bReUseRowBasedOmegas ==
true && restrictionMode_ ==
true) {
90 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
92 FactoryMonitor m(*
this,
"Prolongator smoothing (PG-AMG)", coarseLevel);
101 if (initialPFact == Teuchos::null) {
107 if (restrictionMode_) {
113 bool doFillComplete =
true;
114 bool optimizeStorage =
true;
117 doFillComplete =
true;
118 optimizeStorage =
false;
127 bool bReUseRowBasedOmegas = pL.
get<
bool>(
"ReUseRowBasedOmegas");
128 if (restrictionMode_ ==
false || bReUseRowBasedOmegas ==
false) {
131 ComputeRowBasedOmega(fineLevel, coarseLevel, A, Ptent, DinvAP0, RowBasedOmega);
144 ExportFactory::Build(RowBasedOmega->getMap(), A->getRangeMap());
147 VectorFactory::Build(A->getRangeMap());
149 noRowBasedOmega->doExport(*RowBasedOmega, *exporter,
Xpetra::INSERT);
155 ImportFactory::Build(A->getRangeMap(), A->getRowMap());
158 RowBasedOmega->doImport(*noRowBasedOmega, *importer,
Xpetra::INSERT);
170 P_smoothed->fillComplete(Ptent->getDomainMap(), Ptent->getRangeMap());
175 params->
set(
"printLoadBalancingInfo",
true);
178 if (!restrictionMode_) {
180 Set(coarseLevel,
"P", P_smoothed);
188 Set(coarseLevel,
"RfromPfactory", dummy);
194 if (Ptent->IsView(
"stridedMaps"))
195 P_smoothed->CreateView(
"stridedMaps", Ptent);
200 Set(coarseLevel,
"R", R);
206 if (Ptent->IsView(
"stridedMaps"))
207 R->CreateView(
"stridedMaps", Ptent,
true);
211 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
213 FactoryMonitor m(*
this,
"PgPFactory::ComputeRowBasedOmega", coarseLevel);
238 bool doFillComplete =
true;
239 bool optimizeStorage =
false;
245 Numerator = VectorFactory::Build(ADinvAP0->getColMap(),
true);
246 Denominator = VectorFactory::Build(ADinvAP0->getColMap(),
true);
247 MultiplyAll(AP0, ADinvAP0, Numerator);
248 MultiplySelfAll(ADinvAP0, Denominator);
257 Numerator = VectorFactory::Build(DinvAP0->getColMap(),
true);
258 Denominator = VectorFactory::Build(DinvAP0->getColMap(),
true);
259 MultiplyAll(P0, DinvAP0, Numerator);
260 MultiplySelfAll(DinvAP0, Denominator);
273 bool doFillComplete =
true;
274 bool optimizeStorage =
true;
280 Numerator = VectorFactory::Build(DinvADinvAP0->getColMap(),
true);
281 Denominator = VectorFactory::Build(DinvADinvAP0->getColMap(),
true);
282 MultiplyAll(DinvAP0, DinvADinvAP0, Numerator);
283 MultiplySelfAll(DinvADinvAP0, Denominator);
291 VectorFactory::Build(Numerator->getMap() ,
true);
293 ColBasedOmega->putScalar(-666 );
300 Magnitude min_local = 1000000.0;
301 Magnitude max_local = 0.0;
302 for (
LocalOrdinal i = 0; i < Teuchos::as<LocalOrdinal>(Numerator->getLocalLength()); i++) {
304 ColBasedOmega_local[i] = 0.0;
307 ColBasedOmega_local[i] = Numerator_local[i] / Denominator_local[i];
320 ColBasedOmega_local[i] = 0.0;
336 MueLu_sumAll(A->getRowMap()->getComm(), zero_local, zero_all);
337 MueLu_sumAll(A->getRowMap()->getComm(), nan_local, nan_all);
338 MueLu_minAll(A->getRowMap()->getComm(), min_local, min_all);
339 MueLu_maxAll(A->getRowMap()->getComm(), max_local, max_all);
346 default: GetOStream(
Statistics1) <<
"unknown)" << std::endl;
break;
348 GetOStream(
Statistics1) <<
"Damping parameter: min = " << min_all <<
", max = " << max_all << std::endl;
349 GetOStream(
Statistics) <<
"# negative omegas: " << zero_all <<
" out of " << ColBasedOmega->getGlobalLength() <<
" column-based omegas" << std::endl;
350 GetOStream(
Statistics) <<
"# NaNs: " << nan_all <<
" out of " << ColBasedOmega->getGlobalLength() <<
" column-based omegas" << std::endl;
353 if (coarseLevel.
IsRequested(
"ColBasedOmega",
this)) {
354 coarseLevel.
Set(
"ColBasedOmega", ColBasedOmega,
this);
360 VectorFactory::Build(DinvAP0->getRowMap(),
true);
362 RowBasedOmega->putScalar(-666);
364 bool bAtLeastOneDefined =
false;
366 for (
LocalOrdinal row = 0; row < Teuchos::as<LocalOrdinal>(A->getLocalNumRows()); row++) {
369 DinvAP0->getLocalRowView(row, lindices, lvals);
370 bAtLeastOneDefined =
false;
371 for (
size_t j = 0; j < Teuchos::as<size_t>(lindices.
size()); j++) {
372 Scalar omega = ColBasedOmega_local[lindices[j]];
374 bAtLeastOneDefined =
true;
376 RowBasedOmega_local[row] = omega;
378 RowBasedOmega_local[row] = omega;
381 if (bAtLeastOneDefined ==
true) {
383 RowBasedOmega_local[row] = sZero;
387 if (coarseLevel.
IsRequested(
"RowBasedOmega",
this)) {
388 Set(coarseLevel,
"RowBasedOmega", RowBasedOmega);
392 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
402 for (
size_t n = 0; n < Op->getLocalNumRows(); n++) {
403 Op->getLocalRowView(n, lindices, lvals);
404 for (
size_t i = 0; i < Teuchos::as<size_t>(lindices.
size()); i++) {
405 InnerProd_local[lindices[i]] += lvals[i] * lvals[i];
412 ExportFactory::Build(Op->getColMap(), Op->getDomainMap());
415 VectorFactory::Build(Op->getDomainMap());
417 nonoverlap->doExport(*InnerProdVec, *exporter,
Xpetra::ADD);
423 ImportFactory::Build(Op->getDomainMap(), Op->getColMap());
429 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
433 #if 1 // 1=new "fast code, 0=old "slow", but safe code
434 #if 0 // not necessary - remove me
435 if(InnerProdVec->getMap()->isSameAs(*left->getColMap())) {
438 std::vector<LocalOrdinal> NewRightLocal(right->getColMap()->getLocalNumElements(), Teuchos::as<LocalOrdinal>(left->getColMap()->getLocalNumElements()+1));
441 for (
size_t j=0; j < right->getColMap()->getLocalNumElements(); j++) {
442 while ( (i < Teuchos::as<LocalOrdinal>(left->getColMap()->getLocalNumElements())) &&
443 (left->getColMap()->getGlobalElement(i) < right->getColMap()->getGlobalElement(j)) ) i++;
444 if (left->getColMap()->getGlobalElement(i) == right->getColMap()->getGlobalElement(j)) {
445 NewRightLocal[j] = i;
450 std::vector<Scalar> temp_array(left->getColMap()->getLocalNumElements()+1, 0.0);
452 for(
size_t n=0; n<right->getLocalNumRows(); n++) {
458 left->getLocalRowView (n, lindices_left, lvals_left);
459 right->getLocalRowView(n, lindices_right, lvals_right);
461 for(
size_t j=0; j<Teuchos::as<size_t>(lindices_right.
size()); j++) {
462 temp_array[NewRightLocal[lindices_right[j] ] ] = lvals_right[j];
464 for (
size_t j=0; j < Teuchos::as<size_t>(lindices_left.
size()); j++) {
465 InnerProd_local[lindices_left[j]] += temp_array[lindices_left[j] ]*lvals_left[j];
467 for (
size_t j=0; j < Teuchos::as<size_t>(lindices_right.
size()); j++) {
468 temp_array[NewRightLocal[lindices_right[j] ] ] = 0.0;
473 ExportFactory::Build(left->getColMap(), left->getDomainMap());
476 VectorFactory::Build(left->getDomainMap());
478 nonoverlap->doExport(*InnerProdVec, *exporter,
Xpetra::ADD);
484 ImportFactory::Build(left->getDomainMap(), left->getColMap());
491 #endif // end remove me
492 if (InnerProdVec->getMap()->isSameAs(*right->getColMap())) {
493 size_t szNewLeftLocal = TEUCHOS_MAX(left->getColMap()->getLocalNumElements(), right->getColMap()->getLocalNumElements());
497 for (
size_t i = 0; i < left->getColMap()->getLocalNumElements(); i++) {
498 while ((j < Teuchos::as<LocalOrdinal>(right->getColMap()->getLocalNumElements())) &&
499 (right->getColMap()->getGlobalElement(j) < left->getColMap()->getGlobalElement(i))) j++;
500 if (right->getColMap()->getGlobalElement(j) == left->getColMap()->getGlobalElement(i)) {
501 (*NewLeftLocal)[i] = j;
512 for (
size_t n = 0; n < left->getLocalNumRows(); n++) {
518 left->getLocalRowView(n, lindices_left, lvals_left);
519 right->getLocalRowView(n, lindices_right, lvals_right);
521 for (
size_t i = 0; i < Teuchos::as<size_t>(lindices_left.
size()); i++) {
522 (*temp_array)[(*NewLeftLocal)[lindices_left[i]]] = lvals_left[i];
524 for (
size_t i = 0; i < Teuchos::as<size_t>(lindices_right.
size()); i++) {
525 InnerProd_local[lindices_right[i]] += (*temp_array)[lindices_right[i]] * lvals_right[i];
527 for (
size_t i = 0; i < Teuchos::as<size_t>(lindices_left.
size()); i++) {
528 (*temp_array)[(*NewLeftLocal)[lindices_left[i]]] = 0.0;
534 ExportFactory::Build(right->getColMap(), right->getDomainMap());
537 VectorFactory::Build(right->getDomainMap());
539 nonoverlap->doExport(*InnerProdVec, *exporter,
Xpetra::ADD);
545 ImportFactory::Build(right->getDomainMap(), right->getColMap());
552 #else // old "safe" code
553 if (InnerProdVec->getMap()->isSameAs(*left->getColMap())) {
562 for (
size_t n = 0; n < left->getLocalNumRows(); n++) {
563 left->getLocalRowView(n, lindices_left, lvals_left);
564 right->getLocalRowView(n, lindices_right, lvals_right);
566 for (
size_t i = 0; i < Teuchos::as<size_t>(lindices_left.
size()); i++) {
567 GlobalOrdinal left_gid = left->getColMap()->getGlobalElement(lindices_left[i]);
568 for (
size_t j = 0; j < Teuchos::as<size_t>(lindices_right.
size()); j++) {
569 GlobalOrdinal right_gid = right->getColMap()->getGlobalElement(lindices_right[j]);
570 if (left_gid == right_gid) {
571 InnerProd_local[lindices_left[i]] += lvals_left[i] * lvals_right[j];
580 ExportFactory::Build(left->getColMap(), left->getDomainMap());
583 VectorFactory::Build(left->getDomainMap());
585 nonoverlap->doExport(*InnerProdVec, *exporter,
Xpetra::ADD);
591 ImportFactory::Build(left->getDomainMap(), left->getColMap());
595 }
else if (InnerProdVec->getMap()->isSameAs(*right->getColMap())) {
603 for (
size_t n = 0; n < left->getLocalNumRows(); n++) {
604 left->getLocalRowView(n, lindices_left, lvals_left);
605 right->getLocalRowView(n, lindices_right, lvals_right);
607 for (
size_t i = 0; i < Teuchos::as<size_t>(lindices_left.
size()); i++) {
608 GlobalOrdinal left_gid = left->getColMap()->getGlobalElement(lindices_left[i]);
609 for (
size_t j = 0; j < Teuchos::as<size_t>(lindices_right.
size()); j++) {
610 GlobalOrdinal right_gid = right->getColMap()->getGlobalElement(lindices_right[j]);
611 if (left_gid == right_gid) {
612 InnerProd_local[lindices_right[j]] += lvals_left[i] * lvals_right[j];
621 ExportFactory::Build(right->getColMap(), right->getDomainMap());
624 VectorFactory::Build(right->getDomainMap());
626 nonoverlap->doExport(*InnerProdVec, *exporter,
Xpetra::ADD);
632 ImportFactory::Build(right->getDomainMap(), right->getColMap());
642 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
644 std::cout <<
"TODO: remove me" << std::endl;
647 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
#define MueLu_sumAll(rcpComm, in, out)
MueLu::DefaultLocalOrdinal LocalOrdinal
T & Get(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Get data without decrementing associated storage counter (i.e., read-only access). Usage: Level->Get< RCP<Matrix> >("A", factory) if factory == NULL => use default factory.
static void MyOldScaleMatrix(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Teuchos::ArrayRCP< const Scalar > &scalingVector, bool doInverse=true, bool doFillComplete=true, bool doOptimizeStorage=true)
#define MueLu_maxAll(rcpComm, in, out)
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
T & get(const std::string &name, T def_value)
void BuildP(Level &fineLevel, Level &coarseLevel) const
Abstract Build method.
void ComputeRowBasedOmega(Level &fineLevel, Level &coarseLevel, const RCP< Matrix > &A, const RCP< Matrix > &P0, const RCP< Matrix > &DinvAP0, RCP< Xpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &RowBasedOmega) const
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define MueLu_minAll(rcpComm, in, out)
Print even more statistics.
void SetMinimizationMode(MinimizationNorm minnorm)
Set minimization mode (L2NORM for cheapest, ANORM more expensive, DINVANORM = default) ...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
void Build(Level &fineLevel, Level &coarseLevel) const
Build method.
Class that holds all level-specific information.
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
void MultiplySelfAll(const RCP< Matrix > &Op, Teuchos::RCP< Xpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &InnerProdVec) const
static Teuchos::ArrayRCP< Scalar > GetMatrixDiagonal_arcp(const Matrix &A)
Extract Matrix Diagonal.
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Transpose(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, bool optimizeTranspose=false, const std::string &label=std::string(), const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
MinimizationNorm GetMinimizationMode()
return minimization mode
void Set(const std::string &ename, const T &entry, const FactoryBase *factory=NoFactory::get())
static void TwoMatrixAdd(const Matrix &A, bool transposeA, SC alpha, Matrix &B, SC beta)
static magnitudeType magnitude(T a)
void ReUseDampingParameters(bool bReuse)
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
static void Multiply(const Matrix &A, bool transposeA, const Matrix &B, bool transposeB, Matrix &C, bool call_FillComplete_on_result=true, bool doOptimizeStorage=true, const std::string &label=std::string(), const RCP< ParameterList > ¶ms=null)
const RCP< const FactoryManagerBase > GetFactoryManager()
returns the current factory manager
Exception throws to report errors in the internal logical of the program.
bool IsRequested(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need has been requested. Note: this tells nothing about whether the need's value exist...
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
void MultiplyAll(const RCP< Matrix > &left, const RCP< Matrix > &right, Teuchos::RCP< Xpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &InnerProdVec) const