MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_MatlabSmoother_def.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MueLu: A package for multigrid based preconditioning
4//
5// Copyright 2012 NTESS and the MueLu contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9#ifndef MUELU_MATLABSMOOTHER_DEF_HPP
10#define MUELU_MATLABSMOOTHER_DEF_HPP
13
14#if defined(HAVE_MUELU_MATLAB)
15#include "MueLu_Monitor.hpp"
16
17namespace MueLu {
18
19template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
21 SetParameterList(paramList);
22}
23
24template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
27 ParameterList& pL = const_cast<ParameterList&>(this->GetParameterList());
28 setupFunction_ = pL.get("Setup Function", "");
29 solveFunction_ = pL.get("Solve Function", "");
30 solveDataSize_ = pL.get("Number of Solver Args", 0);
31}
32
33template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
35 using namespace std;
36 this->Input(currentLevel, "A");
37 ParameterList& pL = const_cast<ParameterList&>(this->GetParameterList());
38 needsSetup_ = pL.get<string>("Needs");
39 vector<string> needsList = tokenizeList(needsSetup_);
40 for (size_t i = 0; i < needsList.size(); i++) {
41 if (!IsParamMuemexVariable(needsList[i]) && needsList[i] != "Level")
42 this->Input(currentLevel, needsList[i]);
43 }
44}
45
46template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
48 using namespace std;
49 FactoryMonitor m(*this, "Setup Smoother", currentLevel);
50 if (this->IsSetup() == true)
51 this->GetOStream(Warnings0) << "MueLu::MatlabSmoother::Setup(): Setup() has already been called";
52 vector<RCP<MuemexArg>> InputArgs = processNeeds<Scalar, LocalOrdinal, GlobalOrdinal, Node>(this, needsSetup_, currentLevel);
53 A_ = Factory::Get<RCP<Matrix>>(currentLevel, "A");
54 RCP<MuemexArg> AmatArg = rcp_implicit_cast<MuemexArg>(rcp(new MuemexData<RCP<Matrix>>(A_)));
55 // Always add A to the beginning of InputArgs
56 InputArgs.insert(InputArgs.begin(), AmatArg);
57 // Call mex function
58 if (!setupFunction_.length())
59 throw runtime_error("Invalid matlab function name");
61 this->GetOStream(Statistics1) << description() << endl;
62 this->IsSetup(true); // mark the smoother as set up
63}
64
65template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
66void MatlabSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
67 TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError,
68 "MueLu::MatlabSmoother::Apply(): Setup() has not been called");
69 using namespace Teuchos;
70 using namespace std;
71 if (InitialGuessIsZero)
72 X.putScalar(0.0);
73 // Push on A as first input
74 vector<RCP<MuemexArg>> InputArgs;
75 InputArgs.push_back(rcp(new MuemexData<RCP<Matrix>>(A_)));
76 // Push on LHS & RHS
77 RCP<MultiVector> Xrcp(&X, false);
78 MultiVector* BPtrNonConst = (MultiVector*)&B;
79 RCP<MultiVector> Brcp = rcp<MultiVector>(BPtrNonConst, false);
80 RCP<MuemexData<RCP<MultiVector>>> XData = rcp(new MuemexData<RCP<MultiVector>>(Xrcp));
81 RCP<MuemexData<RCP<MultiVector>>> BData = rcp(new MuemexData<RCP<MultiVector>>(Brcp));
82 InputArgs.push_back(XData);
83 InputArgs.push_back(BData);
84 for (size_t i = 0; i < solveData_.size(); i++)
85 InputArgs.push_back(solveData_[i]);
86 if (!solveFunction_.length()) throw std::runtime_error("Invalid matlab function name");
87 vector<Teuchos::RCP<MuemexArg>> mexOutput = callMatlab(solveFunction_, 1, InputArgs);
88 RCP<MuemexData<RCP<MultiVector>>> mydata = Teuchos::rcp_static_cast<MuemexData<RCP<MultiVector>>>(mexOutput[0]);
89 X = *(mydata->getData());
90}
91
92template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
93RCP<MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node>> MatlabSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Copy() const {
94 RCP<MatlabSmoother> smoother = rcp(new MatlabSmoother(*this));
95 smoother->SetParameterList(this->GetParameterList());
96 return smoother;
97}
98
99template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
101 std::ostringstream out;
103 out << "Matlab Smoother(" << setupFunction_ << "/" << solveFunction_ << ")";
104 } else {
106 }
107 return out.str();
108}
109
110template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
111void MatlabSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::print(Teuchos::FancyOStream& out, const VerbLevel verbLevel) const {
113
114 if (verbLevel & Parameters0)
115 out << "Matlab Smoother(" << setupFunction_ << "/" << solveFunction_ << ")";
116
117 if (verbLevel & Parameters1) {
118 out0 << "Parameter list: " << std::endl;
119 Teuchos::OSTab tab2(out);
120 out << this->GetParameterList();
121 }
122
123 if (verbLevel & Debug) {
124 out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
125 }
126}
127
128// Dummy specializations for GO = long long
129/*template <>
130void MatlabSmoother<double,int,long long>::Setup(Level& currentLevel) {
131 throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
132}
133template <>
134void MatlabSmoother<std::complex<double>,int,long long>::Setup(Level& currentLevel) {
135 throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
136}
137
138template <>
139void MatlabSmoother<double,int,long long>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
140 throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
141}
142template <>
143void MatlabSmoother<std::complex<double>,int,long long>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
144 throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
145}*/
146
147} // namespace MueLu
148
149#endif // HAVE_MUELU_MATLAB
150#endif // MUELU_MATLABSMOOTHER_DEF_HPP
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
virtual std::string description() const
Return a simple one-line description of this object.
Exception throws to report errors in the internal logical of the program.
void Input(Level &level, const std::string &varName) const
T Get(Level &level, const std::string &varName) const
Timer to be used in factories. Similar to Monitor but with additional timers.
Class that holds all level-specific information.
std::string solveFunction_
Matlab solve function.
void DeclareInput(Level &currentLevel) const
Input.
std::string needsSetup_
List of arguments to the MATLAB setup function besides "A", in order.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
void Setup(Level &currentLevel)
Set up the smoother.
size_t solveDataSize_
Amount of solve data (besides A, LHS & RHS).
RCP< SmootherPrototype > Copy() const
RCP< Matrix > A_
Matrix, (maybe) used in apply.
friend class MatlabSmoother
Constructor.
std::vector< Teuchos::RCP< MuemexArg > > solveData_
List of data generated by setup which will be sent to solve after "A", "LHS" and "RHS".
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the preconditioner.
std::string setupFunction_
Matlab setup function.
void SetParameterList(const Teuchos::ParameterList &paramList)
Set parameters from a parameter list and return with default values.
std::string description() const
Return a simple one-line description of this object.
virtual void SetParameterList(const Teuchos::ParameterList &paramList)=0
Set parameters from a parameter list and return with default values.
virtual const Teuchos::ParameterList & GetParameterList() const
bool IsSetup() const
Get the state of a smoother prototype.
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Namespace for MueLu classes and methods.
bool IsParamMuemexVariable(const std::string &name)
@ Warnings0
Important warning messages (one line).
@ Debug
Print additional debugging information.
@ Statistics1
Print more statistics.
@ Parameters0
Print class parameters.
@ Parameters1
Print class parameters (more parameters, more verbose).
std::vector< RCP< MuemexArg > > callMatlab(std::string function, int numOutputs, std::vector< RCP< MuemexArg > > args)
std::vector< Teuchos::RCP< MuemexArg > > processNeeds(const Factory *factory, std::string &needsParam, Level &lvl)
std::vector< std::string > tokenizeList(const std::string &params)