MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
11 #ifndef MUELU_MATLABSMOOTHER_DEF_HPP
12 #define MUELU_MATLABSMOOTHER_DEF_HPP
14 
15 #if defined(HAVE_MUELU_MATLAB)
16 #include "MueLu_Monitor.hpp"
17 
18 namespace MueLu {
19 
20 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
22  SetParameterList(paramList);
23 }
24 
25 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
27  Factory::SetParameterList(paramList);
28  ParameterList& pL = const_cast<ParameterList&>(this->GetParameterList());
29  setupFunction_ = pL.get("Setup Function", "");
30  solveFunction_ = pL.get("Solve Function", "");
31  solveDataSize_ = pL.get("Number of Solver Args", 0);
32 }
33 
34 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
36  using namespace std;
37  this->Input(currentLevel, "A");
38  ParameterList& pL = const_cast<ParameterList&>(this->GetParameterList());
39  needsSetup_ = pL.get<string>("Needs");
40  vector<string> needsList = tokenizeList(needsSetup_);
41  for (size_t i = 0; i < needsList.size(); i++) {
42  if (!IsParamMuemexVariable(needsList[i]) && needsList[i] != "Level")
43  this->Input(currentLevel, needsList[i]);
44  }
45 }
46 
47 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
49  using namespace std;
50  FactoryMonitor m(*this, "Setup Smoother", currentLevel);
51  if (this->IsSetup() == true)
52  this->GetOStream(Warnings0) << "MueLu::MatlabSmoother::Setup(): Setup() has already been called";
53  vector<RCP<MuemexArg>> InputArgs = processNeeds<Scalar, LocalOrdinal, GlobalOrdinal, Node>(this, needsSetup_, currentLevel);
54  A_ = Factory::Get<RCP<Matrix>>(currentLevel, "A");
55  RCP<MuemexArg> AmatArg = rcp_implicit_cast<MuemexArg>(rcp(new MuemexData<RCP<Matrix>>(A_)));
56  // Always add A to the beginning of InputArgs
57  InputArgs.insert(InputArgs.begin(), AmatArg);
58  // Call mex function
59  if (!setupFunction_.length())
60  throw runtime_error("Invalid matlab function name");
61  solveData_ = callMatlab(setupFunction_, solveDataSize_, InputArgs);
62  this->GetOStream(Statistics1) << description() << endl;
63  this->IsSetup(true); // mark the smoother as set up
64 }
65 
66 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
67 void MatlabSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
69  "MueLu::MatlabSmoother::Apply(): Setup() has not been called");
70  using namespace Teuchos;
71  using namespace std;
72  if (InitialGuessIsZero)
73  X.putScalar(0.0);
74  // Push on A as first input
75  vector<RCP<MuemexArg>> InputArgs;
76  InputArgs.push_back(rcp(new MuemexData<RCP<Matrix>>(A_)));
77  // Push on LHS & RHS
78  RCP<MultiVector> Xrcp(&X, false);
79  MultiVector* BPtrNonConst = (MultiVector*)&B;
80  RCP<MultiVector> Brcp = rcp<MultiVector>(BPtrNonConst, false);
83  InputArgs.push_back(XData);
84  InputArgs.push_back(BData);
85  for (size_t i = 0; i < solveData_.size(); i++)
86  InputArgs.push_back(solveData_[i]);
87  if (!solveFunction_.length()) throw std::runtime_error("Invalid matlab function name");
88  vector<Teuchos::RCP<MuemexArg>> mexOutput = callMatlab(solveFunction_, 1, InputArgs);
89  RCP<MuemexData<RCP<MultiVector>>> mydata = Teuchos::rcp_static_cast<MuemexData<RCP<MultiVector>>>(mexOutput[0]);
90  X = *(mydata->getData());
91 }
92 
93 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
95  RCP<MatlabSmoother> smoother = rcp(new MatlabSmoother(*this));
96  smoother->SetParameterList(this->GetParameterList());
97  return smoother;
98 }
99 
100 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
102  std::ostringstream out;
104  out << "Matlab Smoother(" << setupFunction_ << "/" << solveFunction_ << ")";
105  } else {
107  }
108  return out.str();
109 }
110 
111 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
114 
115  if (verbLevel & Parameters0)
116  out << "Matlab Smoother(" << setupFunction_ << "/" << solveFunction_ << ")";
117 
118  if (verbLevel & Parameters1) {
119  out0 << "Parameter list: " << std::endl;
120  Teuchos::OSTab tab2(out);
121  out << this->GetParameterList();
122  }
123 
124  if (verbLevel & Debug) {
125  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
126  }
127 }
128 
129 // Dummy specializations for GO = long long
130 /*template <>
131 void MatlabSmoother<double,int,long long>::Setup(Level& currentLevel) {
132  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
133 }
134 template <>
135 void MatlabSmoother<std::complex<double>,int,long long>::Setup(Level& currentLevel) {
136  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
137 }
138 
139 template <>
140 void MatlabSmoother<double,int,long long>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
141  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
142 }
143 template <>
144 void MatlabSmoother<std::complex<double>,int,long long>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
145  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
146 }*/
147 
148 } // namespace MueLu
149 
150 #endif // HAVE_MUELU_MATLAB
151 #endif // MUELU_MATLABSMOOTHER_DEF_HPP
Important warning messages (one line)
void SetParameterList(const Teuchos::ParameterList &paramList)
Set parameters from a parameter list and return with default values.
void DeclareInput(Level &currentLevel) const
Input.
std::vector< std::string > tokenizeList(const std::string &params)
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the preconditioner.
RCP< SmootherPrototype > Copy() const
std::vector< RCP< MuemexArg > > callMatlab(std::string function, int numOutputs, std::vector< RCP< MuemexArg > > args)
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Print more statistics.
void Setup(Level &currentLevel)
Set up the smoother.
Print additional debugging information.
bool IsParamMuemexVariable(const std::string &name)
virtual void SetParameterList(const Teuchos::ParameterList &paramList)
Set parameters from a parameter list and return with default values.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:63
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
bool IsSetup() const
Get the state of a smoother prototype.
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
friend class MatlabSmoother
Constructor.
Print class parameters.
Class that encapsulates Matlab smoothers.
std::string description() const
Return a simple one-line description of this object.
Print class parameters (more parameters, more verbose)
Exception throws to report errors in the internal logical of the program.
virtual std::string description() const
Return a simple one-line description of this object.
std::string toString(const T &t)