46 #ifndef MUELU_AMESOS2SMOOTHER_DEF_HPP
47 #define MUELU_AMESOS2SMOOTHER_DEF_HPP
52 #if defined (HAVE_MUELU_TPETRA) && defined(HAVE_MUELU_AMESOS2)
53 #include <Xpetra_Matrix.hpp>
55 #include <Amesos2_config.h>
56 #include <Amesos2.hpp>
60 #include "MueLu_Utilities.hpp"
65 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
67 : type_(type), useTransformation_(false) {
73 std::transform(
type_.begin(), ++
type_.begin(),
type_.begin(), ::toupper);
75 if (
type_ ==
"Superlu_dist")
76 type_ =
"Superludist";
81 if (
type_ ==
"" || Amesos2::query(
type_) ==
false) {
82 std::string oldtype =
type_;
83 #if defined(HAVE_AMESOS2_SUPERLU)
85 #elif defined(HAVE_AMESOS2_KLU2)
87 #elif defined(HAVE_AMESOS2_SUPERLUDIST)
88 type_ =
"Superludist";
89 #elif defined(HAVE_AMESOS2_BASKER)
92 this->
declareConstructionOutcome(
true, std::string(
"Amesos2 has been compiled without SuperLU_DIST, SuperLU, Klu, or Basker. By default, MueLu tries") +
93 "to use one of these libraries. Amesos2 must be compiled with one of these solvers, " +
94 "or a valid Amesos2 solver has to be specified explicitly.");
98 this->
GetOStream(
Warnings0) <<
"MueLu::Amesos2Smoother: \"" << oldtype <<
"\" is not available. Using \"" <<
type_ <<
"\" instead" << std::endl;
105 "Amesos2 has been compiled without the support of this solver, or the solver name is misspelled.");
108 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
111 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
116 validParamList->
set<
bool>(
"fix nullspace",
false,
"Remove zero eigenvalue by adding rank one correction.");
117 return validParamList;
120 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
124 this->Input(currentLevel,
"A");
125 if (pL.
get<
bool>(
"fix nullspace"))
126 this->Input(currentLevel,
"Nullspace");
129 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
134 this->GetOStream(
Warnings0) <<
"MueLu::Amesos2Smoother::Setup(): Setup() has already been called" << std::endl;
136 RCP<Matrix> A = Factory::Get< RCP<Matrix> >(currentLevel,
"A");
142 if (pL.
get<
bool>(
"fix nullspace")) {
143 this->GetOStream(
Runtime1) <<
"MueLu::Amesos2Smoother::Setup(): fixing nullspace" << std::endl;
145 rowMap = A->getRowMap();
146 size_t M = rowMap->getGlobalNumElements();
148 RCP<MultiVector> Nullspace = Factory::Get< RCP<MultiVector> >(currentLevel,
"Nullspace");
151 "MueLu::Amesos2Smoother::Setup Fixing nullspace for coarse matrix for Amesos2 for nullspace of dim > 1 has not been implemented yet.");
156 if (rowMap->getComm()->getSize() > 1) {
157 this->GetOStream(
Warnings0) <<
"MueLu::Amesos2Smoother::Setup(): Applying nullspace fix on distributed matrix. Try rebalancing to single rank!" << std::endl;
161 for (
size_t k = 0; k<M; k++)
162 elements[k] = Teuchos::as<GO>(k);
164 importer = ImportFactory::Build(rowMap,colMap);
165 NullspaceImp = MultiVectorFactory::Build(colMap, Nullspace->getNumVectors());
166 NullspaceImp->doImport(*Nullspace,*importer,Xpetra::INSERT);
168 NullspaceImp = Nullspace;
174 nullspaceRCP = Nullspace->getData(0);
175 nullspace = nullspaceRCP();
176 nullspaceImpRCP = NullspaceImp->getData(0);
177 nullspaceImp = nullspaceImpRCP();
182 "MueLu::Amesos2Smoother::Setup Fixing nullspace for coarse matrix for Amesos2 when matrix is not a Crs matrix has not been implemented yet.");
187 Acrs->getCrsMatrix()->getAllValues(rowPointers, colIndices, values);
193 size_t N = rowMap->getNodeNumElements();
194 newRowPointers_RCP.
resize(N+1);
195 newColIndices_RCP.
resize(N*M);
196 newValues_RCP.
resize(N*M);
202 SC normalization = Nullspace->getVector(0)->norm2();
206 for (
size_t i = 0; i < N; i++) {
207 newRowPointers[i] = i*M;
208 for (
size_t j = 0; j < M; j++) {
209 newColIndices[i*M+j] = Teuchos::as<LO>(j);
213 newRowPointers[N] = N*M;
216 for (
size_t i = 0; i < N; i++) {
217 for (
size_t jj = rowPointers[i]; jj < rowPointers[i+1]; jj++) {
218 LO j = colMap->getLocalElement(A->getColMap()->getGlobalElement(colIndices[jj]));
220 newValues[i*M+j] += v;
225 RCP<CrsMatrix> newAcrs = rcp_dynamic_cast<CrsMatrixWrap>(newA)->getCrsMatrix();
227 using Teuchos::arcp_const_cast;
228 newAcrs->setAllValues(arcp_const_cast<size_t>(newRowPointers_RCP), arcp_const_cast<LO>(newColIndices_RCP), arcp_const_cast<SC>(newValues_RCP));
229 newAcrs->expertStaticFillComplete(A->getDomainMap(), A->getRangeMap(),
230 importer, A->getCrsGraph()->getExporter());
239 prec_ = Amesos2::create<Tpetra_CrsMatrix,Tpetra_MultiVector>(type_, tA);
241 if (rowMap->getGlobalNumElements() != as<size_t>((rowMap->getMaxAllGlobalIndex() - rowMap->getMinAllGlobalIndex())+1)) {
243 amesos2_params->sublist(prec_->name()).set(
"IsContiguous",
false,
"Are GIDs Contiguous");
244 prec_->setParameters(amesos2_params);
250 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
255 if (!useTransformation_) {
260 size_t numVectors = X.getNumVectors();
261 size_t length = X.getLocalLength();
264 "MueLu::Amesos2Smoother::Apply: Fixing coarse matrix for Amesos2 for multivectors has not been implemented yet.");
266 ArrayRCP<SC> X_data = X_->getDataNonConst(0), B_data = B_->getDataNonConst(0);
268 for (
size_t i = 0; i < length; i++) {
269 X_data[i] = Xdata[i];
270 B_data[i] = Bdata[i];
282 prec_->setX(Teuchos::null);
283 prec_->setB(Teuchos::null);
285 if (useTransformation_) {
287 size_t length = X.getLocalLength();
292 for (
size_t i = 0; i < length; i++)
293 Xdata[i] = X_data[i];
297 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
305 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
307 std::ostringstream out;
310 out << prec_->description();
314 out <<
"{type = " << type_ <<
"}";
319 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
324 out0 <<
"Prec. type: " << type_ << std::endl;
327 out0 <<
"Parameter list: " << std::endl;
329 out << this->GetParameterList();
332 if ((verbLevel &
External) && prec_ != Teuchos::null) {
334 out << *prec_ << std::endl;
337 if (verbLevel &
Debug)
340 <<
"RCP<prec_>: " << prec_ << std::endl;
343 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
346 return prec_->getStatus().getNnzLU();
353 #endif // HAVE_MUELU_TPETRA && HAVE_MUELU_AMESOS2
354 #endif // MUELU_AMESOS2SMOOTHER_DEF_HPP
Important warning messages (one line)
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
Print external lib objects.
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
std::string type_
amesos2-specific key phrase that denote smoother type
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the direct solver. Solves the linear system AX=B using the constructed solver.
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Print additional debugging information.
std::string tolower(const std::string &str)
Amesos2Smoother(const std::string &type="", const Teuchos::ParameterList ¶mList=Teuchos::ParameterList())
Constructor Creates a MueLu interface to the direct solvers in the Amesos2 package. If you are using type=="", then either SuperLU or KLU2 are used by default.
std::string description() const
Return a simple one-line description of this object.
void resize(const size_type n, const T &val=T())
RCP< SmootherPrototype > Copy() const
virtual void SetParameterList(const Teuchos::ParameterList ¶mList)
Set parameters from a parameter list and return with default values.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void declareConstructionOutcome(bool fail, std::string msg)
Class that holds all level-specific information.
Class that encapsulates Amesos2 direct solvers.
static RCP< Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2NonConstTpetraCrs(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op)
static RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2NonConstTpetraMV2(Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &vec)
bool IsSetup() const
Get the state of a smoother prototype.
void DeclareInput(Level ¤tLevel) const
Input.
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
virtual ~Amesos2Smoother()
Destructor.
void Setup(Level ¤tLevel)
Set up the direct solver. This creates the underlying Amesos2 solver object according to the paramete...
Print class parameters (more parameters, more verbose)
Exception throws to report errors in the internal logical of the program.
Description of what is happening (more verbose)
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
virtual std::string description() const
Return a simple one-line description of this object.
std::string toString(const T &t)