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 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
47 #ifndef MUELU_MATLABSMOOTHER_DEF_HPP
48 #define MUELU_MATLABSMOOTHER_DEF_HPP
50 
51 #if defined(HAVE_MUELU_MATLAB)
52 #include "MueLu_Monitor.hpp"
53 
54 namespace MueLu {
55 
56 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
58  SetParameterList(paramList);
59 }
60 
61 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
63  Factory::SetParameterList(paramList);
64  ParameterList& pL = const_cast<ParameterList&>(this->GetParameterList());
65  setupFunction_ = pL.get("Setup Function", "");
66  solveFunction_ = pL.get("Solve Function", "");
67  solveDataSize_ = pL.get("Number of Solver Args", 0);
68 }
69 
70 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
72  using namespace std;
73  this->Input(currentLevel, "A");
74  ParameterList& pL = const_cast<ParameterList&>(this->GetParameterList());
75  needsSetup_ = pL.get<string>("Needs");
76  vector<string> needsList = tokenizeList(needsSetup_);
77  for (size_t i = 0; i < needsList.size(); i++) {
78  if (!IsParamMuemexVariable(needsList[i]) && needsList[i] != "Level")
79  this->Input(currentLevel, needsList[i]);
80  }
81 }
82 
83 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
85  using namespace std;
86  FactoryMonitor m(*this, "Setup Smoother", currentLevel);
87  if (this->IsSetup() == true)
88  this->GetOStream(Warnings0) << "MueLu::MatlabSmoother::Setup(): Setup() has already been called";
89  vector<RCP<MuemexArg>> InputArgs = processNeeds<Scalar, LocalOrdinal, GlobalOrdinal, Node>(this, needsSetup_, currentLevel);
90  A_ = Factory::Get<RCP<Matrix>>(currentLevel, "A");
91  RCP<MuemexArg> AmatArg = rcp_implicit_cast<MuemexArg>(rcp(new MuemexData<RCP<Matrix>>(A_)));
92  // Always add A to the beginning of InputArgs
93  InputArgs.insert(InputArgs.begin(), AmatArg);
94  // Call mex function
95  if (!setupFunction_.length())
96  throw runtime_error("Invalid matlab function name");
97  solveData_ = callMatlab(setupFunction_, solveDataSize_, InputArgs);
98  this->GetOStream(Statistics1) << description() << endl;
99  this->IsSetup(true); // mark the smoother as set up
100 }
101 
102 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
103 void MatlabSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
105  "MueLu::MatlabSmoother::Apply(): Setup() has not been called");
106  using namespace Teuchos;
107  using namespace std;
108  if (InitialGuessIsZero)
109  X.putScalar(0.0);
110  // Push on A as first input
111  vector<RCP<MuemexArg>> InputArgs;
112  InputArgs.push_back(rcp(new MuemexData<RCP<Matrix>>(A_)));
113  // Push on LHS & RHS
114  RCP<MultiVector> Xrcp(&X, false);
115  MultiVector* BPtrNonConst = (MultiVector*)&B;
116  RCP<MultiVector> Brcp = rcp<MultiVector>(BPtrNonConst, false);
119  InputArgs.push_back(XData);
120  InputArgs.push_back(BData);
121  for (size_t i = 0; i < solveData_.size(); i++)
122  InputArgs.push_back(solveData_[i]);
123  if (!solveFunction_.length()) throw std::runtime_error("Invalid matlab function name");
124  vector<Teuchos::RCP<MuemexArg>> mexOutput = callMatlab(solveFunction_, 1, InputArgs);
125  RCP<MuemexData<RCP<MultiVector>>> mydata = Teuchos::rcp_static_cast<MuemexData<RCP<MultiVector>>>(mexOutput[0]);
126  X = *(mydata->getData());
127 }
128 
129 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
131  RCP<MatlabSmoother> smoother = rcp(new MatlabSmoother(*this));
132  smoother->SetParameterList(this->GetParameterList());
133  return smoother;
134 }
135 
136 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
138  std::ostringstream out;
140  out << "Matlab Smoother(" << setupFunction_ << "/" << solveFunction_ << ")";
141  } else {
143  }
144  return out.str();
145 }
146 
147 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
150 
151  if (verbLevel & Parameters0)
152  out << "Matlab Smoother(" << setupFunction_ << "/" << solveFunction_ << ")";
153 
154  if (verbLevel & Parameters1) {
155  out0 << "Parameter list: " << std::endl;
156  Teuchos::OSTab tab2(out);
157  out << this->GetParameterList();
158  }
159 
160  if (verbLevel & Debug) {
161  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
162  }
163 }
164 
165 // Dummy specializations for GO = long long
166 /*template <>
167 void MatlabSmoother<double,int,long long>::Setup(Level& currentLevel) {
168  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
169 }
170 template <>
171 void MatlabSmoother<std::complex<double>,int,long long>::Setup(Level& currentLevel) {
172  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
173 }
174 
175 template <>
176 void MatlabSmoother<double,int,long long>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
177  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
178 }
179 template <>
180 void MatlabSmoother<std::complex<double>,int,long long>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
181  throw std::runtime_error("MatlabSmoother does not support GlobalOrdinal == long long.");
182 }*/
183 
184 } // namespace MueLu
185 
186 #endif // HAVE_MUELU_MATLAB
187 #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:99
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)